ApplicationTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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-2008 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-2008 Zend Technologies USA Inc. (http://www.zend.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. */
  40. class Zend_Application_ApplicationTest extends PHPUnit_Framework_TestCase
  41. {
  42. public static function main()
  43. {
  44. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  45. $result = PHPUnit_TextUI_TestRunner::run($suite);
  46. }
  47. public function setUp()
  48. {
  49. // Store original autoloaders
  50. $this->loaders = spl_autoload_functions();
  51. if (!is_array($this->loaders)) {
  52. // spl_autoload_functions does not return empty array when no
  53. // autoloaders registered...
  54. $this->loaders = array();
  55. }
  56. // Store original include_path
  57. $this->includePath = get_include_path();
  58. Zend_Loader_Autoloader::resetInstance();
  59. $this->autoloader = Zend_Loader_Autoloader::getInstance();
  60. $this->application = new Zend_Application('testing');
  61. $this->iniOptions = array();
  62. }
  63. public function tearDown()
  64. {
  65. // Restore original autoloaders
  66. $loaders = spl_autoload_functions();
  67. foreach ($loaders as $loader) {
  68. spl_autoload_unregister($loader);
  69. }
  70. foreach ($this->loaders as $loader) {
  71. spl_autoload_register($loader);
  72. }
  73. foreach ($this->iniOptions as $key) {
  74. ini_restore($key);
  75. }
  76. }
  77. public function testConstructorSetsEnvironment()
  78. {
  79. $this->assertEquals('testing', $this->application->getEnvironment());
  80. }
  81. public function testConstructorInstantiatesAutoloader()
  82. {
  83. $autoloader = $this->application->getAutoloader();
  84. $this->assertTrue($autoloader instanceof Zend_Loader_Autoloader);
  85. }
  86. public function testConstructorShouldSetOptionsWhenProvided()
  87. {
  88. $options = array(
  89. 'foo' => 'bar',
  90. 'bar' => 'baz',
  91. );
  92. $application = new Zend_Application('testing', $options);
  93. $this->assertEquals($options, $application->getOptions());
  94. }
  95. public function testHasOptionShouldReturnFalseWhenOptionNotPresent()
  96. {
  97. $this->assertFalse($this->application->hasOption('foo'));
  98. }
  99. public function testHasOptionShouldReturnTrueWhenOptionPresent()
  100. {
  101. $options = array(
  102. 'foo' => 'bar',
  103. 'bar' => 'baz',
  104. );
  105. $application = new Zend_Application('testing', $options);
  106. $this->assertTrue($application->hasOption('foo'));
  107. }
  108. public function testGetOptionShouldReturnNullWhenOptionNotPresent()
  109. {
  110. $this->assertNull($this->application->getOption('foo'));
  111. }
  112. public function testGetOptionShouldReturnOptionValue()
  113. {
  114. $options = array(
  115. 'foo' => 'bar',
  116. 'bar' => 'baz',
  117. );
  118. $application = new Zend_Application('testing', $options);
  119. $this->assertEquals($options['foo'], $application->getOption('foo'));
  120. }
  121. public function testPassingAutoloaderNamespaceOptionsShouldProxyToAutoloader()
  122. {
  123. $autoloader = $this->autoloader;
  124. $this->application->setOptions(array(
  125. 'autoloaderNamespaces' => array(
  126. 'Foo',
  127. ),
  128. ));
  129. $namespaces = $this->autoloader->getRegisteredNamespaces();
  130. $this->assertContains('Foo', $namespaces);
  131. }
  132. public function testPassingIncludePathOptionShouldModifyIncludePath()
  133. {
  134. $expected = dirname(__FILE__) . '/_files';
  135. $this->application->setOptions(array(
  136. 'includePaths' => array(
  137. $expected,
  138. ),
  139. ));
  140. $test = get_include_path();
  141. $this->assertContains($expected, $test);
  142. }
  143. public function testPassingPhpSettingsSetsIniValues()
  144. {
  145. $this->iniOptions[] = 'y2k_compliance';
  146. $orig = ini_get('y2k_compliance');
  147. $expected = $orig ? 0 : 1;
  148. $this->application->setOptions(array(
  149. 'phpSettings' => array(
  150. 'y2k_compliance' => $expected,
  151. ),
  152. ));
  153. $this->assertEquals($expected, ini_get('y2k_compliance'));
  154. }
  155. public function testPassingPhpSettingsAsArrayShouldConstructDotValuesAndSetRelatedIniValues()
  156. {
  157. $this->iniOptions[] = 'date.default_latitude';
  158. $orig = ini_get('date.default_latitude');
  159. $expected = '1.234';
  160. $this->application->setOptions(array(
  161. 'phpSettings' => array(
  162. 'date' => array(
  163. 'default_latitude' => $expected,
  164. ),
  165. ),
  166. ));
  167. $this->assertEquals($expected, ini_get('date.default_latitude'));
  168. }
  169. public function testShouldUseBaseBootstrapClassByDefaultIfNoBootstrapRegistered()
  170. {
  171. $bootstrap = $this->application->getBootstrap();
  172. $this->assertTrue($bootstrap instanceof Zend_Application_Bootstrap_Bootstrap);
  173. }
  174. public function testPassingStringBootstrapPathOptionShouldRegisterBootstrap()
  175. {
  176. $this->application->setOptions(array(
  177. 'bootstrap' => dirname(__FILE__) . '/_files/modules/default/Bootstrap.php',
  178. ));
  179. $bootstrap = $this->application->getBootstrap();
  180. $this->assertTrue($bootstrap instanceof Bootstrap);
  181. }
  182. public function testPassingArrayBootstrapOptionShouldRegisterBootstrapBasedOnPathOption()
  183. {
  184. $this->application->setOptions(array(
  185. 'bootstrap' => array(
  186. 'path' => dirname(__FILE__) . '/_files/modules/default/Bootstrap.php',
  187. ),
  188. ));
  189. $bootstrap = $this->application->getBootstrap();
  190. $this->assertTrue($bootstrap instanceof Bootstrap);
  191. }
  192. public function testPassingArrayBootstrapOptionShouldRegisterBootstrapBasedOnPathAndClassOption()
  193. {
  194. $this->application->setOptions(array(
  195. 'bootstrap' => array(
  196. 'path' => dirname(__FILE__) . '/_files/ZfAppBootstrap.php',
  197. 'class' => 'ZfAppBootstrap',
  198. ),
  199. ));
  200. $bootstrap = $this->application->getBootstrap();
  201. $this->assertTrue($bootstrap instanceof ZfAppBootstrap);
  202. }
  203. /**
  204. * @expectedException Zend_Application_Exception
  205. */
  206. public function testPassingArrayBootstrapWithoutPathOptionShouldRaiseException()
  207. {
  208. $this->application->setOptions(array(
  209. 'bootstrap' => array(
  210. 'class' => 'ZfAppBootstrap',
  211. ),
  212. ));
  213. $bootstrap = $this->application->getBootstrap();
  214. }
  215. /**
  216. * @expectedException Zend_Application_Exception
  217. */
  218. public function testPassingInvalidBootstrapOptionShouldRaiseException()
  219. {
  220. $this->application->setOptions(array(
  221. 'bootstrap' => new stdClass(),
  222. ));
  223. $bootstrap = $this->application->getBootstrap();
  224. }
  225. /**
  226. * @expectedException Zend_Application_Exception
  227. */
  228. public function testPassingInvalidOptionsArgumentToConstructorShouldRaiseException()
  229. {
  230. $application = new Zend_Application('testing', new stdClass());
  231. }
  232. public function testPassingStringIniConfigPathOptionToConstructorShouldLoadOptions()
  233. {
  234. $application = new Zend_Application('testing', dirname(__FILE__) . '/_files/appconfig.ini');
  235. $this->assertTrue($application->hasOption('foo'));
  236. }
  237. public function testPassingStringXmlConfigPathOptionToConstructorShouldLoadOptions()
  238. {
  239. $application = new Zend_Application('testing', dirname(__FILE__) . '/_files/appconfig.xml');
  240. $this->assertTrue($application->hasOption('foo'));
  241. }
  242. public function testPassingStringPhpConfigPathOptionToConstructorShouldLoadOptions()
  243. {
  244. $application = new Zend_Application('testing', dirname(__FILE__) . '/_files/appconfig.php');
  245. $this->assertTrue($application->hasOption('foo'));
  246. }
  247. public function testPassingStringIncConfigPathOptionToConstructorShouldLoadOptions()
  248. {
  249. $application = new Zend_Application('testing', dirname(__FILE__) . '/_files/appconfig.inc');
  250. $this->assertTrue($application->hasOption('foo'));
  251. }
  252. public function testPassingArrayOptionsWithConfigKeyShouldLoadOptions()
  253. {
  254. $application = new Zend_Application('testing', array('bar' => 'baz', 'config' => dirname(__FILE__) . '/_files/appconfig.inc'));
  255. $this->assertTrue($application->hasOption('foo'));
  256. $this->assertTrue($application->hasOption('bar'));
  257. }
  258. public function testPassingArrayOptionsWithConfigKeyShouldLoadOptionsAndOverride()
  259. {
  260. $application = new Zend_Application('testing', array('foo' => 'baz', 'config' => dirname(__FILE__) . '/_files/appconfig.inc'));
  261. $this->assertEquals('bar', $application->getOption('foo'));
  262. }
  263. /**
  264. * @expectedException Zend_Application_Exception
  265. */
  266. public function testPassingInvalidStringOptionToConstructorShouldRaiseException()
  267. {
  268. $application = new Zend_Application('testing', dirname(__FILE__) . '/_files/appconfig');
  269. }
  270. public function testPassingZendConfigToConstructorShouldLoadOptions()
  271. {
  272. $config = new Zend_Config_Ini(dirname(__FILE__) . '/_files/appconfig.ini', 'testing');
  273. $application = new Zend_Application('testing', $config);
  274. $this->assertTrue($application->hasOption('foo'));
  275. }
  276. public function testPassingArrayOptionsToConstructorShouldLoadOptions()
  277. {
  278. $config = new Zend_Config_Ini(dirname(__FILE__) . '/_files/appconfig.ini', 'testing');
  279. $application = new Zend_Application('testing', $config->toArray());
  280. $this->assertTrue($application->hasOption('foo'));
  281. }
  282. public function testBootstrapImplementsFluentInterface()
  283. {
  284. $application = $this->application->bootstrap();
  285. $this->assertSame($application, $this->application);
  286. }
  287. }
  288. if (PHPUnit_MAIN_METHOD == 'Zend_Application_ApplicationTest::main') {
  289. Zend_Application_ApplicationTest::main();
  290. }