ApplicationTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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-2011 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-2011 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-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. public function testPassingArrayOptionsWithConfigKeyShouldLoadOptions()
  285. {
  286. $application = new Zend_Application('testing', array('bar' => 'baz', 'config' => dirname(__FILE__) . '/_files/appconfig.inc'));
  287. $this->assertTrue($application->hasOption('foo'));
  288. $this->assertTrue($application->hasOption('bar'));
  289. }
  290. /**
  291. * This was changed to have the passed in array always overwrite the config file.
  292. * @group ZF-6811
  293. */
  294. public function testPassingArrayOptionsWithConfigKeyShouldLoadOptionsAndNotOverride()
  295. {
  296. $application = new Zend_Application('testing', array('foo' => 'baz', 'config' => dirname(__FILE__) . '/_files/appconfig.inc'));
  297. $this->assertNotEquals('bar', $application->getOption('foo'));
  298. }
  299. /**
  300. * @expectedException Zend_Application_Exception
  301. */
  302. public function testPassingInvalidStringOptionToConstructorShouldRaiseException()
  303. {
  304. $application = new Zend_Application('testing', dirname(__FILE__) . '/_files/appconfig');
  305. }
  306. public function testPassingZendConfigToConstructorShouldLoadOptions()
  307. {
  308. $config = new Zend_Config_Ini(dirname(__FILE__) . '/_files/appconfig.ini', 'testing');
  309. $application = new Zend_Application('testing', $config);
  310. $this->assertTrue($application->hasOption('foo'));
  311. }
  312. public function testPassingArrayOptionsToConstructorShouldLoadOptions()
  313. {
  314. $config = new Zend_Config_Ini(dirname(__FILE__) . '/_files/appconfig.ini', 'testing');
  315. $application = new Zend_Application('testing', $config->toArray());
  316. $this->assertTrue($application->hasOption('foo'));
  317. }
  318. public function testBootstrapImplementsFluentInterface()
  319. {
  320. $application = $this->application->bootstrap();
  321. $this->assertSame($application, $this->application);
  322. }
  323. /**
  324. * @expectedException Zend_Application_Exception
  325. */
  326. public function testApplicationShouldRaiseExceptionIfBootstrapFileDoesNotContainBootstrapClass()
  327. {
  328. $this->application->setOptions(array(
  329. 'bootstrap' => array(
  330. 'path' => dirname(__FILE__) . '/_files/ZfAppNoBootstrap.php',
  331. 'class' => 'ZfAppNoBootstrap',
  332. ),
  333. ));
  334. $bootstrap = $this->application->getBootstrap();
  335. }
  336. /**
  337. * @expectedException Zend_Application_Exception
  338. */
  339. public function testApplicationShouldRaiseExceptionWhenBootstrapClassNotOfCorrectType()
  340. {
  341. $this->application->setOptions(array(
  342. 'bootstrap' => array(
  343. 'path' => dirname(__FILE__) . '/_files/ZfAppBadBootstrap.php',
  344. 'class' => 'ZfAppBadBootstrap',
  345. ),
  346. ));
  347. $bootstrap = $this->application->getBootstrap();
  348. }
  349. public function testOptionsShouldRetainOriginalCase()
  350. {
  351. require_once dirname(__FILE__) . '/_files/ZfModuleBootstrap.php';
  352. $options = array(
  353. 'pluginPaths' => array(
  354. 'Zend_Application_Test_Path' => dirname(__FILE__),
  355. ),
  356. 'Resources' => array(
  357. 'modules' => array(),
  358. 'FrontController' => array(
  359. 'baseUrl' => '/foo',
  360. 'moduleDirectory' => dirname(__FILE__) . '/_files/modules',
  361. ),
  362. ),
  363. 'Bootstrap' => array(
  364. 'path' => dirname(__FILE__) . '/_files/ZfAppBootstrap.php',
  365. 'class' => 'ZfAppBootstrap',
  366. ),
  367. );
  368. $this->application->setOptions($options);
  369. $setOptions = $this->application->getOptions();
  370. $this->assertSame(array_keys($options), array_keys($setOptions));
  371. }
  372. /**
  373. * @group ZF-6679
  374. */
  375. public function testSetOptionsShouldProperlyMergeTwoConfigFileOptions()
  376. {
  377. $application = new Zend_Application(
  378. 'production', dirname(__FILE__) .
  379. '/_files/zf-6679-1.inc'
  380. );
  381. $options = $application->getOptions();
  382. $this->assertEquals(array('includePaths', 'config'), array_keys($options));
  383. }
  384. public function testPassingZfVersionAutoloaderInformationConfiguresAutoloader()
  385. {
  386. if (!constant('TESTS_ZEND_LOADER_AUTOLOADER_MULTIVERSION_ENABLED')) {
  387. $this->markTestSkipped();
  388. }
  389. if (!constant('TESTS_ZEND_LOADER_AUTOLOADER_MULTIVERSION_LATEST')) {
  390. $this->markTestSkipped();
  391. }
  392. $path = constant('TESTS_ZEND_LOADER_AUTOLOADER_MULTIVERSION_PATH');
  393. $latest = constant('TESTS_ZEND_LOADER_AUTOLOADER_MULTIVERSION_LATEST');
  394. $application = new Zend_Application('production', array(
  395. 'autoloaderZfPath' => $path,
  396. 'autoloaderZfVersion' => 'latest',
  397. ));
  398. $autoloader = $application->getAutoloader();
  399. $actual = $autoloader->getZfPath();
  400. $this->assertContains($latest, $actual);
  401. }
  402. /**
  403. * @group ZF-7742
  404. */
  405. public function testHasOptionShouldTreatOptionKeysAsCaseInsensitive()
  406. {
  407. $application = new Zend_Application('production', array(
  408. 'fooBar' => 'baz',
  409. ));
  410. $this->assertTrue($application->hasOption('FooBar'));
  411. }
  412. /**
  413. * @group ZF-7742
  414. */
  415. public function testGetOptionShouldTreatOptionKeysAsCaseInsensitive()
  416. {
  417. $application = new Zend_Application('production', array(
  418. 'fooBar' => 'baz',
  419. ));
  420. $this->assertEquals('baz', $application->getOption('FooBar'));
  421. }
  422. /**
  423. * @group ZF-6618
  424. */
  425. public function testCanExecuteBoostrapResourceViaApplicationInstanceBootstrapMethod()
  426. {
  427. $application = new Zend_Application('testing', array(
  428. 'bootstrap' => array(
  429. 'path' => dirname(__FILE__) . '/_files/ZfAppBootstrap.php',
  430. 'class' => 'ZfAppBootstrap'
  431. )
  432. )
  433. );
  434. $application->bootstrap('foo');
  435. $this->assertEquals(1, $application->getBootstrap()->fooExecuted);
  436. $this->assertEquals(0, $application->getBootstrap()->barExecuted);
  437. }
  438. public function testOptionsCanHandleMuiltipleConigFiles()
  439. {
  440. $application = new Zend_Application('testing', array(
  441. 'config' => array(
  442. dirname(__FILE__) . '/_files/Zf-6719-1.ini',
  443. dirname(__FILE__) . '/_files/Zf-6719-2.ini'
  444. )
  445. )
  446. );
  447. $this->assertEquals('baz', $application->getOption('foo'));
  448. }
  449. }
  450. if (PHPUnit_MAIN_METHOD == 'Zend_Application_ApplicationTest::main') {
  451. Zend_Application_ApplicationTest::main();
  452. }