JsonTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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-2012 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. * Zend_Config_Json
  24. */
  25. require_once 'Zend/Config/Json.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Config
  29. * @subpackage UnitTests
  30. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Config_JsonTest extends PHPUnit_Framework_TestCase
  34. {
  35. protected $_iniFileConfig;
  36. protected $_iniFileAllSectionsConfig;
  37. protected $_iniFileCircularConfig;
  38. public function setUp()
  39. {
  40. $this->_iniFileConfig = dirname(__FILE__) . '/_files/config.json';
  41. $this->_iniFileAllSectionsConfig = dirname(__FILE__) . '/_files/allsections.json';
  42. $this->_iniFileCircularConfig = dirname(__FILE__) . '/_files/circular.json';
  43. $this->_iniFileMultipleInheritanceConfig = dirname(__FILE__) . '/_files/multipleinheritance.json';
  44. $this->_nonReadableConfig = dirname(__FILE__) . '/_files/nonreadable.json';
  45. $this->_iniFileNoSectionsConfig = dirname(__FILE__) . '/_files/nosections.json';
  46. $this->_iniFileInvalid = dirname(__FILE__) . '/_files/invalid.json';
  47. }
  48. public function testLoadSingleSection()
  49. {
  50. $config = new Zend_Config_Json($this->_iniFileConfig, 'all');
  51. $this->assertEquals('all', $config->hostname);
  52. $this->assertEquals('live', $config->db->name);
  53. $this->assertEquals('multi', $config->one->two->three);
  54. $this->assertNull(@$config->nonexistent); // property doesn't exist
  55. }
  56. public function testSectionInclude()
  57. {
  58. $config = new Zend_Config_Json($this->_iniFileConfig, 'staging');
  59. $this->assertEquals('', $config->debug); // only in staging
  60. $this->assertEquals('thisname', $config->name); // only in all
  61. $this->assertEquals('username', $config->db->user); // only in all (nested version)
  62. $this->assertEquals('dbstaging', $config->db->name); // inherited and overridden
  63. }
  64. public function testTrueValues()
  65. {
  66. $config = new Zend_Config_Json($this->_iniFileConfig, 'debug');
  67. $this->assertTrue($config->debug);
  68. $this->assertTrue($config->values->changed);
  69. }
  70. public function testEmptyValues()
  71. {
  72. $config = new Zend_Config_Json($this->_iniFileConfig, 'debug');
  73. $this->assertType('string', $config->special->no);
  74. $this->assertEquals('no', $config->special->no);
  75. $this->assertNull($config->special->null);
  76. $this->assertFalse($config->special->false);
  77. }
  78. /**
  79. * @group review
  80. */
  81. public function testMultiDepthExtends()
  82. {
  83. $config = new Zend_Config_Json($this->_iniFileConfig, 'other_staging');
  84. $this->assertEquals('otherStaging', $config->only_in); // only in other_staging
  85. $this->assertEquals('', $config->debug); // 1 level down: only in staging
  86. $this->assertEquals('thisname', $config->name); // 2 levels down: only in all
  87. $this->assertEquals('username', $config->db->user); // 2 levels down: only in all (nested version)
  88. $this->assertEquals('staging', $config->hostname); // inherited from two to one and overridden
  89. $this->assertEquals('dbstaging', $config->db->name); // inherited from two to one and overridden
  90. $this->assertEquals('anotherpwd', $config->db->pass); // inherited from two to other_staging and overridden
  91. }
  92. public function testRaisesExceptionWhenSectionNotFound()
  93. {
  94. $this->setExpectedException('Zend_Config_Exception', 'cannot be found');
  95. $config = new Zend_Config_Json($this->_iniFileConfig, 'extendserror');
  96. }
  97. public function testRetrievesAndMergesMultipleSections()
  98. {
  99. $config = new Zend_Config_Json($this->_iniFileAllSectionsConfig, array('staging','other_staging'));
  100. $this->assertEquals('otherStaging', $config->only_in);
  101. $this->assertEquals('dbstaging', $config->db->name);
  102. }
  103. public function testCanRetrieveAllSections()
  104. {
  105. $config = new Zend_Config_Json($this->_iniFileAllSectionsConfig, null);
  106. $this->assertEquals('otherStaging', $config->other_staging->only_in);
  107. $this->assertEquals('dbstaging', $config->staging->db->name);
  108. }
  109. public function testAllowsLoadingAllSectionsOrSomeSectionsSelectively()
  110. {
  111. $config = new Zend_Config_Json($this->_iniFileAllSectionsConfig, null);
  112. $this->assertEquals(null, $config->getSectionName());
  113. $this->assertEquals(true, $config->areAllSectionsLoaded());
  114. $config = new Zend_Config_Json($this->_iniFileAllSectionsConfig, 'all');
  115. $this->assertEquals('all', $config->getSectionName());
  116. $this->assertEquals(false, $config->areAllSectionsLoaded());
  117. $config = new Zend_Config_Json($this->_iniFileAllSectionsConfig, array('staging','other_staging'));
  118. $this->assertEquals(array('staging','other_staging'), $config->getSectionName());
  119. $this->assertEquals(false, $config->areAllSectionsLoaded());
  120. }
  121. public function testDetectsCircularInheritance()
  122. {
  123. $this->setExpectedException('Zend_Config_Exception', 'circular inheritance');
  124. $config = new Zend_Config_Json($this->_iniFileCircularConfig, null);
  125. }
  126. public function testRaisesErrorWhenNoFileProvided()
  127. {
  128. $this->setExpectedException('Zend_Config_Exception', 'not set');
  129. $config = new Zend_Config_Json('','');
  130. }
  131. public function testRaisesErrorOnAttemptsToExtendMultipleSectionsAtOnce()
  132. {
  133. $this->setExpectedException('Zend_Config_Exception', 'Invalid');
  134. $config = new Zend_Config_Json($this->_iniFileMultipleInheritanceConfig, 'multiinherit');
  135. }
  136. public function testRaisesErrorWhenSectionNotFound()
  137. {
  138. try {
  139. $config = new Zend_Config_Json($this->_iniFileConfig,array('all', 'notthere'));
  140. $this->fail('An expected Zend_Config_Exception has not been raised');
  141. } catch (Zend_Config_Exception $expected) {
  142. $this->assertContains('cannot be found', $expected->getMessage());
  143. }
  144. try {
  145. $config = new Zend_Config_Json($this->_iniFileConfig,'notthere');
  146. $this->fail('An expected Zend_Config_Exception has not been raised');
  147. } catch (Zend_Config_Exception $expected) {
  148. $this->assertContains('cannot be found', $expected->getMessage());
  149. }
  150. }
  151. public function testCanLoadConfigWithNoSections()
  152. {
  153. $config = new Zend_Config_Json($this->_iniFileNoSectionsConfig);
  154. $this->assertEquals('all', $config->hostname);
  155. $this->assertEquals('two', $config->one->two);
  156. $this->assertEquals('4', $config->one->three->four);
  157. $this->assertEquals('5', $config->one->three->five);
  158. }
  159. public function testRaisesExceptionOnInvalidJsonMarkup()
  160. {
  161. if (version_compare(PHP_VERSION, '5.3.0', '<')) {
  162. $this->setExpectedException('Zend_Config_Exception', 'Error parsing JSON data');
  163. } else {
  164. $this->setExpectedException('Zend_Json_Exception', 'Syntax');
  165. }
  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. if (version_compare(PHP_VERSION, '5.3.0', '<')) {
  245. $this->setExpectedException('Zend_Config_Exception', 'Error parsing JSON data');
  246. } else {
  247. $this->setExpectedException('Zend_Json_Exception', 'Syntax');
  248. }
  249. $config = new Zend_Config_Json($json, null, array('ignore_constants' => true));
  250. }
  251. }