2
0

BootstrapAbstractTest.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  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-2009 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. * Zend_Application_Resource_ResourceAbstract
  35. */
  36. require_once 'Zend/Application/Resource/ResourceAbstract.php';
  37. /**
  38. * Zend_Application_Bootstrap_Bootstrapper
  39. */
  40. require_once 'Zend/Application/Bootstrap/Bootstrapper.php';
  41. /**
  42. * Zend_Application_Bootstrap_ResourceBootstrapper
  43. */
  44. require_once 'Zend/Application/Bootstrap/ResourceBootstrapper.php';
  45. /**
  46. * Zend_Application_Bootstrap_BootstrapAbstract
  47. */
  48. require_once 'Zend/Application/Bootstrap/BootstrapAbstract.php';
  49. /**
  50. * Zend_Application_Bootstrap_Bootstrap
  51. */
  52. require_once 'Zend/Application/Bootstrap/Bootstrap.php';
  53. /**
  54. * @category Zend
  55. * @package Zend_Application
  56. * @subpackage UnitTests
  57. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  58. * @license http://framework.zend.com/license/new-bsd New BSD License
  59. * @group Zend_Application
  60. */
  61. class Zend_Application_Bootstrap_BootstrapAbstractTest extends PHPUnit_Framework_TestCase
  62. {
  63. public static function main()
  64. {
  65. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  66. $result = PHPUnit_TextUI_TestRunner::run($suite);
  67. }
  68. public function setUp()
  69. {
  70. // Store original autoloaders
  71. $this->loaders = spl_autoload_functions();
  72. if (!is_array($this->loaders)) {
  73. // spl_autoload_functions does not return empty array when no
  74. // autoloaders registered...
  75. $this->loaders = array();
  76. }
  77. Zend_Loader_Autoloader::resetInstance();
  78. $this->autoloader = Zend_Loader_Autoloader::getInstance();
  79. $this->application = new Zend_Application('testing');
  80. $this->error = false;
  81. }
  82. public function tearDown()
  83. {
  84. // Restore original autoloaders
  85. $loaders = spl_autoload_functions();
  86. foreach ($loaders as $loader) {
  87. spl_autoload_unregister($loader);
  88. }
  89. foreach ($this->loaders as $loader) {
  90. spl_autoload_register($loader);
  91. }
  92. // Reset autoloader instance so it doesn't affect other tests
  93. Zend_Loader_Autoloader::resetInstance();
  94. }
  95. public function handleError($errno, $errstr)
  96. {
  97. $this->error = $errstr;
  98. return true;
  99. }
  100. public function testConstructorShouldPopulateApplication()
  101. {
  102. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  103. $bootstrap = new ZfAppBootstrap($this->application);
  104. $this->assertSame($this->application, $bootstrap->getApplication());
  105. }
  106. public function testConstructorShouldPopulateOptionsFromApplicationObject()
  107. {
  108. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  109. $options = array(
  110. 'foo' => 'bar',
  111. 'bar' => 'baz',
  112. );
  113. $this->application->setOptions($options);
  114. $bootstrap = new ZfAppBootstrap($this->application);
  115. $this->assertSame($options, $bootstrap->getOptions());
  116. }
  117. public function testConstructorShouldAllowPassingAnotherBootstrapObject()
  118. {
  119. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  120. $bootstrap1 = new ZfAppBootstrap($this->application);
  121. $bootstrap2 = new ZfAppBootstrap($bootstrap1);
  122. $this->assertSame($bootstrap1, $bootstrap2->getApplication());
  123. }
  124. /**
  125. * @expectedException Zend_Application_Bootstrap_Exception
  126. */
  127. public function testConstructorShouldRaiseExceptionForInvalidApplicationArgument()
  128. {
  129. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  130. $bootstrap = new ZfAppBootstrap(new stdClass);
  131. }
  132. public function testSettingOptionsShouldProxyToInternalSetters()
  133. {
  134. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  135. $options = array(
  136. 'arbitrary' => 'foo',
  137. );
  138. $bootstrap = new ZfAppBootstrap($this->application);
  139. $bootstrap->setOptions($options);
  140. $this->assertEquals('foo', $bootstrap->getArbitrary());
  141. }
  142. /**
  143. * @group ZF-6459
  144. */
  145. public function testCallingSetOptionsMultipleTimesShouldMergeOptionsRecursively()
  146. {
  147. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  148. $options = array(
  149. 'deep' => array(
  150. 'foo' => 'bar',
  151. 'bar' => 'baz',
  152. ),
  153. );
  154. $bootstrap = new ZfAppBootstrap($this->application);
  155. $bootstrap->setOptions($options);
  156. $options2 = array(
  157. 'deep' => array(
  158. 'bar' => 'bat',
  159. 'baz' => 'foo',
  160. ),
  161. );
  162. $bootstrap->setOptions($options2);
  163. $expected = $bootstrap->mergeOptions($options, $options2);
  164. $test = $bootstrap->getOptions();
  165. $this->assertEquals($expected, $test);
  166. }
  167. public function testPluginPathsOptionKeyShouldAddPrefixPathsToPluginLoader()
  168. {
  169. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  170. $bootstrap = new ZfAppBootstrap($this->application);
  171. $bootstrap->setOptions(array(
  172. 'pluginPaths' => array(
  173. 'Foo' => 'foo/bar/path/',
  174. ),
  175. ));
  176. $loader = $bootstrap->getPluginLoader();
  177. $paths = $loader->getPaths('Foo');
  178. $this->assertTrue(is_array($paths));
  179. }
  180. public function testResourcesOptionKeyShouldRegisterBootstrapPluginResources()
  181. {
  182. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  183. $bootstrap = new ZfAppBootstrap($this->application);
  184. $bootstrap->setOptions(array(
  185. 'resources' => array(
  186. 'view' => array(
  187. 'basePath' => dirname(__FILE__) . '/../_files/views/scripts',
  188. ),
  189. ),
  190. ));
  191. $this->assertTrue($bootstrap->hasPluginResource('view'));
  192. }
  193. public function testHasOptionShouldReturnFalseWhenOptionUnavailable()
  194. {
  195. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  196. $bootstrap = new ZfAppBootstrap($this->application);
  197. $this->assertFalse($bootstrap->hasOption('foo'));
  198. }
  199. public function testHasOptionShouldReturnTrueWhenOptionPresent()
  200. {
  201. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  202. $bootstrap = new ZfAppBootstrap($this->application);
  203. $bootstrap->setOptions(array('foo' => 'bar'));
  204. $this->assertTrue($bootstrap->hasOption('foo'));
  205. }
  206. public function testGetOptionShouldReturnNullWhenOptionUnavailable()
  207. {
  208. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  209. $bootstrap = new ZfAppBootstrap($this->application);
  210. $this->assertNull($bootstrap->getOption('foo'));
  211. }
  212. public function testGetOptionShouldReturnOptionValue()
  213. {
  214. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  215. $bootstrap = new ZfAppBootstrap($this->application);
  216. $bootstrap->setOptions(array('foo' => 'bar'));
  217. $this->assertEquals('bar', $bootstrap->getOption('foo'));
  218. }
  219. public function testInternalIntializersShouldBeRegisteredAsClassResources()
  220. {
  221. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  222. $bootstrap = new ZfAppBootstrap($this->application);
  223. $test = $bootstrap->getClassResources();
  224. $resources = array('foo' => '_initFoo', 'bar' => '_initBar', 'barbaz' => '_initBarbaz');
  225. $this->assertEquals($resources, $test);
  226. }
  227. public function testInternalInitializersShouldRegisterResourceNames()
  228. {
  229. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  230. $bootstrap = new ZfAppBootstrap($this->application);
  231. $test = $bootstrap->getClassResourceNames();
  232. $resources = array('foo', 'bar', 'barbaz');
  233. $this->assertEquals($resources, $test);
  234. }
  235. /**
  236. * @expectedException Zend_Application_Bootstrap_Exception
  237. */
  238. public function testRegisterPluginResourceShouldThrowExceptionForInvalidResourceType()
  239. {
  240. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  241. $bootstrap = new ZfAppBootstrap($this->application);
  242. $bootstrap->registerPluginResource(array());
  243. }
  244. public function testShouldAllowRegisteringConcretePluginResources()
  245. {
  246. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  247. $bootstrap = new ZfAppBootstrap($this->application);
  248. $resource = new Zend_Application_Resource_View();
  249. $bootstrap->registerPluginResource($resource);
  250. $test = $bootstrap->getPluginResource('view');
  251. $this->assertSame($resource, $test);
  252. }
  253. public function testRegisteringSecondPluginResourceOfSameTypeShouldOverwrite()
  254. {
  255. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  256. $bootstrap = new ZfAppBootstrap($this->application);
  257. $resource1 = new Zend_Application_Resource_View();
  258. $resource2 = new Zend_Application_Resource_View();
  259. $bootstrap->registerPluginResource($resource1)
  260. ->registerPluginResource($resource2);
  261. $test = $bootstrap->getPluginResource('view');
  262. $this->assertSame($resource2, $test);
  263. }
  264. public function testShouldAllowRegisteringPluginResourceUsingNameOnly()
  265. {
  266. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  267. $bootstrap = new ZfAppBootstrap($this->application);
  268. $bootstrap->registerPluginResource('view');
  269. $test = $bootstrap->getPluginResource('view');
  270. $this->assertEquals('Zend_Application_Resource_View', get_class($test));
  271. }
  272. public function testShouldAllowUnregisteringPluginResourcesUsingConcreteInstance()
  273. {
  274. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  275. $bootstrap = new ZfAppBootstrap($this->application);
  276. $resource = new Zend_Application_Resource_View();
  277. $bootstrap->registerPluginResource($resource);
  278. $bootstrap->unregisterPluginResource($resource);
  279. $this->assertFalse($bootstrap->hasPluginResource('view'));
  280. }
  281. /**
  282. * @expectedException Zend_Application_Bootstrap_Exception
  283. */
  284. public function testAttemptingToUnregisterPluginResourcesUsingInvalidResourceTypeShouldThrowException()
  285. {
  286. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  287. $bootstrap = new ZfAppBootstrap($this->application);
  288. $bootstrap->registerPluginResource('view');
  289. $bootstrap->unregisterPluginResource(array());
  290. }
  291. public function testShouldAllowUnregisteringPluginResourcesByName()
  292. {
  293. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  294. $bootstrap = new ZfAppBootstrap($this->application);
  295. $bootstrap->registerPluginResource('view');
  296. $bootstrap->unregisterPluginResource('view');
  297. $this->assertFalse($bootstrap->hasPluginResource('view'));
  298. }
  299. public function testRetrievingNonExistentPluginResourceShouldReturnNull()
  300. {
  301. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  302. $bootstrap = new ZfAppBootstrap($this->application);
  303. $this->assertNull($bootstrap->getPluginResource('view'));
  304. }
  305. public function testRetrievingPluginResourcesShouldRetrieveConcreteInstances()
  306. {
  307. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  308. $bootstrap = new ZfAppBootstrap($this->application);
  309. $bootstrap->registerPluginResource('view');
  310. $test = $bootstrap->getPluginResources();
  311. foreach ($test as $type => $resource) {
  312. $this->assertTrue($resource instanceof Zend_Application_Resource_Resource);
  313. }
  314. }
  315. public function testShouldAllowRetrievingOnlyPluginResourceNames()
  316. {
  317. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  318. $bootstrap = new ZfAppBootstrap($this->application);
  319. $bootstrap->registerPluginResource('view');
  320. $test = $bootstrap->getPluginResourceNames();
  321. $this->assertEquals(array('view'), $test);
  322. }
  323. public function testShouldAllowSettingAlternativePluginLoaderInstance()
  324. {
  325. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  326. $bootstrap = new ZfAppBootstrap($this->application);
  327. $loader = new Zend_Loader_PluginLoader();
  328. $bootstrap->setPluginLoader($loader);
  329. $this->assertSame($loader, $bootstrap->getPluginLoader());
  330. }
  331. public function testDefaultPluginLoaderShouldRegisterPrefixPathForResources()
  332. {
  333. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  334. $bootstrap = new ZfAppBootstrap($this->application);
  335. $loader = $bootstrap->getPluginLoader();
  336. $paths = $loader->getPaths('Zend_Application_Resource');
  337. $this->assertFalse(empty($paths));
  338. }
  339. public function testEnvironmentShouldMatchApplicationEnvironment()
  340. {
  341. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  342. $bootstrap = new ZfAppBootstrap($this->application);
  343. $this->assertSame($this->application->getEnvironment(), $bootstrap->getEnvironment());
  344. }
  345. public function testBootstrappingShouldOnlyExecuteEachInitializerOnce()
  346. {
  347. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  348. $bootstrap = new ZfAppBootstrap($this->application);
  349. $bootstrap->bootstrap('foo');
  350. $bootstrap->bootstrap('foo');
  351. $this->assertEquals(1, $bootstrap->fooExecuted);
  352. }
  353. /**
  354. * @group ZF-7955
  355. */
  356. public function testBootstrappingIsCaseInsensitive()
  357. {
  358. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  359. $bootstrap = new ZfAppBootstrap($this->application);
  360. $bootstrap->bootstrap('Foo');
  361. $bootstrap->bootstrap('Foo');
  362. $bootstrap->bootstrap('foo');
  363. $bootstrap->bootstrap('foo');
  364. $this->assertEquals(1, $bootstrap->fooExecuted);
  365. }
  366. public function testBootstrappingShouldFavorInternalResourcesOverPlugins()
  367. {
  368. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  369. $bootstrap = new ZfAppBootstrap($this->application);
  370. $bootstrap->getPluginLoader()->addPrefixPath('Zend_Application_BootstrapTest_Resource', dirname(__FILE__) . '/../_files/resources');
  371. $bootstrap->bootstrap('foo');
  372. $this->assertFalse($bootstrap->executedFooResource);
  373. }
  374. public function testBootstrappingShouldAllowPassingAnArrayOfResources()
  375. {
  376. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  377. $bootstrap = new ZfAppBootstrap($this->application);
  378. $bootstrap->bootstrap(array('foo', 'bar'));
  379. $this->assertEquals(1, $bootstrap->fooExecuted);
  380. $this->assertEquals(1, $bootstrap->barExecuted);
  381. }
  382. public function testPassingNoValuesToBootstrapExecutesAllResources()
  383. {
  384. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  385. $bootstrap = new ZfAppBootstrap($this->application);
  386. $bootstrap->getPluginLoader()->addPrefixPath('Zend_Application_BootstrapTest_Resource', dirname(__FILE__) . '/../_files/resources');
  387. $bootstrap->registerPluginResource('foobar');
  388. $bootstrap->bootstrap();
  389. $this->assertEquals(1, $bootstrap->fooExecuted);
  390. $this->assertEquals(1, $bootstrap->barExecuted);
  391. $this->assertTrue($bootstrap->executedFoobarResource);
  392. }
  393. /**
  394. * @expectedException Zend_Application_Bootstrap_Exception
  395. */
  396. public function testPassingInvalidResourceArgumentToBootstrapShouldThrowException()
  397. {
  398. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  399. $bootstrap = new ZfAppBootstrap($this->application);
  400. $bootstrap->bootstrap(new stdClass);
  401. }
  402. /**
  403. * @expectedException Zend_Application_Bootstrap_Exception
  404. */
  405. public function testPassingUnknownResourceToBootstrapShouldThrowException()
  406. {
  407. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  408. $bootstrap = new ZfAppBootstrap($this->application);
  409. $bootstrap->bootstrap('bazbat');
  410. }
  411. public function testCallShouldOverloadToBootstrap()
  412. {
  413. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  414. $bootstrap = new ZfAppBootstrap($this->application);
  415. $bootstrap->bootstrapFoo();
  416. $this->assertEquals(1, $bootstrap->fooExecuted);
  417. }
  418. /**
  419. * @expectedException Zend_Application_Bootstrap_Exception
  420. */
  421. public function testCallShouldThrowExceptionForInvalidMethodCall()
  422. {
  423. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  424. $bootstrap = new ZfAppBootstrap($this->application);
  425. $bootstrap->initFoo();
  426. }
  427. /**
  428. * @expectedException Zend_Application_Bootstrap_Exception
  429. */
  430. public function testDependencyTrackingShouldDetectCircularDependencies()
  431. {
  432. require_once dirname(__FILE__) . '/../_files/BootstrapBaseCircularDependency.php';
  433. $bootstrap = new BootstrapBaseCircularDependency($this->application);
  434. $bootstrap->bootstrap();
  435. }
  436. public function testContainerShouldBeRegistryInstanceByDefault()
  437. {
  438. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  439. $bootstrap = new ZfAppBootstrap($this->application);
  440. $container = $bootstrap->getContainer();
  441. $this->assertTrue($container instanceof Zend_Registry);
  442. }
  443. public function testContainerShouldAggregateReturnValuesFromClassResources()
  444. {
  445. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  446. $bootstrap = new ZfAppBootstrap($this->application);
  447. $bootstrap->bootstrap('barbaz');
  448. $container = $bootstrap->getContainer();
  449. $this->assertEquals('Baz', $container->barbaz->baz);
  450. }
  451. public function testContainerShouldAggregateReturnValuesFromPluginResources()
  452. {
  453. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  454. $bootstrap = new ZfAppBootstrap($this->application);
  455. $bootstrap->getPluginLoader()->addPrefixPath('Zend_Application_BootstrapTest_Resource', dirname(__FILE__) . '/../_files/resources');
  456. $bootstrap->registerPluginResource('baz');
  457. $bootstrap->bootstrap('baz');
  458. $container = $bootstrap->getContainer();
  459. $this->assertEquals('Baz', $container->baz->baz);
  460. }
  461. public function testClassResourcesShouldBeAvailableFollowingBootstrapping()
  462. {
  463. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  464. $bootstrap = new ZfAppBootstrap($this->application);
  465. $bootstrap->bootstrap('barbaz');
  466. $this->assertTrue($bootstrap->hasResource('barbaz'));
  467. $resource = $bootstrap->getResource('barbaz');
  468. $this->assertEquals('Baz', $resource->baz);
  469. }
  470. public function testPluginResourcesShouldBeAvailableFollowingBootstrapping()
  471. {
  472. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  473. $bootstrap = new ZfAppBootstrap($this->application);
  474. $bootstrap->getPluginLoader()->addPrefixPath('Zend_Application_BootstrapTest_Resource', dirname(__FILE__) . '/../_files/resources');
  475. $bootstrap->registerPluginResource('baz');
  476. $bootstrap->bootstrap('baz');
  477. $this->assertTrue($bootstrap->hasResource('baz'));
  478. $resource = $bootstrap->getResource('baz');
  479. $this->assertEquals('Baz', $resource->baz);
  480. }
  481. public function testMagicMethodsForPluginResources()
  482. {
  483. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  484. $bootstrap = new ZfAppBootstrap($this->application);
  485. $bootstrap->getPluginLoader()->addPrefixPath('Zend_Application_BootstrapTest_Resource', dirname(__FILE__) . '/../_files/resources');
  486. $bootstrap->registerPluginResource('baz');
  487. $bootstrap->bootstrap('baz');
  488. $this->assertTrue(isset($bootstrap->baz));
  489. $resource = $bootstrap->baz;
  490. $this->assertEquals('Baz', $resource->baz);
  491. }
  492. /**
  493. * @group ZF-6543
  494. */
  495. public function testPassingPluginResourcesByFullClassNameWithMatchingPluginPathShouldRegisterAsShortName()
  496. {
  497. $this->application->setOptions(array(
  498. 'resources' => array(
  499. 'Zend_Application_Bootstrap_BootstrapAbstractTest_View' => array(),
  500. ),
  501. 'pluginPaths' => array(
  502. 'Zend_Application_Bootstrap_BootstrapAbstractTest' => dirname(__FILE__),
  503. ),
  504. ));
  505. $bootstrap = new Zend_Application_Bootstrap_Bootstrap($this->application);
  506. $this->assertTrue($bootstrap->hasPluginResource('View'), var_export(array_keys($bootstrap->getPluginResources()), 1));
  507. }
  508. /**
  509. * @group ZF-6543
  510. */
  511. public function testPassingFullViewClassNameNotMatchingARegisteredPrefixShouldRegisterAsTheClassName()
  512. {
  513. $this->application->setOptions(array(
  514. 'resources' => array(
  515. 'Zend_Application_Bootstrap_BootstrapAbstractTest_View' => array(),
  516. ),
  517. ));
  518. $bootstrap = new Zend_Application_Bootstrap_Bootstrap($this->application);
  519. $this->assertTrue($bootstrap->hasPluginResource('Zend_Application_Bootstrap_BootstrapAbstractTest_View'));
  520. }
  521. /**
  522. * @group ZF-6543
  523. */
  524. public function testPassingFullViewClassNameNotMatchingARegisteredPrefixShouldReturnAppropriateResource()
  525. {
  526. $this->application->setOptions(array(
  527. 'resources' => array(
  528. 'Zend_Application_Bootstrap_BootstrapAbstractTest_View' => array(),
  529. ),
  530. ));
  531. $bootstrap = new Zend_Application_Bootstrap_Bootstrap($this->application);
  532. $bootstrap->bootstrap('Zend_Application_Bootstrap_BootstrapAbstractTest_View');
  533. $resource = $bootstrap->getResource('Zend_Application_Bootstrap_BootstrapAbstractTest_View');
  534. $this->assertTrue($resource instanceof Zend_Application_Bootstrap_BootstrapAbstractTest_View, var_export(array_keys($bootstrap->getPluginResources()), 1));
  535. }
  536. /**
  537. * @group ZF-6543
  538. */
  539. public function testCanMixAndMatchPluginResourcesAndFullClassNames()
  540. {
  541. $this->application->setOptions(array(
  542. 'resources' => array(
  543. 'Zend_Application_Bootstrap_BootstrapAbstractTest_View' => array(),
  544. 'view' => array(),
  545. ),
  546. ));
  547. $bootstrap = new Zend_Application_Bootstrap_Bootstrap($this->application);
  548. $bootstrap->bootstrap('Zend_Application_Bootstrap_BootstrapAbstractTest_View');
  549. $resource1 = $bootstrap->getResource('Zend_Application_Bootstrap_BootstrapAbstractTest_View');
  550. $bootstrap->bootstrap('view');
  551. $resource2 = $bootstrap->getResource('view');
  552. $this->assertNotSame($resource1, $resource2);
  553. $this->assertTrue($resource1 instanceof Zend_Application_Bootstrap_BootstrapAbstractTest_View, var_export(array_keys($bootstrap->getPluginResources()), 1));
  554. $this->assertTrue($resource2 instanceof Zend_View);
  555. }
  556. /**
  557. * @group ZF-6543
  558. */
  559. public function testPluginClassesDefiningExplicitTypeWillBeRegisteredWithThatValue()
  560. {
  561. $this->application->setOptions(array(
  562. 'resources' => array(
  563. 'Zend_Application_Bootstrap_BootstrapAbstractTest_Layout' => array(),
  564. 'layout' => array(),
  565. ),
  566. ));
  567. $bootstrap = new Zend_Application_Bootstrap_Bootstrap($this->application);
  568. $bootstrap->bootstrap('BootstrapAbstractTestLayout');
  569. $resource1 = $bootstrap->getResource('BootstrapAbstractTestLayout');
  570. $bootstrap->bootstrap('layout');
  571. $resource2 = $bootstrap->getResource('layout');
  572. $this->assertNotSame($resource1, $resource2);
  573. $this->assertTrue($resource1 instanceof Zend_Application_Bootstrap_BootstrapAbstractTest_Layout, var_export(array_keys($bootstrap->getPluginResources()), 1));
  574. $this->assertTrue($resource2 instanceof Zend_Layout);
  575. }
  576. /**
  577. * @group ZF-6471
  578. */
  579. public function testBootstrapShouldPassItselfToResourcePluginConstructor()
  580. {
  581. $this->application->setOptions(array(
  582. 'pluginPaths' => array(
  583. 'Zend_Application_Bootstrap_BootstrapAbstractTest' => dirname(__FILE__),
  584. ),
  585. 'resources' => array(
  586. 'Foo' => array(),
  587. ),
  588. ));
  589. $bootstrap = new Zend_Application_Bootstrap_Bootstrap($this->application);
  590. $resource = $bootstrap->getPluginResource('foo');
  591. $this->assertTrue($resource->bootstrapSetInConstructor, var_export(get_object_vars($resource), 1));
  592. }
  593. /**
  594. * @group ZF-6591
  595. */
  596. public function testRequestingPluginsByShortNameShouldNotRaiseFatalErrors()
  597. {
  598. $this->autoloader->setFallbackAutoloader(true)
  599. ->suppressNotFoundWarnings(false);
  600. $this->application->setOptions(array(
  601. 'resources' => array(
  602. 'FrontController' => array(),
  603. ),
  604. ));
  605. set_error_handler(array($this, 'handleError'));
  606. $bootstrap = new Zend_Application_Bootstrap_Bootstrap($this->application);
  607. $resource = $bootstrap->getPluginResource('FrontController');
  608. restore_error_handler();
  609. $this->assertTrue(false === $this->error, $this->error);
  610. }
  611. /**
  612. * @group ZF-7550
  613. */
  614. public function testRequestingPluginsByAutoloadableClassNameShouldNotRaiseFatalErrors()
  615. {
  616. // Using namesapce 'zabt' to prevent conflict with Zend namespace
  617. $rl = new Zend_Loader_Autoloader_Resource(array(
  618. 'namespace' => 'Zabt',
  619. 'basePath' => dirname(__FILE__) . '/../_files',
  620. ));
  621. $rl->addResourceType('resources', 'resources', 'Resource');
  622. $options = array(
  623. 'resources' => array(
  624. 'Zabt_Resource_Autoloaded' => array('bar' => 'baz')
  625. ),
  626. );
  627. $this->application->setOptions($options);
  628. $bootstrap = new Zend_Application_Bootstrap_Bootstrap($this->application);
  629. $bootstrap->bootstrap();
  630. }
  631. /**
  632. * @group ZF-7690
  633. */
  634. public function testCallingSetOptionsMultipleTimesShouldUpdateOptionKeys()
  635. {
  636. $this->application->setOptions(array(
  637. 'resources' => array(
  638. 'layout' => array(),
  639. ),
  640. ));
  641. $bootstrap = new Zend_Application_Bootstrap_BootstrapAbstractTest_OptionKeys($this->application);
  642. $bootstrap->setOptions(array(
  643. 'pluginPaths' => array(
  644. 'Foo' => dirname(__FILE__),
  645. ),
  646. ));
  647. $expected = array('resources', 'pluginpaths');
  648. $actual = $bootstrap->getOptionKeys();
  649. $this->assertEquals($expected, $actual);
  650. }
  651. }
  652. class Zend_Application_Bootstrap_BootstrapAbstractTest_View
  653. extends Zend_Application_Resource_ResourceAbstract
  654. {
  655. public function init()
  656. {
  657. return $this;
  658. }
  659. }
  660. class Zend_Application_Bootstrap_BootstrapAbstractTest_Layout
  661. extends Zend_Application_Resource_ResourceAbstract
  662. {
  663. public $_explicitType = 'BootstrapAbstractTestLayout';
  664. public $bootstrapSetInConstructor = false;
  665. public function __construct($options = null)
  666. {
  667. parent::__construct($options);
  668. if (null !== $this->getBootstrap()) {
  669. $this->bootstrapSetInConstructor = true;
  670. }
  671. }
  672. public function init()
  673. {
  674. return $this;
  675. }
  676. }
  677. class Zend_Application_Bootstrap_BootstrapAbstractTest_Foo
  678. extends Zend_Application_Resource_ResourceAbstract
  679. {
  680. public $bootstrapSetInConstructor = false;
  681. public function __construct($options = null)
  682. {
  683. parent::__construct($options);
  684. if (null !== $this->getBootstrap()) {
  685. $this->bootstrapSetInConstructor = true;
  686. }
  687. }
  688. public function init()
  689. {
  690. return $this;
  691. }
  692. }
  693. class Zend_Application_Bootstrap_BootstrapAbstractTest_OptionKeys
  694. extends Zend_Application_Bootstrap_Bootstrap
  695. {
  696. public function getOptionKeys()
  697. {
  698. return $this->_optionKeys;
  699. }
  700. }
  701. if (PHPUnit_MAIN_METHOD == 'Zend_Application_Bootstrap_BootstrapAbstractTest::main') {
  702. Zend_Application_Bootstrap_BootstrapAbstractTest::main();
  703. }