ApplicationTest.php 15 KB

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