BootstrapTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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-2009 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_Module_BootstrapTest::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-2009 Zend Technologies USA Inc. (http://www.zend.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. * @group Zend_Application
  40. */
  41. class Zend_Application_Module_BootstrapTest extends PHPUnit_Framework_TestCase
  42. {
  43. public static function main()
  44. {
  45. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  46. $result = PHPUnit_TextUI_TestRunner::run($suite);
  47. }
  48. public function setUp()
  49. {
  50. // Store original autoloaders
  51. $this->loaders = spl_autoload_functions();
  52. if (!is_array($this->loaders)) {
  53. // spl_autoload_functions does not return empty array when no
  54. // autoloaders registered...
  55. $this->loaders = array();
  56. }
  57. Zend_Loader_Autoloader::resetInstance();
  58. $this->autoloader = Zend_Loader_Autoloader::getInstance();
  59. $this->application = new Zend_Application('testing');
  60. Zend_Controller_Front::getInstance()->resetInstance();
  61. }
  62. public function tearDown()
  63. {
  64. // Restore original autoloaders
  65. $loaders = spl_autoload_functions();
  66. foreach ($loaders as $loader) {
  67. spl_autoload_unregister($loader);
  68. }
  69. foreach ($this->loaders as $loader) {
  70. spl_autoload_register($loader);
  71. }
  72. // Reset autoloader instance so it doesn't affect other tests
  73. Zend_Loader_Autoloader::resetInstance();
  74. }
  75. public function testConstructorShouldInitializeModuleResourceLoaderWithModulePrefix()
  76. {
  77. require_once dirname(__FILE__) . '/../_files/ZfModuleBootstrap.php';
  78. $bootstrap = new ZfModule_Bootstrap($this->application);
  79. $module = $bootstrap->getModuleName();
  80. $loader = $bootstrap->getResourceLoader();
  81. $this->assertEquals($module, $loader->getNamespace());
  82. }
  83. public function testConstructorShouldAcceptResourceLoaderInOptions()
  84. {
  85. $loader = new Zend_Loader_Autoloader_Resource(array(
  86. 'namespace' => 'Foo',
  87. 'basePath' => dirname(__FILE__),
  88. ));
  89. $this->application->setOptions(array('resourceLoader' => $loader));
  90. require_once dirname(__FILE__) . '/../_files/ZfModuleBootstrap.php';
  91. $bootstrap = new ZfModule_Bootstrap($this->application);
  92. $this->assertSame($loader, $bootstrap->getResourceLoader(), var_export($bootstrap->getOptions(), 1));
  93. }
  94. public function testModuleNameShouldBeFirstSegmentOfClassName()
  95. {
  96. require_once dirname(__FILE__) . '/../_files/ZfModuleBootstrap.php';
  97. $bootstrap = new ZfModule_Bootstrap($this->application);
  98. $this->assertEquals('ZfModule', $bootstrap->getModuleName());
  99. }
  100. public function testShouldPullModuleNamespacedOptionsWhenPresent()
  101. {
  102. $options = array(
  103. 'foo' => 'bar',
  104. 'ZfModule' => array(
  105. 'foo' => 'baz',
  106. )
  107. );
  108. $this->application->setOptions($options);
  109. require_once dirname(__FILE__) . '/../_files/ZfModuleBootstrap.php';
  110. $bootstrap = new ZfModule_Bootstrap($this->application);
  111. $this->assertEquals('baz', $bootstrap->foo);
  112. }
  113. /**
  114. * @group ZF-6545
  115. */
  116. public function testFrontControllerPluginResourceShouldBeRegistered()
  117. {
  118. require_once dirname(__FILE__) . '/../_files/ZfModuleBootstrap.php';
  119. $bootstrap = new ZfModule_Bootstrap($this->application);
  120. $this->assertTrue($bootstrap->hasPluginResource('FrontController'));
  121. }
  122. /**
  123. * @group ZF-6545
  124. */
  125. public function testFrontControllerStateRemainsSameIfNoOptionsPassedToModuleBootstrap()
  126. {
  127. require_once dirname(__FILE__) . '/../_files/ZfModuleBootstrap.php';
  128. $this->application->setOptions(array(
  129. 'resources' => array(
  130. 'frontController' => array(
  131. 'baseUrl' => '/foo',
  132. 'controllerDirectory' => dirname(__FILE__),
  133. ),
  134. ),
  135. 'bootstrap' => array(
  136. 'path' => dirname(__FILE__) . '/../_files/ZfAppBootstrap.php',
  137. 'class' => 'ZfAppBootstrap',
  138. ),
  139. 'ZfModule' => array(
  140. 'resources' => array(
  141. 'FrontController' => array(),
  142. ),
  143. ),
  144. ));
  145. $appBootstrap = $this->application->getBootstrap();
  146. $appBootstrap->bootstrap('FrontController');
  147. $front = $appBootstrap->getResource('FrontController');
  148. $bootstrap = new ZfModule_Bootstrap($appBootstrap);
  149. $bootstrap->bootstrap('FrontController');
  150. $test = $bootstrap->getResource('FrontController');
  151. $this->assertSame($front, $test);
  152. $this->assertEquals('/foo', $test->getBaseUrl());
  153. $this->assertEquals(dirname(__FILE__), $test->getControllerDirectory('default'));
  154. }
  155. /**
  156. * @group ZF-6545
  157. */
  158. public function testModuleBootstrapsShouldNotAcceptModuleResourceInOrderToPreventRecursion()
  159. {
  160. require_once dirname(__FILE__) . '/../_files/ZfModuleBootstrap.php';
  161. $this->application->setOptions(array(
  162. 'resources' => array(
  163. 'modules' => array(),
  164. 'frontController' => array(
  165. 'baseUrl' => '/foo',
  166. 'moduleDirectory' => dirname(__FILE__) . '/../_files/modules',
  167. ),
  168. ),
  169. 'bootstrap' => array(
  170. 'path' => dirname(__FILE__) . '/../_files/ZfAppBootstrap.php',
  171. 'class' => 'ZfAppBootstrap',
  172. )
  173. ));
  174. $appBootstrap = $this->application->getBootstrap();
  175. $appBootstrap->bootstrap('Modules');
  176. $modules = $appBootstrap->getResource('Modules');
  177. foreach ($modules as $module => $bootstrap) {
  178. if ($module == 'default') {
  179. // "default" module gets lumped in, and is not a Module_Bootstrap
  180. continue;
  181. }
  182. $resources = $bootstrap->getPluginResourceNames();
  183. $this->assertFalse($bootstrap->hasPluginResource('Modules'));
  184. }
  185. }
  186. /**
  187. * @group ZF-6567
  188. */
  189. public function testModuleBootstrapShouldInheritApplicationBootstrapPluginPaths()
  190. {
  191. require_once dirname(__FILE__) . '/../_files/ZfModuleBootstrap.php';
  192. $this->application->setOptions(array(
  193. 'resources' => array(
  194. 'modules' => array(),
  195. 'frontController' => array(
  196. 'baseUrl' => '/foo',
  197. 'moduleDirectory' => dirname(__FILE__) . '/../_files/modules',
  198. ),
  199. ),
  200. 'pluginPaths' => array(
  201. 'ZfModuleBootstrap_Resource' => dirname(__FILE__),
  202. ),
  203. 'bootstrap' => array(
  204. 'path' => dirname(__FILE__) . '/../_files/ZfAppBootstrap.php',
  205. 'class' => 'ZfAppBootstrap',
  206. )
  207. ));
  208. $appBootstrap = $this->application->getBootstrap();
  209. $appBootstrap->bootstrap('Modules');
  210. $modules = $appBootstrap->getResource('Modules');
  211. foreach ($modules as $bootstrap) {
  212. $loader = $bootstrap->getPluginLoader();
  213. $paths = $loader->getPaths();
  214. $this->assertTrue(array_key_exists('ZfModuleBootstrap_Resource_', $paths));
  215. }
  216. }
  217. }
  218. if (PHPUnit_MAIN_METHOD == 'Zend_Application_Module_BootstrapTest::main') {
  219. Zend_Application_Module_BootstrapTest::main();
  220. }