From 0fcb6a0c06cbcbf733da284bb94b85a5e1ea1f16 Mon Sep 17 00:00:00 2001 From: "vadim.malesh" Date: Tue, 11 Feb 2020 13:50:50 +0200 Subject: [PATCH] fix static, add unit test --- .../Store/Controller/Store/Redirect.php | 11 ++-- .../Unit/Controller/Store/RedirectTest.php | 60 ++++++++++++++----- 2 files changed, 51 insertions(+), 20 deletions(-) diff --git a/app/code/Magento/Store/Controller/Store/Redirect.php b/app/code/Magento/Store/Controller/Store/Redirect.php index 03cdde5cdff2b..45924b5b0d28a 100644 --- a/app/code/Magento/Store/Controller/Store/Redirect.php +++ b/app/code/Magento/Store/Controller/Store/Redirect.php @@ -44,7 +44,7 @@ class Redirect extends Action implements HttpGetActionInterface, HttpPostActionI private $hashGenerator; /** - * @var \Magento\Store\Model\StoreManagerInterface + * @var StoreManagerInterface */ private $storeManager; @@ -52,10 +52,10 @@ class Redirect extends Action implements HttpGetActionInterface, HttpPostActionI * @param Context $context * @param StoreRepositoryInterface $storeRepository * @param StoreResolverInterface $storeResolver - * @param \Magento\Framework\Session\Generic $session - * @param \Magento\Framework\Session\SidResolverInterface $sidResolver + * @param Generic $session + * @param SidResolverInterface $sidResolver * @param HashGenerator $hashGenerator - * @param \Magento\Store\Model\StoreManagerInterface $storeManager + * @param StoreManagerInterface $storeManager * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function __construct( @@ -96,6 +96,7 @@ public function execute() $fromStore = $this->storeRepository->get($fromStoreCode); /** @var Store $targetStore */ $targetStore = $this->storeRepository->get($targetStoreCode); + $this->storeManager->setCurrentStore($targetStore); } catch (NoSuchEntityException $e) { $error = __("Requested store is not found ({$fromStoreCode})"); } @@ -118,7 +119,7 @@ public function execute() '_nosid' => true, '_query' => $query ]; - $this->storeManager->setCurrentStore($targetStore); + $this->_redirect->redirect($this->_response, 'stores/store/switch', $arguments); } diff --git a/app/code/Magento/Store/Test/Unit/Controller/Store/RedirectTest.php b/app/code/Magento/Store/Test/Unit/Controller/Store/RedirectTest.php index 4408c45d6a640..cb92f14e6227c 100755 --- a/app/code/Magento/Store/Test/Unit/Controller/Store/RedirectTest.php +++ b/app/code/Magento/Store/Test/Unit/Controller/Store/RedirectTest.php @@ -20,6 +20,7 @@ use Magento\Store\Api\StoreResolverInterface; use Magento\Store\Controller\Store\Redirect; use Magento\Store\Model\Store; +use Magento\Store\Model\StoreManagerInterface; use Magento\Store\Model\StoreResolver; use Magento\Store\Model\StoreSwitcher\HashGenerator; use PHPUnit\Framework\MockObject\MockObject; @@ -31,8 +32,20 @@ */ class RedirectTest extends TestCase { - private const DEFAULT_STORE_VIEW_CODE = 'default'; - private const STORE_CODE = 'sv1'; + /** + * Stub for default store view code + */ + private const STUB_DEFAULT_STORE_VIEW_CODE = 'default'; + + /** + * Stub for default store code + */ + private const STUB_STORE_CODE = 'sv1'; + + /** + * @var StoreManagerInterface|MockObject + */ + private $storeManagerMock; /** * @var StoreRepositoryInterface|MockObject @@ -67,7 +80,12 @@ class RedirectTest extends TestCase /** * @var Store|MockObject */ - private $formStoreMock; + private $fromStoreMock; + + /** + * @var Store|MockObject + */ + private $targetStoreMock; /** * @var Store|MockObject @@ -87,13 +105,14 @@ class RedirectTest extends TestCase /** * @var Redirect */ - private $redirectController; + private $model; /** * @inheritDoc */ protected function setUp() { + $this->storeManagerMock = $this->createMock(StoreManagerInterface::class); $this->requestMock = $this->getMockBuilder(RequestInterface::class) ->disableOriginalConstructor() ->setMethods(['getParam']) @@ -117,7 +136,11 @@ protected function setUp() $this->responseMock = $this->getMockBuilder(ResponseInterface::class) ->disableOriginalConstructor() ->getMockForAbstractClass(); - $this->formStoreMock = $this->getMockBuilder(Store::class) + $this->fromStoreMock = $this->getMockBuilder(Store::class) + ->disableOriginalConstructor() + ->setMethods(['getCode']) + ->getMockForAbstractClass(); + $this->targetStoreMock = $this->getMockBuilder(Store::class) ->disableOriginalConstructor() ->setMethods(['getCode']) ->getMockForAbstractClass(); @@ -150,9 +173,10 @@ protected function setUp() 'messageManager' => $this->messageManagerMock, ] ); - $this->redirectController = $objectManager->getObject( + $this->model = $objectManager->getObject( Redirect::class, [ + 'storeManager' => $this->storeManagerMock, 'storeRepository' => $this->storeRepositoryMock, 'storeResolver' => $this->storeResolverMock, 'sidResolver' => $this->sidResolverMock, @@ -186,19 +210,25 @@ public function testRedirect(string $defaultStoreViewCode, string $storeCode): v $defaultStoreViewCode ); $this->storeRepositoryMock - ->expects($this->once()) + ->expects($this->exactly(2)) ->method('get') - ->with($defaultStoreViewCode) - ->willReturn($this->formStoreMock); - $this->formStoreMock + ->willReturnMap([ + [$defaultStoreViewCode, $this->fromStoreMock], + [$storeCode, $this->targetStoreMock], + ]); + $this->fromStoreMock ->expects($this->once()) ->method('getCode') ->willReturn($defaultStoreViewCode); $this->hashGeneratorMock ->expects($this->once()) ->method('generateHash') - ->with($this->formStoreMock) + ->with($this->fromStoreMock) ->willReturn([]); + $this->storeManagerMock + ->expects($this->once()) + ->method('setCurrentStore') + ->with($this->targetStoreMock); $this->redirectMock ->expects($this->once()) ->method('redirect') @@ -214,7 +244,7 @@ public function testRedirect(string $defaultStoreViewCode, string $storeCode): v ] ); - $this->assertEquals(null, $this->redirectController->execute()); + $this->assertEquals(null, $this->model->execute()); } /** @@ -257,7 +287,7 @@ public function testRedirectWithThrowsException(string $defaultStoreViewCode, st ->with($this->responseMock, $this->currentStoreMock) ->willReturnSelf(); - $this->assertEquals(null, $this->redirectController->execute()); + $this->assertEquals(null, $this->model->execute()); } /** @@ -281,7 +311,7 @@ public function testRedirectTargetIsNull(): void ->expects($this->never()) ->method('get'); - $this->assertEquals($this->responseMock, $this->redirectController->execute()); + $this->assertEquals($this->responseMock, $this->model->execute()); } /** @@ -292,7 +322,7 @@ public function testRedirectTargetIsNull(): void public function getConfigDataProvider(): array { return [ - [self::DEFAULT_STORE_VIEW_CODE, self::STORE_CODE] + [self::STUB_DEFAULT_STORE_VIEW_CODE, self::STUB_STORE_CODE] ]; } }