Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
StoreCookieTest.php
Go to the documentation of this file.
1 <?php
8 
13 use \InvalidArgumentException;
14 
20 class StoreCookieTest extends \PHPUnit\Framework\TestCase
21 {
25  protected $plugin;
26 
30  protected $storeManagerMock;
31 
36 
40  protected $storeMock;
41 
45  protected $subjectMock;
46 
50  protected $requestMock;
51 
56 
60  protected function setUp()
61  {
62  $this->storeManagerMock = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
63  ->disableOriginalConstructor()
64  ->setMethods([])
65  ->getMock();
66 
67  $this->storeCookieManagerMock = $this->getMockBuilder(\Magento\Store\Api\StoreCookieManagerInterface::class)
68  ->disableOriginalConstructor()
69  ->setMethods([])
70  ->getMock();
71 
72  $this->storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
73  ->disableOriginalConstructor()
74  ->setMethods([])
75  ->getMock();
76 
77  $this->subjectMock = $this->getMockBuilder(\Magento\Framework\App\FrontController::class)
78  ->disableOriginalConstructor()
79  ->setMethods([])
80  ->getMock();
81 
82  $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
83  ->disableOriginalConstructor()
84  ->setMethods([])
85  ->getMock();
86 
87  $this->storeRepositoryMock = $this->getMockBuilder(\Magento\Store\Api\StoreRepositoryInterface::class)
88  ->disableOriginalConstructor()
89  ->setMethods([])
90  ->getMock();
91 
92  $this->plugin = (new ObjectManager($this))->getObject(
93  \Magento\Store\Model\Plugin\StoreCookie::class,
94  [
95  'storeManager' => $this->storeManagerMock,
96  'storeCookieManager' => $this->storeCookieManagerMock,
97  'storeRepository' => $this->storeRepositoryMock
98  ]
99  );
100  }
101 
106  {
107  $storeCode = 'store';
108  $this->storeManagerMock->expects($this->once())
109  ->method('getDefaultStoreView')
110  ->willReturn($this->storeMock);
111  $this->storeCookieManagerMock->expects($this->atLeastOnce())
112  ->method('getStoreCodeFromCookie')
113  ->willReturn($storeCode);
114  $this->storeRepositoryMock->expects($this->once())
115  ->method('getActiveStoreByCode')
116  ->willThrowException(new NoSuchEntityException);
117  $this->storeCookieManagerMock->expects($this->once())
118  ->method('deleteStoreCookie')
119  ->with($this->storeMock);
120 
121  $this->plugin->beforeDispatch($this->subjectMock, $this->requestMock);
122  }
123 
128  {
129  $storeCode = 'store';
130  $this->storeManagerMock->expects($this->once())
131  ->method('getDefaultStoreView')
132  ->willReturn($this->storeMock);
133  $this->storeCookieManagerMock->expects($this->atLeastOnce())
134  ->method('getStoreCodeFromCookie')
135  ->willReturn($storeCode);
136  $this->storeRepositoryMock->expects($this->once())
137  ->method('getActiveStoreByCode')
138  ->willThrowException(new StoreIsInactiveException);
139  $this->storeCookieManagerMock->expects($this->once())
140  ->method('deleteStoreCookie')
141  ->with($this->storeMock);
142 
143  $this->plugin->beforeDispatch($this->subjectMock, $this->requestMock);
144  }
145 
150  {
151  $storeCode = 'store';
152  $this->storeManagerMock->expects($this->once())
153  ->method('getDefaultStoreView')
154  ->willReturn($this->storeMock);
155  $this->storeCookieManagerMock->expects($this->atLeastOnce())
156  ->method('getStoreCodeFromCookie')
157  ->willReturn($storeCode);
158  $this->storeRepositoryMock->expects($this->once())
159  ->method('getActiveStoreByCode')
160  ->willThrowException(new InvalidArgumentException);
161  $this->storeCookieManagerMock->expects($this->once())
162  ->method('deleteStoreCookie')
163  ->with($this->storeMock);
164 
165  $this->plugin->beforeDispatch($this->subjectMock, $this->requestMock);
166  }
167 
172  {
173  $storeCode = null;
174  $this->storeCookieManagerMock->expects($this->atLeastOnce())
175  ->method('getStoreCodeFromCookie')
176  ->willReturn($storeCode);
177  $this->storeManagerMock->expects($this->never())
178  ->method('getDefaultStoreView')
179  ->willReturn($this->storeMock);
180  $this->storeRepositoryMock->expects($this->never())
181  ->method('getActiveStoreByCode');
182  $this->storeCookieManagerMock->expects($this->never())
183  ->method('deleteStoreCookie')
184  ->with($this->storeMock);
185 
186  $this->plugin->beforeDispatch($this->subjectMock, $this->requestMock);
187  }
188 
193  {
194  $storeCode = 'store';
195  $this->storeCookieManagerMock->expects($this->atLeastOnce())
196  ->method('getStoreCodeFromCookie')
197  ->willReturn($storeCode);
198  $this->storeRepositoryMock->expects($this->atLeastOnce())
199  ->method('getActiveStoreByCode')
200  ->willReturn($this->storeMock);
201  $this->storeCookieManagerMock->expects($this->never())
202  ->method('deleteStoreCookie')
203  ->with($this->storeMock);
204 
205  $this->plugin->beforeDispatch($this->subjectMock, $this->requestMock);
206  }
207 }
$storeCode
Definition: indexer.php:15