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
7 
11 class RouterTest extends \PHPUnit\Framework\TestCase
12 {
16  private $router;
17 
21  private $eventManagerMock;
22 
26  private $pageFactoryMock;
27 
31  private $storeManagerMock;
32 
36  private $storeMock;
37 
41  private $actionFactoryMock;
42 
43  protected function setUp()
44  {
45  $this->eventManagerMock = $this->getMockBuilder(\Magento\Framework\Event\ManagerInterface::class)
46  ->getMockForAbstractClass();
47 
48  $this->pageFactoryMock = $this->getMockBuilder(\Magento\Cms\Model\PageFactory::class)
49  ->disableOriginalConstructor()
50  ->setMethods(['create'])
51  ->getMock();
52 
53  $this->storeMock = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)
54  ->getMockForAbstractClass();
55 
56  $this->storeManagerMock = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
57  ->getMockForAbstractClass();
58  $this->storeManagerMock->expects($this->any())
59  ->method('getStore')
60  ->willReturn($this->storeMock);
61 
62  $this->actionFactoryMock = $this->getMockBuilder(\Magento\Framework\App\ActionFactory::class)
63  ->disableOriginalConstructor()
64  ->getMock();
65 
66  $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
67  $this->router = $objectManagerHelper->getObject(
68  \Magento\Cms\Controller\Router::class,
69  [
70  'eventManager' => $this->eventManagerMock,
71  'pageFactory' => $this->pageFactoryMock,
72  'storeManager' => $this->storeManagerMock,
73  'actionFactory' => $this->actionFactoryMock,
74  ]
75  );
76  }
77 
78  public function testMatchCmsControllerRouterMatchBeforeEventParams()
79  {
80  $identifier = '/test';
81  $trimmedIdentifier = 'test';
82  $pageId = 1;
83  $storeId = 1;
84 
86  $requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
87  ->setMethods([
88  'getPathInfo',
89  'setModuleName',
90  'setControllerName',
91  'setActionName',
92  'setParam',
93  'setAlias',
94  ])
95  ->getMockForAbstractClass();
96  $requestMock->expects($this->once())
97  ->method('getPathInfo')
98  ->willReturn($identifier);
99  $requestMock->expects($this->once())
100  ->method('setModuleName')
101  ->with('cms')
102  ->willReturnSelf();
103  $requestMock->expects($this->once())
104  ->method('setControllerName')
105  ->with('page')
106  ->willReturnSelf();
107  $requestMock->expects($this->once())
108  ->method('setActionName')
109  ->with('view')
110  ->willReturnSelf();
111  $requestMock->expects($this->once())
112  ->method('setParam')
113  ->with('page_id', $pageId)
114  ->willReturnSelf();
115  $requestMock->expects($this->once())
116  ->method('setAlias')
117  ->with(\Magento\Framework\Url::REWRITE_REQUEST_PATH_ALIAS, $trimmedIdentifier)
118  ->willReturnSelf();
119 
120  $condition = new \Magento\Framework\DataObject(['identifier' => $trimmedIdentifier, 'continue' => true]);
121 
122  $this->eventManagerMock->expects($this->once())
123  ->method('dispatch')
124  ->with(
125  'cms_controller_router_match_before',
126  [
127  'router' => $this->router,
128  'condition' => $condition,
129  ]
130  )
131  ->willReturnSelf();
132 
133  $pageMock = $this->getMockBuilder(\Magento\Cms\Model\Page::class)
134  ->disableOriginalConstructor()
135  ->getMock();
136  $pageMock->expects($this->once())
137  ->method('checkIdentifier')
138  ->with($trimmedIdentifier, $storeId)
139  ->willReturn($pageId);
140 
141  $this->pageFactoryMock->expects($this->once())
142  ->method('create')
143  ->willReturn($pageMock);
144 
145  $this->storeMock->expects($this->once())
146  ->method('getId')
147  ->willReturn($storeId);
148 
149  $actionMock = $this->getMockBuilder(\Magento\Framework\App\ActionInterface::class)
150  ->getMockForAbstractClass();
151 
152  $this->actionFactoryMock->expects($this->once())
153  ->method('create')
154  ->with(\Magento\Framework\App\Action\Forward::class)
155  ->willReturn($actionMock);
156 
157  $this->assertEquals($actionMock, $this->router->match($requestMock));
158  }
159 }