|
7 | 7 | namespace Magento\Store\Test\Unit\Model\Plugin;
|
8 | 8 |
|
9 | 9 | use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
|
10 |
| -use Magento\Store\Model\StoreManagerInterface; |
| 10 | +use Magento\Store\Api\StoreResolverInterface; |
11 | 11 | use Magento\Framework\Exception\NoSuchEntityException;
|
12 | 12 | use Magento\Store\Model\StoreIsInactiveException;
|
13 | 13 | use \InvalidArgumentException;
|
14 | 14 |
|
15 | 15 | /**
|
16 |
| - * Class StoreCookieTest |
| 16 | + * Unit tests for \Magento\Store\Model\Plugin\StoreCookie class. |
| 17 | + * |
| 18 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
17 | 19 | */
|
18 | 20 | class StoreCookieTest extends \PHPUnit_Framework_TestCase
|
19 | 21 | {
|
@@ -52,6 +54,11 @@ class StoreCookieTest extends \PHPUnit_Framework_TestCase
|
52 | 54 | */
|
53 | 55 | protected $storeRepositoryMock;
|
54 | 56 |
|
| 57 | + /** |
| 58 | + * @var \Magento\Store\Api\StoreResolverInterface|\PHPUnit_Framework_MockObject_MockObject |
| 59 | + */ |
| 60 | + protected $storeResolverMock; |
| 61 | + |
55 | 62 | /**
|
56 | 63 | * Set up
|
57 | 64 | */
|
@@ -87,59 +94,151 @@ protected function setUp()
|
87 | 94 | ->setMethods([])
|
88 | 95 | ->getMock();
|
89 | 96 |
|
| 97 | + $this->storeResolverMock = $this->getMockBuilder(\Magento\Store\Api\StoreResolverInterface::class) |
| 98 | + ->disableOriginalConstructor() |
| 99 | + ->setMethods([]) |
| 100 | + ->getMock(); |
| 101 | + |
90 | 102 | $this->plugin = (new ObjectManager($this))->getObject(
|
91 | 103 | 'Magento\Store\Model\Plugin\StoreCookie',
|
92 | 104 | [
|
93 | 105 | 'storeManager' => $this->storeManagerMock,
|
94 | 106 | 'storeCookieManager' => $this->storeCookieManagerMock,
|
95 |
| - 'storeRepository' => $this->storeRepositoryMock |
| 107 | + 'storeRepository' => $this->storeRepositoryMock, |
| 108 | + 'storeResolver' => $this->storeResolverMock |
96 | 109 | ]
|
97 | 110 | );
|
98 | 111 | }
|
99 | 112 |
|
100 | 113 | public function testBeforeDispatchNoSuchEntity()
|
101 | 114 | {
|
102 | 115 | $storeCode = 'store';
|
103 |
| - $this->storeManagerMock->expects($this->once())->method('getDefaultStoreView')->willReturn($this->storeMock); |
104 |
| - $this->storeCookieManagerMock->expects($this->once())->method('getStoreCodeFromCookie')->willReturn($storeCode); |
| 116 | + $this->storeManagerMock->expects($this->once()) |
| 117 | + ->method('getDefaultStoreView') |
| 118 | + ->willReturn($this->storeMock); |
| 119 | + $this->storeCookieManagerMock->expects($this->atLeastOnce()) |
| 120 | + ->method('getStoreCodeFromCookie') |
| 121 | + ->willReturn($storeCode); |
105 | 122 | $this->storeRepositoryMock->expects($this->once())
|
106 | 123 | ->method('getActiveStoreByCode')
|
107 | 124 | ->willThrowException(new NoSuchEntityException);
|
108 |
| - $this->storeCookieManagerMock->expects($this->once())->method('deleteStoreCookie')->with($this->storeMock); |
| 125 | + $this->storeCookieManagerMock->expects($this->once()) |
| 126 | + ->method('deleteStoreCookie') |
| 127 | + ->with($this->storeMock); |
| 128 | + $this->requestMock->expects($this->atLeastOnce()) |
| 129 | + ->method('getParam') |
| 130 | + ->with(StoreResolverInterface::PARAM_NAME) |
| 131 | + ->willReturn(null); |
| 132 | + |
109 | 133 | $this->plugin->beforeDispatch($this->subjectMock, $this->requestMock);
|
110 | 134 | }
|
111 | 135 |
|
112 | 136 | public function testBeforeDispatchStoreIsInactive()
|
113 | 137 | {
|
114 | 138 | $storeCode = 'store';
|
115 |
| - $this->storeManagerMock->expects($this->once())->method('getDefaultStoreView')->willReturn($this->storeMock); |
116 |
| - $this->storeCookieManagerMock->expects($this->once())->method('getStoreCodeFromCookie')->willReturn($storeCode); |
| 139 | + $this->storeManagerMock->expects($this->once()) |
| 140 | + ->method('getDefaultStoreView') |
| 141 | + ->willReturn($this->storeMock); |
| 142 | + $this->storeCookieManagerMock->expects($this->atLeastOnce()) |
| 143 | + ->method('getStoreCodeFromCookie') |
| 144 | + ->willReturn($storeCode); |
117 | 145 | $this->storeRepositoryMock->expects($this->once())
|
118 | 146 | ->method('getActiveStoreByCode')
|
119 | 147 | ->willThrowException(new StoreIsInactiveException);
|
120 |
| - $this->storeCookieManagerMock->expects($this->once())->method('deleteStoreCookie')->with($this->storeMock); |
| 148 | + $this->storeCookieManagerMock->expects($this->once()) |
| 149 | + ->method('deleteStoreCookie') |
| 150 | + ->with($this->storeMock); |
| 151 | + $this->requestMock->expects($this->atLeastOnce()) |
| 152 | + ->method('getParam') |
| 153 | + ->with(StoreResolverInterface::PARAM_NAME) |
| 154 | + ->willReturn(null); |
| 155 | + |
121 | 156 | $this->plugin->beforeDispatch($this->subjectMock, $this->requestMock);
|
122 | 157 | }
|
123 | 158 |
|
124 | 159 | public function testBeforeDispatchInvalidArgument()
|
125 | 160 | {
|
126 | 161 | $storeCode = 'store';
|
127 |
| - $this->storeManagerMock->expects($this->once())->method('getDefaultStoreView')->willReturn($this->storeMock); |
128 |
| - $this->storeCookieManagerMock->expects($this->once())->method('getStoreCodeFromCookie')->willReturn($storeCode); |
| 162 | + $this->storeManagerMock->expects($this->once()) |
| 163 | + ->method('getDefaultStoreView') |
| 164 | + ->willReturn($this->storeMock); |
| 165 | + $this->storeCookieManagerMock->expects($this->atLeastOnce()) |
| 166 | + ->method('getStoreCodeFromCookie') |
| 167 | + ->willReturn($storeCode); |
129 | 168 | $this->storeRepositoryMock->expects($this->once())
|
130 | 169 | ->method('getActiveStoreByCode')
|
131 | 170 | ->willThrowException(new InvalidArgumentException);
|
132 |
| - $this->storeCookieManagerMock->expects($this->once())->method('deleteStoreCookie')->with($this->storeMock); |
| 171 | + $this->storeCookieManagerMock->expects($this->once()) |
| 172 | + ->method('deleteStoreCookie') |
| 173 | + ->with($this->storeMock); |
| 174 | + $this->requestMock->expects($this->atLeastOnce()) |
| 175 | + ->method('getParam') |
| 176 | + ->with(StoreResolverInterface::PARAM_NAME) |
| 177 | + ->willReturn(null); |
| 178 | + |
133 | 179 | $this->plugin->beforeDispatch($this->subjectMock, $this->requestMock);
|
134 | 180 | }
|
135 | 181 |
|
136 | 182 | public function testBeforeDispatchNoStoreCookie()
|
137 | 183 | {
|
138 | 184 | $storeCode = null;
|
139 |
| - $this->storeCookieManagerMock->expects($this->once())->method('getStoreCodeFromCookie')->willReturn($storeCode); |
140 |
| - $this->storeManagerMock->expects($this->never())->method('getDefaultStoreView')->willReturn($this->storeMock); |
141 |
| - $this->storeRepositoryMock->expects($this->never())->method('getActiveStoreByCode'); |
142 |
| - $this->storeCookieManagerMock->expects($this->never())->method('deleteStoreCookie')->with($this->storeMock); |
| 185 | + $this->storeCookieManagerMock->expects($this->atLeastOnce()) |
| 186 | + ->method('getStoreCodeFromCookie') |
| 187 | + ->willReturn($storeCode); |
| 188 | + $this->storeManagerMock->expects($this->never()) |
| 189 | + ->method('getDefaultStoreView') |
| 190 | + ->willReturn($this->storeMock); |
| 191 | + $this->storeRepositoryMock->expects($this->never()) |
| 192 | + ->method('getActiveStoreByCode'); |
| 193 | + $this->storeCookieManagerMock->expects($this->never()) |
| 194 | + ->method('deleteStoreCookie') |
| 195 | + ->with($this->storeMock); |
| 196 | + |
| 197 | + $this->storeResolverMock->expects($this->atLeastOnce()) |
| 198 | + ->method('getCurrentStoreId') |
| 199 | + ->willReturn(1); |
| 200 | + |
| 201 | + $this->storeRepositoryMock->expects($this->atLeastOnce()) |
| 202 | + ->method('getActiveStoreById') |
| 203 | + ->willReturn($this->storeMock); |
| 204 | + |
| 205 | + $this->storeCookieManagerMock->expects($this->atLeastOnce()) |
| 206 | + ->method('setStoreCookie') |
| 207 | + ->with($this->storeMock); |
| 208 | + |
| 209 | + $this->plugin->beforeDispatch($this->subjectMock, $this->requestMock); |
| 210 | + } |
| 211 | + |
| 212 | + public function testBeforeDispatchWithStoreRequestParam() |
| 213 | + { |
| 214 | + $storeCode = 'store'; |
| 215 | + $this->storeCookieManagerMock->expects($this->atLeastOnce()) |
| 216 | + ->method('getStoreCodeFromCookie') |
| 217 | + ->willReturn($storeCode); |
| 218 | + $this->storeRepositoryMock->expects($this->atLeastOnce()) |
| 219 | + ->method('getActiveStoreByCode') |
| 220 | + ->willReturn($this->storeMock); |
| 221 | + $this->storeCookieManagerMock->expects($this->never()) |
| 222 | + ->method('deleteStoreCookie') |
| 223 | + ->with($this->storeMock); |
| 224 | + |
| 225 | + $this->requestMock->expects($this->atLeastOnce()) |
| 226 | + ->method('getParam') |
| 227 | + ->with(StoreResolverInterface::PARAM_NAME) |
| 228 | + ->willReturn($storeCode); |
| 229 | + |
| 230 | + $this->storeResolverMock->expects($this->atLeastOnce()) |
| 231 | + ->method('getCurrentStoreId') |
| 232 | + ->willReturn(1); |
| 233 | + |
| 234 | + $this->storeRepositoryMock->expects($this->atLeastOnce()) |
| 235 | + ->method('getActiveStoreById') |
| 236 | + ->willReturn($this->storeMock); |
| 237 | + |
| 238 | + $this->storeCookieManagerMock->expects($this->atLeastOnce()) |
| 239 | + ->method('setStoreCookie') |
| 240 | + ->with($this->storeMock); |
| 241 | + |
143 | 242 | $this->plugin->beforeDispatch($this->subjectMock, $this->requestMock);
|
144 | 243 | }
|
145 | 244 | }
|
0 commit comments