BaseTest.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. /**
  3. * Tests Magento\Framework\App\Router\Base
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\App\Test\Unit\Router;
  9. class BaseTest extends \Magento\Framework\TestFramework\Unit\BaseTestCase
  10. {
  11. /**
  12. * @var \Magento\Framework\App\Router\Base
  13. */
  14. private $model;
  15. /**
  16. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\Request\Http
  17. */
  18. private $requestMock;
  19. /**
  20. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\Route\ConfigInterface
  21. */
  22. private $routeConfigMock;
  23. /**
  24. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\State
  25. */
  26. private $appStateMock;
  27. /**
  28. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\Router\ActionList
  29. */
  30. private $actionListMock;
  31. /**
  32. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\ActionFactory
  33. */
  34. private $actionFactoryMock;
  35. /**
  36. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Code\NameBuilder
  37. */
  38. private $nameBuilderMock;
  39. /**
  40. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\DefaultPathInterface
  41. */
  42. private $defaultPathMock;
  43. protected function setUp()
  44. {
  45. parent::setUp();
  46. // Create mocks
  47. $this->requestMock = $this->basicMock(\Magento\Framework\App\Request\Http::class);
  48. $this->routeConfigMock = $this->basicMock(\Magento\Framework\App\Route\ConfigInterface::class);
  49. $this->appStateMock = $this->createPartialMock(\Magento\Framework\App\State::class, ['isInstalled']);
  50. $this->actionListMock = $this->basicMock(\Magento\Framework\App\Router\ActionList::class);
  51. $this->actionFactoryMock = $this->basicMock(\Magento\Framework\App\ActionFactory::class);
  52. $this->nameBuilderMock = $this->basicMock(\Magento\Framework\Code\NameBuilder::class);
  53. $this->defaultPathMock = $this->basicMock(\Magento\Framework\App\DefaultPathInterface::class);
  54. // Prepare SUT
  55. $mocks = [
  56. 'actionList' => $this->actionListMock,
  57. 'actionFactory' => $this->actionFactoryMock,
  58. 'routeConfig' => $this->routeConfigMock,
  59. 'appState' => $this->appStateMock,
  60. 'nameBuilder' => $this->nameBuilderMock,
  61. 'defaultPath' => $this->defaultPathMock,
  62. ];
  63. $this->model = $this->objectManager->getObject(\Magento\Framework\App\Router\Base::class, $mocks);
  64. }
  65. public function testMatch()
  66. {
  67. // Test Data
  68. $actionInstance = 'action instance';
  69. $moduleFrontName = 'module front name';
  70. $actionPath = 'action path';
  71. $actionName = 'action name';
  72. $actionClassName = \Magento\Framework\App\Action\Action::class;
  73. $moduleName = 'module name';
  74. $moduleList = [$moduleName];
  75. // Stubs
  76. $this->requestMock->expects($this->any())->method('getModuleName')->willReturn($moduleFrontName);
  77. $this->requestMock->expects($this->any())->method('getControllerName')->willReturn($actionPath);
  78. $this->requestMock->expects($this->any())->method('getActionName')->willReturn($actionName);
  79. $this->routeConfigMock->expects($this->any())->method('getModulesByFrontName')->willReturn($moduleList);
  80. $this->appStateMock->expects($this->any())->method('isInstalled')->willReturn(true);
  81. $this->actionListMock->expects($this->any())->method('get')->willReturn($actionClassName);
  82. $this->actionFactoryMock->expects($this->any())->method('create')->willReturn($actionInstance);
  83. // Expectations and Test
  84. $this->requestExpects('setModuleName', $moduleFrontName)
  85. ->requestExpects('setControllerName', $actionPath)
  86. ->requestExpects('setActionName', $actionName)
  87. ->requestExpects('setControllerModule', $moduleName);
  88. $this->assertSame($actionInstance, $this->model->match($this->requestMock));
  89. }
  90. public function testMatchUseParams()
  91. {
  92. // Test Data
  93. $actionInstance = 'action instance';
  94. $moduleFrontName = 'module front name';
  95. $actionPath = 'action path';
  96. $actionName = 'action name';
  97. $actionClassName = \Magento\Framework\App\Action\Action::class;
  98. $moduleName = 'module name';
  99. $moduleList = [$moduleName];
  100. $paramList = $moduleFrontName . '/' . $actionPath . '/' . $actionName . '/key/val/key2/val2/';
  101. // Stubs
  102. $this->requestMock->expects($this->any())->method('getPathInfo')->willReturn($paramList);
  103. $this->routeConfigMock->expects($this->any())->method('getModulesByFrontName')->willReturn($moduleList);
  104. $this->appStateMock->expects($this->any())->method('isInstalled')->willReturn(false);
  105. $this->actionListMock->expects($this->any())->method('get')->willReturn($actionClassName);
  106. $this->actionFactoryMock->expects($this->any())->method('create')->willReturn($actionInstance);
  107. // Expectations and Test
  108. $this->requestExpects('setModuleName', $moduleFrontName)
  109. ->requestExpects('setControllerName', $actionPath)
  110. ->requestExpects('setActionName', $actionName)
  111. ->requestExpects('setControllerModule', $moduleName);
  112. $this->assertSame($actionInstance, $this->model->match($this->requestMock));
  113. }
  114. public function testMatchUseDefaultPath()
  115. {
  116. // Test Data
  117. $actionInstance = 'action instance';
  118. $moduleFrontName = 'module front name';
  119. $actionPath = 'action path';
  120. $actionName = 'action name';
  121. $actionClassName = \Magento\Framework\App\Action\Action::class;
  122. $moduleName = 'module name';
  123. $moduleList = [$moduleName];
  124. // Stubs
  125. $defaultReturnMap = [
  126. ['module', $moduleFrontName],
  127. ['controller', $actionPath],
  128. ['action', $actionName],
  129. ];
  130. $this->defaultPathMock->expects($this->any())->method('getPart')->willReturnMap($defaultReturnMap);
  131. $this->routeConfigMock->expects($this->any())->method('getModulesByFrontName')->willReturn($moduleList);
  132. $this->appStateMock->expects($this->any())->method('isInstalled')->willReturn(false);
  133. $this->actionListMock->expects($this->any())->method('get')->willReturn($actionClassName);
  134. $this->actionFactoryMock->expects($this->any())->method('create')->willReturn($actionInstance);
  135. // Expectations and Test
  136. $this->requestExpects('setModuleName', $moduleFrontName)
  137. ->requestExpects('setControllerName', $actionPath)
  138. ->requestExpects('setActionName', $actionName)
  139. ->requestExpects('setControllerModule', $moduleName);
  140. $this->assertSame($actionInstance, $this->model->match($this->requestMock));
  141. }
  142. public function testMatchEmptyModuleList()
  143. {
  144. // Test Data
  145. $actionInstance = 'action instance';
  146. $moduleFrontName = 'module front name';
  147. $actionPath = 'action path';
  148. $actionName = 'action name';
  149. $actionClassName = \Magento\Framework\App\Action\Action::class;
  150. $emptyModuleList = [];
  151. // Stubs
  152. $this->requestMock->expects($this->any())->method('getModuleName')->willReturn($moduleFrontName);
  153. $this->routeConfigMock->expects($this->any())->method('getModulesByFrontName')->willReturn($emptyModuleList);
  154. $this->requestMock->expects($this->any())->method('getControllerName')->willReturn($actionPath);
  155. $this->requestMock->expects($this->any())->method('getActionName')->willReturn($actionName);
  156. $this->appStateMock->expects($this->any())->method('isInstalled')->willReturn(false);
  157. $this->actionListMock->expects($this->any())->method('get')->willReturn($actionClassName);
  158. $this->actionFactoryMock->expects($this->any())->method('create')->willReturn($actionInstance);
  159. // Test
  160. $this->assertNull($this->model->match($this->requestMock));
  161. }
  162. public function testMatchEmptyActionInstance()
  163. {
  164. // Test Data
  165. $nullActionInstance = null;
  166. $moduleFrontName = 'module front name';
  167. $actionPath = 'action path';
  168. $actionName = 'action name';
  169. $actionClassName = \Magento\Framework\App\Action\Action::class;
  170. $moduleName = 'module name';
  171. $moduleList = [$moduleName];
  172. // Stubs
  173. $this->requestMock->expects($this->any())->method('getModuleName')->willReturn($moduleFrontName);
  174. $this->routeConfigMock->expects($this->any())->method('getModulesByFrontName')->willReturn($moduleList);
  175. $this->requestMock->expects($this->any())->method('getControllerName')->willReturn($actionPath);
  176. $this->requestMock->expects($this->any())->method('getActionName')->willReturn($actionName);
  177. $this->appStateMock->expects($this->any())->method('isInstalled')->willReturn(false);
  178. $this->actionListMock->expects($this->any())->method('get')->willReturn($actionClassName);
  179. $this->actionFactoryMock->expects($this->any())->method('create')->willReturn($nullActionInstance);
  180. // Expectations and Test
  181. $this->assertNull($this->model->match($this->requestMock));
  182. }
  183. public function testGetActionClassName()
  184. {
  185. $className = 'name of class';
  186. $module = 'module';
  187. $prefix = 'Controller';
  188. $actionPath = 'action path';
  189. $this->nameBuilderMock->expects($this->once())
  190. ->method('buildClassName')
  191. ->with([$module, $prefix, $actionPath])
  192. ->willReturn($className);
  193. $this->assertEquals($className, $this->model->getActionClassName($module, $actionPath));
  194. }
  195. /**
  196. * Generate a stub with an expected usage for the request mock object
  197. *
  198. * @param string $method
  199. * @param string $with
  200. * @return $this
  201. */
  202. private function requestExpects($method, $with)
  203. {
  204. $this->requestMock->expects($this->once())
  205. ->method($method)
  206. ->with($with);
  207. return $this;
  208. }
  209. }