|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Store\Test\Unit\Controller\Store; |
| 7 | + |
| 8 | +use Magento\Store\Api\StoreRepositoryInterface; |
| 9 | +use Magento\Store\Api\StoreResolverInterface; |
| 10 | +use Magento\Store\Model\StoreManagerInterface; |
| 11 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 12 | +use Magento\Store\Model\StoreResolver; |
| 13 | + |
| 14 | +/** |
| 15 | + * Test class for \Magento\Store\Controller\Store\SwitchAction |
| 16 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 17 | + */ |
| 18 | +class RedirectTest extends \PHPUnit\Framework\TestCase |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @var \Magento\Store\Controller\Store\SwitchAction |
| 22 | + */ |
| 23 | + private $model; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var StoreRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject |
| 27 | + */ |
| 28 | + private $storeRepositoryMock; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject |
| 32 | + */ |
| 33 | + private $storeManagerMock; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject |
| 37 | + */ |
| 38 | + private $requestMock; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var \Magento\Framework\App\ResponseInterface|\PHPUnit_Framework_MockObject_MockObject |
| 42 | + */ |
| 43 | + private $responseMock; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var \Magento\Framework\App\Response\RedirectInterface|\PHPUnit_Framework_MockObject_MockObject |
| 47 | + */ |
| 48 | + private $redirectMock; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var StoreResolverInterface|\PHPUnit_Framework_MockObject_MockObject |
| 52 | + */ |
| 53 | + private $storeResolverMock; |
| 54 | + |
| 55 | + |
| 56 | + /** |
| 57 | + * @return void |
| 58 | + */ |
| 59 | + protected function setUp() |
| 60 | + { |
| 61 | + $this->storeManagerMock = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)->getMock(); |
| 62 | + $this->storeRepositoryMock = $this->getMockBuilder(\Magento\Store\Api\StoreRepositoryInterface::class)->getMock(); |
| 63 | + $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class) |
| 64 | + ->disableOriginalConstructor() |
| 65 | + ->setMethods(['getHttpHost']) |
| 66 | + ->getMockForAbstractClass(); |
| 67 | + $this->responseMock = $this->getMockBuilder(\Magento\Framework\App\ResponseInterface::class) |
| 68 | + ->disableOriginalConstructor() |
| 69 | + ->setMethods(['setRedirect']) |
| 70 | + ->getMockForAbstractClass(); |
| 71 | + $this->storeResolverMock = $this->getMockBuilder(StoreResolverInterface::class)->getMock(); |
| 72 | + $this->redirectMock = $this->getMockBuilder(\Magento\Framework\App\Response\RedirectInterface::class)->getMock(); |
| 73 | + |
| 74 | + $this->model = (new ObjectManager($this))->getObject( |
| 75 | + \Magento\Store\Controller\Store\Redirect::class, |
| 76 | + [ |
| 77 | + 'storeRepository' => $this->storeRepositoryMock, |
| 78 | + 'storeManager' => $this->storeManagerMock, |
| 79 | + 'storeResolver' => $this->storeResolverMock, |
| 80 | + '_request' => $this->requestMock, |
| 81 | + '_response' => $this->responseMock, |
| 82 | + '_redirect' => $this->redirectMock, |
| 83 | + ] |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @return void |
| 89 | + */ |
| 90 | + public function testExecute() |
| 91 | + { |
| 92 | + $storeToSwitchToCode = 'sv2'; |
| 93 | + $defaultStoreViewCode = 'default'; |
| 94 | + $defaultStoreViewMock = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)->getMock(); |
| 95 | + $storeToSwitchToMock = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class) |
| 96 | + ->disableOriginalConstructor() |
| 97 | + ->setMethods(['isUseStoreInUrl']) |
| 98 | + ->getMockForAbstractClass(); |
| 99 | + |
| 100 | + $this->storeResolverMock |
| 101 | + ->expects($this->once()) |
| 102 | + ->method('getCurrentStoreId') |
| 103 | + ->willReturn(1); |
| 104 | + |
| 105 | + $this->storeRepositoryMock |
| 106 | + ->expects($this->once()) |
| 107 | + ->method('getById') |
| 108 | + ->with(1) |
| 109 | + ->willReturn($defaultStoreViewCode); |
| 110 | + $this->requestMock->expects($this->any())->method('getParam')->willReturnMap( |
| 111 | + [ |
| 112 | + [StoreResolver::PARAM_NAME, null, $storeToSwitchToCode], |
| 113 | + ['___from_store', null, $defaultStoreViewCode] |
| 114 | + ] |
| 115 | + ); |
| 116 | + $this->storeRepositoryMock |
| 117 | + ->expects($this->any()) |
| 118 | + ->method('get') |
| 119 | + ->willReturnMap( |
| 120 | + [ |
| 121 | + [$defaultStoreViewCode, $defaultStoreViewMock], |
| 122 | + [$storeToSwitchToCode, $storeToSwitchToMock] |
| 123 | + ] |
| 124 | + ); |
| 125 | + |
| 126 | + $defaultStoreViewMock |
| 127 | + ->expects($this->once()) |
| 128 | + ->method('getCode') |
| 129 | + ->willReturn("default"); |
| 130 | + |
| 131 | + $this->storeManagerMock |
| 132 | + ->expects($this->once()) |
| 133 | + ->method('setCurrentStore') |
| 134 | + ->with($storeToSwitchToMock); |
| 135 | + |
| 136 | + $this->redirectMock->expects($this->once())->method('redirect'); |
| 137 | + |
| 138 | + $this->model->execute(); |
| 139 | + } |
| 140 | +} |
0 commit comments