ApplicationTest.php 18 KB

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