JsonTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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_Config
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 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. /**
  23. * Test helper
  24. */
  25. require_once dirname(__FILE__) . '/../../TestHelper.php';
  26. /**
  27. * Zend_Config_Json
  28. */
  29. require_once 'Zend/Config/Json.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Config
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class Zend_Config_JsonTest extends PHPUnit_Framework_TestCase
  38. {
  39. protected $_iniFileConfig;
  40. protected $_iniFileAllSectionsConfig;
  41. protected $_iniFileCircularConfig;
  42. public function setUp()
  43. {
  44. $this->_iniFileConfig = dirname(__FILE__) . '/_files/config.json';
  45. $this->_iniFileAllSectionsConfig = dirname(__FILE__) . '/_files/allsections.json';
  46. $this->_iniFileCircularConfig = dirname(__FILE__) . '/_files/circular.json';
  47. $this->_iniFileMultipleInheritanceConfig = dirname(__FILE__) . '/_files/multipleinheritance.json';
  48. $this->_nonReadableConfig = dirname(__FILE__) . '/_files/nonreadable.json';
  49. $this->_iniFileNoSectionsConfig = dirname(__FILE__) . '/_files/nosections.json';
  50. $this->_iniFileInvalid = dirname(__FILE__) . '/_files/invalid.json';
  51. }
  52. public function testLoadSingleSection()
  53. {
  54. $config = new Zend_Config_Json($this->_iniFileConfig, 'all');
  55. $this->assertEquals('all', $config->hostname);
  56. $this->assertEquals('live', $config->db->name);
  57. $this->assertEquals('multi', $config->one->two->three);
  58. $this->assertNull(@$config->nonexistent); // property doesn't exist
  59. }
  60. public function testSectionInclude()
  61. {
  62. $config = new Zend_Config_Json($this->_iniFileConfig, 'staging');
  63. $this->assertEquals('', $config->debug); // only in staging
  64. $this->assertEquals('thisname', $config->name); // only in all
  65. $this->assertEquals('username', $config->db->user); // only in all (nested version)
  66. $this->assertEquals('dbstaging', $config->db->name); // inherited and overridden
  67. }
  68. public function testTrueValues()
  69. {
  70. $config = new Zend_Config_Json($this->_iniFileConfig, 'debug');
  71. $this->assertTrue($config->debug);
  72. $this->assertTrue($config->values->changed);
  73. }
  74. public function testEmptyValues()
  75. {
  76. $config = new Zend_Config_Json($this->_iniFileConfig, 'debug');
  77. $this->assertType('string', $config->special->no);
  78. $this->assertEquals('no', $config->special->no);
  79. $this->assertNull($config->special->null);
  80. $this->assertFalse($config->special->false);
  81. }
  82. /**
  83. * @group review
  84. */
  85. public function testMultiDepthExtends()
  86. {
  87. $config = new Zend_Config_Json($this->_iniFileConfig, 'other_staging');
  88. $this->assertEquals('otherStaging', $config->only_in); // only in other_staging
  89. $this->assertEquals('', $config->debug); // 1 level down: only in staging
  90. $this->assertEquals('thisname', $config->name); // 2 levels down: only in all
  91. $this->assertEquals('username', $config->db->user); // 2 levels down: only in all (nested version)
  92. $this->assertEquals('staging', $config->hostname); // inherited from two to one and overridden
  93. $this->assertEquals('dbstaging', $config->db->name); // inherited from two to one and overridden
  94. $this->assertEquals('anotherpwd', $config->db->pass); // inherited from two to other_staging and overridden
  95. }
  96. public function testRaisesExceptionWhenSectionNotFound()
  97. {
  98. $this->setExpectedException('Zend_Config_Exception', 'cannot be found');
  99. $config = new Zend_Config_Json($this->_iniFileConfig, 'extendserror');
  100. }
  101. public function testRetrievesAndMergesMultipleSections()
  102. {
  103. $config = new Zend_Config_Json($this->_iniFileAllSectionsConfig, array('staging','other_staging'));
  104. $this->assertEquals('otherStaging', $config->only_in);
  105. $this->assertEquals('dbstaging', $config->db->name);
  106. }
  107. public function testCanRetrieveAllSections()
  108. {
  109. $config = new Zend_Config_Json($this->_iniFileAllSectionsConfig, null);
  110. $this->assertEquals('otherStaging', $config->other_staging->only_in);
  111. $this->assertEquals('dbstaging', $config->staging->db->name);
  112. }
  113. public function testAllowsLoadingAllSectionsOrSomeSectionsSelectively()
  114. {
  115. $config = new Zend_Config_Json($this->_iniFileAllSectionsConfig, null);
  116. $this->assertEquals(null, $config->getSectionName());
  117. $this->assertEquals(true, $config->areAllSectionsLoaded());
  118. $config = new Zend_Config_Json($this->_iniFileAllSectionsConfig, 'all');
  119. $this->assertEquals('all', $config->getSectionName());
  120. $this->assertEquals(false, $config->areAllSectionsLoaded());
  121. $config = new Zend_Config_Json($this->_iniFileAllSectionsConfig, array('staging','other_staging'));
  122. $this->assertEquals(array('staging','other_staging'), $config->getSectionName());
  123. $this->assertEquals(false, $config->areAllSectionsLoaded());
  124. }
  125. public function testDetectsCircularInheritance()
  126. {
  127. $this->setExpectedException('Zend_Config_Exception', 'circular inheritance');
  128. $config = new Zend_Config_Json($this->_iniFileCircularConfig, null);
  129. }
  130. public function testRaisesErrorWhenNoFileProvided()
  131. {
  132. $this->setExpectedException('Zend_Config_Exception', 'not set');
  133. $config = new Zend_Config_Json('','');
  134. }
  135. public function testRaisesErrorOnAttemptsToExtendMultipleSectionsAtOnce()
  136. {
  137. $this->setExpectedException('Zend_Config_Exception', 'Invalid');
  138. $config = new Zend_Config_Json($this->_iniFileMultipleInheritanceConfig, 'multiinherit');
  139. }
  140. public function testRaisesErrorWhenSectionNotFound()
  141. {
  142. try {
  143. $config = new Zend_Config_Json($this->_iniFileConfig,array('all', 'notthere'));
  144. $this->fail('An expected Zend_Config_Exception has not been raised');
  145. } catch (Zend_Config_Exception $expected) {
  146. $this->assertContains('cannot be found', $expected->getMessage());
  147. }
  148. try {
  149. $config = new Zend_Config_Json($this->_iniFileConfig,'notthere');
  150. $this->fail('An expected Zend_Config_Exception has not been raised');
  151. } catch (Zend_Config_Exception $expected) {
  152. $this->assertContains('cannot be found', $expected->getMessage());
  153. }
  154. }
  155. public function testCanLoadConfigWithNoSections()
  156. {
  157. $config = new Zend_Config_Json($this->_iniFileNoSectionsConfig);
  158. $this->assertEquals('all', $config->hostname);
  159. $this->assertEquals('two', $config->one->two);
  160. $this->assertEquals('4', $config->one->three->four);
  161. $this->assertEquals('5', $config->one->three->five);
  162. }
  163. public function testRaisesExceptionOnInvalidJsonMarkup()
  164. {
  165. $this->setExpectedException('Zend_Json_Exception', 'Syntax error');
  166. $config = new Zend_Config_Json($this->_iniFileInvalid);
  167. }
  168. public function testOptionsPassedAreHonored()
  169. {
  170. $config = new Zend_Config_Json($this->_iniFileConfig, 'staging', array(
  171. 'skipExtends' => true,
  172. 'allowModifications' => true,
  173. 'bar' => 'foo', // ignored
  174. ));
  175. $this->assertNull($config->name); // demonstrates extends were skipped
  176. $config->foo = 'bar';
  177. $this->assertEquals('bar', $config->foo); // demonstrates modifications were made
  178. }
  179. public function testZf2StyleOptionsAreHonored()
  180. {
  181. $config = new Zend_Config_Json($this->_iniFileConfig, 'staging', array(
  182. 'skip_extends' => true,
  183. 'allow_modifications' => true,
  184. 'bar' => 'foo', // ignored
  185. ));
  186. $this->assertNull($config->name); // demonstrates extends were skipped
  187. $config->foo = 'bar';
  188. $this->assertEquals('bar', $config->foo); // demonstrates modifications were made
  189. }
  190. public function testAllowsPassingJsonStringsToConstructor()
  191. {
  192. $json =<<<EOJ
  193. {"all":{"foo":"bar"},"staging":{"_extends":"all","bar":"baz"},"debug":{"_extends":"all","debug":true}}
  194. EOJ;
  195. $config = new Zend_Config_Json($json, 'debug');
  196. $this->assertTrue($config->debug);
  197. $this->assertEquals('bar', $config->foo);
  198. $this->assertNull($config->bar);
  199. }
  200. public function testProcessesSectionsWithSingleValues()
  201. {
  202. $json = '{"all":"values"}';
  203. $config = new Zend_Config_Json($json, 'all');
  204. $this->assertEquals('values', $config->all);
  205. }
  206. public function testReplacesConstantNamesWithValuesByDefault()
  207. {
  208. if (!defined('ZEND_CONFIG_JSON_ENV')) {
  209. define('ZEND_CONFIG_JSON_ENV', 'testing');
  210. }
  211. if (!defined('ZEND_CONFIG_JSON_ENV_PATH')) {
  212. define('ZEND_CONFIG_JSON_ENV_PATH', dirname(__FILE__));
  213. }
  214. if (!defined('ZEND_CONFIG_JSON_ENV_INT')) {
  215. define('ZEND_CONFIG_JSON_ENV_INT', 42);
  216. }
  217. $json = '{"env":"ZEND_CONFIG_JSON_ENV","path":"ZEND_CONFIG_JSON_ENV_PATH/tests","int":ZEND_CONFIG_JSON_ENV_INT}';
  218. $config = new Zend_Config_Json($json);
  219. $this->assertEquals(ZEND_CONFIG_JSON_ENV, $config->env);
  220. $this->assertEquals(ZEND_CONFIG_JSON_ENV_PATH . '/tests', $config->path);
  221. $this->assertEquals(ZEND_CONFIG_JSON_ENV_INT, $config->int);
  222. }
  223. public function testCanIgnoreConstantsWhenParsing()
  224. {
  225. if (!defined('ZEND_CONFIG_JSON_ENV')) {
  226. define('ZEND_CONFIG_JSON_ENV', 'testing');
  227. }
  228. $json = '{"env":"ZEND_CONFIG_JSON_ENV"}';
  229. $config = new Zend_Config_Json($json, null, array('ignore_constants' => true));
  230. $this->assertEquals('ZEND_CONFIG_JSON_ENV', $config->env);
  231. }
  232. public function testIgnoringConstantsCanLeadToParseErrors()
  233. {
  234. if (!defined('ZEND_CONFIG_JSON_ENV')) {
  235. define('ZEND_CONFIG_JSON_ENV', 'testing');
  236. }
  237. if (!defined('ZEND_CONFIG_JSON_ENV_PATH')) {
  238. define('ZEND_CONFIG_JSON_ENV_PATH', dirname(__FILE__));
  239. }
  240. if (!defined('ZEND_CONFIG_JSON_ENV_INT')) {
  241. define('ZEND_CONFIG_JSON_ENV_INT', 42);
  242. }
  243. $json = '{"env":"ZEND_CONFIG_JSON_ENV","path":"ZEND_CONFIG_JSON_ENV_PATH/tests","int":ZEND_CONFIG_JSON_ENV_INT}';
  244. $this->setExpectedException('Zend_Json_Exception', 'Syntax');
  245. $config = new Zend_Config_Json($json, null, array('ignore_constants' => true));
  246. }
  247. }