BootstrapTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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_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-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_Module_BootstrapTest 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. Zend_Controller_Front::getInstance()->resetInstance();
  60. }
  61. public function tearDown()
  62. {
  63. // Restore original autoloaders
  64. $loaders = spl_autoload_functions();
  65. foreach ($loaders as $loader) {
  66. spl_autoload_unregister($loader);
  67. }
  68. foreach ($this->loaders as $loader) {
  69. spl_autoload_register($loader);
  70. }
  71. // Reset autoloader instance so it doesn't affect other tests
  72. Zend_Loader_Autoloader::resetInstance();
  73. }
  74. public function testConstructorShouldInitializeModuleResourceLoaderWithModulePrefix()
  75. {
  76. require_once dirname(__FILE__) . '/../_files/ZfModuleBootstrap.php';
  77. $bootstrap = new ZfModule_Bootstrap($this->application);
  78. $module = $bootstrap->getModuleName();
  79. $loader = $bootstrap->getResourceLoader();
  80. $this->assertEquals($module, $loader->getNamespace());
  81. }
  82. public function testConstructorShouldAcceptResourceLoaderInOptions()
  83. {
  84. $loader = new Zend_Loader_Autoloader_Resource(array(
  85. 'namespace' => 'Foo',
  86. 'basePath' => dirname(__FILE__),
  87. ));
  88. $this->application->setOptions(array('resourceLoader' => $loader));
  89. require_once dirname(__FILE__) . '/../_files/ZfModuleBootstrap.php';
  90. $bootstrap = new ZfModule_Bootstrap($this->application);
  91. $this->assertSame($loader, $bootstrap->getResourceLoader(), var_export($bootstrap->getOptions(), 1));
  92. }
  93. public function testModuleNameShouldBeFirstSegmentOfClassName()
  94. {
  95. require_once dirname(__FILE__) . '/../_files/ZfModuleBootstrap.php';
  96. $bootstrap = new ZfModule_Bootstrap($this->application);
  97. $this->assertEquals('ZfModule', $bootstrap->getModuleName());
  98. }
  99. public function testShouldPullModuleNamespacedOptionsWhenPresent()
  100. {
  101. $options = array(
  102. 'foo' => 'bar',
  103. 'ZfModule' => array(
  104. 'foo' => 'baz',
  105. )
  106. );
  107. $this->application->setOptions($options);
  108. require_once dirname(__FILE__) . '/../_files/ZfModuleBootstrap.php';
  109. $bootstrap = new ZfModule_Bootstrap($this->application);
  110. $this->assertEquals('baz', $bootstrap->foo);
  111. }
  112. /**
  113. * @group ZF-6545
  114. */
  115. public function testFrontControllerPluginResourceShouldBeRegistered()
  116. {
  117. require_once dirname(__FILE__) . '/../_files/ZfModuleBootstrap.php';
  118. $bootstrap = new ZfModule_Bootstrap($this->application);
  119. $this->assertTrue($bootstrap->hasPluginResource('FrontController'));
  120. }
  121. /**
  122. * @group ZF-6545
  123. */
  124. public function testFrontControllerStateRemainsSameIfNoOptionsPassedToModuleBootstrap()
  125. {
  126. require_once dirname(__FILE__) . '/../_files/ZfModuleBootstrap.php';
  127. $this->application->setOptions(array(
  128. 'resources' => array(
  129. 'frontController' => array(
  130. 'baseUrl' => '/foo',
  131. 'controllerDirectory' => dirname(__FILE__),
  132. ),
  133. ),
  134. 'bootstrap' => array(
  135. 'path' => dirname(__FILE__) . '/../_files/ZfAppBootstrap.php',
  136. 'class' => 'ZfAppBootstrap',
  137. ),
  138. 'ZfModule' => array(
  139. 'resources' => array(
  140. 'FrontController' => array(),
  141. ),
  142. ),
  143. ));
  144. $appBootstrap = $this->application->getBootstrap();
  145. $appBootstrap->bootstrap('FrontController');
  146. $front = $appBootstrap->getResource('FrontController');
  147. $bootstrap = new ZfModule_Bootstrap($appBootstrap);
  148. $bootstrap->bootstrap('FrontController');
  149. $test = $bootstrap->getResource('FrontController');
  150. $this->assertSame($front, $test);
  151. $this->assertEquals('/foo', $test->getBaseUrl());
  152. $this->assertEquals(dirname(__FILE__), $test->getControllerDirectory('default'));
  153. }
  154. /**
  155. * @group ZF-6545
  156. */
  157. public function testModuleBootstrapsShouldNotAcceptModuleResourceInOrderToPreventRecursion()
  158. {
  159. require_once dirname(__FILE__) . '/../_files/ZfModuleBootstrap.php';
  160. $this->application->setOptions(array(
  161. 'resources' => array(
  162. 'modules' => array(),
  163. 'frontController' => array(
  164. 'baseUrl' => '/foo',
  165. 'moduleDirectory' => dirname(__FILE__) . '/../_files/modules',
  166. ),
  167. ),
  168. 'bootstrap' => array(
  169. 'path' => dirname(__FILE__) . '/../_files/ZfAppBootstrap.php',
  170. 'class' => 'ZfAppBootstrap',
  171. )
  172. ));
  173. $appBootstrap = $this->application->getBootstrap();
  174. $appBootstrap->bootstrap('Modules');
  175. $modules = $appBootstrap->getResource('Modules');
  176. foreach ($modules as $bootstrap) {
  177. $resources = $bootstrap->getPluginResourceNames();
  178. $this->assertFalse($bootstrap->hasPluginResource('Modules'), var_export($resources, 1));
  179. }
  180. }
  181. /**
  182. * @group ZF-6567
  183. */
  184. public function testModuleBootstrapShouldInheritApplicationBootstrapPluginPaths()
  185. {
  186. require_once dirname(__FILE__) . '/../_files/ZfModuleBootstrap.php';
  187. $this->application->setOptions(array(
  188. 'resources' => array(
  189. 'modules' => array(),
  190. 'frontController' => array(
  191. 'baseUrl' => '/foo',
  192. 'moduleDirectory' => dirname(__FILE__) . '/../_files/modules',
  193. ),
  194. ),
  195. 'pluginPaths' => array(
  196. 'ZfModuleBootstrap_Resource' => dirname(__FILE__),
  197. ),
  198. 'bootstrap' => array(
  199. 'path' => dirname(__FILE__) . '/../_files/ZfAppBootstrap.php',
  200. 'class' => 'ZfAppBootstrap',
  201. )
  202. ));
  203. $appBootstrap = $this->application->getBootstrap();
  204. $appBootstrap->bootstrap('Modules');
  205. $modules = $appBootstrap->getResource('Modules');
  206. foreach ($modules as $bootstrap) {
  207. $loader = $bootstrap->getPluginLoader();
  208. $paths = $loader->getPaths();
  209. $this->assertTrue(array_key_exists('ZfModuleBootstrap_Resource_', $paths));
  210. }
  211. }
  212. }
  213. if (PHPUnit_MAIN_METHOD == 'Zend_Application_Module_BootstrapTest::main') {
  214. Zend_Application_Module_BootstrapTest::main();
  215. }