ModulesTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Application
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: $
  21. */
  22. if (!defined('PHPUnit_MAIN_METHOD')) {
  23. define('PHPUnit_MAIN_METHOD', 'Zend_Application_Resource_ModulesTest::main');
  24. }
  25. /**
  26. * Test helper
  27. */
  28. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  29. /**
  30. * Zend_Loader_Autoloader
  31. */
  32. require_once 'Zend/Loader/Autoloader.php';
  33. /**
  34. * @category Zend
  35. * @package Zend_Application
  36. * @subpackage UnitTests
  37. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. */
  40. class Zend_Application_Resource_ModulesTest extends PHPUnit_Framework_TestCase
  41. {
  42. public static function main()
  43. {
  44. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  45. $result = PHPUnit_TextUI_TestRunner::run($suite);
  46. }
  47. public function setUp()
  48. {
  49. // Store original autoloaders
  50. $this->loaders = spl_autoload_functions();
  51. if (!is_array($this->loaders)) {
  52. // spl_autoload_functions does not return empty array when no
  53. // autoloaders registered...
  54. $this->loaders = array();
  55. }
  56. Zend_Loader_Autoloader::resetInstance();
  57. $this->autoloader = Zend_Loader_Autoloader::getInstance();
  58. $this->application = new Zend_Application('testing');
  59. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  60. $this->bootstrap = new ZfAppBootstrap($this->application);
  61. $this->front = Zend_Controller_Front::getInstance();
  62. $this->front->resetInstance();
  63. }
  64. public function tearDown()
  65. {
  66. // Restore original autoloaders
  67. $loaders = spl_autoload_functions();
  68. foreach ($loaders as $loader) {
  69. spl_autoload_unregister($loader);
  70. }
  71. foreach ($this->loaders as $loader) {
  72. spl_autoload_register($loader);
  73. }
  74. // Reset autoloader instance so it doesn't affect other tests
  75. Zend_Loader_Autoloader::resetInstance();
  76. }
  77. public function testInitializationTriggersNothingIfNoModulesRegistered()
  78. {
  79. require_once 'Zend/Application/Resource/Modules.php';
  80. $this->bootstrap->registerPluginResource('Frontcontroller', array());
  81. $resource = new Zend_Application_Resource_Modules(array());
  82. $resource->setBootstrap($this->bootstrap);
  83. $resource->init();
  84. $this->assertFalse(isset($this->bootstrap->default));
  85. $this->assertFalse(isset($this->bootstrap->foo));
  86. $this->assertFalse(isset($this->bootstrap->bar));
  87. }
  88. public function testInitializationNeverTriggersDefaultModuleBootstrap()
  89. {
  90. require_once 'Zend/Application/Resource/Modules.php';
  91. $this->bootstrap->registerPluginResource('Frontcontroller', array(
  92. 'moduleDirectory' => dirname(__FILE__) . '/../_files/modules',
  93. ));
  94. $resource = new Zend_Application_Resource_Modules(array());
  95. $resource->setBootstrap($this->bootstrap);
  96. $resource->init();
  97. $this->assertFalse(isset($this->bootstrap->default));
  98. }
  99. public function testInitializationShouldTriggerModuleBootstrapsWhenTheyExist()
  100. {
  101. require_once 'Zend/Application/Resource/Modules.php';
  102. $this->bootstrap->registerPluginResource('Frontcontroller', array(
  103. 'moduleDirectory' => dirname(__FILE__) . '/../_files/modules',
  104. ));
  105. $resource = new Zend_Application_Resource_Modules(array());
  106. $resource->setBootstrap($this->bootstrap);
  107. $resource->init();
  108. $this->assertTrue($this->bootstrap->foo);
  109. $this->assertTrue($this->bootstrap->bar);
  110. }
  111. public function testInitializationShouldSkipModulesWithoutBootstraps()
  112. {
  113. require_once 'Zend/Application/Resource/Modules.php';
  114. $this->bootstrap->registerPluginResource('Frontcontroller', array(
  115. 'moduleDirectory' => dirname(__FILE__) . '/../_files/modules',
  116. ));
  117. $resource = new Zend_Application_Resource_Modules(array());
  118. $resource->setBootstrap($this->bootstrap);
  119. $resource->init();
  120. $bootstraps = $resource->getExecutedBootstraps();
  121. $this->assertEquals(3, count((array)$bootstraps));
  122. $this->assertArrayHasKey('bar', (array)$bootstraps);
  123. $this->assertArrayHasKey('foo-bar', (array)$bootstraps);
  124. $this->assertArrayHasKey('foo', (array)$bootstraps);
  125. }
  126. public function testShouldReturnExecutedBootstrapsWhenComplete()
  127. {
  128. require_once 'Zend/Application/Resource/Modules.php';
  129. $this->bootstrap->registerPluginResource('Frontcontroller', array(
  130. 'moduleDirectory' => dirname(__FILE__) . '/../_files/modules',
  131. ));
  132. $resource = new Zend_Application_Resource_Modules(array());
  133. $resource->setBootstrap($this->bootstrap);
  134. $bootstraps = $resource->init();
  135. $this->assertEquals(3, count((array)$bootstraps));
  136. $this->assertArrayHasKey('bar', (array)$bootstraps);
  137. $this->assertArrayHasKey('foo-bar', (array)$bootstraps);
  138. $this->assertArrayHasKey('foo', (array)$bootstraps);
  139. }
  140. }
  141. if (PHPUnit_MAIN_METHOD == 'Zend_Application_Resource_ModulesTest::main') {
  142. Zend_Application_Resource_ModulesTest::main();
  143. }