123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\App\Test\Unit;
- use Magento\Framework\App\Bootstrap;
- use Magento\Framework\App\Filesystem\DirectoryList;
- use Magento\Framework\App\MaintenanceMode;
- use Magento\Framework\App\State;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class BootstrapTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\Framework\AppInterface | \PHPUnit_Framework_MockObject_MockObject
- */
- protected $application;
- /**
- * @var \Magento\Framework\App\ObjectManagerFactory | \PHPUnit_Framework_MockObject_MockObject
- */
- protected $objectManagerFactory;
- /**
- * @var \Magento\Framework\ObjectManager\ObjectManager | \PHPUnit_Framework_MockObject_MockObject
- */
- protected $objectManager;
- /**
- * @var \Psr\Log\LoggerInterface | \PHPUnit_Framework_MockObject_MockObject
- */
- protected $logger;
- /**
- * @var \Magento\Framework\App\Filesystem\DirectoryList | \PHPUnit_Framework_MockObject_MockObject
- */
- protected $dirs;
- /**
- * @var \Magento\Framework\Filesystem\Directory\ReadInterface | \PHPUnit_Framework_MockObject_MockObject
- */
- protected $configDir;
- /**
- * @var MaintenanceMode | \PHPUnit_Framework_MockObject_MockObject
- */
- protected $maintenanceMode;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $deploymentConfig;
- /**
- * @var \Magento\Framework\App\Bootstrap | \PHPUnit_Framework_MockObject_MockObject
- */
- protected $bootstrapMock;
- /**
- * @var \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress | \PHPUnit_Framework_MockObject_MockObject
- */
- protected $remoteAddress;
- protected function setUp()
- {
- $this->objectManagerFactory = $this->createMock(\Magento\Framework\App\ObjectManagerFactory::class);
- $this->objectManager = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
- $this->dirs = $this->createPartialMock(\Magento\Framework\App\Filesystem\DirectoryList::class, ['getPath']);
- $this->maintenanceMode = $this->createPartialMock(\Magento\Framework\App\MaintenanceMode::class, ['isOn']);
- $this->remoteAddress = $this->createMock(\Magento\Framework\HTTP\PhpEnvironment\RemoteAddress::class);
- $filesystem = $this->createMock(\Magento\Framework\Filesystem::class);
- $this->logger = $this->createMock(\Psr\Log\LoggerInterface::class);
- $this->deploymentConfig = $this->createMock(\Magento\Framework\App\DeploymentConfig::class);
- $mapObjectManager = [
- [\Magento\Framework\App\Filesystem\DirectoryList::class, $this->dirs],
- [\Magento\Framework\App\MaintenanceMode::class, $this->maintenanceMode],
- [\Magento\Framework\HTTP\PhpEnvironment\RemoteAddress::class, $this->remoteAddress],
- [\Magento\Framework\Filesystem::class, $filesystem],
- [\Magento\Framework\App\DeploymentConfig::class, $this->deploymentConfig],
- [\Psr\Log\LoggerInterface::class, $this->logger],
- ];
- $this->objectManager->expects($this->any())->method('get')
- ->will(($this->returnValueMap($mapObjectManager)));
- $this->configDir = $this->getMockForAbstractClass(\Magento\Framework\Filesystem\Directory\ReadInterface::class);
- $filesystem->expects($this->any())->method('getDirectoryRead')
- ->will(($this->returnValue($this->configDir)));
- $this->application = $this->getMockForAbstractClass(\Magento\Framework\AppInterface::class);
- $this->objectManager->expects($this->any())->method('create')
- ->will(($this->returnValue($this->application)));
- $this->objectManagerFactory->expects($this->any())->method('create')
- ->will(($this->returnValue($this->objectManager)));
- $this->bootstrapMock = $this->getMockBuilder(\Magento\Framework\App\Bootstrap::class)
- ->setMethods(['assertMaintenance', 'assertInstalled', 'getIsExpected', 'isInstalled', 'terminate'])
- ->setConstructorArgs([$this->objectManagerFactory, '', ['value1', 'value2']])
- ->getMock();
- }
- public function testCreateObjectManagerFactory()
- {
- $result = Bootstrap::createObjectManagerFactory('test', []);
- $this->assertInstanceOf(\Magento\Framework\App\ObjectManagerFactory::class, $result);
- }
- public function testCreateFilesystemDirectoryList()
- {
- $result = Bootstrap::createFilesystemDirectoryList(
- 'test',
- [Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS => [DirectoryList::APP => ['path' => '/custom/path']]]
- );
- /** @var \Magento\Framework\App\Filesystem\DirectoryList $result */
- $this->assertInstanceOf(\Magento\Framework\App\Filesystem\DirectoryList::class, $result);
- $this->assertEquals('/custom/path', $result->getPath(DirectoryList::APP));
- }
- public function testCreateFilesystemDriverPool()
- {
- $driverClass = get_class($this->getMockForAbstractClass(\Magento\Framework\Filesystem\DriverInterface::class));
- $result = Bootstrap::createFilesystemDriverPool(
- [Bootstrap::INIT_PARAM_FILESYSTEM_DRIVERS => ['custom' => $driverClass]]
- );
- /** @var \Magento\Framework\Filesystem\DriverPool $result */
- $this->assertInstanceOf(\Magento\Framework\Filesystem\DriverPool::class, $result);
- $this->assertInstanceOf($driverClass, $result->getDriver('custom'));
- }
- public function testGetParams()
- {
- $testParams = ['testValue1', 'testValue2'];
- $bootstrap = self::createBootstrap($testParams);
- $this->assertSame($testParams, $bootstrap->getParams());
- }
- /**
- * Creates a bootstrap object
- *
- * @param array $testParams
- * @return Bootstrap
- */
- private function createBootstrap($testParams = ['value1', 'value2'])
- {
- return new Bootstrap($this->objectManagerFactory, '', $testParams);
- }
- public function testCreateApplication()
- {
- $bootstrap = self::createBootstrap();
- $testArgs = ['arg1', 'arg2'];
- $this->assertSame($this->application, $bootstrap->createApplication('someApplicationType', $testArgs));
- }
- public function testGetObjectManager()
- {
- $bootstrap = self::createBootstrap();
- $this->assertSame($this->objectManager, $bootstrap->getObjectManager());
- }
- /**
- * @param $modeFromEnvironment
- * @param $modeFromDeployment
- * @param $isDeveloper
- *
- * @dataProvider testIsDeveloperModeDataProvider
- */
- public function testIsDeveloperMode($modeFromEnvironment, $modeFromDeployment, $isDeveloper)
- {
- $testParams = [];
- if ($modeFromEnvironment) {
- $testParams[State::PARAM_MODE] = $modeFromEnvironment;
- }
- if ($modeFromDeployment) {
- $this->deploymentConfig->method('get')->willReturn($modeFromDeployment);
- }
- $bootstrap = self::createBootstrap($testParams);
- $this->assertEquals($isDeveloper, $bootstrap->isDeveloperMode());
- }
- /**
- * @return array
- */
- public function testIsDeveloperModeDataProvider()
- {
- return [
- [null, null, false],
- [State::MODE_DEVELOPER, State::MODE_PRODUCTION, true],
- [State::MODE_PRODUCTION, State::MODE_DEVELOPER, false],
- [null, State::MODE_DEVELOPER, true],
- [null, State::MODE_PRODUCTION, false]
- ];
- }
- public function testRunNoErrors()
- {
- $responseMock = $this->getMockForAbstractClass(\Magento\Framework\App\ResponseInterface::class);
- $this->bootstrapMock->expects($this->once())->method('assertMaintenance')->will($this->returnValue(null));
- $this->bootstrapMock->expects($this->once())->method('assertInstalled')->will($this->returnValue(null));
- $this->application->expects($this->once())->method('launch')->willReturn($responseMock);
- $this->bootstrapMock->run($this->application);
- }
- public function testRunWithMaintenanceErrors()
- {
- $expectedException = new \Exception('');
- $this->bootstrapMock->expects($this->once())->method('assertMaintenance')
- ->will($this->throwException($expectedException));
- $this->bootstrapMock->expects($this->once())->method('terminate')->with($expectedException);
- $this->application->expects($this->once())->method('catchException')->willReturn(false);
- $this->bootstrapMock->run($this->application);
- }
- public function testRunWithInstallErrors()
- {
- $expectedException = new \Exception('');
- $this->bootstrapMock->expects($this->once())->method('assertMaintenance')->will($this->returnValue(null));
- $this->bootstrapMock->expects($this->once())->method('assertInstalled')
- ->will($this->throwException($expectedException));
- $this->bootstrapMock->expects($this->once())->method('terminate')->with($expectedException);
- $this->application->expects($this->once())->method('catchException')->willReturn(false);
- $this->bootstrapMock->run($this->application);
- }
- public function testRunWithBothErrors()
- {
- $expectedMaintenanceException = new \Exception('');
- $this->bootstrapMock->expects($this->once())->method('assertMaintenance')
- ->will($this->throwException($expectedMaintenanceException));
- $this->bootstrapMock->expects($this->never())->method('assertInstalled');
- $this->bootstrapMock->expects($this->once())->method('terminate')->with($expectedMaintenanceException);
- $this->application->expects($this->once())->method('catchException')->willReturn(false);
- $this->bootstrapMock->run($this->application);
- }
- /**
- * @param bool $isOn
- * @param bool $isExpected
- *
- * @dataProvider assertMaintenanceDataProvider
- */
- public function testAssertMaintenance($isOn, $isExpected)
- {
- $bootstrap = self::createBootstrap([Bootstrap::PARAM_REQUIRE_MAINTENANCE => $isExpected]);
- $this->maintenanceMode->expects($this->once())->method('isOn')->willReturn($isOn);
- $this->remoteAddress->expects($this->once())->method('getRemoteAddress')->willReturn(false);
- $this->application->expects($this->never())->method('launch');
- $this->application->expects($this->once())->method('catchException')->willReturn(true);
- $bootstrap->run($this->application);
- $this->assertEquals(Bootstrap::ERR_MAINTENANCE, $bootstrap->getErrorCode());
- }
- /**
- * @return array
- */
- public function assertMaintenanceDataProvider()
- {
- return [
- [true, false],
- [false, true]
- ];
- }
- /**
- * @param bool $isInstalled
- * @param bool $isExpected
- *
- * @dataProvider assertInstalledDataProvider
- */
- public function testAssertInstalled($isInstalled, $isExpected)
- {
- $bootstrap = self::createBootstrap([Bootstrap::PARAM_REQUIRE_IS_INSTALLED => $isExpected]);
- $this->deploymentConfig->expects($this->once())->method('isAvailable')->willReturn($isInstalled);
- $this->application->expects($this->never())->method('launch');
- $this->application->expects($this->once())->method('catchException')->willReturn(true);
- $bootstrap->run($this->application);
- $this->assertEquals(Bootstrap::ERR_IS_INSTALLED, $bootstrap->getErrorCode());
- }
- /**
- * @return array
- */
- public function assertInstalledDataProvider()
- {
- return [
- [false, true],
- [true, false],
- ];
- }
- }
|