BootstrapTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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-2010 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-2010 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->assertNotNull($loader, "resource loader is unexpectedly NULL");
  82. $this->assertEquals($module, $loader->getNamespace());
  83. }
  84. public function testConstructorShouldAcceptResourceLoaderInOptions()
  85. {
  86. $loader = new Zend_Loader_Autoloader_Resource(array(
  87. 'namespace' => 'Foo',
  88. 'basePath' => dirname(__FILE__),
  89. ));
  90. $this->application->setOptions(array('resourceLoader' => $loader));
  91. require_once dirname(__FILE__) . '/../_files/ZfModuleBootstrap.php';
  92. $bootstrap = new ZfModule_Bootstrap($this->application);
  93. $this->assertSame($loader, $bootstrap->getResourceLoader(), var_export($bootstrap->getOptions(), 1));
  94. }
  95. public function testModuleNameShouldBeFirstSegmentOfClassName()
  96. {
  97. require_once dirname(__FILE__) . '/../_files/ZfModuleBootstrap.php';
  98. $bootstrap = new ZfModule_Bootstrap($this->application);
  99. $this->assertEquals('ZfModule', $bootstrap->getModuleName());
  100. }
  101. public function testShouldPullModuleNamespacedOptionsWhenPresent()
  102. {
  103. $options = array(
  104. 'foo' => 'bar',
  105. 'ZfModule' => array(
  106. 'foo' => 'baz',
  107. )
  108. );
  109. $this->application->setOptions($options);
  110. require_once dirname(__FILE__) . '/../_files/ZfModuleBootstrap.php';
  111. $bootstrap = new ZfModule_Bootstrap($this->application);
  112. $this->assertEquals('baz', $bootstrap->foo);
  113. }
  114. /**
  115. * @group ZF-6545
  116. */
  117. public function testFrontControllerPluginResourceShouldBeRegistered()
  118. {
  119. require_once dirname(__FILE__) . '/../_files/ZfModuleBootstrap.php';
  120. $bootstrap = new ZfModule_Bootstrap($this->application);
  121. $this->assertTrue($bootstrap->hasPluginResource('FrontController'));
  122. }
  123. /**
  124. * @group ZF-6545
  125. */
  126. public function testFrontControllerStateRemainsSameIfNoOptionsPassedToModuleBootstrap()
  127. {
  128. require_once dirname(__FILE__) . '/../_files/ZfModuleBootstrap.php';
  129. $this->application->setOptions(array(
  130. 'resources' => array(
  131. 'frontController' => array(
  132. 'baseUrl' => '/foo',
  133. 'controllerDirectory' => dirname(__FILE__),
  134. ),
  135. ),
  136. 'bootstrap' => array(
  137. 'path' => dirname(__FILE__) . '/../_files/ZfAppBootstrap.php',
  138. 'class' => 'ZfAppBootstrap',
  139. ),
  140. 'ZfModule' => array(
  141. 'resources' => array(
  142. 'FrontController' => array(),
  143. ),
  144. ),
  145. ));
  146. $appBootstrap = $this->application->getBootstrap();
  147. $appBootstrap->bootstrap('FrontController');
  148. $front = $appBootstrap->getResource('FrontController');
  149. $bootstrap = new ZfModule_Bootstrap($appBootstrap);
  150. $bootstrap->bootstrap('FrontController');
  151. $test = $bootstrap->getResource('FrontController');
  152. $this->assertSame($front, $test);
  153. $this->assertEquals('/foo', $test->getBaseUrl());
  154. $this->assertEquals(dirname(__FILE__), $test->getControllerDirectory('default'));
  155. }
  156. /**
  157. * @group ZF-6545
  158. */
  159. public function testModuleBootstrapsShouldNotAcceptModuleResourceInOrderToPreventRecursion()
  160. {
  161. require_once dirname(__FILE__) . '/../_files/ZfModuleBootstrap.php';
  162. $this->application->setOptions(array(
  163. 'resources' => array(
  164. 'modules' => array(),
  165. 'frontController' => array(
  166. 'baseUrl' => '/foo',
  167. 'moduleDirectory' => dirname(__FILE__) . '/../_files/modules',
  168. ),
  169. ),
  170. 'bootstrap' => array(
  171. 'path' => dirname(__FILE__) . '/../_files/ZfAppBootstrap.php',
  172. 'class' => 'ZfAppBootstrap',
  173. )
  174. ));
  175. $appBootstrap = $this->application->getBootstrap();
  176. $appBootstrap->bootstrap('Modules');
  177. $modules = $appBootstrap->getResource('Modules');
  178. foreach ($modules as $module => $bootstrap) {
  179. if ($module == 'default') {
  180. // "default" module gets lumped in, and is not a Module_Bootstrap
  181. continue;
  182. }
  183. $resources = $bootstrap->getPluginResourceNames();
  184. $this->assertFalse($bootstrap->hasPluginResource('Modules'));
  185. }
  186. }
  187. /**
  188. * @group ZF-6567
  189. */
  190. public function testModuleBootstrapShouldInheritApplicationBootstrapPluginPaths()
  191. {
  192. require_once dirname(__FILE__) . '/../_files/ZfModuleBootstrap.php';
  193. $this->application->setOptions(array(
  194. 'resources' => array(
  195. 'modules' => array(),
  196. 'frontController' => array(
  197. 'baseUrl' => '/foo',
  198. 'moduleDirectory' => dirname(__FILE__) . '/../_files/modules',
  199. ),
  200. ),
  201. 'pluginPaths' => array(
  202. 'ZfModuleBootstrap_Resource' => dirname(__FILE__),
  203. ),
  204. 'bootstrap' => array(
  205. 'path' => dirname(__FILE__) . '/../_files/ZfAppBootstrap.php',
  206. 'class' => 'ZfAppBootstrap',
  207. )
  208. ));
  209. $appBootstrap = $this->application->getBootstrap();
  210. $appBootstrap->bootstrap('Modules');
  211. $modules = $appBootstrap->getResource('Modules');
  212. foreach ($modules as $bootstrap) {
  213. $loader = $bootstrap->getPluginLoader();
  214. $paths = $loader->getPaths();
  215. $this->assertTrue(array_key_exists('ZfModuleBootstrap_Resource_', $paths));
  216. }
  217. }
  218. }
  219. if (PHPUnit_MAIN_METHOD == 'Zend_Application_Module_BootstrapTest::main') {
  220. Zend_Application_Module_BootstrapTest::main();
  221. }