Skip to content

Commit

Permalink
fix static, add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
engcom-Charlie committed Feb 11, 2020
1 parent 597bb18 commit 0fcb6a0
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 20 deletions.
11 changes: 6 additions & 5 deletions app/code/Magento/Store/Controller/Store/Redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,18 @@ class Redirect extends Action implements HttpGetActionInterface, HttpPostActionI
private $hashGenerator;

/**
* @var \Magento\Store\Model\StoreManagerInterface
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @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(
Expand Down Expand Up @@ -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})");
}
Expand All @@ -118,7 +119,7 @@ public function execute()
'_nosid' => true,
'_query' => $query
];
$this->storeManager->setCurrentStore($targetStore);

$this->_redirect->redirect($this->_response, 'stores/store/switch', $arguments);
}

Expand Down
60 changes: 45 additions & 15 deletions app/code/Magento/Store/Test/Unit/Controller/Store/RedirectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -67,7 +80,12 @@ class RedirectTest extends TestCase
/**
* @var Store|MockObject
*/
private $formStoreMock;
private $fromStoreMock;

/**
* @var Store|MockObject
*/
private $targetStoreMock;

/**
* @var Store|MockObject
Expand All @@ -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'])
Expand All @@ -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();
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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')
Expand All @@ -214,7 +244,7 @@ public function testRedirect(string $defaultStoreViewCode, string $storeCode): v
]
);

$this->assertEquals(null, $this->redirectController->execute());
$this->assertEquals(null, $this->model->execute());
}

/**
Expand Down Expand Up @@ -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());
}

/**
Expand All @@ -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());
}

/**
Expand All @@ -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]
];
}
}

0 comments on commit 0fcb6a0

Please sign in to comment.