BootstrapAbstractTest.php 27 KB

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