ApplicationTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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. /** 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-2010 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[] = 'y2k_compliance';
  145. $orig = ini_get('y2k_compliance');
  146. $expected = $orig ? 0 : 1;
  147. $this->application->setOptions(array(
  148. 'phpSettings' => array(
  149. 'y2k_compliance' => $expected,
  150. ),
  151. ));
  152. $this->assertEquals($expected, ini_get('y2k_compliance'));
  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-10568
  253. */
  254. public function testPassingStringYamlConfigPathOptionToConstructorShouldLoadOptions()
  255. {
  256. $application = new Zend_Application('testing', dirname(__FILE__) . '/_files/appconfig.yaml');
  257. $this->assertTrue($application->hasOption('foo'));
  258. }
  259. /**
  260. * @group ZF-10568
  261. */
  262. public function testPassingStringJsonConfigPathOptionToConstructorShouldLoadOptions()
  263. {
  264. $application = new Zend_Application('testing', dirname(__FILE__) . '/_files/appconfig.json');
  265. $this->assertTrue($application->hasOption('foo'));
  266. }
  267. public function testPassingArrayOptionsWithConfigKeyShouldLoadOptions()
  268. {
  269. $application = new Zend_Application('testing', array('bar' => 'baz', 'config' => dirname(__FILE__) . '/_files/appconfig.inc'));
  270. $this->assertTrue($application->hasOption('foo'));
  271. $this->assertTrue($application->hasOption('bar'));
  272. }
  273. /**
  274. * This was changed to have the passed in array always overwrite the config file.
  275. * @group ZF-6811
  276. */
  277. public function testPassingArrayOptionsWithConfigKeyShouldLoadOptionsAndNotOverride()
  278. {
  279. $application = new Zend_Application('testing', array('foo' => 'baz', 'config' => dirname(__FILE__) . '/_files/appconfig.inc'));
  280. $this->assertNotEquals('bar', $application->getOption('foo'));
  281. }
  282. /**
  283. * @expectedException Zend_Application_Exception
  284. */
  285. public function testPassingInvalidStringOptionToConstructorShouldRaiseException()
  286. {
  287. $application = new Zend_Application('testing', dirname(__FILE__) . '/_files/appconfig');
  288. }
  289. public function testPassingZendConfigToConstructorShouldLoadOptions()
  290. {
  291. $config = new Zend_Config_Ini(dirname(__FILE__) . '/_files/appconfig.ini', 'testing');
  292. $application = new Zend_Application('testing', $config);
  293. $this->assertTrue($application->hasOption('foo'));
  294. }
  295. public function testPassingArrayOptionsToConstructorShouldLoadOptions()
  296. {
  297. $config = new Zend_Config_Ini(dirname(__FILE__) . '/_files/appconfig.ini', 'testing');
  298. $application = new Zend_Application('testing', $config->toArray());
  299. $this->assertTrue($application->hasOption('foo'));
  300. }
  301. public function testBootstrapImplementsFluentInterface()
  302. {
  303. $application = $this->application->bootstrap();
  304. $this->assertSame($application, $this->application);
  305. }
  306. /**
  307. * @expectedException Zend_Application_Exception
  308. */
  309. public function testApplicationShouldRaiseExceptionIfBootstrapFileDoesNotContainBootstrapClass()
  310. {
  311. $this->application->setOptions(array(
  312. 'bootstrap' => array(
  313. 'path' => dirname(__FILE__) . '/_files/ZfAppNoBootstrap.php',
  314. 'class' => 'ZfAppNoBootstrap',
  315. ),
  316. ));
  317. $bootstrap = $this->application->getBootstrap();
  318. }
  319. /**
  320. * @expectedException Zend_Application_Exception
  321. */
  322. public function testApplicationShouldRaiseExceptionWhenBootstrapClassNotOfCorrectType()
  323. {
  324. $this->application->setOptions(array(
  325. 'bootstrap' => array(
  326. 'path' => dirname(__FILE__) . '/_files/ZfAppBadBootstrap.php',
  327. 'class' => 'ZfAppBadBootstrap',
  328. ),
  329. ));
  330. $bootstrap = $this->application->getBootstrap();
  331. }
  332. public function testOptionsShouldRetainOriginalCase()
  333. {
  334. require_once dirname(__FILE__) . '/_files/ZfModuleBootstrap.php';
  335. $options = array(
  336. 'pluginPaths' => array(
  337. 'Zend_Application_Test_Path' => dirname(__FILE__),
  338. ),
  339. 'Resources' => array(
  340. 'modules' => array(),
  341. 'FrontController' => array(
  342. 'baseUrl' => '/foo',
  343. 'moduleDirectory' => dirname(__FILE__) . '/_files/modules',
  344. ),
  345. ),
  346. 'Bootstrap' => array(
  347. 'path' => dirname(__FILE__) . '/_files/ZfAppBootstrap.php',
  348. 'class' => 'ZfAppBootstrap',
  349. ),
  350. );
  351. $this->application->setOptions($options);
  352. $setOptions = $this->application->getOptions();
  353. $this->assertSame(array_keys($options), array_keys($setOptions));
  354. }
  355. /**
  356. * @group ZF-6679
  357. */
  358. public function testSetOptionsShouldProperlyMergeTwoConfigFileOptions()
  359. {
  360. $application = new Zend_Application(
  361. 'production', dirname(__FILE__) .
  362. '/_files/zf-6679-1.inc'
  363. );
  364. $options = $application->getOptions();
  365. $this->assertEquals(array('includePaths', 'config'), array_keys($options));
  366. }
  367. public function testPassingZfVersionAutoloaderInformationConfiguresAutoloader()
  368. {
  369. if (!constant('TESTS_ZEND_LOADER_AUTOLOADER_MULTIVERSION_ENABLED')) {
  370. $this->markTestSkipped();
  371. }
  372. if (!constant('TESTS_ZEND_LOADER_AUTOLOADER_MULTIVERSION_LATEST')) {
  373. $this->markTestSkipped();
  374. }
  375. $path = constant('TESTS_ZEND_LOADER_AUTOLOADER_MULTIVERSION_PATH');
  376. $latest = constant('TESTS_ZEND_LOADER_AUTOLOADER_MULTIVERSION_LATEST');
  377. $application = new Zend_Application('production', array(
  378. 'autoloaderZfPath' => $path,
  379. 'autoloaderZfVersion' => 'latest',
  380. ));
  381. $autoloader = $application->getAutoloader();
  382. $actual = $autoloader->getZfPath();
  383. $this->assertContains($latest, $actual);
  384. }
  385. /**
  386. * @group ZF-7742
  387. */
  388. public function testHasOptionShouldTreatOptionKeysAsCaseInsensitive()
  389. {
  390. $application = new Zend_Application('production', array(
  391. 'fooBar' => 'baz',
  392. ));
  393. $this->assertTrue($application->hasOption('FooBar'));
  394. }
  395. /**
  396. * @group ZF-7742
  397. */
  398. public function testGetOptionShouldTreatOptionKeysAsCaseInsensitive()
  399. {
  400. $application = new Zend_Application('production', array(
  401. 'fooBar' => 'baz',
  402. ));
  403. $this->assertEquals('baz', $application->getOption('FooBar'));
  404. }
  405. /**
  406. * @group ZF-6618
  407. */
  408. public function testCanExecuteBoostrapResourceViaApplicationInstanceBootstrapMethod()
  409. {
  410. $application = new Zend_Application('testing', array(
  411. 'bootstrap' => array(
  412. 'path' => dirname(__FILE__) . '/_files/ZfAppBootstrap.php',
  413. 'class' => 'ZfAppBootstrap'
  414. )
  415. )
  416. );
  417. $application->bootstrap('foo');
  418. $this->assertEquals(1, $application->getBootstrap()->fooExecuted);
  419. $this->assertEquals(0, $application->getBootstrap()->barExecuted);
  420. }
  421. public function testOptionsCanHandleMuiltipleConigFiles()
  422. {
  423. $application = new Zend_Application('testing', array(
  424. 'config' => array(
  425. dirname(__FILE__) . '/_files/Zf-6719-1.ini',
  426. dirname(__FILE__) . '/_files/Zf-6719-2.ini'
  427. )
  428. )
  429. );
  430. $this->assertEquals('baz', $application->getOption('foo'));
  431. }
  432. }
  433. if (PHPUnit_MAIN_METHOD == 'Zend_Application_ApplicationTest::main') {
  434. Zend_Application_ApplicationTest::main();
  435. }