PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/zendframework/zendframework/tests/ZendTest/ModuleManager/ModuleManagerTest.php

https://bitbucket.org/pcelta/zf2
PHP | 164 lines | 131 code | 19 blank | 14 comment | 2 complexity | 9854c71e5d2487b0b04c9ee254236a18 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. * @package Zend_ModuleManager
  9. */
  10. namespace ZendTest\ModuleManager;
  11. use PHPUnit_Framework_TestCase as TestCase;
  12. use stdClass;
  13. use Zend\EventManager\EventManager;
  14. use Zend\Loader\AutoloaderFactory;
  15. use Zend\ModuleManager\Listener\ListenerOptions;
  16. use Zend\ModuleManager\Listener\DefaultListenerAggregate;
  17. use Zend\ModuleManager\ModuleEvent;
  18. use Zend\ModuleManager\ModuleManager;
  19. use InvalidArgumentException;
  20. class ModuleManagerTest extends TestCase
  21. {
  22. public function setUp()
  23. {
  24. $this->tmpdir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'zend_module_cache_dir';
  25. @mkdir($this->tmpdir);
  26. $this->configCache = $this->tmpdir . DIRECTORY_SEPARATOR . 'config.cache.php';
  27. // Store original autoloaders
  28. $this->loaders = spl_autoload_functions();
  29. if (!is_array($this->loaders)) {
  30. // spl_autoload_functions does not return empty array when no
  31. // autoloaders registered...
  32. $this->loaders = array();
  33. }
  34. // Store original include_path
  35. $this->includePath = get_include_path();
  36. $this->defaultListeners = new DefaultListenerAggregate(
  37. new ListenerOptions(array(
  38. 'module_paths' => array(
  39. realpath(__DIR__ . '/TestAsset'),
  40. ),
  41. ))
  42. );
  43. }
  44. public function tearDown()
  45. {
  46. $file = glob($this->tmpdir . DIRECTORY_SEPARATOR . '*');
  47. @unlink($file[0]); // change this if there's ever > 1 file
  48. @rmdir($this->tmpdir);
  49. // Restore original autoloaders
  50. AutoloaderFactory::unregisterAutoloaders();
  51. $loaders = spl_autoload_functions();
  52. if (is_array($loaders)) {
  53. foreach ($loaders as $loader) {
  54. spl_autoload_unregister($loader);
  55. }
  56. }
  57. foreach ($this->loaders as $loader) {
  58. spl_autoload_register($loader);
  59. }
  60. // Restore original include_path
  61. set_include_path($this->includePath);
  62. }
  63. public function testEventManagerIdentifiers()
  64. {
  65. $moduleManager = new ModuleManager(array());
  66. $identifiers = $moduleManager->getEventManager()->getIdentifiers();
  67. $expected = array('Zend\ModuleManager\ModuleManager', 'module_manager');
  68. $this->assertEquals($expected, array_values($identifiers));
  69. }
  70. public function testCanLoadSomeModule()
  71. {
  72. $configListener = $this->defaultListeners->getConfigListener();
  73. $moduleManager = new ModuleManager(array('SomeModule'), new EventManager);
  74. $moduleManager->getEventManager()->attachAggregate($this->defaultListeners);
  75. $moduleManager->loadModules();
  76. $loadedModules = $moduleManager->getLoadedModules();
  77. $this->assertInstanceOf('SomeModule\Module', $loadedModules['SomeModule']);
  78. $config = $configListener->getMergedConfig();
  79. $this->assertSame($config->some, 'thing');
  80. }
  81. public function testCanLoadMultipleModules()
  82. {
  83. $configListener = $this->defaultListeners->getConfigListener();
  84. $moduleManager = new ModuleManager(array('BarModule', 'BazModule'));
  85. $moduleManager->getEventManager()->attachAggregate($this->defaultListeners);
  86. $moduleManager->loadModules();
  87. $loadedModules = $moduleManager->getLoadedModules();
  88. $this->assertInstanceOf('BarModule\Module', $loadedModules['BarModule']);
  89. $this->assertInstanceOf('BazModule\Module', $loadedModules['BazModule']);
  90. $this->assertInstanceOf('BarModule\Module', $moduleManager->getModule('BarModule'));
  91. $this->assertInstanceOf('BazModule\Module', $moduleManager->getModule('BazModule'));
  92. $this->assertNull($moduleManager->getModule('NotLoaded'));
  93. $config = $configListener->getMergedConfig();
  94. $this->assertSame('foo', $config->bar);
  95. $this->assertSame('bar', $config->baz);
  96. }
  97. public function testModuleLoadingBehavior()
  98. {
  99. $moduleManager = new ModuleManager(array('BarModule'));
  100. $moduleManager->getEventManager()->attachAggregate($this->defaultListeners);
  101. $modules = $moduleManager->getLoadedModules();
  102. $this->assertSame(0, count($modules));
  103. $modules = $moduleManager->getLoadedModules(true);
  104. $this->assertSame(1, count($modules));
  105. $moduleManager->loadModules(); // should not cause any problems
  106. $moduleManager->loadModule('BarModule'); // should not cause any problems
  107. $modules = $moduleManager->getLoadedModules(true); // BarModule already loaded so nothing happens
  108. $this->assertSame(1, count($modules));
  109. }
  110. public function testConstructorThrowsInvalidArgumentException()
  111. {
  112. $this->setExpectedException('InvalidArgumentException');
  113. $moduleManager = new ModuleManager('stringShouldBeArray');
  114. }
  115. public function testNotFoundModuleThrowsRuntimeException()
  116. {
  117. $this->setExpectedException('RuntimeException');
  118. $moduleManager = new ModuleManager(array('NotFoundModule'));
  119. $moduleManager->loadModules();
  120. }
  121. public function testCanLoadModuleDuringTheLoadModuleEvent()
  122. {
  123. $configListener = $this->defaultListeners->getConfigListener();
  124. $moduleManager = new ModuleManager(array('LoadOtherModule', 'BarModule'));
  125. $moduleManager->getEventManager()->attachAggregate($this->defaultListeners);
  126. $moduleManager->loadModules();
  127. $config = $configListener->getMergedConfig();
  128. $this->assertTrue(isset($config['loaded']));
  129. $this->assertSame('oh, yeah baby!', $config['loaded']);
  130. }
  131. public function testModuleIsMarkedAsLoadedWhenLoadModuleEventIsTriggered()
  132. {
  133. $test = new stdClass;
  134. $moduleManager = new ModuleManager(array('BarModule'));
  135. $events = $moduleManager->getEventManager();
  136. $events->attachAggregate($this->defaultListeners);
  137. $events->attach(ModuleEvent::EVENT_LOAD_MODULE, function ($e) use ($test) {
  138. $test->modules = $e->getTarget()->getLoadedModules(false);
  139. });
  140. $moduleManager->loadModules();
  141. $this->assertTrue(isset($test->modules));
  142. $this->assertArrayHasKey('BarModule', $test->modules);
  143. $this->assertInstanceOf('BarModule\Module', $test->modules['BarModule']);
  144. }
  145. }