20
20
use Magento \Store \Api \StoreResolverInterface ;
21
21
use Magento \Store \Controller \Store \Redirect ;
22
22
use Magento \Store \Model \Store ;
23
+ use Magento \Store \Model \StoreManagerInterface ;
23
24
use Magento \Store \Model \StoreResolver ;
24
25
use Magento \Store \Model \StoreSwitcher \HashGenerator ;
25
26
use PHPUnit \Framework \MockObject \MockObject ;
31
32
*/
32
33
class RedirectTest extends TestCase
33
34
{
34
- private const DEFAULT_STORE_VIEW_CODE = 'default ' ;
35
- private const STORE_CODE = 'sv1 ' ;
35
+ /**
36
+ * Stub for default store view code
37
+ */
38
+ private const STUB_DEFAULT_STORE_VIEW_CODE = 'default ' ;
39
+
40
+ /**
41
+ * Stub for default store code
42
+ */
43
+ private const STUB_STORE_CODE = 'sv1 ' ;
44
+
45
+ /**
46
+ * @var StoreManagerInterface|MockObject
47
+ */
48
+ private $ storeManagerMock ;
36
49
37
50
/**
38
51
* @var StoreRepositoryInterface|MockObject
@@ -67,7 +80,12 @@ class RedirectTest extends TestCase
67
80
/**
68
81
* @var Store|MockObject
69
82
*/
70
- private $ formStoreMock ;
83
+ private $ fromStoreMock ;
84
+
85
+ /**
86
+ * @var Store|MockObject
87
+ */
88
+ private $ targetStoreMock ;
71
89
72
90
/**
73
91
* @var Store|MockObject
@@ -87,13 +105,14 @@ class RedirectTest extends TestCase
87
105
/**
88
106
* @var Redirect
89
107
*/
90
- private $ redirectController ;
108
+ private $ model ;
91
109
92
110
/**
93
111
* @inheritDoc
94
112
*/
95
113
protected function setUp ()
96
114
{
115
+ $ this ->storeManagerMock = $ this ->createMock (StoreManagerInterface::class);
97
116
$ this ->requestMock = $ this ->getMockBuilder (RequestInterface::class)
98
117
->disableOriginalConstructor ()
99
118
->setMethods (['getParam ' ])
@@ -117,7 +136,11 @@ protected function setUp()
117
136
$ this ->responseMock = $ this ->getMockBuilder (ResponseInterface::class)
118
137
->disableOriginalConstructor ()
119
138
->getMockForAbstractClass ();
120
- $ this ->formStoreMock = $ this ->getMockBuilder (Store::class)
139
+ $ this ->fromStoreMock = $ this ->getMockBuilder (Store::class)
140
+ ->disableOriginalConstructor ()
141
+ ->setMethods (['getCode ' ])
142
+ ->getMockForAbstractClass ();
143
+ $ this ->targetStoreMock = $ this ->getMockBuilder (Store::class)
121
144
->disableOriginalConstructor ()
122
145
->setMethods (['getCode ' ])
123
146
->getMockForAbstractClass ();
@@ -150,9 +173,10 @@ protected function setUp()
150
173
'messageManager ' => $ this ->messageManagerMock ,
151
174
]
152
175
);
153
- $ this ->redirectController = $ objectManager ->getObject (
176
+ $ this ->model = $ objectManager ->getObject (
154
177
Redirect::class,
155
178
[
179
+ 'storeManager ' => $ this ->storeManagerMock ,
156
180
'storeRepository ' => $ this ->storeRepositoryMock ,
157
181
'storeResolver ' => $ this ->storeResolverMock ,
158
182
'sidResolver ' => $ this ->sidResolverMock ,
@@ -186,19 +210,25 @@ public function testRedirect(string $defaultStoreViewCode, string $storeCode): v
186
210
$ defaultStoreViewCode
187
211
);
188
212
$ this ->storeRepositoryMock
189
- ->expects ($ this ->once ( ))
213
+ ->expects ($ this ->exactly ( 2 ))
190
214
->method ('get ' )
191
- ->with ($ defaultStoreViewCode )
192
- ->willReturn ($ this ->formStoreMock );
193
- $ this ->formStoreMock
215
+ ->willReturnMap ([
216
+ [$ defaultStoreViewCode , $ this ->fromStoreMock ],
217
+ [$ storeCode , $ this ->targetStoreMock ],
218
+ ]);
219
+ $ this ->fromStoreMock
194
220
->expects ($ this ->once ())
195
221
->method ('getCode ' )
196
222
->willReturn ($ defaultStoreViewCode );
197
223
$ this ->hashGeneratorMock
198
224
->expects ($ this ->once ())
199
225
->method ('generateHash ' )
200
- ->with ($ this ->formStoreMock )
226
+ ->with ($ this ->fromStoreMock )
201
227
->willReturn ([]);
228
+ $ this ->storeManagerMock
229
+ ->expects ($ this ->once ())
230
+ ->method ('setCurrentStore ' )
231
+ ->with ($ this ->targetStoreMock );
202
232
$ this ->redirectMock
203
233
->expects ($ this ->once ())
204
234
->method ('redirect ' )
@@ -214,7 +244,7 @@ public function testRedirect(string $defaultStoreViewCode, string $storeCode): v
214
244
]
215
245
);
216
246
217
- $ this ->assertEquals (null , $ this ->redirectController ->execute ());
247
+ $ this ->assertEquals (null , $ this ->model ->execute ());
218
248
}
219
249
220
250
/**
@@ -257,7 +287,7 @@ public function testRedirectWithThrowsException(string $defaultStoreViewCode, st
257
287
->with ($ this ->responseMock , $ this ->currentStoreMock )
258
288
->willReturnSelf ();
259
289
260
- $ this ->assertEquals (null , $ this ->redirectController ->execute ());
290
+ $ this ->assertEquals (null , $ this ->model ->execute ());
261
291
}
262
292
263
293
/**
@@ -281,7 +311,7 @@ public function testRedirectTargetIsNull(): void
281
311
->expects ($ this ->never ())
282
312
->method ('get ' );
283
313
284
- $ this ->assertEquals ($ this ->responseMock , $ this ->redirectController ->execute ());
314
+ $ this ->assertEquals ($ this ->responseMock , $ this ->model ->execute ());
285
315
}
286
316
287
317
/**
@@ -292,7 +322,7 @@ public function testRedirectTargetIsNull(): void
292
322
public function getConfigDataProvider (): array
293
323
{
294
324
return [
295
- [self ::DEFAULT_STORE_VIEW_CODE , self ::STORE_CODE ]
325
+ [self ::STUB_DEFAULT_STORE_VIEW_CODE , self ::STUB_STORE_CODE ]
296
326
];
297
327
}
298
328
}
0 commit comments