Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RouterTest.php
Go to the documentation of this file.
1 <?php
8 
9 class RouterTest extends \PHPUnit\Framework\TestCase
10 {
14  private $actionFactoryMock;
15 
19  private $actionListMock;
20 
24  private $routeConfigMock;
25 
29  private $router;
30 
31  protected function setUp()
32  {
33  $this->actionFactoryMock = $this->getMockBuilder(\Magento\Framework\App\ActionFactory::class)
34  ->disableOriginalConstructor()
35  ->setMethods(['create'])
36  ->getMock();
37 
38  $this->actionListMock = $this->getMockBuilder(\Magento\Framework\App\Router\ActionList::class)
39  ->disableOriginalConstructor()
40  ->getMock();
41 
42  $this->routeConfigMock = $this->getMockBuilder(\Magento\Framework\App\Route\ConfigInterface::class)
43  ->getMockForAbstractClass();
44 
45  $this->router = new \Magento\Robots\Controller\Router(
46  $this->actionFactoryMock,
47  $this->actionListMock,
48  $this->routeConfigMock
49  );
50  }
51 
55  public function testMatchNoRobotsRequested()
56  {
57  $identifier = 'test';
58 
59  $requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
60  ->setMethods(['getPathInfo'])
61  ->getMockForAbstractClass();
62  $requestMock->expects($this->once())
63  ->method('getPathInfo')
64  ->willReturn($identifier);
65 
66  $this->assertNull($this->router->match($requestMock));
67  }
68 
72  public function testMatchNoRobotsModules()
73  {
74  $identifier = 'robots.txt';
75 
76  $requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
77  ->setMethods(['getPathInfo'])
78  ->getMockForAbstractClass();
79  $requestMock->expects($this->once())
80  ->method('getPathInfo')
81  ->willReturn($identifier);
82 
83  $this->routeConfigMock->expects($this->once())
84  ->method('getModulesByFrontName')
85  ->with('robots')
86  ->willReturn([]);
87 
88  $this->assertNull($this->router->match($requestMock));
89  }
90 
94  public function testMatch()
95  {
96  $identifier = 'robots.txt';
97  $moduleName = 'Magento_Robots';
98  $actionClassName = \Magento\Robots\Controller\Index\Index::class;
99 
100  $requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
101  ->setMethods(['getPathInfo'])
102  ->getMockForAbstractClass();
103  $requestMock->expects($this->once())
104  ->method('getPathInfo')
105  ->willReturn($identifier);
106 
107  $this->routeConfigMock->expects($this->once())
108  ->method('getModulesByFrontName')
109  ->with('robots')
110  ->willReturn([$moduleName]);
111 
112  $this->actionListMock->expects($this->once())
113  ->method('get')
114  ->with($moduleName, null, 'index', 'index')
115  ->willReturn($actionClassName);
116 
117  $actionClassMock = $this->getMockBuilder(\Magento\Robots\Controller\Index\Index::class)
118  ->disableOriginalConstructor()
119  ->getMock();
120 
121  $this->actionFactoryMock->expects($this->once())
122  ->method('create')
123  ->with($actionClassName)
124  ->willReturn($actionClassMock);
125 
126  $this->assertInstanceOf($actionClassName, $this->router->match($requestMock));
127  }
128 }