BootstrapAbstractTest.php 26 KB

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