ApplicationTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. public function testPassingArrayOptionsWithConfigKeyShouldLoadOptionsAndOverride()
  262. {
  263. $application = new Zend_Application('testing', array('foo' => 'baz', 'config' => dirname(__FILE__) . '/_files/appconfig.inc'));
  264. $this->assertEquals('bar', $application->getOption('foo'));
  265. }
  266. /**
  267. * @expectedException Zend_Application_Exception
  268. */
  269. public function testPassingInvalidStringOptionToConstructorShouldRaiseException()
  270. {
  271. $application = new Zend_Application('testing', dirname(__FILE__) . '/_files/appconfig');
  272. }
  273. public function testPassingZendConfigToConstructorShouldLoadOptions()
  274. {
  275. $config = new Zend_Config_Ini(dirname(__FILE__) . '/_files/appconfig.ini', 'testing');
  276. $application = new Zend_Application('testing', $config);
  277. $this->assertTrue($application->hasOption('foo'));
  278. }
  279. public function testPassingArrayOptionsToConstructorShouldLoadOptions()
  280. {
  281. $config = new Zend_Config_Ini(dirname(__FILE__) . '/_files/appconfig.ini', 'testing');
  282. $application = new Zend_Application('testing', $config->toArray());
  283. $this->assertTrue($application->hasOption('foo'));
  284. }
  285. public function testBootstrapImplementsFluentInterface()
  286. {
  287. $application = $this->application->bootstrap();
  288. $this->assertSame($application, $this->application);
  289. }
  290. /**
  291. * @expectedException Zend_Application_Exception
  292. */
  293. public function testApplicationShouldRaiseExceptionIfBootstrapFileDoesNotContainBootstrapClass()
  294. {
  295. $this->application->setOptions(array(
  296. 'bootstrap' => array(
  297. 'path' => dirname(__FILE__) . '/_files/ZfAppNoBootstrap.php',
  298. 'class' => 'ZfAppNoBootstrap',
  299. ),
  300. ));
  301. $bootstrap = $this->application->getBootstrap();
  302. }
  303. /**
  304. * @expectedException Zend_Application_Exception
  305. */
  306. public function testApplicationShouldRaiseExceptionWhenBootstrapClassNotOfCorrectType()
  307. {
  308. $this->application->setOptions(array(
  309. 'bootstrap' => array(
  310. 'path' => dirname(__FILE__) . '/_files/ZfAppBadBootstrap.php',
  311. 'class' => 'ZfAppBadBootstrap',
  312. ),
  313. ));
  314. $bootstrap = $this->application->getBootstrap();
  315. }
  316. public function testOptionsShouldRetainOriginalCase()
  317. {
  318. require_once dirname(__FILE__) . '/_files/ZfModuleBootstrap.php';
  319. $options = array(
  320. 'pluginPaths' => array(
  321. 'Zend_Application_Test_Path' => dirname(__FILE__),
  322. ),
  323. 'Resources' => array(
  324. 'modules' => array(),
  325. 'FrontController' => array(
  326. 'baseUrl' => '/foo',
  327. 'moduleDirectory' => dirname(__FILE__) . '/_files/modules',
  328. ),
  329. ),
  330. 'Bootstrap' => array(
  331. 'path' => dirname(__FILE__) . '/_files/ZfAppBootstrap.php',
  332. 'class' => 'ZfAppBootstrap',
  333. ),
  334. );
  335. $this->application->setOptions($options);
  336. $setOptions = $this->application->getOptions();
  337. $this->assertSame(array_keys($options), array_keys($setOptions));
  338. }
  339. /**
  340. * @group ZF-6679
  341. */
  342. public function testSetOptionsShouldProperlyMergeTwoConfigFileOptions()
  343. {
  344. $application = new Zend_Application(
  345. 'production', dirname(__FILE__) .
  346. '/_files/zf-6679-1.inc'
  347. );
  348. $options = $application->getOptions();
  349. $this->assertEquals(array('config', 'includePaths'), array_keys($options));
  350. }
  351. public function testPassingZfVersionAutoloaderInformationConfiguresAutoloader()
  352. {
  353. if (!constant('TESTS_ZEND_LOADER_AUTOLOADER_MULTIVERSION_ENABLED')) {
  354. $this->markTestSkipped();
  355. }
  356. if (!constant('TESTS_ZEND_LOADER_AUTOLOADER_MULTIVERSION_LATEST')) {
  357. $this->markTestSkipped();
  358. }
  359. $path = constant('TESTS_ZEND_LOADER_AUTOLOADER_MULTIVERSION_PATH');
  360. $latest = constant('TESTS_ZEND_LOADER_AUTOLOADER_MULTIVERSION_LATEST');
  361. $application = new Zend_Application('production', array(
  362. 'autoloaderZfPath' => $path,
  363. 'autoloaderZfVersion' => 'latest',
  364. ));
  365. $autoloader = $application->getAutoloader();
  366. $actual = $autoloader->getZfPath();
  367. $this->assertContains($latest, $actual);
  368. }
  369. }
  370. if (PHPUnit_MAIN_METHOD == 'Zend_Application_ApplicationTest::main') {
  371. Zend_Application_ApplicationTest::main();
  372. }