Skip to content

Commit 0fcb6a0

Browse files
fix static, add unit test
1 parent 597bb18 commit 0fcb6a0

File tree

2 files changed

+51
-20
lines changed

2 files changed

+51
-20
lines changed

app/code/Magento/Store/Controller/Store/Redirect.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,18 @@ class Redirect extends Action implements HttpGetActionInterface, HttpPostActionI
4444
private $hashGenerator;
4545

4646
/**
47-
* @var \Magento\Store\Model\StoreManagerInterface
47+
* @var StoreManagerInterface
4848
*/
4949
private $storeManager;
5050

5151
/**
5252
* @param Context $context
5353
* @param StoreRepositoryInterface $storeRepository
5454
* @param StoreResolverInterface $storeResolver
55-
* @param \Magento\Framework\Session\Generic $session
56-
* @param \Magento\Framework\Session\SidResolverInterface $sidResolver
55+
* @param Generic $session
56+
* @param SidResolverInterface $sidResolver
5757
* @param HashGenerator $hashGenerator
58-
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
58+
* @param StoreManagerInterface $storeManager
5959
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
6060
*/
6161
public function __construct(
@@ -96,6 +96,7 @@ public function execute()
9696
$fromStore = $this->storeRepository->get($fromStoreCode);
9797
/** @var Store $targetStore */
9898
$targetStore = $this->storeRepository->get($targetStoreCode);
99+
$this->storeManager->setCurrentStore($targetStore);
99100
} catch (NoSuchEntityException $e) {
100101
$error = __("Requested store is not found ({$fromStoreCode})");
101102
}
@@ -118,7 +119,7 @@ public function execute()
118119
'_nosid' => true,
119120
'_query' => $query
120121
];
121-
$this->storeManager->setCurrentStore($targetStore);
122+
122123
$this->_redirect->redirect($this->_response, 'stores/store/switch', $arguments);
123124
}
124125

app/code/Magento/Store/Test/Unit/Controller/Store/RedirectTest.php

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Magento\Store\Api\StoreResolverInterface;
2121
use Magento\Store\Controller\Store\Redirect;
2222
use Magento\Store\Model\Store;
23+
use Magento\Store\Model\StoreManagerInterface;
2324
use Magento\Store\Model\StoreResolver;
2425
use Magento\Store\Model\StoreSwitcher\HashGenerator;
2526
use PHPUnit\Framework\MockObject\MockObject;
@@ -31,8 +32,20 @@
3132
*/
3233
class RedirectTest extends TestCase
3334
{
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;
3649

3750
/**
3851
* @var StoreRepositoryInterface|MockObject
@@ -67,7 +80,12 @@ class RedirectTest extends TestCase
6780
/**
6881
* @var Store|MockObject
6982
*/
70-
private $formStoreMock;
83+
private $fromStoreMock;
84+
85+
/**
86+
* @var Store|MockObject
87+
*/
88+
private $targetStoreMock;
7189

7290
/**
7391
* @var Store|MockObject
@@ -87,13 +105,14 @@ class RedirectTest extends TestCase
87105
/**
88106
* @var Redirect
89107
*/
90-
private $redirectController;
108+
private $model;
91109

92110
/**
93111
* @inheritDoc
94112
*/
95113
protected function setUp()
96114
{
115+
$this->storeManagerMock = $this->createMock(StoreManagerInterface::class);
97116
$this->requestMock = $this->getMockBuilder(RequestInterface::class)
98117
->disableOriginalConstructor()
99118
->setMethods(['getParam'])
@@ -117,7 +136,11 @@ protected function setUp()
117136
$this->responseMock = $this->getMockBuilder(ResponseInterface::class)
118137
->disableOriginalConstructor()
119138
->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)
121144
->disableOriginalConstructor()
122145
->setMethods(['getCode'])
123146
->getMockForAbstractClass();
@@ -150,9 +173,10 @@ protected function setUp()
150173
'messageManager' => $this->messageManagerMock,
151174
]
152175
);
153-
$this->redirectController = $objectManager->getObject(
176+
$this->model = $objectManager->getObject(
154177
Redirect::class,
155178
[
179+
'storeManager' => $this->storeManagerMock,
156180
'storeRepository' => $this->storeRepositoryMock,
157181
'storeResolver' => $this->storeResolverMock,
158182
'sidResolver' => $this->sidResolverMock,
@@ -186,19 +210,25 @@ public function testRedirect(string $defaultStoreViewCode, string $storeCode): v
186210
$defaultStoreViewCode
187211
);
188212
$this->storeRepositoryMock
189-
->expects($this->once())
213+
->expects($this->exactly(2))
190214
->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
194220
->expects($this->once())
195221
->method('getCode')
196222
->willReturn($defaultStoreViewCode);
197223
$this->hashGeneratorMock
198224
->expects($this->once())
199225
->method('generateHash')
200-
->with($this->formStoreMock)
226+
->with($this->fromStoreMock)
201227
->willReturn([]);
228+
$this->storeManagerMock
229+
->expects($this->once())
230+
->method('setCurrentStore')
231+
->with($this->targetStoreMock);
202232
$this->redirectMock
203233
->expects($this->once())
204234
->method('redirect')
@@ -214,7 +244,7 @@ public function testRedirect(string $defaultStoreViewCode, string $storeCode): v
214244
]
215245
);
216246

217-
$this->assertEquals(null, $this->redirectController->execute());
247+
$this->assertEquals(null, $this->model->execute());
218248
}
219249

220250
/**
@@ -257,7 +287,7 @@ public function testRedirectWithThrowsException(string $defaultStoreViewCode, st
257287
->with($this->responseMock, $this->currentStoreMock)
258288
->willReturnSelf();
259289

260-
$this->assertEquals(null, $this->redirectController->execute());
290+
$this->assertEquals(null, $this->model->execute());
261291
}
262292

263293
/**
@@ -281,7 +311,7 @@ public function testRedirectTargetIsNull(): void
281311
->expects($this->never())
282312
->method('get');
283313

284-
$this->assertEquals($this->responseMock, $this->redirectController->execute());
314+
$this->assertEquals($this->responseMock, $this->model->execute());
285315
}
286316

287317
/**
@@ -292,7 +322,7 @@ public function testRedirectTargetIsNull(): void
292322
public function getConfigDataProvider(): array
293323
{
294324
return [
295-
[self::DEFAULT_STORE_VIEW_CODE, self::STORE_CODE]
325+
[self::STUB_DEFAULT_STORE_VIEW_CODE, self::STUB_STORE_CODE]
296326
];
297327
}
298328
}

0 commit comments

Comments
 (0)