2
0

BootstrapTest.php 8.0 KB

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