Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
InitParamListenerTest.php
Go to the documentation of this file.
1 <?php
7 
8 use \Magento\Setup\Mvc\Bootstrap\InitParamListener;
9 
10 use Magento\Framework\App\Bootstrap as AppBootstrap;
12 use Zend\Mvc\MvcEvent;
13 
17 class InitParamListenerTest extends \PHPUnit\Framework\TestCase
18 {
19 
23  private $listener;
24 
28  private $callbackHandler;
29 
30  protected function setUp()
31  {
32  $this->listener = new InitParamListener();
33  }
34 
35  public function testAttach()
36  {
37  $events = $this->prepareEventManager();
38  $this->listener->attach($events);
39  }
40 
41  public function testDetach()
42  {
43  $events = $this->prepareEventManager();
44  $this->listener->attach($events);
45  $events->expects($this->once())->method('detach')->with($this->callbackHandler)->willReturn(true);
46  $this->listener->detach($events);
47  }
48 
49  public function testOnBootstrap()
50  {
52  $mvcEvent = $this->createMock(\Zend\Mvc\MvcEvent::class);
53  $mvcApplication = $this->getMockBuilder(\Zend\Mvc\Application::class)->disableOriginalConstructor()->getMock();
54  $mvcEvent->expects($this->once())->method('getApplication')->willReturn($mvcApplication);
55  $serviceManager = $this->createMock(\Zend\ServiceManager\ServiceManager::class);
56  $initParams[AppBootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS][DirectoryList::ROOT] = ['path' => '/test'];
57  $serviceManager->expects($this->once())->method('get')
58  ->willReturn($initParams);
59  $serviceManager->expects($this->exactly(2))->method('setService')
60  ->withConsecutive(
61  [
62  \Magento\Framework\App\Filesystem\DirectoryList::class,
63  $this->isInstanceOf(\Magento\Framework\App\Filesystem\DirectoryList::class),
64  ],
65  [
66  \Magento\Framework\Filesystem::class,
67  $this->isInstanceOf(\Magento\Framework\Filesystem::class),
68  ]
69  );
70  $mvcApplication->expects($this->any())->method('getServiceManager')->willReturn($serviceManager);
71 
72  $eventManager = $this->getMockForAbstractClass(\Zend\EventManager\EventManagerInterface::class);
73  $mvcApplication->expects($this->any())->method('getEventManager')->willReturn($eventManager);
74  $eventManager->expects($this->any())->method('attach');
75 
76  $this->listener->onBootstrap($mvcEvent);
77  }
78 
79  public function testCreateDirectoryList()
80  {
81  $initParams[AppBootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS] =
82  [DirectoryList::ROOT => [DirectoryList::PATH => '/test/root']];
83 
84  $directoryList = $this->listener->createDirectoryList($initParams);
85  $this->assertEquals('/test/root/app', $directoryList->getPath(DirectoryList::APP));
86  }
87 
93  {
94  $this->listener->createDirectoryList([]);
95  }
96 
97  public function testCreateServiceNotConsole()
98  {
102  $serviceLocator = $this->createMock(\Zend\ServiceManager\ServiceLocatorInterface::class);
103  $mvcApplication = $this->getMockBuilder(\Zend\Mvc\Application::class)->disableOriginalConstructor()->getMock();
104  $request = $this->createMock(\Zend\Stdlib\RequestInterface::class);
105  $mvcApplication->expects($this->any())->method('getRequest')->willReturn($request);
106  $serviceLocator->expects($this->once())->method('get')->with('Application')
107  ->willReturn($mvcApplication);
108  $this->assertEquals([], $this->listener->createService($serviceLocator));
109  }
110 
119  public function testCreateService($zfAppConfig, $env, $cliParam, $expectedArray)
120  {
121  foreach ($env as $envKey => $envValue) {
122  $_SERVER[$envKey] = $envValue;
123  }
124  $listener = new InitParamListener();
128  $serviceLocator = $this->createMock(\Zend\ServiceManager\ServiceLocatorInterface::class);
129  $mvcApplication = $this->getMockBuilder(\Zend\Mvc\Application::class)->disableOriginalConstructor()->getMock();
130  $request = $this->getMockBuilder(\Zend\Console\Request::class)->disableOriginalConstructor()->getMock();
131  $request->expects($this->any())
132  ->method('getContent')
133  ->willReturn(
134  $cliParam ? ['install', '--magento-init-params=' . $cliParam] : ['install']
135  );
136  $mvcApplication->expects($this->any())->method('getConfig')->willReturn(
137  $zfAppConfig ? [InitParamListener::BOOTSTRAP_PARAM => $zfAppConfig] : []
138  );
139 
140  $mvcApplication->expects($this->any())->method('getRequest')->willReturn($request);
141  $serviceLocator->expects($this->once())->method('get')->with('Application')
142  ->willReturn($mvcApplication);
143 
144  $this->assertEquals($expectedArray, $listener->createService($serviceLocator));
145  }
146 
150  public function createServiceDataProvider()
151  {
152  return [
153  'none' => [[], [], '', []],
154  'mage_mode App' => [['MAGE_MODE' => 'developer'], [], '', ['MAGE_MODE' => 'developer']],
155  'mage_mode Env' => [[], ['MAGE_MODE' => 'developer'], '', ['MAGE_MODE' => 'developer']],
156  'mage_mode CLI' => [[], [], 'MAGE_MODE=developer', ['MAGE_MODE' => 'developer']],
157  'one MAGE_DIRS CLI' => [
158  [],
159  [],
160  'MAGE_MODE=developer&MAGE_DIRS[base][path]=/var/www/magento2',
161  ['MAGE_DIRS' => ['base' => ['path' => '/var/www/magento2']], 'MAGE_MODE' => 'developer'],
162  ],
163  'two MAGE_DIRS CLI' => [
164  [],
165  [],
166  'MAGE_MODE=developer&MAGE_DIRS[base][path]=/var/www/magento2&MAGE_DIRS[cache][path]=/tmp/cache',
167  [
168  'MAGE_DIRS' => ['base' => ['path' => '/var/www/magento2'], 'cache' => ['path' => '/tmp/cache']],
169  'MAGE_MODE' => 'developer',
170  ],
171  ],
172  'mage_mode only' => [[], [], 'MAGE_MODE=developer', ['MAGE_MODE' => 'developer']],
173  'MAGE_DIRS Env' => [
174  [],
175  ['MAGE_DIRS' => ['base' => ['path' => '/var/www/magento2']], 'MAGE_MODE' => 'developer'],
176  '',
177  ['MAGE_DIRS' => ['base' => ['path' => '/var/www/magento2']], 'MAGE_MODE' => 'developer'],
178  ],
179  'two MAGE_DIRS' => [
180  [],
181  [],
182  'MAGE_MODE=developer&MAGE_DIRS[base][path]=/var/www/magento2&MAGE_DIRS[cache][path]=/tmp/cache',
183  [
184  'MAGE_DIRS' => ['base' => ['path' => '/var/www/magento2'], 'cache' => ['path' => '/tmp/cache']],
185  'MAGE_MODE' => 'developer',
186  ],
187  ],
188  'Env overwrites App' => [
189  ['MAGE_DIRS' => ['base' => ['path' => '/var/www/magento2/App']], 'MAGE_MODE' => 'developer'],
190  ['MAGE_DIRS' => ['base' => ['path' => '/var/www/magento2/Env']], 'MAGE_MODE' => 'developer'],
191  '',
192  ['MAGE_DIRS' => ['base' => ['path' => '/var/www/magento2/Env']], 'MAGE_MODE' => 'developer'],
193  ],
194  'CLI overwrites Env' => [
195  ['MAGE_MODE' => 'developer'],
196  ['MAGE_DIRS' => ['base' => ['path' => '/var/www/magento2/Env']]],
197  'MAGE_DIRS[base][path]=/var/www/magento2/CLI',
198  ['MAGE_DIRS' => ['base' => ['path' => '/var/www/magento2/CLI']], 'MAGE_MODE' => 'developer'],
199  ],
200  'CLI overwrites All' => [
201  ['MAGE_DIRS' => ['base' => ['path' => '/var/www/magento2/App']], 'MAGE_MODE' => 'production'],
202  ['MAGE_DIRS' => ['base' => ['path' => '/var/www/magento2/Env']]],
203  'MAGE_DIRS[base][path]=/var/www/magento2/CLI',
204  ['MAGE_DIRS' => ['base' => ['path' => '/var/www/magento2/CLI']], 'MAGE_MODE' => 'developer'],
205  ],
206  ];
207  }
208 
209  public function testCreateFilesystem()
210  {
211  $testPath = 'test/path/';
212 
217  $directoryList = $this->getMockBuilder(\Magento\Framework\App\Filesystem\DirectoryList::class)
218  ->disableOriginalConstructor()->getMock();
219  $directoryList->expects($this->any())->method('getPath')->willReturn($testPath);
220  $filesystem = $this->listener->createFilesystem($directoryList);
221 
222  // Verifies the filesystem was created with the directory list passed in
223  $this->assertEquals($testPath, $filesystem->getDirectoryRead('app')->getAbsolutePath());
224  }
225 
231  private function prepareEventManager()
232  {
233  $this->callbackHandler = $this->getMockBuilder(\Zend\Stdlib\CallbackHandler::class)
234  ->disableOriginalConstructor()
235  ->getMock();
236 
238  $eventManager = $this->createMock(\Zend\EventManager\EventManagerInterface::class);
239 
240  $sharedManager = $this->createMock(\Zend\EventManager\SharedEventManager::class);
241  $sharedManager->expects($this->once())->method('attach')->with(
242  \Zend\Mvc\Application::class,
243  MvcEvent::EVENT_BOOTSTRAP,
244  [$this->listener, 'onBootstrap']
245  )->willReturn($this->callbackHandler);
246  $eventManager->expects($this->once())->method('getSharedManager')->willReturn($sharedManager);
247 
248  return $eventManager;
249  }
250 
254  public function testAuthPreDispatch()
255  {
256  $cookiePath = 'test';
257  $eventMock = $this->getMockBuilder(\Zend\Mvc\MvcEvent::class)
258  ->disableOriginalConstructor()
259  ->getMock();
260  $routeMatchMock = $this->getMockBuilder(\Zend\Mvc\Router\Http\RouteMatch::class)
261  ->disableOriginalConstructor()
262  ->getMock();
263  $applicationMock = $this->getMockBuilder(\Zend\Mvc\Application::class)
264  ->disableOriginalConstructor()
265  ->getMock();
266  $serviceManagerMock = $this->getMockBuilder(\Zend\ServiceManager\ServiceManager::class)
267  ->disableOriginalConstructor()
268  ->getMock();
269  $deploymentConfigMock = $this->getMockBuilder(\Magento\Framework\App\DeploymentConfig::class)
270  ->disableOriginalConstructor()
271  ->getMock();
272  $deploymentConfigMock->expects($this->once())
273  ->method('isAvailable')
274  ->willReturn(true);
275  $omProvider = $this->getMockBuilder(\Magento\Setup\Model\ObjectManagerProvider::class)
276  ->disableOriginalConstructor()
277  ->getMock();
278  $objectManagerMock = $this->getMockForAbstractClass(\Magento\Framework\ObjectManagerInterface::class);
279  $adminAppStateMock = $this->getMockBuilder(\Magento\Framework\App\State::class)
280  ->disableOriginalConstructor()
281  ->getMock();
282  $sessionConfigMock = $this->getMockBuilder(\Magento\Backend\Model\Session\AdminConfig::class)
283  ->disableOriginalConstructor()
284  ->getMock();
285  $backendAppListMock = $this->getMockBuilder(\Magento\Backend\App\BackendAppList::class)
286  ->disableOriginalConstructor()
287  ->getMock();
288  $backendAppMock = $this->getMockBuilder(\Magento\Backend\App\BackendApp::class)
289  ->disableOriginalConstructor()
290  ->getMock();
291  $urlMock = $this->getMockBuilder(\Magento\Backend\Model\Url::class)
292  ->disableOriginalConstructor()
293  ->getMock();
294  $authenticationMock = $this->getMockBuilder(\Magento\Backend\Model\Auth::class)
295  ->disableOriginalConstructor()
296  ->getMock();
297  $adminSessionMock = $this->getMockBuilder(\Magento\Backend\Model\Auth\Session::class)
298  ->disableOriginalConstructor()
299  ->getMock();
300  $responseMock = $this->getMockBuilder(\Zend\Http\Response::class)
301  ->disableOriginalConstructor()
302  ->getMock();
303  $headersMock = $this->getMockBuilder(\Zend\Http\Headers::class)
304  ->disableOriginalConstructor()
305  ->getMock();
306 
307  $routeMatchMock->expects($this->exactly(2))
308  ->method('getParam')
309  ->willReturnMap([
310  ['controller', null, 'testController'],
311  ['action', null, 'testAction']
312  ]);
313  $eventMock->expects($this->once())
314  ->method('getRouteMatch')
315  ->willReturn($routeMatchMock);
316  $eventMock->expects($this->once())
317  ->method('getApplication')
318  ->willReturn($applicationMock);
319  $serviceManagerMock->expects($this->any())
320  ->method('get')
321  ->willReturnMap(
322  [
323  [
324  \Magento\Framework\App\DeploymentConfig::class,
325  true,
326  $deploymentConfigMock,
327  ],
328  [
329  \Magento\Setup\Model\ObjectManagerProvider::class,
330  true,
331  $omProvider,
332  ],
333  ]
334  );
335  $objectManagerMock->expects($this->any())
336  ->method('get')
337  ->willReturnMap(
338  [
339  [
340  \Magento\Framework\App\State::class,
341  $adminAppStateMock,
342  ],
343  [
344  \Magento\Backend\Model\Session\AdminConfig::class,
345  $sessionConfigMock,
346  ],
347  [
348  \Magento\Backend\App\BackendAppList::class,
349  $backendAppListMock,
350  ],
351  [
352  \Magento\Backend\Model\Auth::class,
353  $authenticationMock,
354  ],
355  ]
356  );
357  $objectManagerMock->expects($this->any())
358  ->method('create')
359  ->willReturnMap(
360  [
361  [
362  \Magento\Backend\Model\Auth\Session::class,
363  [
364  'sessionConfig' => $sessionConfigMock,
365  'appState' => $adminAppStateMock
366  ],
367  $adminSessionMock,
368  ],
369  [
370  \Magento\Backend\Model\Url::class,
371  [],
372  $urlMock,
373  ],
374  ]
375  );
376  $omProvider->expects($this->once())
377  ->method('get')
378  ->willReturn($objectManagerMock);
379  $adminAppStateMock->expects($this->once())
380  ->method('setAreaCode')
381  ->with(\Magento\Framework\App\Area::AREA_ADMINHTML);
382  $applicationMock->expects($this->once())
383  ->method('getServiceManager')
384  ->willReturn($serviceManagerMock);
385  $backendAppMock->expects($this->once())
386  ->method('getCookiePath')
387  ->willReturn($cookiePath);
388  $urlMock->expects($this->once())
389  ->method('getBaseUrl')
390  ->willReturn('http://base-url/');
391  $sessionConfigMock->expects($this->once())
392  ->method('setCookiePath')
393  ->with('/' . $cookiePath);
394  $backendAppListMock->expects($this->once())
395  ->method('getBackendApp')
396  ->willReturn($backendAppMock);
397  $authenticationMock->expects($this->once())
398  ->method('isLoggedIn')
399  ->willReturn(true);
400  $adminSessionMock->expects($this->once())
401  ->method('isAllowed')
402  ->with('Magento_Backend::setup_wizard', null)
403  ->willReturn(false);
404  $adminSessionMock->expects($this->once())
405  ->method('destroy');
406  $eventMock->expects($this->once())
407  ->method('getResponse')
408  ->willReturn($responseMock);
409  $responseMock->expects($this->once())
410  ->method('getHeaders')
411  ->willReturn($headersMock);
412  $headersMock->expects($this->once())
413  ->method('addHeaderLine');
414  $responseMock->expects($this->once())
415  ->method('setStatusCode')
416  ->with(302);
417  $eventMock->expects($this->once())
418  ->method('stopPropagation');
419 
420  $this->assertSame(
421  $this->listener->authPreDispatch($eventMock),
422  $responseMock
423  );
424  }
425 
426  public function testAuthPreDispatchSkip()
427  {
428  $eventMock = $this->getMockBuilder(\Zend\Mvc\MvcEvent::class)
429  ->disableOriginalConstructor()
430  ->getMock();
431  $routeMatchMock = $this->getMockBuilder(\Zend\Mvc\Router\Http\RouteMatch::class)
432  ->disableOriginalConstructor()
433  ->getMock();
434  $deploymentConfigMock = $this->getMockBuilder(\Magento\Framework\App\DeploymentConfig::class)
435  ->disableOriginalConstructor()
436  ->getMock();
437 
438  $deploymentConfigMock->expects($this->never())
439  ->method('isAvailable');
440  $routeMatchMock->expects($this->exactly(2))
441  ->method('getParam')
442  ->willReturnMap([
443  ['controller', null, \Magento\Setup\Controller\Session::class],
444  ['action', null, 'unlogin']
445  ]);
446  $eventMock->expects($this->once())
447  ->method('getRouteMatch')
448  ->willReturn($routeMatchMock);
449  $eventMock->expects($this->never())
450  ->method('getApplication');
451 
452  $this->assertSame(
453  $this->listener->authPreDispatch($eventMock),
454  false
455  );
456  }
457 }
return false
Definition: gallery.phtml:36
$filesystem