Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SwitcherTest.php
Go to the documentation of this file.
1 <?php
8 
10 
11 class SwitcherTest extends \PHPUnit\Framework\TestCase
12 {
14  protected $switcher;
15 
17  protected $context;
18 
21 
23  protected $storeManager;
24 
26  protected $urlBuilder;
27 
29  private $store;
30 
34  protected function setUp()
35  {
36  $this->storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)->getMock();
37  $this->urlBuilder = $this->createMock(\Magento\Framework\UrlInterface::class);
38  $this->context = $this->createMock(\Magento\Framework\View\Element\Template\Context::class);
39  $this->context->expects($this->any())->method('getStoreManager')->will($this->returnValue($this->storeManager));
40  $this->context->expects($this->any())->method('getUrlBuilder')->will($this->returnValue($this->urlBuilder));
41  $this->corePostDataHelper = $this->createMock(\Magento\Framework\Data\Helper\PostHelper::class);
42  $this->store = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)
43  ->disableOriginalConstructor()
44  ->getMockForAbstractClass();
45  $this->switcher = (new ObjectManager($this))->getObject(
46  \Magento\Store\Block\Switcher::class,
47  [
48  'context' => $this->context,
49  'postDataHelper' => $this->corePostDataHelper,
50  ]
51  );
52  }
53 
57  public function testGetTargetStorePostData()
58  {
59  $store = $this->getMockBuilder(\Magento\Store\Model\Store::class)
60  ->disableOriginalConstructor()
61  ->getMock();
62  $store->expects($this->any())
63  ->method('getCode')
64  ->willReturn('new-store');
65  $storeSwitchUrl = 'http://domain.com/stores/store/redirect';
66  $store->expects($this->atLeastOnce())
67  ->method('getCurrentUrl')
68  ->with(false)
69  ->willReturn($storeSwitchUrl);
70  $this->storeManager->expects($this->once())
71  ->method('getStore')
72  ->willReturn($this->store);
73  $this->store->expects($this->once())
74  ->method('getCode')
75  ->willReturn('old-store');
76  $this->urlBuilder->expects($this->once())
77  ->method('getUrl')
78  ->willReturn($storeSwitchUrl);
79  $this->corePostDataHelper->expects($this->any())
80  ->method('getPostData')
81  ->with($storeSwitchUrl, ['___store' => 'new-store', 'uenc' => null, '___from_store' => 'old-store']);
82 
83  $this->switcher->getTargetStorePostData($store);
84  }
85 
90  public function testIsStoreInUrl($isUseStoreInUrl)
91  {
92  $storeMock = $this->createMock(\Magento\Store\Model\Store::class);
93 
94  $storeMock->expects($this->once())->method('isUseStoreInUrl')->will($this->returnValue($isUseStoreInUrl));
95 
96  $this->storeManager->expects($this->any())->method('getStore')->will($this->returnValue($storeMock));
97  $this->assertEquals($this->switcher->isStoreInUrl(), $isUseStoreInUrl);
98  // check value is cached
99  $this->assertEquals($this->switcher->isStoreInUrl(), $isUseStoreInUrl);
100  }
101 
106  public function isStoreInUrlDataProvider()
107  {
108  return [[true], [false]];
109  }
110 }