ApplicationTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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-2014 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-2014 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. public function testHasOptionShouldReturnFalseWhenOptionNotPresent()
  95. {
  96. $this->assertFalse($this->application->hasOption('foo'));
  97. }
  98. public function testHasOptionShouldReturnTrueWhenOptionPresent()
  99. {
  100. $options = array(
  101. 'foo' => 'bar',
  102. 'bar' => 'baz',
  103. );
  104. $application = new Zend_Application('testing', $options);
  105. $this->assertTrue($application->hasOption('foo'));
  106. }
  107. public function testGetOptionShouldReturnNullWhenOptionNotPresent()
  108. {
  109. $this->assertNull($this->application->getOption('foo'));
  110. }
  111. public function testGetOptionShouldReturnOptionValue()
  112. {
  113. $options = array(
  114. 'foo' => 'bar',
  115. 'bar' => 'baz',
  116. );
  117. $application = new Zend_Application('testing', $options);
  118. $this->assertEquals($options['foo'], $application->getOption('foo'));
  119. }
  120. public function testPassingAutoloaderNamespaceOptionsShouldProxyToAutoloader()
  121. {
  122. $autoloader = $this->autoloader;
  123. $this->application->setOptions(array(
  124. 'autoloaderNamespaces' => array(
  125. 'Foo',
  126. ),
  127. ));
  128. $namespaces = $this->autoloader->getRegisteredNamespaces();
  129. $this->assertContains('Foo', $namespaces);
  130. }
  131. public function testPassingIncludePathOptionShouldModifyIncludePath()
  132. {
  133. $expected = dirname(__FILE__) . '/_files';
  134. $this->application->setOptions(array(
  135. 'includePaths' => array(
  136. $expected,
  137. ),
  138. ));
  139. $test = get_include_path();
  140. $this->assertContains($expected, $test);
  141. }
  142. public function testPassingPhpSettingsSetsIniValues()
  143. {
  144. $this->iniOptions[] = 'html_errors';
  145. $orig = ini_get('html_errors');
  146. $expected = $orig ? 0 : 1;
  147. $this->application->setOptions(array(
  148. 'phpSettings' => array(
  149. 'html_errors' => $expected,
  150. ),
  151. ));
  152. $this->assertEquals($expected, ini_get('html_errors'));
  153. }
  154. public function testPassingPhpSettingsAsArrayShouldConstructDotValuesAndSetRelatedIniValues()
  155. {
  156. $this->iniOptions[] = 'date.default_latitude';
  157. $orig = ini_get('date.default_latitude');
  158. $expected = '1.234';
  159. $this->application->setOptions(array(
  160. 'phpSettings' => array(
  161. 'date' => array(
  162. 'default_latitude' => $expected,
  163. ),
  164. ),
  165. ));
  166. $this->assertEquals($expected, ini_get('date.default_latitude'));
  167. }
  168. public function testShouldUseBaseBootstrapClassByDefaultIfNoBootstrapRegistered()
  169. {
  170. $bootstrap = $this->application->getBootstrap();
  171. $this->assertTrue($bootstrap instanceof Zend_Application_Bootstrap_Bootstrap);
  172. }
  173. public function testPassingStringBootstrapPathOptionShouldRegisterBootstrap()
  174. {
  175. $this->application->setOptions(array(
  176. 'bootstrap' => dirname(__FILE__) . '/_files/modules/default/Bootstrap.php',
  177. ));
  178. $bootstrap = $this->application->getBootstrap();
  179. $this->assertTrue($bootstrap instanceof Bootstrap);
  180. }
  181. public function testPassingArrayBootstrapOptionShouldRegisterBootstrapBasedOnPathOption()
  182. {
  183. $this->application->setOptions(array(
  184. 'bootstrap' => array(
  185. 'path' => dirname(__FILE__) . '/_files/modules/default/Bootstrap.php',
  186. ),
  187. ));
  188. $bootstrap = $this->application->getBootstrap();
  189. $this->assertTrue($bootstrap instanceof Bootstrap);
  190. }
  191. public function testPassingArrayBootstrapOptionShouldRegisterBootstrapBasedOnPathAndClassOption()
  192. {
  193. $this->application->setOptions(array(
  194. 'bootstrap' => array(
  195. 'path' => dirname(__FILE__) . '/_files/ZfAppBootstrap.php',
  196. 'class' => 'ZfAppBootstrap',
  197. ),
  198. ));
  199. $bootstrap = $this->application->getBootstrap();
  200. $this->assertTrue($bootstrap instanceof ZfAppBootstrap);
  201. }
  202. /**
  203. * @expectedException Zend_Application_Exception
  204. */
  205. public function testPassingArrayBootstrapWithoutPathOptionShouldRaiseException()
  206. {
  207. $this->application->setOptions(array(
  208. 'bootstrap' => array(
  209. 'class' => 'ZfAppBootstrap',
  210. ),
  211. ));
  212. $bootstrap = $this->application->getBootstrap();
  213. }
  214. /**
  215. * @expectedException Zend_Application_Exception
  216. */
  217. public function testPassingInvalidBootstrapOptionShouldRaiseException()
  218. {
  219. $this->application->setOptions(array(
  220. 'bootstrap' => new stdClass(),
  221. ));
  222. $bootstrap = $this->application->getBootstrap();
  223. }
  224. /**
  225. * @expectedException Zend_Application_Exception
  226. */
  227. public function testPassingInvalidOptionsArgumentToConstructorShouldRaiseException()
  228. {
  229. $application = new Zend_Application('testing', new stdClass());
  230. }
  231. public function testPassingStringIniConfigPathOptionToConstructorShouldLoadOptions()
  232. {
  233. $application = new Zend_Application('testing', dirname(__FILE__) . '/_files/appconfig.ini');
  234. $this->assertTrue($application->hasOption('foo'));
  235. }
  236. public function testPassingStringXmlConfigPathOptionToConstructorShouldLoadOptions()
  237. {
  238. $application = new Zend_Application('testing', dirname(__FILE__) . '/_files/appconfig.xml');
  239. $this->assertTrue($application->hasOption('foo'));
  240. }
  241. public function testPassingStringPhpConfigPathOptionToConstructorShouldLoadOptions()
  242. {
  243. $application = new Zend_Application('testing', dirname(__FILE__) . '/_files/appconfig.php');
  244. $this->assertTrue($application->hasOption('foo'));
  245. }
  246. public function testPassingStringIncConfigPathOptionToConstructorShouldLoadOptions()
  247. {
  248. $application = new Zend_Application('testing', dirname(__FILE__) . '/_files/appconfig.inc');
  249. $this->assertTrue($application->hasOption('foo'));
  250. }
  251. /**
  252. * @group ZF-10898
  253. */
  254. public function testPassingStringIniDistfileConfigPathOptionToConstructorShouldLoadOptions()
  255. {
  256. $application = new Zend_Application('testing', dirname(__FILE__) . '/_files/appconfig.ini.dist');
  257. $this->assertTrue($application->hasOption('foo'));
  258. }
  259. /**
  260. * @group ZF-10898
  261. */
  262. public function testPassingArrayOptionsWithConfigKeyDistfileShouldLoadOptions()
  263. {
  264. $application = new Zend_Application('testing', array('bar' => 'baz', 'config' => dirname(__FILE__) . '/_files/appconfig.ini.dist'));
  265. $this->assertTrue($application->hasOption('foo'));
  266. $this->assertTrue($application->hasOption('bar'));
  267. }
  268. /**
  269. * @group ZF-10568
  270. */
  271. public function testPassingStringYamlConfigPathOptionToConstructorShouldLoadOptions()
  272. {
  273. $application = new Zend_Application('testing', dirname(__FILE__) . '/_files/appconfig.yaml');
  274. $this->assertTrue($application->hasOption('foo'));
  275. }
  276. /**
  277. * @group ZF-10568
  278. */
  279. public function testPassingStringJsonConfigPathOptionToConstructorShouldLoadOptions()
  280. {
  281. $application = new Zend_Application('testing', dirname(__FILE__) . '/_files/appconfig.json');
  282. $this->assertTrue($application->hasOption('foo'));
  283. }
  284. /**
  285. * @group ZF-11425
  286. */
  287. public function testPassingStringYmlConfigPathOptionToConstructorShouldLoadOptionsAsYaml()
  288. {
  289. $application = new Zend_Application('testing', dirname(__FILE__) . '/_files/appconfig.yml');
  290. $this->assertTrue($application->hasOption('foo'));
  291. }
  292. public function testPassingArrayOptionsWithConfigKeyShouldLoadOptions()
  293. {
  294. $application = new Zend_Application('testing', array('bar' => 'baz', 'config' => dirname(__FILE__) . '/_files/appconfig.inc'));
  295. $this->assertTrue($application->hasOption('foo'));
  296. $this->assertTrue($application->hasOption('bar'));
  297. }
  298. /**
  299. * This was changed to have the passed in array always overwrite the config file.
  300. * @group ZF-6811
  301. */
  302. public function testPassingArrayOptionsWithConfigKeyShouldLoadOptionsAndNotOverride()
  303. {
  304. $application = new Zend_Application('testing', array('foo' => 'baz', 'config' => dirname(__FILE__) . '/_files/appconfig.inc'));
  305. $this->assertNotEquals('bar', $application->getOption('foo'));
  306. }
  307. /**
  308. * @expectedException Zend_Application_Exception
  309. */
  310. public function testPassingInvalidStringOptionToConstructorShouldRaiseException()
  311. {
  312. $application = new Zend_Application('testing', dirname(__FILE__) . '/_files/appconfig');
  313. }
  314. public function testPassingZendConfigToConstructorShouldLoadOptions()
  315. {
  316. $config = new Zend_Config_Ini(dirname(__FILE__) . '/_files/appconfig.ini', 'testing');
  317. $application = new Zend_Application('testing', $config);
  318. $this->assertTrue($application->hasOption('foo'));
  319. }
  320. public function testPassingArrayOptionsToConstructorShouldLoadOptions()
  321. {
  322. $config = new Zend_Config_Ini(dirname(__FILE__) . '/_files/appconfig.ini', 'testing');
  323. $application = new Zend_Application('testing', $config->toArray());
  324. $this->assertTrue($application->hasOption('foo'));
  325. }
  326. public function testBootstrapImplementsFluentInterface()
  327. {
  328. $application = $this->application->bootstrap();
  329. $this->assertSame($application, $this->application);
  330. }
  331. /**
  332. * @expectedException Zend_Application_Exception
  333. */
  334. public function testApplicationShouldRaiseExceptionIfBootstrapFileDoesNotContainBootstrapClass()
  335. {
  336. $this->application->setOptions(array(
  337. 'bootstrap' => array(
  338. 'path' => dirname(__FILE__) . '/_files/ZfAppNoBootstrap.php',
  339. 'class' => 'ZfAppNoBootstrap',
  340. ),
  341. ));
  342. $bootstrap = $this->application->getBootstrap();
  343. }
  344. /**
  345. * @expectedException Zend_Application_Exception
  346. */
  347. public function testApplicationShouldRaiseExceptionWhenBootstrapClassNotOfCorrectType()
  348. {
  349. $this->application->setOptions(array(
  350. 'bootstrap' => array(
  351. 'path' => dirname(__FILE__) . '/_files/ZfAppBadBootstrap.php',
  352. 'class' => 'ZfAppBadBootstrap',
  353. ),
  354. ));
  355. $bootstrap = $this->application->getBootstrap();
  356. }
  357. public function testOptionsShouldRetainOriginalCase()
  358. {
  359. require_once dirname(__FILE__) . '/_files/ZfModuleBootstrap.php';
  360. $options = array(
  361. 'pluginPaths' => array(
  362. 'Zend_Application_Test_Path' => dirname(__FILE__),
  363. ),
  364. 'Resources' => array(
  365. 'modules' => array(),
  366. 'FrontController' => array(
  367. 'baseUrl' => '/foo',
  368. 'moduleDirectory' => dirname(__FILE__) . '/_files/modules',
  369. ),
  370. ),
  371. 'Bootstrap' => array(
  372. 'path' => dirname(__FILE__) . '/_files/ZfAppBootstrap.php',
  373. 'class' => 'ZfAppBootstrap',
  374. ),
  375. );
  376. $this->application->setOptions($options);
  377. $setOptions = $this->application->getOptions();
  378. $this->assertSame(array_keys($options), array_keys($setOptions));
  379. }
  380. /**
  381. * @group ZF-6679
  382. */
  383. public function testSetOptionsShouldProperlyMergeTwoConfigFileOptions()
  384. {
  385. $application = new Zend_Application(
  386. 'production', dirname(__FILE__) .
  387. '/_files/zf-6679-1.inc'
  388. );
  389. $options = $application->getOptions();
  390. $this->assertEquals(array('includePaths', 'config'), array_keys($options));
  391. }
  392. public function testPassingZfVersionAutoloaderInformationConfiguresAutoloader()
  393. {
  394. if (!constant('TESTS_ZEND_LOADER_AUTOLOADER_MULTIVERSION_ENABLED')) {
  395. $this->markTestSkipped();
  396. }
  397. if (!constant('TESTS_ZEND_LOADER_AUTOLOADER_MULTIVERSION_LATEST')) {
  398. $this->markTestSkipped();
  399. }
  400. $path = constant('TESTS_ZEND_LOADER_AUTOLOADER_MULTIVERSION_PATH');
  401. $latest = constant('TESTS_ZEND_LOADER_AUTOLOADER_MULTIVERSION_LATEST');
  402. $application = new Zend_Application('production', array(
  403. 'autoloaderZfPath' => $path,
  404. 'autoloaderZfVersion' => 'latest',
  405. ));
  406. $autoloader = $application->getAutoloader();
  407. $actual = $autoloader->getZfPath();
  408. $this->assertContains($latest, $actual);
  409. }
  410. /**
  411. * @group ZF-7742
  412. */
  413. public function testHasOptionShouldTreatOptionKeysAsCaseInsensitive()
  414. {
  415. $application = new Zend_Application('production', array(
  416. 'fooBar' => 'baz',
  417. ));
  418. $this->assertTrue($application->hasOption('FooBar'));
  419. }
  420. /**
  421. * @group ZF-7742
  422. */
  423. public function testGetOptionShouldTreatOptionKeysAsCaseInsensitive()
  424. {
  425. $application = new Zend_Application('production', array(
  426. 'fooBar' => 'baz',
  427. ));
  428. $this->assertEquals('baz', $application->getOption('FooBar'));
  429. }
  430. /**
  431. * @group ZF-6618
  432. */
  433. public function testCanExecuteBoostrapResourceViaApplicationInstanceBootstrapMethod()
  434. {
  435. $application = new Zend_Application('testing', array(
  436. 'bootstrap' => array(
  437. 'path' => dirname(__FILE__) . '/_files/ZfAppBootstrap.php',
  438. 'class' => 'ZfAppBootstrap'
  439. )
  440. )
  441. );
  442. $application->bootstrap('foo');
  443. $this->assertEquals(1, $application->getBootstrap()->fooExecuted);
  444. $this->assertEquals(0, $application->getBootstrap()->barExecuted);
  445. }
  446. public function testOptionsCanHandleMuiltipleConigFiles()
  447. {
  448. $application = new Zend_Application('testing', array(
  449. 'config' => array(
  450. dirname(__FILE__) . '/_files/Zf-6719-1.ini',
  451. dirname(__FILE__) . '/_files/Zf-6719-2.ini'
  452. )
  453. )
  454. );
  455. $this->assertEquals('baz', $application->getOption('foo'));
  456. }
  457. }
  458. if (PHPUnit_MAIN_METHOD == 'Zend_Application_ApplicationTest::main') {
  459. Zend_Application_ApplicationTest::main();
  460. }