BootstrapAbstractTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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_Bootstrap_BootstrapAbstractTest::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_Bootstrap_BootstrapAbstractTest 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. }
  60. public function tearDown()
  61. {
  62. // Restore original autoloaders
  63. $loaders = spl_autoload_functions();
  64. foreach ($loaders as $loader) {
  65. spl_autoload_unregister($loader);
  66. }
  67. foreach ($this->loaders as $loader) {
  68. spl_autoload_register($loader);
  69. }
  70. }
  71. public function testConstructorShouldPopulateApplication()
  72. {
  73. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  74. $bootstrap = new ZfAppBootstrap($this->application);
  75. $this->assertSame($this->application, $bootstrap->getApplication());
  76. }
  77. public function testConstructorShouldPopulateOptionsFromApplicationObject()
  78. {
  79. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  80. $options = array(
  81. 'foo' => 'bar',
  82. 'bar' => 'baz',
  83. );
  84. $this->application->setOptions($options);
  85. $bootstrap = new ZfAppBootstrap($this->application);
  86. $this->assertSame($options, $bootstrap->getOptions());
  87. }
  88. public function testConstructorShouldAllowPassingAnotherBootstrapObject()
  89. {
  90. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  91. $bootstrap1 = new ZfAppBootstrap($this->application);
  92. $bootstrap2 = new ZfAppBootstrap($bootstrap1);
  93. $this->assertSame($bootstrap1, $bootstrap2->getApplication());
  94. }
  95. /**
  96. * @expectedException Zend_Application_Bootstrap_Exception
  97. */
  98. public function testConstructorShouldRaiseExceptionForInvalidApplicationArgument()
  99. {
  100. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  101. $bootstrap = new ZfAppBootstrap(new stdClass);
  102. }
  103. public function testSettingOptionsShouldProxyToInternalSetters()
  104. {
  105. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  106. $options = array(
  107. 'arbitrary' => 'foo',
  108. );
  109. $bootstrap = new ZfAppBootstrap($this->application);
  110. $bootstrap->setOptions($options);
  111. $this->assertEquals('foo', $bootstrap->getArbitrary());
  112. }
  113. public function testPluginPathsOptionKeyShouldAddPrefixPathsToPluginLoader()
  114. {
  115. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  116. $bootstrap = new ZfAppBootstrap($this->application);
  117. $bootstrap->setOptions(array(
  118. 'pluginPaths' => array(
  119. 'Foo' => 'foo/bar/path/',
  120. ),
  121. ));
  122. $loader = $bootstrap->getPluginLoader();
  123. $paths = $loader->getPaths('Foo');
  124. $this->assertTrue(is_array($paths));
  125. }
  126. public function testResourcesOptionKeyShouldRegisterBootstrapPluginResources()
  127. {
  128. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  129. $bootstrap = new ZfAppBootstrap($this->application);
  130. $bootstrap->setOptions(array(
  131. 'resources' => array(
  132. 'view' => array(
  133. 'basePath' => dirname(__FILE__) . '/../_files/views/scripts',
  134. ),
  135. ),
  136. ));
  137. $this->assertTrue($bootstrap->hasPluginResource('view'));
  138. }
  139. public function testHasOptionShouldReturnFalseWhenOptionUnavailable()
  140. {
  141. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  142. $bootstrap = new ZfAppBootstrap($this->application);
  143. $this->assertFalse($bootstrap->hasOption('foo'));
  144. }
  145. public function testHasOptionShouldReturnTrueWhenOptionPresent()
  146. {
  147. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  148. $bootstrap = new ZfAppBootstrap($this->application);
  149. $bootstrap->setOptions(array('foo' => 'bar'));
  150. $this->assertTrue($bootstrap->hasOption('foo'));
  151. }
  152. public function testGetOptionShouldReturnNullWhenOptionUnavailable()
  153. {
  154. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  155. $bootstrap = new ZfAppBootstrap($this->application);
  156. $this->assertNull($bootstrap->getOption('foo'));
  157. }
  158. public function testGetOptionShouldReturnOptionValue()
  159. {
  160. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  161. $bootstrap = new ZfAppBootstrap($this->application);
  162. $bootstrap->setOptions(array('foo' => 'bar'));
  163. $this->assertEquals('bar', $bootstrap->getOption('foo'));
  164. }
  165. public function testInternalIntializersShouldBeRegisteredAsClassResources()
  166. {
  167. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  168. $bootstrap = new ZfAppBootstrap($this->application);
  169. $test = $bootstrap->getClassResources();
  170. $resources = array('foo' => '_initFoo', 'bar' => '_initBar', 'barbaz' => '_initBarbaz');
  171. $this->assertEquals($resources, $test);
  172. }
  173. public function testInternalInitializersShouldRegisterResourceNames()
  174. {
  175. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  176. $bootstrap = new ZfAppBootstrap($this->application);
  177. $test = $bootstrap->getClassResourceNames();
  178. $resources = array('foo', 'bar', 'barbaz');
  179. $this->assertEquals($resources, $test);
  180. }
  181. /**
  182. * @expectedException Zend_Application_Bootstrap_Exception
  183. */
  184. public function testRegisterPluginResourceShouldThrowExceptionForInvalidResourceType()
  185. {
  186. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  187. $bootstrap = new ZfAppBootstrap($this->application);
  188. $bootstrap->registerPluginResource(array());
  189. }
  190. public function testShouldAllowRegisteringConcretePluginResources()
  191. {
  192. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  193. $bootstrap = new ZfAppBootstrap($this->application);
  194. $resource = new Zend_Application_Resource_View();
  195. $bootstrap->registerPluginResource($resource);
  196. $test = $bootstrap->getPluginResource('view');
  197. $this->assertSame($resource, $test);
  198. }
  199. public function testRegisteringSecondPluginResourceOfSameTypeShouldOverwrite()
  200. {
  201. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  202. $bootstrap = new ZfAppBootstrap($this->application);
  203. $resource1 = new Zend_Application_Resource_View();
  204. $resource2 = new Zend_Application_Resource_View();
  205. $bootstrap->registerPluginResource($resource1)
  206. ->registerPluginResource($resource2);
  207. $test = $bootstrap->getPluginResource('view');
  208. $this->assertSame($resource2, $test);
  209. }
  210. public function testShouldAllowRegisteringPluginResourceUsingNameOnly()
  211. {
  212. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  213. $bootstrap = new ZfAppBootstrap($this->application);
  214. $bootstrap->registerPluginResource('view');
  215. $test = $bootstrap->getPluginResource('view');
  216. $this->assertEquals('Zend_Application_Resource_View', get_class($test));
  217. }
  218. public function testShouldAllowUnregisteringPluginResourcesUsingConcreteInstance()
  219. {
  220. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  221. $bootstrap = new ZfAppBootstrap($this->application);
  222. $resource = new Zend_Application_Resource_View();
  223. $bootstrap->registerPluginResource($resource);
  224. $bootstrap->unregisterPluginResource($resource);
  225. $this->assertFalse($bootstrap->hasPluginResource('view'));
  226. }
  227. /**
  228. * @expectedException Zend_Application_Bootstrap_Exception
  229. */
  230. public function testAttemptingToUnregisterPluginResourcesUsingInvalidResourceTypeShouldThrowException()
  231. {
  232. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  233. $bootstrap = new ZfAppBootstrap($this->application);
  234. $bootstrap->registerPluginResource('view');
  235. $bootstrap->unregisterPluginResource(array());
  236. }
  237. public function testShouldAllowUnregisteringPluginResourcesByName()
  238. {
  239. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  240. $bootstrap = new ZfAppBootstrap($this->application);
  241. $bootstrap->registerPluginResource('view');
  242. $bootstrap->unregisterPluginResource('view');
  243. $this->assertFalse($bootstrap->hasPluginResource('view'));
  244. }
  245. public function testRetrievingNonExistentPluginResourceShouldReturnNull()
  246. {
  247. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  248. $bootstrap = new ZfAppBootstrap($this->application);
  249. $this->assertNull($bootstrap->getPluginResource('view'));
  250. }
  251. public function testRetrievingPluginResourcesShouldRetrieveConcreteInstances()
  252. {
  253. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  254. $bootstrap = new ZfAppBootstrap($this->application);
  255. $bootstrap->registerPluginResource('view');
  256. $test = $bootstrap->getPluginResources();
  257. foreach ($test as $type => $resource) {
  258. $this->assertTrue($resource instanceof Zend_Application_Resource_Resource);
  259. }
  260. }
  261. public function testShouldAllowRetrievingOnlyPluginResourceNames()
  262. {
  263. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  264. $bootstrap = new ZfAppBootstrap($this->application);
  265. $bootstrap->registerPluginResource('view');
  266. $test = $bootstrap->getPluginResourceNames();
  267. $this->assertEquals(array('view'), $test);
  268. }
  269. public function testShouldAllowSettingAlternativePluginLoaderInstance()
  270. {
  271. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  272. $bootstrap = new ZfAppBootstrap($this->application);
  273. $loader = new Zend_Loader_PluginLoader();
  274. $bootstrap->setPluginLoader($loader);
  275. $this->assertSame($loader, $bootstrap->getPluginLoader());
  276. }
  277. public function testDefaultPluginLoaderShouldRegisterPrefixPathForResources()
  278. {
  279. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  280. $bootstrap = new ZfAppBootstrap($this->application);
  281. $loader = $bootstrap->getPluginLoader();
  282. $paths = $loader->getPaths('Zend_Application_Resource');
  283. $this->assertFalse(empty($paths));
  284. }
  285. public function testEnvironmentShouldMatchApplicationEnvironment()
  286. {
  287. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  288. $bootstrap = new ZfAppBootstrap($this->application);
  289. $this->assertSame($this->application->getEnvironment(), $bootstrap->getEnvironment());
  290. }
  291. public function testBootstrappingShouldOnlyExecuteEachInitializerOnce()
  292. {
  293. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  294. $bootstrap = new ZfAppBootstrap($this->application);
  295. $bootstrap->bootstrap('foo');
  296. $bootstrap->bootstrap('foo');
  297. $this->assertEquals(1, $bootstrap->fooExecuted);
  298. }
  299. public function testBootstrappingShouldFavorInternalResourcesOverPlugins()
  300. {
  301. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  302. $bootstrap = new ZfAppBootstrap($this->application);
  303. $bootstrap->getPluginLoader()->addPrefixPath('Zend_Application_BootstrapTest_Resource', dirname(__FILE__) . '/../_files/resources');
  304. $bootstrap->bootstrap('foo');
  305. $this->assertFalse($bootstrap->executedFooResource);
  306. }
  307. public function testBootstrappingShouldAllowPassingAnArrayOfResources()
  308. {
  309. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  310. $bootstrap = new ZfAppBootstrap($this->application);
  311. $bootstrap->bootstrap(array('foo', 'bar'));
  312. $this->assertEquals(1, $bootstrap->fooExecuted);
  313. $this->assertEquals(1, $bootstrap->barExecuted);
  314. }
  315. public function testPassingNoValuesToBootstrapExecutesAllResources()
  316. {
  317. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  318. $bootstrap = new ZfAppBootstrap($this->application);
  319. $bootstrap->getPluginLoader()->addPrefixPath('Zend_Application_BootstrapTest_Resource', dirname(__FILE__) . '/../_files/resources');
  320. $bootstrap->registerPluginResource('foobar');
  321. $bootstrap->bootstrap();
  322. $this->assertEquals(1, $bootstrap->fooExecuted);
  323. $this->assertEquals(1, $bootstrap->barExecuted);
  324. $this->assertTrue($bootstrap->executedFoobarResource);
  325. }
  326. /**
  327. * @expectedException Zend_Application_Bootstrap_Exception
  328. */
  329. public function testPassingInvalidResourceArgumentToBootstrapShouldThrowException()
  330. {
  331. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  332. $bootstrap = new ZfAppBootstrap($this->application);
  333. $bootstrap->bootstrap(new stdClass);
  334. }
  335. /**
  336. * @expectedException Zend_Application_Bootstrap_Exception
  337. */
  338. public function testPassingUnknownResourceToBootstrapShouldThrowException()
  339. {
  340. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  341. $bootstrap = new ZfAppBootstrap($this->application);
  342. $bootstrap->bootstrap('bazbat');
  343. }
  344. public function testCallShouldOverloadToBootstrap()
  345. {
  346. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  347. $bootstrap = new ZfAppBootstrap($this->application);
  348. $bootstrap->bootstrapFoo();
  349. $this->assertEquals(1, $bootstrap->fooExecuted);
  350. }
  351. /**
  352. * @expectedException Zend_Application_Bootstrap_Exception
  353. */
  354. public function testCallShouldThrowExceptionForInvalidMethodCall()
  355. {
  356. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  357. $bootstrap = new ZfAppBootstrap($this->application);
  358. $bootstrap->initFoo();
  359. }
  360. /**
  361. * @expectedException Zend_Application_Bootstrap_Exception
  362. */
  363. public function testDependencyTrackingShouldDetectCircularDependencies()
  364. {
  365. require_once dirname(__FILE__) . '/../_files/BootstrapBaseCircularDependency.php';
  366. $bootstrap = new BootstrapBaseCircularDependency($this->application);
  367. $bootstrap->bootstrap();
  368. }
  369. public function testContainerShouldBeRegistryInstanceByDefault()
  370. {
  371. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  372. $bootstrap = new ZfAppBootstrap($this->application);
  373. $container = $bootstrap->getContainer();
  374. $this->assertTrue($container instanceof Zend_Registry);
  375. }
  376. public function testContainerShouldAggregateReturnValuesFromClassResources()
  377. {
  378. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  379. $bootstrap = new ZfAppBootstrap($this->application);
  380. $bootstrap->bootstrap('barbaz');
  381. $container = $bootstrap->getContainer();
  382. $this->assertEquals('Baz', $container->barbaz->baz);
  383. }
  384. public function testContainerShouldAggregateReturnValuesFromPluginResources()
  385. {
  386. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  387. $bootstrap = new ZfAppBootstrap($this->application);
  388. $bootstrap->getPluginLoader()->addPrefixPath('Zend_Application_BootstrapTest_Resource', dirname(__FILE__) . '/../_files/resources');
  389. $bootstrap->registerPluginResource('baz');
  390. $bootstrap->bootstrap('baz');
  391. $container = $bootstrap->getContainer();
  392. $this->assertEquals('Baz', $container->baz->baz);
  393. }
  394. public function testClassResourcesShouldBeAvailableFollowingBootstrapping()
  395. {
  396. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  397. $bootstrap = new ZfAppBootstrap($this->application);
  398. $bootstrap->bootstrap('barbaz');
  399. $this->assertTrue($bootstrap->hasResource('barbaz'));
  400. $resource = $bootstrap->getResource('barbaz');
  401. $this->assertEquals('Baz', $resource->baz);
  402. }
  403. public function testPluginResourcesShouldBeAvailableFollowingBootstrapping()
  404. {
  405. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  406. $bootstrap = new ZfAppBootstrap($this->application);
  407. $bootstrap->getPluginLoader()->addPrefixPath('Zend_Application_BootstrapTest_Resource', dirname(__FILE__) . '/../_files/resources');
  408. $bootstrap->registerPluginResource('baz');
  409. $bootstrap->bootstrap('baz');
  410. $this->assertTrue($bootstrap->hasResource('baz'));
  411. $resource = $bootstrap->getResource('baz');
  412. $this->assertEquals('Baz', $resource->baz);
  413. }
  414. }
  415. if (PHPUnit_MAIN_METHOD == 'Zend_Application_Bootstrap_BootstrapAbstractTest::main') {
  416. Zend_Application_Bootstrap_BootstrapAbstractTest::main();
  417. }