PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/ZendTest/ModuleManager/ModuleManagerTest.php

https://bitbucket.org/gencer/zf2
PHP | 265 lines | 197 code | 33 blank | 35 comment | 2 complexity | 922fcb09b9537ac162fa38e14ac86844 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-2014 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace ZendTest\ModuleManager;
  10. use PHPUnit_Framework_TestCase as TestCase;
  11. use stdClass;
  12. use Zend\EventManager\EventManager;
  13. use Zend\Loader\AutoloaderFactory;
  14. use Zend\ModuleManager\Listener\ListenerOptions;
  15. use Zend\ModuleManager\Listener\DefaultListenerAggregate;
  16. use Zend\ModuleManager\ModuleEvent;
  17. use Zend\ModuleManager\ModuleManager;
  18. use InvalidArgumentException;
  19. class ModuleManagerTest extends TestCase
  20. {
  21. /**
  22. * @var string
  23. */
  24. protected $tmpdir;
  25. /**
  26. * @var string
  27. */
  28. protected $configCache;
  29. /**
  30. * @var array
  31. */
  32. protected $loaders;
  33. /**
  34. * @var string
  35. */
  36. protected $includePath;
  37. /**
  38. * @var DefaultListenerAggregate
  39. */
  40. protected $defaultListeners;
  41. public function setUp()
  42. {
  43. $this->tmpdir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'zend_module_cache_dir';
  44. @mkdir($this->tmpdir);
  45. $this->configCache = $this->tmpdir . DIRECTORY_SEPARATOR . 'config.cache.php';
  46. // Store original autoloaders
  47. $this->loaders = spl_autoload_functions();
  48. if (!is_array($this->loaders)) {
  49. // spl_autoload_functions does not return empty array when no
  50. // autoloaders registered...
  51. $this->loaders = array();
  52. }
  53. // Store original include_path
  54. $this->includePath = get_include_path();
  55. $this->defaultListeners = new DefaultListenerAggregate(
  56. new ListenerOptions(array(
  57. 'module_paths' => array(
  58. realpath(__DIR__ . '/TestAsset'),
  59. ),
  60. ))
  61. );
  62. }
  63. public function tearDown()
  64. {
  65. $file = glob($this->tmpdir . DIRECTORY_SEPARATOR . '*');
  66. @unlink($file[0]); // change this if there's ever > 1 file
  67. @rmdir($this->tmpdir);
  68. // Restore original autoloaders
  69. AutoloaderFactory::unregisterAutoloaders();
  70. $loaders = spl_autoload_functions();
  71. if (is_array($loaders)) {
  72. foreach ($loaders as $loader) {
  73. spl_autoload_unregister($loader);
  74. }
  75. }
  76. foreach ($this->loaders as $loader) {
  77. spl_autoload_register($loader);
  78. }
  79. // Restore original include_path
  80. set_include_path($this->includePath);
  81. }
  82. public function testEventManagerIdentifiers()
  83. {
  84. $moduleManager = new ModuleManager(array());
  85. $identifiers = $moduleManager->getEventManager()->getIdentifiers();
  86. $expected = array('Zend\ModuleManager\ModuleManager', 'module_manager');
  87. $this->assertEquals($expected, array_values($identifiers));
  88. }
  89. public function testCanLoadSomeModule()
  90. {
  91. $configListener = $this->defaultListeners->getConfigListener();
  92. $moduleManager = new ModuleManager(array('SomeModule'), new EventManager);
  93. $moduleManager->getEventManager()->attachAggregate($this->defaultListeners);
  94. $moduleManager->loadModules();
  95. $loadedModules = $moduleManager->getLoadedModules();
  96. $this->assertInstanceOf('SomeModule\Module', $loadedModules['SomeModule']);
  97. $config = $configListener->getMergedConfig();
  98. $this->assertSame($config->some, 'thing');
  99. }
  100. public function testCanLoadMultipleModules()
  101. {
  102. $configListener = $this->defaultListeners->getConfigListener();
  103. $moduleManager = new ModuleManager(array('BarModule', 'BazModule', 'SubModule\Sub'));
  104. $moduleManager->getEventManager()->attachAggregate($this->defaultListeners);
  105. $moduleManager->loadModules();
  106. $loadedModules = $moduleManager->getLoadedModules();
  107. $this->assertInstanceOf('BarModule\Module', $loadedModules['BarModule']);
  108. $this->assertInstanceOf('BazModule\Module', $loadedModules['BazModule']);
  109. $this->assertInstanceOf('SubModule\Sub\Module', $loadedModules['SubModule\Sub']);
  110. $this->assertInstanceOf('BarModule\Module', $moduleManager->getModule('BarModule'));
  111. $this->assertInstanceOf('BazModule\Module', $moduleManager->getModule('BazModule'));
  112. $this->assertInstanceOf('SubModule\Sub\Module', $moduleManager->getModule('SubModule\Sub'));
  113. $this->assertNull($moduleManager->getModule('NotLoaded'));
  114. $config = $configListener->getMergedConfig();
  115. $this->assertSame('foo', $config->bar);
  116. $this->assertSame('bar', $config->baz);
  117. }
  118. public function testModuleLoadingBehavior()
  119. {
  120. $moduleManager = new ModuleManager(array('BarModule'));
  121. $moduleManager->getEventManager()->attachAggregate($this->defaultListeners);
  122. $modules = $moduleManager->getLoadedModules();
  123. $this->assertSame(0, count($modules));
  124. $modules = $moduleManager->getLoadedModules(true);
  125. $this->assertSame(1, count($modules));
  126. $moduleManager->loadModules(); // should not cause any problems
  127. $moduleManager->loadModule('BarModule'); // should not cause any problems
  128. $modules = $moduleManager->getLoadedModules(true); // BarModule already loaded so nothing happens
  129. $this->assertSame(1, count($modules));
  130. }
  131. public function testConstructorThrowsInvalidArgumentException()
  132. {
  133. $this->setExpectedException('InvalidArgumentException');
  134. $moduleManager = new ModuleManager('stringShouldBeArray');
  135. }
  136. public function testNotFoundModuleThrowsRuntimeException()
  137. {
  138. $this->setExpectedException('RuntimeException');
  139. $moduleManager = new ModuleManager(array('NotFoundModule'));
  140. $moduleManager->loadModules();
  141. }
  142. public function testCanLoadModuleDuringTheLoadModuleEvent()
  143. {
  144. $configListener = $this->defaultListeners->getConfigListener();
  145. $moduleManager = new ModuleManager(array('LoadOtherModule', 'BarModule'));
  146. $moduleManager->getEventManager()->attachAggregate($this->defaultListeners);
  147. $moduleManager->loadModules();
  148. $config = $configListener->getMergedConfig();
  149. $this->assertTrue(isset($config['loaded']));
  150. $this->assertSame('oh, yeah baby!', $config['loaded']);
  151. }
  152. /**
  153. * @group 5651
  154. */
  155. public function testLoadingModuleFromAnotherModuleDemonstratesAppropriateSideEffects()
  156. {
  157. $configListener = $this->defaultListeners->getConfigListener();
  158. $moduleManager = new ModuleManager(array('LoadOtherModule', 'BarModule'));
  159. $moduleManager->getEventManager()->attachAggregate($this->defaultListeners);
  160. $moduleManager->loadModules();
  161. $config = $configListener->getMergedConfig();
  162. $this->assertTrue(isset($config['baz']));
  163. $this->assertSame('bar', $config['baz']);
  164. }
  165. /**
  166. * @group 5651
  167. * @group 5948
  168. */
  169. public function testLoadingModuleFromAnotherModuleDoesNotInfiniteLoop()
  170. {
  171. $configListener = $this->defaultListeners->getConfigListener();
  172. $moduleManager = new ModuleManager(array('LoadBarModule', 'LoadFooModule'));
  173. $moduleManager->getEventManager()->attachAggregate($this->defaultListeners);
  174. $moduleManager->loadModules();
  175. $config = $configListener->getMergedConfig();
  176. $this->assertTrue(isset($config['bar']));
  177. $this->assertSame('bar', $config['bar']);
  178. $this->assertTrue(isset($config['foo']));
  179. $this->assertSame('bar', $config['foo']);
  180. }
  181. public function testModuleIsMarkedAsLoadedWhenLoadModuleEventIsTriggered()
  182. {
  183. $test = new stdClass;
  184. $moduleManager = new ModuleManager(array('BarModule'));
  185. $events = $moduleManager->getEventManager();
  186. $events->attachAggregate($this->defaultListeners);
  187. $events->attach(ModuleEvent::EVENT_LOAD_MODULE, function ($e) use ($test) {
  188. $test->modules = $e->getTarget()->getLoadedModules(false);
  189. });
  190. $moduleManager->loadModules();
  191. $this->assertTrue(isset($test->modules));
  192. $this->assertArrayHasKey('BarModule', $test->modules);
  193. $this->assertInstanceOf('BarModule\Module', $test->modules['BarModule']);
  194. }
  195. public function testCanLoadSomeObjectModule()
  196. {
  197. require_once __DIR__ . '/TestAsset/SomeModule/Module.php';
  198. require_once __DIR__ . '/TestAsset/SubModule/Sub/Module.php';
  199. $configListener = $this->defaultListeners->getConfigListener();
  200. $moduleManager = new ModuleManager(array(
  201. 'SomeModule' => new \SomeModule\Module(),
  202. 'SubModule' => new \SubModule\Sub\Module(),
  203. ), new EventManager);
  204. $moduleManager->getEventManager()->attachAggregate($this->defaultListeners);
  205. $moduleManager->loadModules();
  206. $loadedModules = $moduleManager->getLoadedModules();
  207. $this->assertInstanceOf('SomeModule\Module', $loadedModules['SomeModule']);
  208. $config = $configListener->getMergedConfig();
  209. $this->assertSame($config->some, 'thing');
  210. }
  211. public function testCanLoadMultipleModulesObjectWithString()
  212. {
  213. require_once __DIR__ . '/TestAsset/SomeModule/Module.php';
  214. $configListener = $this->defaultListeners->getConfigListener();
  215. $moduleManager = new ModuleManager(array('SomeModule' => new \SomeModule\Module(), 'BarModule'), new EventManager);
  216. $moduleManager->getEventManager()->attachAggregate($this->defaultListeners);
  217. $moduleManager->loadModules();
  218. $loadedModules = $moduleManager->getLoadedModules();
  219. $this->assertInstanceOf('SomeModule\Module', $loadedModules['SomeModule']);
  220. $config = $configListener->getMergedConfig();
  221. $this->assertSame($config->some, 'thing');
  222. }
  223. public function testCanNotLoadSomeObjectModuleWithoutIdentifier()
  224. {
  225. require_once __DIR__ . '/TestAsset/SomeModule/Module.php';
  226. $configListener = $this->defaultListeners->getConfigListener();
  227. $moduleManager = new ModuleManager(array(new \SomeModule\Module()), new EventManager);
  228. $moduleManager->getEventManager()->attachAggregate($this->defaultListeners);
  229. $this->setExpectedException('Zend\ModuleManager\Exception\RuntimeException');
  230. $moduleManager->loadModules();
  231. }
  232. }