Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BootstrapTest.php
Go to the documentation of this file.
1 <?php
8 
13 
17 class BootstrapTest extends \PHPUnit\Framework\TestCase
18 {
22  protected $application;
23 
28 
32  protected $objectManager;
33 
37  protected $logger;
38 
42  protected $dirs;
43 
47  protected $configDir;
48 
52  protected $maintenanceMode;
53 
57  protected $deploymentConfig;
58 
62  protected $bootstrapMock;
63 
67  protected $remoteAddress;
68 
69  protected function setUp()
70  {
71  $this->objectManagerFactory = $this->createMock(\Magento\Framework\App\ObjectManagerFactory::class);
72  $this->objectManager = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
73  $this->dirs = $this->createPartialMock(\Magento\Framework\App\Filesystem\DirectoryList::class, ['getPath']);
74  $this->maintenanceMode = $this->createPartialMock(\Magento\Framework\App\MaintenanceMode::class, ['isOn']);
75  $this->remoteAddress = $this->createMock(\Magento\Framework\HTTP\PhpEnvironment\RemoteAddress::class);
76  $filesystem = $this->createMock(\Magento\Framework\Filesystem::class);
77 
78  $this->logger = $this->createMock(\Psr\Log\LoggerInterface::class);
79 
80  $this->deploymentConfig = $this->createMock(\Magento\Framework\App\DeploymentConfig::class);
81 
82  $mapObjectManager = [
83  [\Magento\Framework\App\Filesystem\DirectoryList::class, $this->dirs],
84  [\Magento\Framework\App\MaintenanceMode::class, $this->maintenanceMode],
85  [\Magento\Framework\HTTP\PhpEnvironment\RemoteAddress::class, $this->remoteAddress],
86  [\Magento\Framework\Filesystem::class, $filesystem],
87  [\Magento\Framework\App\DeploymentConfig::class, $this->deploymentConfig],
88  [\Psr\Log\LoggerInterface::class, $this->logger],
89  ];
90 
91  $this->objectManager->expects($this->any())->method('get')
92  ->will(($this->returnValueMap($mapObjectManager)));
93 
94  $this->configDir = $this->getMockForAbstractClass(\Magento\Framework\Filesystem\Directory\ReadInterface::class);
95 
96  $filesystem->expects($this->any())->method('getDirectoryRead')
97  ->will(($this->returnValue($this->configDir)));
98 
99  $this->application = $this->getMockForAbstractClass(\Magento\Framework\AppInterface::class);
100 
101  $this->objectManager->expects($this->any())->method('create')
102  ->will(($this->returnValue($this->application)));
103 
104  $this->objectManagerFactory->expects($this->any())->method('create')
105  ->will(($this->returnValue($this->objectManager)));
106 
107  $this->bootstrapMock = $this->getMockBuilder(\Magento\Framework\App\Bootstrap::class)
108  ->setMethods(['assertMaintenance', 'assertInstalled', 'getIsExpected', 'isInstalled', 'terminate'])
109  ->setConstructorArgs([$this->objectManagerFactory, '', ['value1', 'value2']])
110  ->getMock();
111  }
112 
114  {
116  $this->assertInstanceOf(\Magento\Framework\App\ObjectManagerFactory::class, $result);
117  }
118 
119  public function testCreateFilesystemDirectoryList()
120  {
122  'test',
123  [Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS => [DirectoryList::APP => ['path' => '/custom/path']]]
124  );
126  $this->assertInstanceOf(\Magento\Framework\App\Filesystem\DirectoryList::class, $result);
127  $this->assertEquals('/custom/path', $result->getPath(DirectoryList::APP));
128  }
129 
130  public function testCreateFilesystemDriverPool()
131  {
132  $driverClass = get_class($this->getMockForAbstractClass(\Magento\Framework\Filesystem\DriverInterface::class));
134  [Bootstrap::INIT_PARAM_FILESYSTEM_DRIVERS => ['custom' => $driverClass]]
135  );
137  $this->assertInstanceOf(\Magento\Framework\Filesystem\DriverPool::class, $result);
138  $this->assertInstanceOf($driverClass, $result->getDriver('custom'));
139  }
140 
141  public function testGetParams()
142  {
143  $testParams = ['testValue1', 'testValue2'];
144  $bootstrap = self::createBootstrap($testParams);
145  $this->assertSame($testParams, $bootstrap->getParams());
146  }
147 
154  private function createBootstrap($testParams = ['value1', 'value2'])
155  {
156  return new Bootstrap($this->objectManagerFactory, '', $testParams);
157  }
158 
159  public function testCreateApplication()
160  {
161  $bootstrap = self::createBootstrap();
162  $testArgs = ['arg1', 'arg2'];
163  $this->assertSame($this->application, $bootstrap->createApplication('someApplicationType', $testArgs));
164  }
165 
166  public function testGetObjectManager()
167  {
168  $bootstrap = self::createBootstrap();
169  $this->assertSame($this->objectManager, $bootstrap->getObjectManager());
170  }
171 
179  public function testIsDeveloperMode($modeFromEnvironment, $modeFromDeployment, $isDeveloper)
180  {
181  $testParams = [];
182  if ($modeFromEnvironment) {
183  $testParams[State::PARAM_MODE] = $modeFromEnvironment;
184  }
185  if ($modeFromDeployment) {
186  $this->deploymentConfig->method('get')->willReturn($modeFromDeployment);
187  }
188  $bootstrap = self::createBootstrap($testParams);
189  $this->assertEquals($isDeveloper, $bootstrap->isDeveloperMode());
190  }
191 
196  {
197  return [
198  [null, null, false],
201  [null, State::MODE_DEVELOPER, true],
202  [null, State::MODE_PRODUCTION, false]
203  ];
204  }
205 
206  public function testRunNoErrors()
207  {
208  $responseMock = $this->getMockForAbstractClass(\Magento\Framework\App\ResponseInterface::class);
209  $this->bootstrapMock->expects($this->once())->method('assertMaintenance')->will($this->returnValue(null));
210  $this->bootstrapMock->expects($this->once())->method('assertInstalled')->will($this->returnValue(null));
211  $this->application->expects($this->once())->method('launch')->willReturn($responseMock);
212  $this->bootstrapMock->run($this->application);
213  }
214 
216  {
217  $expectedException = new \Exception('');
218  $this->bootstrapMock->expects($this->once())->method('assertMaintenance')
219  ->will($this->throwException($expectedException));
220  $this->bootstrapMock->expects($this->once())->method('terminate')->with($expectedException);
221  $this->application->expects($this->once())->method('catchException')->willReturn(false);
222  $this->bootstrapMock->run($this->application);
223  }
224 
225  public function testRunWithInstallErrors()
226  {
227  $expectedException = new \Exception('');
228  $this->bootstrapMock->expects($this->once())->method('assertMaintenance')->will($this->returnValue(null));
229  $this->bootstrapMock->expects($this->once())->method('assertInstalled')
230  ->will($this->throwException($expectedException));
231  $this->bootstrapMock->expects($this->once())->method('terminate')->with($expectedException);
232  $this->application->expects($this->once())->method('catchException')->willReturn(false);
233  $this->bootstrapMock->run($this->application);
234  }
235 
236  public function testRunWithBothErrors()
237  {
238  $expectedMaintenanceException = new \Exception('');
239  $this->bootstrapMock->expects($this->once())->method('assertMaintenance')
240  ->will($this->throwException($expectedMaintenanceException));
241  $this->bootstrapMock->expects($this->never())->method('assertInstalled');
242  $this->bootstrapMock->expects($this->once())->method('terminate')->with($expectedMaintenanceException);
243  $this->application->expects($this->once())->method('catchException')->willReturn(false);
244  $this->bootstrapMock->run($this->application);
245  }
246 
253  public function testAssertMaintenance($isOn, $isExpected)
254  {
255  $bootstrap = self::createBootstrap([Bootstrap::PARAM_REQUIRE_MAINTENANCE => $isExpected]);
256  $this->maintenanceMode->expects($this->once())->method('isOn')->willReturn($isOn);
257  $this->remoteAddress->expects($this->once())->method('getRemoteAddress')->willReturn(false);
258  $this->application->expects($this->never())->method('launch');
259  $this->application->expects($this->once())->method('catchException')->willReturn(true);
260  $bootstrap->run($this->application);
261  $this->assertEquals(Bootstrap::ERR_MAINTENANCE, $bootstrap->getErrorCode());
262  }
263 
268  {
269  return [
270  [true, false],
271  [false, true]
272  ];
273  }
274 
281  public function testAssertInstalled($isInstalled, $isExpected)
282  {
283  $bootstrap = self::createBootstrap([Bootstrap::PARAM_REQUIRE_IS_INSTALLED => $isExpected]);
284  $this->deploymentConfig->expects($this->once())->method('isAvailable')->willReturn($isInstalled);
285  $this->application->expects($this->never())->method('launch');
286  $this->application->expects($this->once())->method('catchException')->willReturn(true);
287  $bootstrap->run($this->application);
288  $this->assertEquals(Bootstrap::ERR_IS_INSTALLED, $bootstrap->getErrorCode());
289  }
290 
294  public function assertInstalledDataProvider()
295  {
296  return [
297  [false, true],
298  [true, false],
299  ];
300  }
301 }
testIsDeveloperMode($modeFromEnvironment, $modeFromDeployment, $isDeveloper)
if(defined('TESTS_MAGENTO_INSTALLATION') &&TESTS_MAGENTO_INSTALLATION==='enabled') $bootstrap
Definition: bootstrap.php:73
static createFilesystemDirectoryList($rootDir, array $initParams)
Definition: Bootstrap.php:164
static createFilesystemDriverPool(array $initParams)
Definition: Bootstrap.php:179
static createObjectManagerFactory($rootDir, array $initParams)
Definition: Bootstrap.php:149
testAssertInstalled($isInstalled, $isExpected)
$filesystem