2
0

BootstrapAbstractTest.php 19 KB

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