RouterTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Cms\Test\Unit\Controller;
  7. /**
  8. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  9. */
  10. class RouterTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Cms\Controller\Router
  14. */
  15. private $router;
  16. /**
  17. * @var \Magento\Framework\Event\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. private $eventManagerMock;
  20. /**
  21. * @var \Magento\Cms\Model\PageFactory|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. private $pageFactoryMock;
  24. /**
  25. * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. private $storeManagerMock;
  28. /**
  29. * @var \Magento\Store\Api\Data\StoreInterface|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. private $storeMock;
  32. /**
  33. * @var \Magento\Framework\App\ActionFactory|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. private $actionFactoryMock;
  36. protected function setUp()
  37. {
  38. $this->eventManagerMock = $this->getMockBuilder(\Magento\Framework\Event\ManagerInterface::class)
  39. ->getMockForAbstractClass();
  40. $this->pageFactoryMock = $this->getMockBuilder(\Magento\Cms\Model\PageFactory::class)
  41. ->disableOriginalConstructor()
  42. ->setMethods(['create'])
  43. ->getMock();
  44. $this->storeMock = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)
  45. ->getMockForAbstractClass();
  46. $this->storeManagerMock = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
  47. ->getMockForAbstractClass();
  48. $this->storeManagerMock->expects($this->any())
  49. ->method('getStore')
  50. ->willReturn($this->storeMock);
  51. $this->actionFactoryMock = $this->getMockBuilder(\Magento\Framework\App\ActionFactory::class)
  52. ->disableOriginalConstructor()
  53. ->getMock();
  54. $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  55. $this->router = $objectManagerHelper->getObject(
  56. \Magento\Cms\Controller\Router::class,
  57. [
  58. 'eventManager' => $this->eventManagerMock,
  59. 'pageFactory' => $this->pageFactoryMock,
  60. 'storeManager' => $this->storeManagerMock,
  61. 'actionFactory' => $this->actionFactoryMock,
  62. ]
  63. );
  64. }
  65. public function testMatchCmsControllerRouterMatchBeforeEventParams()
  66. {
  67. $identifier = '/test';
  68. $trimmedIdentifier = 'test';
  69. $pageId = 1;
  70. $storeId = 1;
  71. /** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject $requestMock */
  72. $requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
  73. ->setMethods([
  74. 'getPathInfo',
  75. 'setModuleName',
  76. 'setControllerName',
  77. 'setActionName',
  78. 'setParam',
  79. 'setAlias',
  80. ])
  81. ->getMockForAbstractClass();
  82. $requestMock->expects($this->once())
  83. ->method('getPathInfo')
  84. ->willReturn($identifier);
  85. $requestMock->expects($this->once())
  86. ->method('setModuleName')
  87. ->with('cms')
  88. ->willReturnSelf();
  89. $requestMock->expects($this->once())
  90. ->method('setControllerName')
  91. ->with('page')
  92. ->willReturnSelf();
  93. $requestMock->expects($this->once())
  94. ->method('setActionName')
  95. ->with('view')
  96. ->willReturnSelf();
  97. $requestMock->expects($this->once())
  98. ->method('setParam')
  99. ->with('page_id', $pageId)
  100. ->willReturnSelf();
  101. $requestMock->expects($this->once())
  102. ->method('setAlias')
  103. ->with(\Magento\Framework\Url::REWRITE_REQUEST_PATH_ALIAS, $trimmedIdentifier)
  104. ->willReturnSelf();
  105. $condition = new \Magento\Framework\DataObject(['identifier' => $trimmedIdentifier, 'continue' => true]);
  106. $this->eventManagerMock->expects($this->once())
  107. ->method('dispatch')
  108. ->with(
  109. 'cms_controller_router_match_before',
  110. [
  111. 'router' => $this->router,
  112. 'condition' => $condition,
  113. ]
  114. )
  115. ->willReturnSelf();
  116. $pageMock = $this->getMockBuilder(\Magento\Cms\Model\Page::class)
  117. ->disableOriginalConstructor()
  118. ->getMock();
  119. $pageMock->expects($this->once())
  120. ->method('checkIdentifier')
  121. ->with($trimmedIdentifier, $storeId)
  122. ->willReturn($pageId);
  123. $this->pageFactoryMock->expects($this->once())
  124. ->method('create')
  125. ->willReturn($pageMock);
  126. $this->storeMock->expects($this->once())
  127. ->method('getId')
  128. ->willReturn($storeId);
  129. $actionMock = $this->getMockBuilder(\Magento\Framework\App\ActionInterface::class)
  130. ->getMockForAbstractClass();
  131. $this->actionFactoryMock->expects($this->once())
  132. ->method('create')
  133. ->with(\Magento\Framework\App\Action\Forward::class)
  134. ->willReturn($actionMock);
  135. $this->assertEquals($actionMock, $this->router->match($requestMock));
  136. }
  137. }