IndexTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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\Noroute;
  7. class IndexTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Cms\Controller\Noroute\Index
  11. */
  12. protected $_controller;
  13. /**
  14. * @var \PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $_cmsHelperMock;
  17. /**
  18. * @var \PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $_requestMock;
  21. /**
  22. * @var \Magento\Framework\Controller\Result\ForwardFactory|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $forwardFactoryMock;
  25. /**
  26. * @var \Magento\Framework\Controller\Result\Forward|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $forwardMock;
  29. /**
  30. * @var \Magento\Framework\View\Result\Page|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $resultPageMock;
  33. protected function setUp()
  34. {
  35. $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  36. $objectManagerMock = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
  37. $responseMock = $this->createMock(\Magento\Framework\App\Response\Http::class);
  38. $this->resultPageMock = $this->getMockBuilder(\Magento\Framework\View\Result\Page::class)
  39. ->disableOriginalConstructor()
  40. ->getMock();
  41. $this->forwardFactoryMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\ForwardFactory::class)
  42. ->setMethods(['create'])
  43. ->disableOriginalConstructor()
  44. ->getMock();
  45. $this->forwardMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Forward::class)
  46. ->disableOriginalConstructor()
  47. ->getMock();
  48. $this->forwardFactoryMock->expects($this->any())
  49. ->method('create')
  50. ->willReturn($this->forwardMock);
  51. $scopeConfigMock = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
  52. $this->_requestMock = $this->createMock(\Magento\Framework\App\Request\Http::class);
  53. $this->_cmsHelperMock = $this->createMock(\Magento\Cms\Helper\Page::class);
  54. $valueMap = [
  55. [\Magento\Framework\App\Config\ScopeConfigInterface::class,
  56. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  57. $scopeConfigMock,
  58. ],
  59. [\Magento\Cms\Helper\Page::class, $this->_cmsHelperMock],
  60. ];
  61. $objectManagerMock->expects($this->any())->method('get')->will($this->returnValueMap($valueMap));
  62. $scopeConfigMock->expects(
  63. $this->once()
  64. )->method(
  65. 'getValue'
  66. )->with(
  67. \Magento\Cms\Helper\Page::XML_PATH_NO_ROUTE_PAGE
  68. )->will(
  69. $this->returnValue('pageId')
  70. );
  71. $this->_controller = $helper->getObject(
  72. \Magento\Cms\Controller\Noroute\Index::class,
  73. ['response' => $responseMock, 'objectManager' => $objectManagerMock, 'request' => $this->_requestMock,
  74. 'resultForwardFactory' => $this->forwardFactoryMock
  75. ]
  76. );
  77. }
  78. public function testExecuteResultPage()
  79. {
  80. $this->resultPageMock->expects(
  81. $this->at(0)
  82. )->method(
  83. 'setStatusHeader'
  84. )->with(404, '1.1', 'Not Found')->will(
  85. $this->returnSelf()
  86. );
  87. $this->resultPageMock->expects(
  88. $this->at(1)
  89. )->method(
  90. 'setHeader'
  91. )->with(
  92. 'Status',
  93. '404 File not found'
  94. )->will(
  95. $this->returnSelf()
  96. );
  97. $this->_cmsHelperMock->expects(
  98. $this->once()
  99. )->method(
  100. 'prepareResultPage'
  101. )->will(
  102. $this->returnValue($this->resultPageMock)
  103. );
  104. $this->assertSame(
  105. $this->resultPageMock,
  106. $this->_controller->execute()
  107. );
  108. }
  109. public function testExecuteResultForward()
  110. {
  111. $this->forwardMock->expects(
  112. $this->once()
  113. )->method(
  114. 'setController'
  115. )->with(
  116. 'index'
  117. )->will(
  118. $this->returnSelf()
  119. );
  120. $this->forwardMock->expects(
  121. $this->once()
  122. )->method(
  123. 'forward'
  124. )->with(
  125. 'defaultNoRoute'
  126. )->will(
  127. $this->returnSelf()
  128. );
  129. $this->_cmsHelperMock->expects(
  130. $this->once()
  131. )->method(
  132. 'prepareResultPage'
  133. )->will(
  134. $this->returnValue(false)
  135. );
  136. $this->assertSame(
  137. $this->forwardMock,
  138. $this->_controller->execute()
  139. );
  140. }
  141. }