BootstrapTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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-2015 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_Bootstrap_BootstrapTest::main');
  24. }
  25. /**
  26. * Zend_Loader_Autoloader
  27. */
  28. require_once 'Zend/Loader/Autoloader.php';
  29. /**
  30. * @category Zend
  31. * @package Zend_Application
  32. * @subpackage UnitTests
  33. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. * @group Zend_Application
  36. */
  37. class Zend_Application_Bootstrap_BootstrapTest extends PHPUnit_Framework_TestCase
  38. {
  39. public static function main()
  40. {
  41. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  42. $result = PHPUnit_TextUI_TestRunner::run($suite);
  43. }
  44. public function setUp()
  45. {
  46. // Store original autoloaders
  47. $this->loaders = spl_autoload_functions();
  48. if (!is_array($this->loaders)) {
  49. // spl_autoload_functions does not return empty array when no
  50. // autoloaders registered...
  51. $this->loaders = array();
  52. }
  53. Zend_Loader_Autoloader::resetInstance();
  54. $this->autoloader = Zend_Loader_Autoloader::getInstance();
  55. $this->application = new Zend_Application('testing');
  56. $this->bootstrap = new Zend_Application_Bootstrap_Bootstrap(
  57. $this->application
  58. );
  59. $this->resetFrontController();
  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 resetFrontController()
  75. {
  76. $front = Zend_Controller_Front::getInstance();
  77. $front->resetInstance();
  78. $front->setRequest(new Zend_Controller_Request_HttpTestCase)
  79. ->setResponse(new Zend_Controller_Response_HttpTestCase);
  80. }
  81. public function testFrontControllerResourcePluginShouldBeRegisteredByDefault()
  82. {
  83. $this->assertTrue($this->bootstrap->hasPluginResource('FrontController'));
  84. }
  85. /**
  86. * @expectedException Zend_Application_Bootstrap_Exception
  87. */
  88. public function testRunShouldRaiseExceptionIfNoControllerDirectoryRegisteredWithFrontController()
  89. {
  90. $this->bootstrap->bootstrap();
  91. $this->bootstrap->run();
  92. }
  93. public function testRunShouldDispatchFrontController()
  94. {
  95. $this->bootstrap->setOptions(array(
  96. 'resources' => array(
  97. 'frontcontroller' => array(
  98. 'moduleDirectory' => dirname(__FILE__) . '/../_files/modules',
  99. ),
  100. ),
  101. ));
  102. $this->bootstrap->bootstrap();
  103. $front = $this->bootstrap->getResource('FrontController');
  104. $request = $front->getRequest();
  105. $request->setRequestUri('/zfappbootstrap');
  106. $this->bootstrap->run();
  107. $this->assertTrue($this->bootstrap->getContainer()->zfappbootstrap);
  108. }
  109. /**
  110. * @group ZF-8496
  111. */
  112. public function testBootstrapModuleAutoloaderShouldNotBeInitializedByDefault()
  113. {
  114. $this->assertFalse($this->bootstrap->getResourceLoader() instanceof Zend_Application_Module_Autoloader);
  115. }
  116. /**
  117. * @group ZF-8496
  118. */
  119. public function testBootstrapShouldInitializeModuleAutoloaderWhenNamespaceSpecified()
  120. {
  121. $application = new Zend_Application('testing', array(
  122. 'appnamespace' => 'Application',
  123. ));
  124. $bootstrap = new Zend_Application_Bootstrap_Bootstrap(
  125. $application
  126. );
  127. $this->assertTrue($bootstrap->getResourceLoader() instanceof Zend_Application_Module_Autoloader);
  128. $al = $bootstrap->getResourceLoader();
  129. $this->assertEquals('Application', $al->getNamespace());
  130. }
  131. /**
  132. * @group ZF-8496
  133. */
  134. public function testBootstrapAutoloaderNamespaceShouldBeConfigurable()
  135. {
  136. $application = new Zend_Application('testing', array(
  137. 'appnamespace' => 'Default',
  138. ));
  139. $bootstrap = new Zend_Application_Bootstrap_Bootstrap(
  140. $application
  141. );
  142. $al = $bootstrap->getResourceLoader();
  143. $this->assertEquals('Default', $al->getNamespace());
  144. }
  145. /**
  146. * @group ZF-9435
  147. */
  148. public function testBootstrapShouldInitializeModuleAutoloaderWhenNamespaceSpecifiedAsEmpty()
  149. {
  150. $application = new Zend_Application(
  151. 'testing',
  152. array(
  153. 'appnamespace' => null,
  154. )
  155. );
  156. $bootstrap = new Zend_Application_Bootstrap_Bootstrap(
  157. $application
  158. );
  159. // Tests
  160. $this->assertTrue(
  161. $bootstrap->getResourceLoader() instanceof Zend_Application_Module_Autoloader
  162. );
  163. $al = $bootstrap->getResourceLoader();
  164. $this->assertEquals('', $al->getNamespace());
  165. }
  166. /**
  167. * @group ZF-7367
  168. */
  169. public function testBootstrapRunMethodShouldReturnResponseIfFlagEnabled()
  170. {
  171. $this->bootstrap->setOptions(array(
  172. 'resources' => array(
  173. 'frontcontroller' => array(
  174. 'moduleDirectory' => dirname(__FILE__) . '/../_files/modules',
  175. 'returnresponse' => true,
  176. ),
  177. ),
  178. ));
  179. $this->bootstrap->bootstrap();
  180. $front = $this->bootstrap->getResource('FrontController');
  181. $request = $front->getRequest();
  182. $request->setRequestUri('/zfappbootstrap');
  183. $result = $this->bootstrap->run();
  184. $this->assertTrue($result instanceof Zend_Controller_Response_Abstract);
  185. }
  186. }
  187. if (PHPUnit_MAIN_METHOD == 'Zend_Application_Bootstrap_BootstrapTest::main') {
  188. Zend_Application_Bootstrap_BootstrapTest::main();
  189. }