Skip to content

Commit

Permalink
Added Test and backwards compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiasLufinity committed Jan 31, 2020
1 parent 346e544 commit 392bd3b
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 3 deletions.
5 changes: 2 additions & 3 deletions app/code/Magento/Store/Controller/Store/Redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ public function __construct(
\Magento\Framework\Session\Generic $session,
\Magento\Framework\Session\SidResolverInterface $sidResolver,
HashGenerator $hashGenerator,
StoreManagerInterface $storeManager
StoreManagerInterface $storeManager = null
) {
parent::__construct($context);
$this->storeRepository = $storeRepository;
$this->storeResolver = $storeResolver;
$this->hashGenerator = $hashGenerator;
$this->storeManager = $storeManager;
$this->storeManager = $storeManager ?: \Magento\Framework\App\ObjectManager::getInstance()->get(StoreManagerInterface::class);
}

/**
Expand Down Expand Up @@ -101,7 +101,6 @@ public function execute()
$this->_redirect->redirect($this->_response, $currentStore->getBaseUrl());
} else {
$encodedUrl = $this->_request->getParam(\Magento\Framework\App\ActionInterface::PARAM_NAME_URL_ENCODED);

$query = [
'___from_store' => $fromStore->getCode(),
StoreResolverInterface::PARAM_NAME => $targetStoreCode,
Expand Down
140 changes: 140 additions & 0 deletions app/code/Magento/Store/Test/Unit/Controller/Store/RedirectTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Store\Test\Unit\Controller\Store;

use Magento\Store\Api\StoreRepositoryInterface;
use Magento\Store\Api\StoreResolverInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Store\Model\StoreResolver;

/**
* Test class for \Magento\Store\Controller\Store\SwitchAction
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class RedirectTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\Store\Controller\Store\SwitchAction
*/
private $model;

/**
* @var StoreRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $storeRepositoryMock;

/**
* @var StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $storeManagerMock;

/**
* @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $requestMock;

/**
* @var \Magento\Framework\App\ResponseInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $responseMock;

/**
* @var \Magento\Framework\App\Response\RedirectInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $redirectMock;

/**
* @var StoreResolverInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $storeResolverMock;


/**
* @return void
*/
protected function setUp()
{
$this->storeManagerMock = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)->getMock();
$this->storeRepositoryMock = $this->getMockBuilder(\Magento\Store\Api\StoreRepositoryInterface::class)->getMock();
$this->requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
->disableOriginalConstructor()
->setMethods(['getHttpHost'])
->getMockForAbstractClass();
$this->responseMock = $this->getMockBuilder(\Magento\Framework\App\ResponseInterface::class)
->disableOriginalConstructor()
->setMethods(['setRedirect'])
->getMockForAbstractClass();
$this->storeResolverMock = $this->getMockBuilder(StoreResolverInterface::class)->getMock();
$this->redirectMock = $this->getMockBuilder(\Magento\Framework\App\Response\RedirectInterface::class)->getMock();

$this->model = (new ObjectManager($this))->getObject(
\Magento\Store\Controller\Store\Redirect::class,
[
'storeRepository' => $this->storeRepositoryMock,
'storeManager' => $this->storeManagerMock,
'storeResolver' => $this->storeResolverMock,
'_request' => $this->requestMock,
'_response' => $this->responseMock,
'_redirect' => $this->redirectMock,
]
);
}

/**
* @return void
*/
public function testExecute()
{
$storeToSwitchToCode = 'sv2';
$defaultStoreViewCode = 'default';
$defaultStoreViewMock = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)->getMock();
$storeToSwitchToMock = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)
->disableOriginalConstructor()
->setMethods(['isUseStoreInUrl'])
->getMockForAbstractClass();

$this->storeResolverMock
->expects($this->once())
->method('getCurrentStoreId')
->willReturn(1);

$this->storeRepositoryMock
->expects($this->once())
->method('getById')
->with(1)
->willReturn($defaultStoreViewCode);
$this->requestMock->expects($this->any())->method('getParam')->willReturnMap(
[
[StoreResolver::PARAM_NAME, null, $storeToSwitchToCode],
['___from_store', null, $defaultStoreViewCode]
]
);
$this->storeRepositoryMock
->expects($this->any())
->method('get')
->willReturnMap(
[
[$defaultStoreViewCode, $defaultStoreViewMock],
[$storeToSwitchToCode, $storeToSwitchToMock]
]
);

$defaultStoreViewMock
->expects($this->once())
->method('getCode')
->willReturn("default");

$this->storeManagerMock
->expects($this->once())
->method('setCurrentStore')
->with($storeToSwitchToMock);

$this->redirectMock->expects($this->once())->method('redirect');

$this->model->execute();
}
}

0 comments on commit 392bd3b

Please sign in to comment.