2
0

BootstrapAbstractTest.php 26 KB

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