BootstrapTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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\Bootstrap;
  8. use Magento\Framework\App\Filesystem\DirectoryList;
  9. use Magento\Framework\App\MaintenanceMode;
  10. use Magento\Framework\App\State;
  11. /**
  12. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. */
  14. class BootstrapTest extends \PHPUnit\Framework\TestCase
  15. {
  16. /**
  17. * @var \Magento\Framework\AppInterface | \PHPUnit_Framework_MockObject_MockObject
  18. */
  19. protected $application;
  20. /**
  21. * @var \Magento\Framework\App\ObjectManagerFactory | \PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $objectManagerFactory;
  24. /**
  25. * @var \Magento\Framework\ObjectManager\ObjectManager | \PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $objectManager;
  28. /**
  29. * @var \Psr\Log\LoggerInterface | \PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $logger;
  32. /**
  33. * @var \Magento\Framework\App\Filesystem\DirectoryList | \PHPUnit_Framework_MockObject_MockObject
  34. */
  35. protected $dirs;
  36. /**
  37. * @var \Magento\Framework\Filesystem\Directory\ReadInterface | \PHPUnit_Framework_MockObject_MockObject
  38. */
  39. protected $configDir;
  40. /**
  41. * @var MaintenanceMode | \PHPUnit_Framework_MockObject_MockObject
  42. */
  43. protected $maintenanceMode;
  44. /**
  45. * @var \PHPUnit_Framework_MockObject_MockObject
  46. */
  47. protected $deploymentConfig;
  48. /**
  49. * @var \Magento\Framework\App\Bootstrap | \PHPUnit_Framework_MockObject_MockObject
  50. */
  51. protected $bootstrapMock;
  52. /**
  53. * @var \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress | \PHPUnit_Framework_MockObject_MockObject
  54. */
  55. protected $remoteAddress;
  56. protected function setUp()
  57. {
  58. $this->objectManagerFactory = $this->createMock(\Magento\Framework\App\ObjectManagerFactory::class);
  59. $this->objectManager = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
  60. $this->dirs = $this->createPartialMock(\Magento\Framework\App\Filesystem\DirectoryList::class, ['getPath']);
  61. $this->maintenanceMode = $this->createPartialMock(\Magento\Framework\App\MaintenanceMode::class, ['isOn']);
  62. $this->remoteAddress = $this->createMock(\Magento\Framework\HTTP\PhpEnvironment\RemoteAddress::class);
  63. $filesystem = $this->createMock(\Magento\Framework\Filesystem::class);
  64. $this->logger = $this->createMock(\Psr\Log\LoggerInterface::class);
  65. $this->deploymentConfig = $this->createMock(\Magento\Framework\App\DeploymentConfig::class);
  66. $mapObjectManager = [
  67. [\Magento\Framework\App\Filesystem\DirectoryList::class, $this->dirs],
  68. [\Magento\Framework\App\MaintenanceMode::class, $this->maintenanceMode],
  69. [\Magento\Framework\HTTP\PhpEnvironment\RemoteAddress::class, $this->remoteAddress],
  70. [\Magento\Framework\Filesystem::class, $filesystem],
  71. [\Magento\Framework\App\DeploymentConfig::class, $this->deploymentConfig],
  72. [\Psr\Log\LoggerInterface::class, $this->logger],
  73. ];
  74. $this->objectManager->expects($this->any())->method('get')
  75. ->will(($this->returnValueMap($mapObjectManager)));
  76. $this->configDir = $this->getMockForAbstractClass(\Magento\Framework\Filesystem\Directory\ReadInterface::class);
  77. $filesystem->expects($this->any())->method('getDirectoryRead')
  78. ->will(($this->returnValue($this->configDir)));
  79. $this->application = $this->getMockForAbstractClass(\Magento\Framework\AppInterface::class);
  80. $this->objectManager->expects($this->any())->method('create')
  81. ->will(($this->returnValue($this->application)));
  82. $this->objectManagerFactory->expects($this->any())->method('create')
  83. ->will(($this->returnValue($this->objectManager)));
  84. $this->bootstrapMock = $this->getMockBuilder(\Magento\Framework\App\Bootstrap::class)
  85. ->setMethods(['assertMaintenance', 'assertInstalled', 'getIsExpected', 'isInstalled', 'terminate'])
  86. ->setConstructorArgs([$this->objectManagerFactory, '', ['value1', 'value2']])
  87. ->getMock();
  88. }
  89. public function testCreateObjectManagerFactory()
  90. {
  91. $result = Bootstrap::createObjectManagerFactory('test', []);
  92. $this->assertInstanceOf(\Magento\Framework\App\ObjectManagerFactory::class, $result);
  93. }
  94. public function testCreateFilesystemDirectoryList()
  95. {
  96. $result = Bootstrap::createFilesystemDirectoryList(
  97. 'test',
  98. [Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS => [DirectoryList::APP => ['path' => '/custom/path']]]
  99. );
  100. /** @var \Magento\Framework\App\Filesystem\DirectoryList $result */
  101. $this->assertInstanceOf(\Magento\Framework\App\Filesystem\DirectoryList::class, $result);
  102. $this->assertEquals('/custom/path', $result->getPath(DirectoryList::APP));
  103. }
  104. public function testCreateFilesystemDriverPool()
  105. {
  106. $driverClass = get_class($this->getMockForAbstractClass(\Magento\Framework\Filesystem\DriverInterface::class));
  107. $result = Bootstrap::createFilesystemDriverPool(
  108. [Bootstrap::INIT_PARAM_FILESYSTEM_DRIVERS => ['custom' => $driverClass]]
  109. );
  110. /** @var \Magento\Framework\Filesystem\DriverPool $result */
  111. $this->assertInstanceOf(\Magento\Framework\Filesystem\DriverPool::class, $result);
  112. $this->assertInstanceOf($driverClass, $result->getDriver('custom'));
  113. }
  114. public function testGetParams()
  115. {
  116. $testParams = ['testValue1', 'testValue2'];
  117. $bootstrap = self::createBootstrap($testParams);
  118. $this->assertSame($testParams, $bootstrap->getParams());
  119. }
  120. /**
  121. * Creates a bootstrap object
  122. *
  123. * @param array $testParams
  124. * @return Bootstrap
  125. */
  126. private function createBootstrap($testParams = ['value1', 'value2'])
  127. {
  128. return new Bootstrap($this->objectManagerFactory, '', $testParams);
  129. }
  130. public function testCreateApplication()
  131. {
  132. $bootstrap = self::createBootstrap();
  133. $testArgs = ['arg1', 'arg2'];
  134. $this->assertSame($this->application, $bootstrap->createApplication('someApplicationType', $testArgs));
  135. }
  136. public function testGetObjectManager()
  137. {
  138. $bootstrap = self::createBootstrap();
  139. $this->assertSame($this->objectManager, $bootstrap->getObjectManager());
  140. }
  141. /**
  142. * @param $modeFromEnvironment
  143. * @param $modeFromDeployment
  144. * @param $isDeveloper
  145. *
  146. * @dataProvider testIsDeveloperModeDataProvider
  147. */
  148. public function testIsDeveloperMode($modeFromEnvironment, $modeFromDeployment, $isDeveloper)
  149. {
  150. $testParams = [];
  151. if ($modeFromEnvironment) {
  152. $testParams[State::PARAM_MODE] = $modeFromEnvironment;
  153. }
  154. if ($modeFromDeployment) {
  155. $this->deploymentConfig->method('get')->willReturn($modeFromDeployment);
  156. }
  157. $bootstrap = self::createBootstrap($testParams);
  158. $this->assertEquals($isDeveloper, $bootstrap->isDeveloperMode());
  159. }
  160. /**
  161. * @return array
  162. */
  163. public function testIsDeveloperModeDataProvider()
  164. {
  165. return [
  166. [null, null, false],
  167. [State::MODE_DEVELOPER, State::MODE_PRODUCTION, true],
  168. [State::MODE_PRODUCTION, State::MODE_DEVELOPER, false],
  169. [null, State::MODE_DEVELOPER, true],
  170. [null, State::MODE_PRODUCTION, false]
  171. ];
  172. }
  173. public function testRunNoErrors()
  174. {
  175. $responseMock = $this->getMockForAbstractClass(\Magento\Framework\App\ResponseInterface::class);
  176. $this->bootstrapMock->expects($this->once())->method('assertMaintenance')->will($this->returnValue(null));
  177. $this->bootstrapMock->expects($this->once())->method('assertInstalled')->will($this->returnValue(null));
  178. $this->application->expects($this->once())->method('launch')->willReturn($responseMock);
  179. $this->bootstrapMock->run($this->application);
  180. }
  181. public function testRunWithMaintenanceErrors()
  182. {
  183. $expectedException = new \Exception('');
  184. $this->bootstrapMock->expects($this->once())->method('assertMaintenance')
  185. ->will($this->throwException($expectedException));
  186. $this->bootstrapMock->expects($this->once())->method('terminate')->with($expectedException);
  187. $this->application->expects($this->once())->method('catchException')->willReturn(false);
  188. $this->bootstrapMock->run($this->application);
  189. }
  190. public function testRunWithInstallErrors()
  191. {
  192. $expectedException = new \Exception('');
  193. $this->bootstrapMock->expects($this->once())->method('assertMaintenance')->will($this->returnValue(null));
  194. $this->bootstrapMock->expects($this->once())->method('assertInstalled')
  195. ->will($this->throwException($expectedException));
  196. $this->bootstrapMock->expects($this->once())->method('terminate')->with($expectedException);
  197. $this->application->expects($this->once())->method('catchException')->willReturn(false);
  198. $this->bootstrapMock->run($this->application);
  199. }
  200. public function testRunWithBothErrors()
  201. {
  202. $expectedMaintenanceException = new \Exception('');
  203. $this->bootstrapMock->expects($this->once())->method('assertMaintenance')
  204. ->will($this->throwException($expectedMaintenanceException));
  205. $this->bootstrapMock->expects($this->never())->method('assertInstalled');
  206. $this->bootstrapMock->expects($this->once())->method('terminate')->with($expectedMaintenanceException);
  207. $this->application->expects($this->once())->method('catchException')->willReturn(false);
  208. $this->bootstrapMock->run($this->application);
  209. }
  210. /**
  211. * @param bool $isOn
  212. * @param bool $isExpected
  213. *
  214. * @dataProvider assertMaintenanceDataProvider
  215. */
  216. public function testAssertMaintenance($isOn, $isExpected)
  217. {
  218. $bootstrap = self::createBootstrap([Bootstrap::PARAM_REQUIRE_MAINTENANCE => $isExpected]);
  219. $this->maintenanceMode->expects($this->once())->method('isOn')->willReturn($isOn);
  220. $this->remoteAddress->expects($this->once())->method('getRemoteAddress')->willReturn(false);
  221. $this->application->expects($this->never())->method('launch');
  222. $this->application->expects($this->once())->method('catchException')->willReturn(true);
  223. $bootstrap->run($this->application);
  224. $this->assertEquals(Bootstrap::ERR_MAINTENANCE, $bootstrap->getErrorCode());
  225. }
  226. /**
  227. * @return array
  228. */
  229. public function assertMaintenanceDataProvider()
  230. {
  231. return [
  232. [true, false],
  233. [false, true]
  234. ];
  235. }
  236. /**
  237. * @param bool $isInstalled
  238. * @param bool $isExpected
  239. *
  240. * @dataProvider assertInstalledDataProvider
  241. */
  242. public function testAssertInstalled($isInstalled, $isExpected)
  243. {
  244. $bootstrap = self::createBootstrap([Bootstrap::PARAM_REQUIRE_IS_INSTALLED => $isExpected]);
  245. $this->deploymentConfig->expects($this->once())->method('isAvailable')->willReturn($isInstalled);
  246. $this->application->expects($this->never())->method('launch');
  247. $this->application->expects($this->once())->method('catchException')->willReturn(true);
  248. $bootstrap->run($this->application);
  249. $this->assertEquals(Bootstrap::ERR_IS_INSTALLED, $bootstrap->getErrorCode());
  250. }
  251. /**
  252. * @return array
  253. */
  254. public function assertInstalledDataProvider()
  255. {
  256. return [
  257. [false, true],
  258. [true, false],
  259. ];
  260. }
  261. }