|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2013-2017 Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Cms\Test\Unit\Controller; |
| 7 | + |
| 8 | +/** |
| 9 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 10 | + */ |
| 11 | +class RouterTest extends \PHPUnit_Framework_TestCase |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @var \Magento\Cms\Controller\Router |
| 15 | + */ |
| 16 | + private $router; |
| 17 | + |
| 18 | + /** |
| 19 | + * @var \Magento\Framework\Event\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject |
| 20 | + */ |
| 21 | + private $eventManagerMock; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var \Magento\Cms\Model\PageFactory|\PHPUnit_Framework_MockObject_MockObject |
| 25 | + */ |
| 26 | + private $pageFactoryMock; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject |
| 30 | + */ |
| 31 | + private $storeManagerMock; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var \Magento\Store\Api\Data\StoreInterface|\PHPUnit_Framework_MockObject_MockObject |
| 35 | + */ |
| 36 | + private $storeMock; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var \Magento\Framework\App\ActionFactory|\PHPUnit_Framework_MockObject_MockObject |
| 40 | + */ |
| 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 | + $trimedIdentifier = 'test'; |
| 82 | + $pageId = 1; |
| 83 | + $storeId = 1; |
| 84 | + |
| 85 | + /** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject $requestMock */ |
| 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, $trimedIdentifier) |
| 118 | + ->willReturnSelf(); |
| 119 | + |
| 120 | + $condition = new \Magento\Framework\DataObject(['identifier' => $trimedIdentifier, '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($trimedIdentifier, $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 | +} |
0 commit comments