RouterTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\App;
  7. /**
  8. * @magentoAppArea adminhtml
  9. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  10. */
  11. class RouterTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Backend\App\Router
  15. */
  16. protected $model;
  17. /**
  18. * @var \Magento\Framework\ObjectManagerInterface
  19. */
  20. protected $objectManager;
  21. protected function setUp()
  22. {
  23. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  24. $this->model = $this->objectManager->create(\Magento\Backend\App\Router::class);
  25. }
  26. public function testRouterCanProcessRequestsWithProperPathInfo()
  27. {
  28. $request = $this->createMock(\Magento\Framework\App\Request\Http::class);
  29. $request->expects($this->once())->method('getPathInfo')->will($this->returnValue('backend/admin/dashboard'));
  30. $this->assertInstanceOf(\Magento\Backend\Controller\Adminhtml\Dashboard::class, $this->model->match($request));
  31. }
  32. /**
  33. * @param string $module
  34. * @param string $controller
  35. * @param string $className
  36. *
  37. * @dataProvider getControllerClassNameDataProvider
  38. */
  39. public function testGetControllerClassName($module, $controller, $className)
  40. {
  41. $this->assertEquals($className, $this->model->getActionClassName($module, $controller));
  42. }
  43. public function getControllerClassNameDataProvider()
  44. {
  45. return [
  46. ['Magento_TestModule', 'controller', \Magento\TestModule\Controller\Adminhtml\Controller::class],
  47. ];
  48. }
  49. public function testMatchCustomNoRouteAction()
  50. {
  51. if (!\Magento\TestFramework\Helper\Bootstrap::canTestHeaders()) {
  52. $this->markTestSkipped('Can\'t test get match without sending headers');
  53. }
  54. $routers = [
  55. 'testmodule' => [
  56. 'frontName' => 'testfixture',
  57. 'id' => 'testfixture',
  58. 'modules' => ['Magento_TestFixture'],
  59. ],
  60. ];
  61. $routeConfig = $this->getMockBuilder(\Magento\Framework\App\Route\Config::class)
  62. ->setMethods(['_getRoutes'])
  63. ->setConstructorArgs(
  64. [
  65. 'reader' => $this->objectManager->get(\Magento\Framework\App\Route\Config\Reader::class),
  66. 'cache' => $this->objectManager->get(\Magento\Framework\Config\CacheInterface::class),
  67. 'configScope' => $this->objectManager->get(\Magento\Framework\Config\ScopeInterface::class),
  68. 'areaList' => $this->objectManager->get(\Magento\Framework\App\AreaList::class),
  69. 'cacheId' => 'RoutesConfig'
  70. ]
  71. )
  72. ->getMock();
  73. $routeConfig->expects($this->any())->method('_getRoutes')->will($this->returnValue($routers));
  74. $defaultRouter = $this->objectManager->create(
  75. \Magento\Backend\App\Router::class,
  76. ['routeConfig' => $routeConfig]
  77. );
  78. /** @var $request \Magento\TestFramework\Request */
  79. $request = $this->objectManager->get(\Magento\TestFramework\Request::class);
  80. $request->setPathInfo('backend/testfixture/test_controller');
  81. $controller = $defaultRouter->match($request);
  82. $this->assertInstanceOf(\Magento\TestFixture\Controller\Adminhtml\Noroute::class, $controller);
  83. $this->assertEquals('noroute', $request->getActionName());
  84. }
  85. }