2
0

ApplicationTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. // Reset autoloader instance so it doesn't affect other tests
  77. Zend_Loader_Autoloader::resetInstance();
  78. }
  79. public function testConstructorSetsEnvironment()
  80. {
  81. $this->assertEquals('testing', $this->application->getEnvironment());
  82. }
  83. public function testConstructorInstantiatesAutoloader()
  84. {
  85. $autoloader = $this->application->getAutoloader();
  86. $this->assertTrue($autoloader instanceof Zend_Loader_Autoloader);
  87. }
  88. public function testConstructorShouldSetOptionsWhenProvided()
  89. {
  90. $options = array(
  91. 'foo' => 'bar',
  92. 'bar' => 'baz',
  93. );
  94. $application = new Zend_Application('testing', $options);
  95. $this->assertEquals($options, $application->getOptions());
  96. }
  97. public function testHasOptionShouldReturnFalseWhenOptionNotPresent()
  98. {
  99. $this->assertFalse($this->application->hasOption('foo'));
  100. }
  101. public function testHasOptionShouldReturnTrueWhenOptionPresent()
  102. {
  103. $options = array(
  104. 'foo' => 'bar',
  105. 'bar' => 'baz',
  106. );
  107. $application = new Zend_Application('testing', $options);
  108. $this->assertTrue($application->hasOption('foo'));
  109. }
  110. public function testGetOptionShouldReturnNullWhenOptionNotPresent()
  111. {
  112. $this->assertNull($this->application->getOption('foo'));
  113. }
  114. public function testGetOptionShouldReturnOptionValue()
  115. {
  116. $options = array(
  117. 'foo' => 'bar',
  118. 'bar' => 'baz',
  119. );
  120. $application = new Zend_Application('testing', $options);
  121. $this->assertEquals($options['foo'], $application->getOption('foo'));
  122. }
  123. public function testPassingAutoloaderNamespaceOptionsShouldProxyToAutoloader()
  124. {
  125. $autoloader = $this->autoloader;
  126. $this->application->setOptions(array(
  127. 'autoloaderNamespaces' => array(
  128. 'Foo',
  129. ),
  130. ));
  131. $namespaces = $this->autoloader->getRegisteredNamespaces();
  132. $this->assertContains('Foo', $namespaces);
  133. }
  134. public function testPassingIncludePathOptionShouldModifyIncludePath()
  135. {
  136. $expected = dirname(__FILE__) . '/_files';
  137. $this->application->setOptions(array(
  138. 'includePaths' => array(
  139. $expected,
  140. ),
  141. ));
  142. $test = get_include_path();
  143. $this->assertContains($expected, $test);
  144. }
  145. public function testPassingPhpSettingsSetsIniValues()
  146. {
  147. $this->iniOptions[] = 'y2k_compliance';
  148. $orig = ini_get('y2k_compliance');
  149. $expected = $orig ? 0 : 1;
  150. $this->application->setOptions(array(
  151. 'phpSettings' => array(
  152. 'y2k_compliance' => $expected,
  153. ),
  154. ));
  155. $this->assertEquals($expected, ini_get('y2k_compliance'));
  156. }
  157. public function testPassingPhpSettingsAsArrayShouldConstructDotValuesAndSetRelatedIniValues()
  158. {
  159. $this->iniOptions[] = 'date.default_latitude';
  160. $orig = ini_get('date.default_latitude');
  161. $expected = '1.234';
  162. $this->application->setOptions(array(
  163. 'phpSettings' => array(
  164. 'date' => array(
  165. 'default_latitude' => $expected,
  166. ),
  167. ),
  168. ));
  169. $this->assertEquals($expected, ini_get('date.default_latitude'));
  170. }
  171. public function testShouldUseBaseBootstrapClassByDefaultIfNoBootstrapRegistered()
  172. {
  173. $bootstrap = $this->application->getBootstrap();
  174. $this->assertTrue($bootstrap instanceof Zend_Application_Bootstrap_Bootstrap);
  175. }
  176. public function testPassingStringBootstrapPathOptionShouldRegisterBootstrap()
  177. {
  178. $this->application->setOptions(array(
  179. 'bootstrap' => dirname(__FILE__) . '/_files/modules/default/Bootstrap.php',
  180. ));
  181. $bootstrap = $this->application->getBootstrap();
  182. $this->assertTrue($bootstrap instanceof Bootstrap);
  183. }
  184. public function testPassingArrayBootstrapOptionShouldRegisterBootstrapBasedOnPathOption()
  185. {
  186. $this->application->setOptions(array(
  187. 'bootstrap' => array(
  188. 'path' => dirname(__FILE__) . '/_files/modules/default/Bootstrap.php',
  189. ),
  190. ));
  191. $bootstrap = $this->application->getBootstrap();
  192. $this->assertTrue($bootstrap instanceof Bootstrap);
  193. }
  194. public function testPassingArrayBootstrapOptionShouldRegisterBootstrapBasedOnPathAndClassOption()
  195. {
  196. $this->application->setOptions(array(
  197. 'bootstrap' => array(
  198. 'path' => dirname(__FILE__) . '/_files/ZfAppBootstrap.php',
  199. 'class' => 'ZfAppBootstrap',
  200. ),
  201. ));
  202. $bootstrap = $this->application->getBootstrap();
  203. $this->assertTrue($bootstrap instanceof ZfAppBootstrap);
  204. }
  205. /**
  206. * @expectedException Zend_Application_Exception
  207. */
  208. public function testPassingArrayBootstrapWithoutPathOptionShouldRaiseException()
  209. {
  210. $this->application->setOptions(array(
  211. 'bootstrap' => array(
  212. 'class' => 'ZfAppBootstrap',
  213. ),
  214. ));
  215. $bootstrap = $this->application->getBootstrap();
  216. }
  217. /**
  218. * @expectedException Zend_Application_Exception
  219. */
  220. public function testPassingInvalidBootstrapOptionShouldRaiseException()
  221. {
  222. $this->application->setOptions(array(
  223. 'bootstrap' => new stdClass(),
  224. ));
  225. $bootstrap = $this->application->getBootstrap();
  226. }
  227. /**
  228. * @expectedException Zend_Application_Exception
  229. */
  230. public function testPassingInvalidOptionsArgumentToConstructorShouldRaiseException()
  231. {
  232. $application = new Zend_Application('testing', new stdClass());
  233. }
  234. public function testPassingStringIniConfigPathOptionToConstructorShouldLoadOptions()
  235. {
  236. $application = new Zend_Application('testing', dirname(__FILE__) . '/_files/appconfig.ini');
  237. $this->assertTrue($application->hasOption('foo'));
  238. }
  239. public function testPassingStringXmlConfigPathOptionToConstructorShouldLoadOptions()
  240. {
  241. $application = new Zend_Application('testing', dirname(__FILE__) . '/_files/appconfig.xml');
  242. $this->assertTrue($application->hasOption('foo'));
  243. }
  244. public function testPassingStringPhpConfigPathOptionToConstructorShouldLoadOptions()
  245. {
  246. $application = new Zend_Application('testing', dirname(__FILE__) . '/_files/appconfig.php');
  247. $this->assertTrue($application->hasOption('foo'));
  248. }
  249. public function testPassingStringIncConfigPathOptionToConstructorShouldLoadOptions()
  250. {
  251. $application = new Zend_Application('testing', dirname(__FILE__) . '/_files/appconfig.inc');
  252. $this->assertTrue($application->hasOption('foo'));
  253. }
  254. public function testPassingArrayOptionsWithConfigKeyShouldLoadOptions()
  255. {
  256. $application = new Zend_Application('testing', array('bar' => 'baz', 'config' => dirname(__FILE__) . '/_files/appconfig.inc'));
  257. $this->assertTrue($application->hasOption('foo'));
  258. $this->assertTrue($application->hasOption('bar'));
  259. }
  260. public function testPassingArrayOptionsWithConfigKeyShouldLoadOptionsAndOverride()
  261. {
  262. $application = new Zend_Application('testing', array('foo' => 'baz', 'config' => dirname(__FILE__) . '/_files/appconfig.inc'));
  263. $this->assertEquals('bar', $application->getOption('foo'));
  264. }
  265. /**
  266. * @expectedException Zend_Application_Exception
  267. */
  268. public function testPassingInvalidStringOptionToConstructorShouldRaiseException()
  269. {
  270. $application = new Zend_Application('testing', dirname(__FILE__) . '/_files/appconfig');
  271. }
  272. public function testPassingZendConfigToConstructorShouldLoadOptions()
  273. {
  274. $config = new Zend_Config_Ini(dirname(__FILE__) . '/_files/appconfig.ini', 'testing');
  275. $application = new Zend_Application('testing', $config);
  276. $this->assertTrue($application->hasOption('foo'));
  277. }
  278. public function testPassingArrayOptionsToConstructorShouldLoadOptions()
  279. {
  280. $config = new Zend_Config_Ini(dirname(__FILE__) . '/_files/appconfig.ini', 'testing');
  281. $application = new Zend_Application('testing', $config->toArray());
  282. $this->assertTrue($application->hasOption('foo'));
  283. }
  284. public function testBootstrapImplementsFluentInterface()
  285. {
  286. $application = $this->application->bootstrap();
  287. $this->assertSame($application, $this->application);
  288. }
  289. }
  290. if (PHPUnit_MAIN_METHOD == 'Zend_Application_ApplicationTest::main') {
  291. Zend_Application_ApplicationTest::main();
  292. }