123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- <?php
- /**
- * Tests Magento\Framework\App\Router\Base
- *
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\App\Test\Unit\Router;
- class BaseTest extends \Magento\Framework\TestFramework\Unit\BaseTestCase
- {
- /**
- * @var \Magento\Framework\App\Router\Base
- */
- private $model;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\Request\Http
- */
- private $requestMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\Route\ConfigInterface
- */
- private $routeConfigMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\State
- */
- private $appStateMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\Router\ActionList
- */
- private $actionListMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\ActionFactory
- */
- private $actionFactoryMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Code\NameBuilder
- */
- private $nameBuilderMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\DefaultPathInterface
- */
- private $defaultPathMock;
- protected function setUp()
- {
- parent::setUp();
- // Create mocks
- $this->requestMock = $this->basicMock(\Magento\Framework\App\Request\Http::class);
- $this->routeConfigMock = $this->basicMock(\Magento\Framework\App\Route\ConfigInterface::class);
- $this->appStateMock = $this->createPartialMock(\Magento\Framework\App\State::class, ['isInstalled']);
- $this->actionListMock = $this->basicMock(\Magento\Framework\App\Router\ActionList::class);
- $this->actionFactoryMock = $this->basicMock(\Magento\Framework\App\ActionFactory::class);
- $this->nameBuilderMock = $this->basicMock(\Magento\Framework\Code\NameBuilder::class);
- $this->defaultPathMock = $this->basicMock(\Magento\Framework\App\DefaultPathInterface::class);
- // Prepare SUT
- $mocks = [
- 'actionList' => $this->actionListMock,
- 'actionFactory' => $this->actionFactoryMock,
- 'routeConfig' => $this->routeConfigMock,
- 'appState' => $this->appStateMock,
- 'nameBuilder' => $this->nameBuilderMock,
- 'defaultPath' => $this->defaultPathMock,
- ];
- $this->model = $this->objectManager->getObject(\Magento\Framework\App\Router\Base::class, $mocks);
- }
- public function testMatch()
- {
- // Test Data
- $actionInstance = 'action instance';
- $moduleFrontName = 'module front name';
- $actionPath = 'action path';
- $actionName = 'action name';
- $actionClassName = \Magento\Framework\App\Action\Action::class;
- $moduleName = 'module name';
- $moduleList = [$moduleName];
- // Stubs
- $this->requestMock->expects($this->any())->method('getModuleName')->willReturn($moduleFrontName);
- $this->requestMock->expects($this->any())->method('getControllerName')->willReturn($actionPath);
- $this->requestMock->expects($this->any())->method('getActionName')->willReturn($actionName);
- $this->routeConfigMock->expects($this->any())->method('getModulesByFrontName')->willReturn($moduleList);
- $this->appStateMock->expects($this->any())->method('isInstalled')->willReturn(true);
- $this->actionListMock->expects($this->any())->method('get')->willReturn($actionClassName);
- $this->actionFactoryMock->expects($this->any())->method('create')->willReturn($actionInstance);
- // Expectations and Test
- $this->requestExpects('setModuleName', $moduleFrontName)
- ->requestExpects('setControllerName', $actionPath)
- ->requestExpects('setActionName', $actionName)
- ->requestExpects('setControllerModule', $moduleName);
- $this->assertSame($actionInstance, $this->model->match($this->requestMock));
- }
- public function testMatchUseParams()
- {
- // Test Data
- $actionInstance = 'action instance';
- $moduleFrontName = 'module front name';
- $actionPath = 'action path';
- $actionName = 'action name';
- $actionClassName = \Magento\Framework\App\Action\Action::class;
- $moduleName = 'module name';
- $moduleList = [$moduleName];
- $paramList = $moduleFrontName . '/' . $actionPath . '/' . $actionName . '/key/val/key2/val2/';
- // Stubs
- $this->requestMock->expects($this->any())->method('getPathInfo')->willReturn($paramList);
- $this->routeConfigMock->expects($this->any())->method('getModulesByFrontName')->willReturn($moduleList);
- $this->appStateMock->expects($this->any())->method('isInstalled')->willReturn(false);
- $this->actionListMock->expects($this->any())->method('get')->willReturn($actionClassName);
- $this->actionFactoryMock->expects($this->any())->method('create')->willReturn($actionInstance);
- // Expectations and Test
- $this->requestExpects('setModuleName', $moduleFrontName)
- ->requestExpects('setControllerName', $actionPath)
- ->requestExpects('setActionName', $actionName)
- ->requestExpects('setControllerModule', $moduleName);
- $this->assertSame($actionInstance, $this->model->match($this->requestMock));
- }
- public function testMatchUseDefaultPath()
- {
- // Test Data
- $actionInstance = 'action instance';
- $moduleFrontName = 'module front name';
- $actionPath = 'action path';
- $actionName = 'action name';
- $actionClassName = \Magento\Framework\App\Action\Action::class;
- $moduleName = 'module name';
- $moduleList = [$moduleName];
- // Stubs
- $defaultReturnMap = [
- ['module', $moduleFrontName],
- ['controller', $actionPath],
- ['action', $actionName],
- ];
- $this->defaultPathMock->expects($this->any())->method('getPart')->willReturnMap($defaultReturnMap);
- $this->routeConfigMock->expects($this->any())->method('getModulesByFrontName')->willReturn($moduleList);
- $this->appStateMock->expects($this->any())->method('isInstalled')->willReturn(false);
- $this->actionListMock->expects($this->any())->method('get')->willReturn($actionClassName);
- $this->actionFactoryMock->expects($this->any())->method('create')->willReturn($actionInstance);
- // Expectations and Test
- $this->requestExpects('setModuleName', $moduleFrontName)
- ->requestExpects('setControllerName', $actionPath)
- ->requestExpects('setActionName', $actionName)
- ->requestExpects('setControllerModule', $moduleName);
- $this->assertSame($actionInstance, $this->model->match($this->requestMock));
- }
- public function testMatchEmptyModuleList()
- {
- // Test Data
- $actionInstance = 'action instance';
- $moduleFrontName = 'module front name';
- $actionPath = 'action path';
- $actionName = 'action name';
- $actionClassName = \Magento\Framework\App\Action\Action::class;
- $emptyModuleList = [];
- // Stubs
- $this->requestMock->expects($this->any())->method('getModuleName')->willReturn($moduleFrontName);
- $this->routeConfigMock->expects($this->any())->method('getModulesByFrontName')->willReturn($emptyModuleList);
- $this->requestMock->expects($this->any())->method('getControllerName')->willReturn($actionPath);
- $this->requestMock->expects($this->any())->method('getActionName')->willReturn($actionName);
- $this->appStateMock->expects($this->any())->method('isInstalled')->willReturn(false);
- $this->actionListMock->expects($this->any())->method('get')->willReturn($actionClassName);
- $this->actionFactoryMock->expects($this->any())->method('create')->willReturn($actionInstance);
- // Test
- $this->assertNull($this->model->match($this->requestMock));
- }
- public function testMatchEmptyActionInstance()
- {
- // Test Data
- $nullActionInstance = null;
- $moduleFrontName = 'module front name';
- $actionPath = 'action path';
- $actionName = 'action name';
- $actionClassName = \Magento\Framework\App\Action\Action::class;
- $moduleName = 'module name';
- $moduleList = [$moduleName];
- // Stubs
- $this->requestMock->expects($this->any())->method('getModuleName')->willReturn($moduleFrontName);
- $this->routeConfigMock->expects($this->any())->method('getModulesByFrontName')->willReturn($moduleList);
- $this->requestMock->expects($this->any())->method('getControllerName')->willReturn($actionPath);
- $this->requestMock->expects($this->any())->method('getActionName')->willReturn($actionName);
- $this->appStateMock->expects($this->any())->method('isInstalled')->willReturn(false);
- $this->actionListMock->expects($this->any())->method('get')->willReturn($actionClassName);
- $this->actionFactoryMock->expects($this->any())->method('create')->willReturn($nullActionInstance);
- // Expectations and Test
- $this->assertNull($this->model->match($this->requestMock));
- }
- public function testGetActionClassName()
- {
- $className = 'name of class';
- $module = 'module';
- $prefix = 'Controller';
- $actionPath = 'action path';
- $this->nameBuilderMock->expects($this->once())
- ->method('buildClassName')
- ->with([$module, $prefix, $actionPath])
- ->willReturn($className);
- $this->assertEquals($className, $this->model->getActionClassName($module, $actionPath));
- }
- /**
- * Generate a stub with an expected usage for the request mock object
- *
- * @param string $method
- * @param string $with
- * @return $this
- */
- private function requestExpects($method, $with)
- {
- $this->requestMock->expects($this->once())
- ->method($method)
- ->with($with);
- return $this;
- }
- }
|