RouterTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Robots\Test\Unit\Controller;
  7. class RouterTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Framework\App\ActionFactory|\PHPUnit_Framework_MockObject_MockObject
  11. */
  12. private $actionFactoryMock;
  13. /**
  14. * @var \Magento\Framework\App\Router\ActionList|\PHPUnit_Framework_MockObject_MockObject
  15. */
  16. private $actionListMock;
  17. /**
  18. * @var \Magento\Framework\App\Route\ConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. private $routeConfigMock;
  21. /**
  22. * @var \Magento\Robots\Controller\Router
  23. */
  24. private $router;
  25. protected function setUp()
  26. {
  27. $this->actionFactoryMock = $this->getMockBuilder(\Magento\Framework\App\ActionFactory::class)
  28. ->disableOriginalConstructor()
  29. ->setMethods(['create'])
  30. ->getMock();
  31. $this->actionListMock = $this->getMockBuilder(\Magento\Framework\App\Router\ActionList::class)
  32. ->disableOriginalConstructor()
  33. ->getMock();
  34. $this->routeConfigMock = $this->getMockBuilder(\Magento\Framework\App\Route\ConfigInterface::class)
  35. ->getMockForAbstractClass();
  36. $this->router = new \Magento\Robots\Controller\Router(
  37. $this->actionFactoryMock,
  38. $this->actionListMock,
  39. $this->routeConfigMock
  40. );
  41. }
  42. /**
  43. * Check case when robots.txt file is not requested
  44. */
  45. public function testMatchNoRobotsRequested()
  46. {
  47. $identifier = 'test';
  48. $requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
  49. ->setMethods(['getPathInfo'])
  50. ->getMockForAbstractClass();
  51. $requestMock->expects($this->once())
  52. ->method('getPathInfo')
  53. ->willReturn($identifier);
  54. $this->assertNull($this->router->match($requestMock));
  55. }
  56. /**
  57. * Check case, when no existed modules in Magento to process 'robots' route
  58. */
  59. public function testMatchNoRobotsModules()
  60. {
  61. $identifier = 'robots.txt';
  62. $requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
  63. ->setMethods(['getPathInfo'])
  64. ->getMockForAbstractClass();
  65. $requestMock->expects($this->once())
  66. ->method('getPathInfo')
  67. ->willReturn($identifier);
  68. $this->routeConfigMock->expects($this->once())
  69. ->method('getModulesByFrontName')
  70. ->with('robots')
  71. ->willReturn([]);
  72. $this->assertNull($this->router->match($requestMock));
  73. }
  74. /**
  75. * Check the basic flow of match() method
  76. */
  77. public function testMatch()
  78. {
  79. $identifier = 'robots.txt';
  80. $moduleName = 'Magento_Robots';
  81. $actionClassName = \Magento\Robots\Controller\Index\Index::class;
  82. $requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
  83. ->setMethods(['getPathInfo'])
  84. ->getMockForAbstractClass();
  85. $requestMock->expects($this->once())
  86. ->method('getPathInfo')
  87. ->willReturn($identifier);
  88. $this->routeConfigMock->expects($this->once())
  89. ->method('getModulesByFrontName')
  90. ->with('robots')
  91. ->willReturn([$moduleName]);
  92. $this->actionListMock->expects($this->once())
  93. ->method('get')
  94. ->with($moduleName, null, 'index', 'index')
  95. ->willReturn($actionClassName);
  96. $actionClassMock = $this->getMockBuilder(\Magento\Robots\Controller\Index\Index::class)
  97. ->disableOriginalConstructor()
  98. ->getMock();
  99. $this->actionFactoryMock->expects($this->once())
  100. ->method('create')
  101. ->with($actionClassName)
  102. ->willReturn($actionClassMock);
  103. $this->assertInstanceOf($actionClassName, $this->router->match($requestMock));
  104. }
  105. }