BootstrapAbstractTest.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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. public function testBootstrappingShouldFavorInternalResourcesOverPlugins()
  354. {
  355. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  356. $bootstrap = new ZfAppBootstrap($this->application);
  357. $bootstrap->getPluginLoader()->addPrefixPath('Zend_Application_BootstrapTest_Resource', dirname(__FILE__) . '/../_files/resources');
  358. $bootstrap->bootstrap('foo');
  359. $this->assertFalse($bootstrap->executedFooResource);
  360. }
  361. public function testBootstrappingShouldAllowPassingAnArrayOfResources()
  362. {
  363. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  364. $bootstrap = new ZfAppBootstrap($this->application);
  365. $bootstrap->bootstrap(array('foo', 'bar'));
  366. $this->assertEquals(1, $bootstrap->fooExecuted);
  367. $this->assertEquals(1, $bootstrap->barExecuted);
  368. }
  369. public function testPassingNoValuesToBootstrapExecutesAllResources()
  370. {
  371. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  372. $bootstrap = new ZfAppBootstrap($this->application);
  373. $bootstrap->getPluginLoader()->addPrefixPath('Zend_Application_BootstrapTest_Resource', dirname(__FILE__) . '/../_files/resources');
  374. $bootstrap->registerPluginResource('foobar');
  375. $bootstrap->bootstrap();
  376. $this->assertEquals(1, $bootstrap->fooExecuted);
  377. $this->assertEquals(1, $bootstrap->barExecuted);
  378. $this->assertTrue($bootstrap->executedFoobarResource);
  379. }
  380. /**
  381. * @expectedException Zend_Application_Bootstrap_Exception
  382. */
  383. public function testPassingInvalidResourceArgumentToBootstrapShouldThrowException()
  384. {
  385. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  386. $bootstrap = new ZfAppBootstrap($this->application);
  387. $bootstrap->bootstrap(new stdClass);
  388. }
  389. /**
  390. * @expectedException Zend_Application_Bootstrap_Exception
  391. */
  392. public function testPassingUnknownResourceToBootstrapShouldThrowException()
  393. {
  394. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  395. $bootstrap = new ZfAppBootstrap($this->application);
  396. $bootstrap->bootstrap('bazbat');
  397. }
  398. public function testCallShouldOverloadToBootstrap()
  399. {
  400. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  401. $bootstrap = new ZfAppBootstrap($this->application);
  402. $bootstrap->bootstrapFoo();
  403. $this->assertEquals(1, $bootstrap->fooExecuted);
  404. }
  405. /**
  406. * @expectedException Zend_Application_Bootstrap_Exception
  407. */
  408. public function testCallShouldThrowExceptionForInvalidMethodCall()
  409. {
  410. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  411. $bootstrap = new ZfAppBootstrap($this->application);
  412. $bootstrap->initFoo();
  413. }
  414. /**
  415. * @expectedException Zend_Application_Bootstrap_Exception
  416. */
  417. public function testDependencyTrackingShouldDetectCircularDependencies()
  418. {
  419. require_once dirname(__FILE__) . '/../_files/BootstrapBaseCircularDependency.php';
  420. $bootstrap = new BootstrapBaseCircularDependency($this->application);
  421. $bootstrap->bootstrap();
  422. }
  423. public function testContainerShouldBeRegistryInstanceByDefault()
  424. {
  425. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  426. $bootstrap = new ZfAppBootstrap($this->application);
  427. $container = $bootstrap->getContainer();
  428. $this->assertTrue($container instanceof Zend_Registry);
  429. }
  430. public function testContainerShouldAggregateReturnValuesFromClassResources()
  431. {
  432. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  433. $bootstrap = new ZfAppBootstrap($this->application);
  434. $bootstrap->bootstrap('barbaz');
  435. $container = $bootstrap->getContainer();
  436. $this->assertEquals('Baz', $container->barbaz->baz);
  437. }
  438. public function testContainerShouldAggregateReturnValuesFromPluginResources()
  439. {
  440. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  441. $bootstrap = new ZfAppBootstrap($this->application);
  442. $bootstrap->getPluginLoader()->addPrefixPath('Zend_Application_BootstrapTest_Resource', dirname(__FILE__) . '/../_files/resources');
  443. $bootstrap->registerPluginResource('baz');
  444. $bootstrap->bootstrap('baz');
  445. $container = $bootstrap->getContainer();
  446. $this->assertEquals('Baz', $container->baz->baz);
  447. }
  448. public function testClassResourcesShouldBeAvailableFollowingBootstrapping()
  449. {
  450. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  451. $bootstrap = new ZfAppBootstrap($this->application);
  452. $bootstrap->bootstrap('barbaz');
  453. $this->assertTrue($bootstrap->hasResource('barbaz'));
  454. $resource = $bootstrap->getResource('barbaz');
  455. $this->assertEquals('Baz', $resource->baz);
  456. }
  457. public function testPluginResourcesShouldBeAvailableFollowingBootstrapping()
  458. {
  459. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  460. $bootstrap = new ZfAppBootstrap($this->application);
  461. $bootstrap->getPluginLoader()->addPrefixPath('Zend_Application_BootstrapTest_Resource', dirname(__FILE__) . '/../_files/resources');
  462. $bootstrap->registerPluginResource('baz');
  463. $bootstrap->bootstrap('baz');
  464. $this->assertTrue($bootstrap->hasResource('baz'));
  465. $resource = $bootstrap->getResource('baz');
  466. $this->assertEquals('Baz', $resource->baz);
  467. }
  468. public function testMagicMethodsForPluginResources()
  469. {
  470. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  471. $bootstrap = new ZfAppBootstrap($this->application);
  472. $bootstrap->getPluginLoader()->addPrefixPath('Zend_Application_BootstrapTest_Resource', dirname(__FILE__) . '/../_files/resources');
  473. $bootstrap->registerPluginResource('baz');
  474. $bootstrap->bootstrap('baz');
  475. $this->assertTrue(isset($bootstrap->baz));
  476. $resource = $bootstrap->baz;
  477. $this->assertEquals('Baz', $resource->baz);
  478. }
  479. /**
  480. * @group ZF-6543
  481. */
  482. public function testPassingPluginResourcesByFullClassNameWithMatchingPluginPathShouldRegisterAsShortName()
  483. {
  484. $this->application->setOptions(array(
  485. 'resources' => array(
  486. 'Zend_Application_Bootstrap_BootstrapAbstractTest_View' => array(),
  487. ),
  488. 'pluginPaths' => array(
  489. 'Zend_Application_Bootstrap_BootstrapAbstractTest' => dirname(__FILE__),
  490. ),
  491. ));
  492. $bootstrap = new Zend_Application_Bootstrap_Bootstrap($this->application);
  493. $this->assertTrue($bootstrap->hasPluginResource('View'), var_export(array_keys($bootstrap->getPluginResources()), 1));
  494. }
  495. /**
  496. * @group ZF-6543
  497. */
  498. public function testPassingFullViewClassNameNotMatchingARegisteredPrefixShouldRegisterAsTheClassName()
  499. {
  500. $this->application->setOptions(array(
  501. 'resources' => array(
  502. 'Zend_Application_Bootstrap_BootstrapAbstractTest_View' => array(),
  503. ),
  504. ));
  505. $bootstrap = new Zend_Application_Bootstrap_Bootstrap($this->application);
  506. $this->assertTrue($bootstrap->hasPluginResource('Zend_Application_Bootstrap_BootstrapAbstractTest_View'));
  507. }
  508. /**
  509. * @group ZF-6543
  510. */
  511. public function testPassingFullViewClassNameNotMatchingARegisteredPrefixShouldReturnAppropriateResource()
  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. $bootstrap->bootstrap('Zend_Application_Bootstrap_BootstrapAbstractTest_View');
  520. $resource = $bootstrap->getResource('Zend_Application_Bootstrap_BootstrapAbstractTest_View');
  521. $this->assertTrue($resource instanceof Zend_Application_Bootstrap_BootstrapAbstractTest_View, var_export(array_keys($bootstrap->getPluginResources()), 1));
  522. }
  523. /**
  524. * @group ZF-6543
  525. */
  526. public function testCanMixAndMatchPluginResourcesAndFullClassNames()
  527. {
  528. $this->application->setOptions(array(
  529. 'resources' => array(
  530. 'Zend_Application_Bootstrap_BootstrapAbstractTest_View' => array(),
  531. 'view' => array(),
  532. ),
  533. ));
  534. $bootstrap = new Zend_Application_Bootstrap_Bootstrap($this->application);
  535. $bootstrap->bootstrap('Zend_Application_Bootstrap_BootstrapAbstractTest_View');
  536. $resource1 = $bootstrap->getResource('Zend_Application_Bootstrap_BootstrapAbstractTest_View');
  537. $bootstrap->bootstrap('view');
  538. $resource2 = $bootstrap->getResource('view');
  539. $this->assertNotSame($resource1, $resource2);
  540. $this->assertTrue($resource1 instanceof Zend_Application_Bootstrap_BootstrapAbstractTest_View, var_export(array_keys($bootstrap->getPluginResources()), 1));
  541. $this->assertTrue($resource2 instanceof Zend_View);
  542. }
  543. /**
  544. * @group ZF-6543
  545. */
  546. public function testPluginClassesDefiningExplicitTypeWillBeRegisteredWithThatValue()
  547. {
  548. $this->application->setOptions(array(
  549. 'resources' => array(
  550. 'Zend_Application_Bootstrap_BootstrapAbstractTest_Layout' => array(),
  551. 'layout' => array(),
  552. ),
  553. ));
  554. $bootstrap = new Zend_Application_Bootstrap_Bootstrap($this->application);
  555. $bootstrap->bootstrap('BootstrapAbstractTestLayout');
  556. $resource1 = $bootstrap->getResource('BootstrapAbstractTestLayout');
  557. $bootstrap->bootstrap('layout');
  558. $resource2 = $bootstrap->getResource('layout');
  559. $this->assertNotSame($resource1, $resource2);
  560. $this->assertTrue($resource1 instanceof Zend_Application_Bootstrap_BootstrapAbstractTest_Layout, var_export(array_keys($bootstrap->getPluginResources()), 1));
  561. $this->assertTrue($resource2 instanceof Zend_Layout);
  562. }
  563. /**
  564. * @group ZF-6471
  565. */
  566. public function testBootstrapShouldPassItselfToResourcePluginConstructor()
  567. {
  568. $this->application->setOptions(array(
  569. 'pluginPaths' => array(
  570. 'Zend_Application_Bootstrap_BootstrapAbstractTest' => dirname(__FILE__),
  571. ),
  572. 'resources' => array(
  573. 'Foo' => array(),
  574. ),
  575. ));
  576. $bootstrap = new Zend_Application_Bootstrap_Bootstrap($this->application);
  577. $resource = $bootstrap->getPluginResource('foo');
  578. $this->assertTrue($resource->bootstrapSetInConstructor, var_export(get_object_vars($resource), 1));
  579. }
  580. /**
  581. * @group ZF-6591
  582. */
  583. public function testRequestingPluginsByShortNameShouldNotRaiseFatalErrors()
  584. {
  585. $this->autoloader->setFallbackAutoloader(true)
  586. ->suppressNotFoundWarnings(false);
  587. $this->application->setOptions(array(
  588. 'resources' => array(
  589. 'FrontController' => array(),
  590. ),
  591. ));
  592. set_error_handler(array($this, 'handleError'));
  593. $bootstrap = new Zend_Application_Bootstrap_Bootstrap($this->application);
  594. $resource = $bootstrap->getPluginResource('FrontController');
  595. restore_error_handler();
  596. $this->assertTrue(false === $this->error, $this->error);
  597. }
  598. /**
  599. * @group ZF-7550
  600. */
  601. public function testRequestingPluginsByAutoloadableClassNameShouldNotRaiseFatalErrors()
  602. {
  603. // Using namesapce 'zabt' to prevent conflict with Zend namespace
  604. $rl = new Zend_Loader_Autoloader_Resource(array(
  605. 'namespace' => 'Zabt',
  606. 'basePath' => dirname(__FILE__) . '/../_files',
  607. ));
  608. $rl->addResourceType('resources', 'resources', 'Resource');
  609. $options = array(
  610. 'resources' => array(
  611. 'Zabt_Resource_Autoloaded' => array('bar' => 'baz')
  612. ),
  613. );
  614. $this->application->setOptions($options);
  615. $bootstrap = new Zend_Application_Bootstrap_Bootstrap($this->application);
  616. $bootstrap->bootstrap();
  617. }
  618. /**
  619. * @group ZF-7690
  620. */
  621. public function testCallingSetOptionsMultipleTimesShouldUpdateOptionKeys()
  622. {
  623. $this->application->setOptions(array(
  624. 'resources' => array(
  625. 'layout' => array(),
  626. ),
  627. ));
  628. $bootstrap = new Zend_Application_Bootstrap_BootstrapAbstractTest_OptionKeys($this->application);
  629. $bootstrap->setOptions(array(
  630. 'pluginPaths' => array(
  631. 'Foo' => dirname(__FILE__),
  632. ),
  633. ));
  634. $expected = array('resources', 'pluginpaths');
  635. $actual = $bootstrap->getOptionKeys();
  636. $this->assertEquals($expected, $actual);
  637. }
  638. }
  639. class Zend_Application_Bootstrap_BootstrapAbstractTest_View
  640. extends Zend_Application_Resource_ResourceAbstract
  641. {
  642. public function init()
  643. {
  644. return $this;
  645. }
  646. }
  647. class Zend_Application_Bootstrap_BootstrapAbstractTest_Layout
  648. extends Zend_Application_Resource_ResourceAbstract
  649. {
  650. public $_explicitType = 'BootstrapAbstractTestLayout';
  651. public $bootstrapSetInConstructor = false;
  652. public function __construct($options = null)
  653. {
  654. parent::__construct($options);
  655. if (null !== $this->getBootstrap()) {
  656. $this->bootstrapSetInConstructor = true;
  657. }
  658. }
  659. public function init()
  660. {
  661. return $this;
  662. }
  663. }
  664. class Zend_Application_Bootstrap_BootstrapAbstractTest_Foo
  665. extends Zend_Application_Resource_ResourceAbstract
  666. {
  667. public $bootstrapSetInConstructor = false;
  668. public function __construct($options = null)
  669. {
  670. parent::__construct($options);
  671. if (null !== $this->getBootstrap()) {
  672. $this->bootstrapSetInConstructor = true;
  673. }
  674. }
  675. public function init()
  676. {
  677. return $this;
  678. }
  679. }
  680. class Zend_Application_Bootstrap_BootstrapAbstractTest_OptionKeys
  681. extends Zend_Application_Bootstrap_Bootstrap
  682. {
  683. public function getOptionKeys()
  684. {
  685. return $this->_optionKeys;
  686. }
  687. }
  688. if (PHPUnit_MAIN_METHOD == 'Zend_Application_Bootstrap_BootstrapAbstractTest::main') {
  689. Zend_Application_Bootstrap_BootstrapAbstractTest::main();
  690. }