ApplicationTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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-2010 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_ApplicationTest::main');
  24. }
  25. /**
  26. * Test helper
  27. */
  28. require_once dirname(__FILE__) . '/../../TestHelper.php';
  29. /** Zend_Loader_Autoloader */
  30. require_once 'Zend/Loader/Autoloader.php';
  31. /** Zend_Application */
  32. require_once 'Zend/Application.php';
  33. /**
  34. * @category Zend
  35. * @package Zend_Application
  36. * @subpackage UnitTests
  37. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. * @group Zend_Application
  40. */
  41. class Zend_Application_ApplicationTest extends PHPUnit_Framework_TestCase
  42. {
  43. public static function main()
  44. {
  45. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  46. $result = PHPUnit_TextUI_TestRunner::run($suite);
  47. }
  48. public function setUp()
  49. {
  50. // Store original autoloaders
  51. $this->loaders = spl_autoload_functions();
  52. if (!is_array($this->loaders)) {
  53. // spl_autoload_functions does not return empty array when no
  54. // autoloaders registered...
  55. $this->loaders = array();
  56. }
  57. // Store original include_path
  58. $this->includePath = get_include_path();
  59. Zend_Loader_Autoloader::resetInstance();
  60. $this->autoloader = Zend_Loader_Autoloader::getInstance();
  61. $this->application = new Zend_Application('testing');
  62. $this->iniOptions = array();
  63. }
  64. public function tearDown()
  65. {
  66. // Restore original autoloaders
  67. $loaders = spl_autoload_functions();
  68. foreach ($loaders as $loader) {
  69. spl_autoload_unregister($loader);
  70. }
  71. foreach ($this->loaders as $loader) {
  72. spl_autoload_register($loader);
  73. }
  74. foreach ($this->iniOptions as $key) {
  75. ini_restore($key);
  76. }
  77. // Reset autoloader instance so it doesn't affect other tests
  78. Zend_Loader_Autoloader::resetInstance();
  79. }
  80. public function testConstructorSetsEnvironment()
  81. {
  82. $this->assertEquals('testing', $this->application->getEnvironment());
  83. }
  84. public function testConstructorInstantiatesAutoloader()
  85. {
  86. $autoloader = $this->application->getAutoloader();
  87. $this->assertTrue($autoloader instanceof Zend_Loader_Autoloader);
  88. }
  89. public function testConstructorShouldSetOptionsWhenProvided()
  90. {
  91. $options = array(
  92. 'foo' => 'bar',
  93. 'bar' => 'baz',
  94. );
  95. $application = new Zend_Application('testing', $options);
  96. $this->assertEquals($options, $application->getOptions());
  97. }
  98. public function testHasOptionShouldReturnFalseWhenOptionNotPresent()
  99. {
  100. $this->assertFalse($this->application->hasOption('foo'));
  101. }
  102. public function testHasOptionShouldReturnTrueWhenOptionPresent()
  103. {
  104. $options = array(
  105. 'foo' => 'bar',
  106. 'bar' => 'baz',
  107. );
  108. $application = new Zend_Application('testing', $options);
  109. $this->assertTrue($application->hasOption('foo'));
  110. }
  111. public function testGetOptionShouldReturnNullWhenOptionNotPresent()
  112. {
  113. $this->assertNull($this->application->getOption('foo'));
  114. }
  115. public function testGetOptionShouldReturnOptionValue()
  116. {
  117. $options = array(
  118. 'foo' => 'bar',
  119. 'bar' => 'baz',
  120. );
  121. $application = new Zend_Application('testing', $options);
  122. $this->assertEquals($options['foo'], $application->getOption('foo'));
  123. }
  124. public function testPassingAutoloaderNamespaceOptionsShouldProxyToAutoloader()
  125. {
  126. $autoloader = $this->autoloader;
  127. $this->application->setOptions(array(
  128. 'autoloaderNamespaces' => array(
  129. 'Foo',
  130. ),
  131. ));
  132. $namespaces = $this->autoloader->getRegisteredNamespaces();
  133. $this->assertContains('Foo', $namespaces);
  134. }
  135. public function testPassingIncludePathOptionShouldModifyIncludePath()
  136. {
  137. $expected = dirname(__FILE__) . '/_files';
  138. $this->application->setOptions(array(
  139. 'includePaths' => array(
  140. $expected,
  141. ),
  142. ));
  143. $test = get_include_path();
  144. $this->assertContains($expected, $test);
  145. }
  146. public function testPassingPhpSettingsSetsIniValues()
  147. {
  148. $this->iniOptions[] = 'y2k_compliance';
  149. $orig = ini_get('y2k_compliance');
  150. $expected = $orig ? 0 : 1;
  151. $this->application->setOptions(array(
  152. 'phpSettings' => array(
  153. 'y2k_compliance' => $expected,
  154. ),
  155. ));
  156. $this->assertEquals($expected, ini_get('y2k_compliance'));
  157. }
  158. public function testPassingPhpSettingsAsArrayShouldConstructDotValuesAndSetRelatedIniValues()
  159. {
  160. $this->iniOptions[] = 'date.default_latitude';
  161. $orig = ini_get('date.default_latitude');
  162. $expected = '1.234';
  163. $this->application->setOptions(array(
  164. 'phpSettings' => array(
  165. 'date' => array(
  166. 'default_latitude' => $expected,
  167. ),
  168. ),
  169. ));
  170. $this->assertEquals($expected, ini_get('date.default_latitude'));
  171. }
  172. public function testShouldUseBaseBootstrapClassByDefaultIfNoBootstrapRegistered()
  173. {
  174. $bootstrap = $this->application->getBootstrap();
  175. $this->assertTrue($bootstrap instanceof Zend_Application_Bootstrap_Bootstrap);
  176. }
  177. public function testPassingStringBootstrapPathOptionShouldRegisterBootstrap()
  178. {
  179. $this->application->setOptions(array(
  180. 'bootstrap' => dirname(__FILE__) . '/_files/modules/default/Bootstrap.php',
  181. ));
  182. $bootstrap = $this->application->getBootstrap();
  183. $this->assertTrue($bootstrap instanceof Bootstrap);
  184. }
  185. public function testPassingArrayBootstrapOptionShouldRegisterBootstrapBasedOnPathOption()
  186. {
  187. $this->application->setOptions(array(
  188. 'bootstrap' => array(
  189. 'path' => dirname(__FILE__) . '/_files/modules/default/Bootstrap.php',
  190. ),
  191. ));
  192. $bootstrap = $this->application->getBootstrap();
  193. $this->assertTrue($bootstrap instanceof Bootstrap);
  194. }
  195. public function testPassingArrayBootstrapOptionShouldRegisterBootstrapBasedOnPathAndClassOption()
  196. {
  197. $this->application->setOptions(array(
  198. 'bootstrap' => array(
  199. 'path' => dirname(__FILE__) . '/_files/ZfAppBootstrap.php',
  200. 'class' => 'ZfAppBootstrap',
  201. ),
  202. ));
  203. $bootstrap = $this->application->getBootstrap();
  204. $this->assertTrue($bootstrap instanceof ZfAppBootstrap);
  205. }
  206. /**
  207. * @expectedException Zend_Application_Exception
  208. */
  209. public function testPassingArrayBootstrapWithoutPathOptionShouldRaiseException()
  210. {
  211. $this->application->setOptions(array(
  212. 'bootstrap' => array(
  213. 'class' => 'ZfAppBootstrap',
  214. ),
  215. ));
  216. $bootstrap = $this->application->getBootstrap();
  217. }
  218. /**
  219. * @expectedException Zend_Application_Exception
  220. */
  221. public function testPassingInvalidBootstrapOptionShouldRaiseException()
  222. {
  223. $this->application->setOptions(array(
  224. 'bootstrap' => new stdClass(),
  225. ));
  226. $bootstrap = $this->application->getBootstrap();
  227. }
  228. /**
  229. * @expectedException Zend_Application_Exception
  230. */
  231. public function testPassingInvalidOptionsArgumentToConstructorShouldRaiseException()
  232. {
  233. $application = new Zend_Application('testing', new stdClass());
  234. }
  235. public function testPassingStringIniConfigPathOptionToConstructorShouldLoadOptions()
  236. {
  237. $application = new Zend_Application('testing', dirname(__FILE__) . '/_files/appconfig.ini');
  238. $this->assertTrue($application->hasOption('foo'));
  239. }
  240. public function testPassingStringXmlConfigPathOptionToConstructorShouldLoadOptions()
  241. {
  242. $application = new Zend_Application('testing', dirname(__FILE__) . '/_files/appconfig.xml');
  243. $this->assertTrue($application->hasOption('foo'));
  244. }
  245. public function testPassingStringPhpConfigPathOptionToConstructorShouldLoadOptions()
  246. {
  247. $application = new Zend_Application('testing', dirname(__FILE__) . '/_files/appconfig.php');
  248. $this->assertTrue($application->hasOption('foo'));
  249. }
  250. public function testPassingStringIncConfigPathOptionToConstructorShouldLoadOptions()
  251. {
  252. $application = new Zend_Application('testing', dirname(__FILE__) . '/_files/appconfig.inc');
  253. $this->assertTrue($application->hasOption('foo'));
  254. }
  255. /**
  256. * @group ZF-10568
  257. */
  258. public function testPassingStringYamlConfigPathOptionToConstructorShouldLoadOptions()
  259. {
  260. $application = new Zend_Application('testing', dirname(__FILE__) . '/_files/appconfig.yaml');
  261. $this->assertTrue($application->hasOption('foo'));
  262. }
  263. /**
  264. * @group ZF-10568
  265. */
  266. public function testPassingStringJsonConfigPathOptionToConstructorShouldLoadOptions()
  267. {
  268. $application = new Zend_Application('testing', dirname(__FILE__) . '/_files/appconfig.json');
  269. $this->assertTrue($application->hasOption('foo'));
  270. }
  271. public function testPassingArrayOptionsWithConfigKeyShouldLoadOptions()
  272. {
  273. $application = new Zend_Application('testing', array('bar' => 'baz', 'config' => dirname(__FILE__) . '/_files/appconfig.inc'));
  274. $this->assertTrue($application->hasOption('foo'));
  275. $this->assertTrue($application->hasOption('bar'));
  276. }
  277. /**
  278. * This was changed to have the passed in array always overwrite the config file.
  279. * @group ZF-6811
  280. */
  281. public function testPassingArrayOptionsWithConfigKeyShouldLoadOptionsAndNotOverride()
  282. {
  283. $application = new Zend_Application('testing', array('foo' => 'baz', 'config' => dirname(__FILE__) . '/_files/appconfig.inc'));
  284. $this->assertNotEquals('bar', $application->getOption('foo'));
  285. }
  286. /**
  287. * @expectedException Zend_Application_Exception
  288. */
  289. public function testPassingInvalidStringOptionToConstructorShouldRaiseException()
  290. {
  291. $application = new Zend_Application('testing', dirname(__FILE__) . '/_files/appconfig');
  292. }
  293. public function testPassingZendConfigToConstructorShouldLoadOptions()
  294. {
  295. $config = new Zend_Config_Ini(dirname(__FILE__) . '/_files/appconfig.ini', 'testing');
  296. $application = new Zend_Application('testing', $config);
  297. $this->assertTrue($application->hasOption('foo'));
  298. }
  299. public function testPassingArrayOptionsToConstructorShouldLoadOptions()
  300. {
  301. $config = new Zend_Config_Ini(dirname(__FILE__) . '/_files/appconfig.ini', 'testing');
  302. $application = new Zend_Application('testing', $config->toArray());
  303. $this->assertTrue($application->hasOption('foo'));
  304. }
  305. public function testBootstrapImplementsFluentInterface()
  306. {
  307. $application = $this->application->bootstrap();
  308. $this->assertSame($application, $this->application);
  309. }
  310. /**
  311. * @expectedException Zend_Application_Exception
  312. */
  313. public function testApplicationShouldRaiseExceptionIfBootstrapFileDoesNotContainBootstrapClass()
  314. {
  315. $this->application->setOptions(array(
  316. 'bootstrap' => array(
  317. 'path' => dirname(__FILE__) . '/_files/ZfAppNoBootstrap.php',
  318. 'class' => 'ZfAppNoBootstrap',
  319. ),
  320. ));
  321. $bootstrap = $this->application->getBootstrap();
  322. }
  323. /**
  324. * @expectedException Zend_Application_Exception
  325. */
  326. public function testApplicationShouldRaiseExceptionWhenBootstrapClassNotOfCorrectType()
  327. {
  328. $this->application->setOptions(array(
  329. 'bootstrap' => array(
  330. 'path' => dirname(__FILE__) . '/_files/ZfAppBadBootstrap.php',
  331. 'class' => 'ZfAppBadBootstrap',
  332. ),
  333. ));
  334. $bootstrap = $this->application->getBootstrap();
  335. }
  336. public function testOptionsShouldRetainOriginalCase()
  337. {
  338. require_once dirname(__FILE__) . '/_files/ZfModuleBootstrap.php';
  339. $options = array(
  340. 'pluginPaths' => array(
  341. 'Zend_Application_Test_Path' => dirname(__FILE__),
  342. ),
  343. 'Resources' => array(
  344. 'modules' => array(),
  345. 'FrontController' => array(
  346. 'baseUrl' => '/foo',
  347. 'moduleDirectory' => dirname(__FILE__) . '/_files/modules',
  348. ),
  349. ),
  350. 'Bootstrap' => array(
  351. 'path' => dirname(__FILE__) . '/_files/ZfAppBootstrap.php',
  352. 'class' => 'ZfAppBootstrap',
  353. ),
  354. );
  355. $this->application->setOptions($options);
  356. $setOptions = $this->application->getOptions();
  357. $this->assertSame(array_keys($options), array_keys($setOptions));
  358. }
  359. /**
  360. * @group ZF-6679
  361. */
  362. public function testSetOptionsShouldProperlyMergeTwoConfigFileOptions()
  363. {
  364. $application = new Zend_Application(
  365. 'production', dirname(__FILE__) .
  366. '/_files/zf-6679-1.inc'
  367. );
  368. $options = $application->getOptions();
  369. $this->assertEquals(array('includePaths', 'config'), array_keys($options));
  370. }
  371. public function testPassingZfVersionAutoloaderInformationConfiguresAutoloader()
  372. {
  373. if (!constant('TESTS_ZEND_LOADER_AUTOLOADER_MULTIVERSION_ENABLED')) {
  374. $this->markTestSkipped();
  375. }
  376. if (!constant('TESTS_ZEND_LOADER_AUTOLOADER_MULTIVERSION_LATEST')) {
  377. $this->markTestSkipped();
  378. }
  379. $path = constant('TESTS_ZEND_LOADER_AUTOLOADER_MULTIVERSION_PATH');
  380. $latest = constant('TESTS_ZEND_LOADER_AUTOLOADER_MULTIVERSION_LATEST');
  381. $application = new Zend_Application('production', array(
  382. 'autoloaderZfPath' => $path,
  383. 'autoloaderZfVersion' => 'latest',
  384. ));
  385. $autoloader = $application->getAutoloader();
  386. $actual = $autoloader->getZfPath();
  387. $this->assertContains($latest, $actual);
  388. }
  389. /**
  390. * @group ZF-7742
  391. */
  392. public function testHasOptionShouldTreatOptionKeysAsCaseInsensitive()
  393. {
  394. $application = new Zend_Application('production', array(
  395. 'fooBar' => 'baz',
  396. ));
  397. $this->assertTrue($application->hasOption('FooBar'));
  398. }
  399. /**
  400. * @group ZF-7742
  401. */
  402. public function testGetOptionShouldTreatOptionKeysAsCaseInsensitive()
  403. {
  404. $application = new Zend_Application('production', array(
  405. 'fooBar' => 'baz',
  406. ));
  407. $this->assertEquals('baz', $application->getOption('FooBar'));
  408. }
  409. /**
  410. * @group ZF-6618
  411. */
  412. public function testCanExecuteBoostrapResourceViaApplicationInstanceBootstrapMethod()
  413. {
  414. $application = new Zend_Application('testing', array(
  415. 'bootstrap' => array(
  416. 'path' => dirname(__FILE__) . '/_files/ZfAppBootstrap.php',
  417. 'class' => 'ZfAppBootstrap'
  418. )
  419. )
  420. );
  421. $application->bootstrap('foo');
  422. $this->assertEquals(1, $application->getBootstrap()->fooExecuted);
  423. $this->assertEquals(0, $application->getBootstrap()->barExecuted);
  424. }
  425. public function testOptionsCanHandleMuiltipleConigFiles()
  426. {
  427. $application = new Zend_Application('testing', array(
  428. 'config' => array(
  429. dirname(__FILE__) . '/_files/Zf-6719-1.ini',
  430. dirname(__FILE__) . '/_files/Zf-6719-2.ini'
  431. )
  432. )
  433. );
  434. $this->assertEquals('baz', $application->getOption('foo'));
  435. }
  436. }
  437. if (PHPUnit_MAIN_METHOD == 'Zend_Application_ApplicationTest::main') {
  438. Zend_Application_ApplicationTest::main();
  439. }