HttpTest.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App\Test\Unit;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. use Magento\Framework\App\SetupInfo;
  9. use Magento\Framework\App\Bootstrap;
  10. /**
  11. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  12. */
  13. class HttpTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  17. */
  18. protected $objectManager;
  19. /**
  20. * @var \PHPUnit_Framework_MockObject_MockObject
  21. */
  22. protected $responseMock;
  23. /**
  24. * @var \Magento\Framework\App\Http
  25. */
  26. protected $http;
  27. /**
  28. * @var \PHPUnit_Framework_MockObject_MockObject
  29. */
  30. protected $frontControllerMock;
  31. /**
  32. * @var \PHPUnit_Framework_MockObject_MockObject
  33. */
  34. protected $eventManagerMock;
  35. /**
  36. * @var \PHPUnit_Framework_MockObject_MockObject
  37. */
  38. protected $requestMock;
  39. /**
  40. * @var \PHPUnit_Framework_MockObject_MockObject
  41. */
  42. protected $objectManagerMock;
  43. /**
  44. * @var \PHPUnit_Framework_MockObject_MockObject
  45. */
  46. protected $areaListMock;
  47. /**
  48. * @var \PHPUnit_Framework_MockObject_MockObject
  49. */
  50. protected $configLoaderMock;
  51. /**
  52. * @var \PHPUnit_Framework_MockObject_MockObject
  53. */
  54. protected $filesystemMock;
  55. protected function setUp()
  56. {
  57. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  58. $cookieReaderMock = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\CookieReaderInterface::class)
  59. ->disableOriginalConstructor()
  60. ->getMock();
  61. $routeConfigMock = $this->getMockBuilder(\Magento\Framework\App\Route\ConfigInterface\Proxy::class)
  62. ->disableOriginalConstructor()
  63. ->getMock();
  64. $pathInfoProcessorMock = $this->getMockBuilder(\Magento\Framework\App\Request\PathInfoProcessorInterface::class)
  65. ->disableOriginalConstructor()
  66. ->getMock();
  67. $converterMock = $this->getMockBuilder(\Magento\Framework\Stdlib\StringUtils::class)
  68. ->disableOriginalConstructor()
  69. ->setMethods(['cleanString'])
  70. ->getMock();
  71. $objectManagerMock = $this->getMockBuilder(\Magento\Framework\ObjectManagerInterface::class)
  72. ->disableOriginalConstructor()
  73. ->getMock();
  74. $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
  75. ->setConstructorArgs([
  76. 'cookieReader' => $cookieReaderMock,
  77. 'converter' => $converterMock,
  78. 'routeConfig' => $routeConfigMock,
  79. 'pathInfoProcessor' => $pathInfoProcessorMock,
  80. 'objectManager' => $objectManagerMock
  81. ])
  82. ->setMethods(['getFrontName'])
  83. ->getMock();
  84. $this->areaListMock = $this->getMockBuilder(\Magento\Framework\App\AreaList::class)
  85. ->disableOriginalConstructor()
  86. ->setMethods(['getCodeByFrontName'])
  87. ->getMock();
  88. $this->configLoaderMock = $this->getMockBuilder(\Magento\Framework\App\ObjectManager\ConfigLoader::class)
  89. ->disableOriginalConstructor()
  90. ->setMethods(['load'])
  91. ->getMock();
  92. $this->objectManagerMock = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
  93. $this->responseMock = $this->createMock(\Magento\Framework\App\Response\Http::class);
  94. $this->frontControllerMock = $this->getMockBuilder(\Magento\Framework\App\FrontControllerInterface::class)
  95. ->disableOriginalConstructor()
  96. ->setMethods(['dispatch'])
  97. ->getMock();
  98. $this->eventManagerMock = $this->getMockBuilder(\Magento\Framework\Event\Manager::class)
  99. ->disableOriginalConstructor()
  100. ->setMethods(['dispatch'])
  101. ->getMock();
  102. $this->filesystemMock = $this->createMock(\Magento\Framework\Filesystem::class);
  103. $this->http = $this->objectManager->getObject(
  104. \Magento\Framework\App\Http::class,
  105. [
  106. 'objectManager' => $this->objectManagerMock,
  107. 'eventManager' => $this->eventManagerMock,
  108. 'areaList' => $this->areaListMock,
  109. 'request' => $this->requestMock,
  110. 'response' => $this->responseMock,
  111. 'configLoader' => $this->configLoaderMock,
  112. 'filesystem' => $this->filesystemMock,
  113. ]
  114. );
  115. }
  116. /**
  117. * Asserts mock objects with methods that are expected to be called when http->launch() is invoked.
  118. */
  119. private function setUpLaunch()
  120. {
  121. $frontName = 'frontName';
  122. $areaCode = 'areaCode';
  123. $this->requestMock->expects($this->once())->method('getFrontName')->will($this->returnValue($frontName));
  124. $this->areaListMock->expects($this->once())
  125. ->method('getCodeByFrontName')
  126. ->with($frontName)->will($this->returnValue($areaCode));
  127. $this->configLoaderMock->expects($this->once())
  128. ->method('load')->with($areaCode)->will($this->returnValue([]));
  129. $this->objectManagerMock->expects($this->once())->method('configure')->with([]);
  130. $this->objectManagerMock->expects($this->once())
  131. ->method('get')
  132. ->with(\Magento\Framework\App\FrontControllerInterface::class)
  133. ->will($this->returnValue($this->frontControllerMock));
  134. $this->frontControllerMock->expects($this->once())
  135. ->method('dispatch')
  136. ->with($this->requestMock)
  137. ->will($this->returnValue($this->responseMock));
  138. }
  139. public function testLaunchSuccess()
  140. {
  141. $this->setUpLaunch();
  142. $this->eventManagerMock->expects($this->once())
  143. ->method('dispatch')
  144. ->with(
  145. 'controller_front_send_response_before',
  146. ['request' => $this->requestMock, 'response' => $this->responseMock]
  147. );
  148. $this->assertSame($this->responseMock, $this->http->launch());
  149. }
  150. /**
  151. * @expectedException \Exception
  152. * @expectedExceptionMessage Message
  153. */
  154. public function testLaunchException()
  155. {
  156. $this->setUpLaunch();
  157. $this->frontControllerMock->expects($this->once())->method('dispatch')->with($this->requestMock)->will(
  158. $this->returnCallback(
  159. function () {
  160. throw new \Exception('Message');
  161. }
  162. )
  163. );
  164. $this->http->launch();
  165. }
  166. public function testHandleDeveloperModeNotInstalled()
  167. {
  168. $dir = $this->getMockForAbstractClass(\Magento\Framework\Filesystem\Directory\ReadInterface::class);
  169. $dir->expects($this->once())->method('getAbsolutePath')->willReturn(__DIR__);
  170. $this->filesystemMock->expects($this->once())
  171. ->method('getDirectoryRead')
  172. ->with(DirectoryList::ROOT)
  173. ->willReturn($dir);
  174. $this->responseMock->expects($this->once())->method('setRedirect')->with('/_files/');
  175. $this->responseMock->expects($this->once())->method('sendHeaders');
  176. $bootstrap = $this->getBootstrapNotInstalled();
  177. $bootstrap->expects($this->once())->method('getParams')->willReturn([
  178. 'SCRIPT_NAME' => '/index.php',
  179. 'DOCUMENT_ROOT' => __DIR__,
  180. 'SCRIPT_FILENAME' => __DIR__ . '/index.php',
  181. SetupInfo::PARAM_NOT_INSTALLED_URL_PATH => '_files',
  182. ]);
  183. $this->assertTrue($this->http->catchException($bootstrap, new \Exception('Test Message')));
  184. }
  185. public function testHandleDeveloperMode()
  186. {
  187. $this->filesystemMock->expects($this->once())
  188. ->method('getDirectoryRead')
  189. ->will($this->throwException(new \Exception('strange error')));
  190. $this->responseMock->expects($this->once())->method('setHttpResponseCode')->with(500);
  191. $this->responseMock->expects($this->once())->method('setHeader')->with('Content-Type', 'text/plain');
  192. $constraint = new \PHPUnit\Framework\Constraint\StringStartsWith('1 exception(s):');
  193. $this->responseMock->expects($this->once())->method('setBody')->with($constraint);
  194. $this->responseMock->expects($this->once())->method('sendResponse');
  195. $bootstrap = $this->getBootstrapNotInstalled();
  196. $bootstrap->expects($this->once())->method('getParams')->willReturn(
  197. ['DOCUMENT_ROOT' => 'something', 'SCRIPT_FILENAME' => 'something/else']
  198. );
  199. $this->assertTrue($this->http->catchException($bootstrap, new \Exception('Test')));
  200. }
  201. public function testCatchExceptionSessionException()
  202. {
  203. $this->responseMock->expects($this->once())->method('setRedirect');
  204. $this->responseMock->expects($this->once())->method('sendHeaders');
  205. $bootstrap = $this->createMock(\Magento\Framework\App\Bootstrap::class);
  206. $bootstrap->expects($this->once())->method('isDeveloperMode')->willReturn(false);
  207. $this->assertTrue($this->http->catchException(
  208. $bootstrap,
  209. new \Magento\Framework\Exception\SessionException(new \Magento\Framework\Phrase('Test'))
  210. ));
  211. }
  212. /**
  213. * Prepares a mock of bootstrap in "not installed" state
  214. *
  215. * @return \PHPUnit_Framework_MockObject_MockObject
  216. */
  217. private function getBootstrapNotInstalled()
  218. {
  219. $bootstrap = $this->createMock(\Magento\Framework\App\Bootstrap::class);
  220. $bootstrap->expects($this->once())->method('isDeveloperMode')->willReturn(true);
  221. $bootstrap->expects($this->once())->method('getErrorCode')->willReturn(Bootstrap::ERR_IS_INSTALLED);
  222. return $bootstrap;
  223. }
  224. }