JsonTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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-2015 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-2015 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->assertTrue(is_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. $this->setExpectedException('Zend_Config_Exception', 'Error parsing JSON data');
  162. $config = new Zend_Config_Json($this->_iniFileInvalid);
  163. }
  164. public function testOptionsPassedAreHonored()
  165. {
  166. $config = new Zend_Config_Json($this->_iniFileConfig, 'staging', array(
  167. 'skipExtends' => true,
  168. 'allowModifications' => true,
  169. 'bar' => 'foo', // ignored
  170. ));
  171. $this->assertNull($config->name); // demonstrates extends were skipped
  172. $config->foo = 'bar';
  173. $this->assertEquals('bar', $config->foo); // demonstrates modifications were made
  174. }
  175. public function testZf2StyleOptionsAreHonored()
  176. {
  177. $config = new Zend_Config_Json($this->_iniFileConfig, 'staging', array(
  178. 'skip_extends' => true,
  179. 'allow_modifications' => true,
  180. 'bar' => 'foo', // ignored
  181. ));
  182. $this->assertNull($config->name); // demonstrates extends were skipped
  183. $config->foo = 'bar';
  184. $this->assertEquals('bar', $config->foo); // demonstrates modifications were made
  185. }
  186. public function testAllowsPassingJsonStringsToConstructor()
  187. {
  188. $json =<<<EOJ
  189. {"all":{"foo":"bar"},"staging":{"_extends":"all","bar":"baz"},"debug":{"_extends":"all","debug":true}}
  190. EOJ;
  191. $config = new Zend_Config_Json($json, 'debug');
  192. $this->assertTrue($config->debug);
  193. $this->assertEquals('bar', $config->foo);
  194. $this->assertNull($config->bar);
  195. }
  196. public function testProcessesSectionsWithSingleValues()
  197. {
  198. $json = '{"all":"values"}';
  199. $config = new Zend_Config_Json($json, 'all');
  200. $this->assertEquals('values', $config->all);
  201. }
  202. public function testReplacesConstantNamesWithValuesByDefault()
  203. {
  204. if (!defined('ZEND_CONFIG_JSON_ENV')) {
  205. define('ZEND_CONFIG_JSON_ENV', 'testing');
  206. }
  207. if (!defined('ZEND_CONFIG_JSON_ENV_PATH')) {
  208. define('ZEND_CONFIG_JSON_ENV_PATH', dirname(__FILE__));
  209. }
  210. if (!defined('ZEND_CONFIG_JSON_ENV_INT')) {
  211. define('ZEND_CONFIG_JSON_ENV_INT', 42);
  212. }
  213. $json = '{"env":"ZEND_CONFIG_JSON_ENV","path":"ZEND_CONFIG_JSON_ENV_PATH/tests","int":ZEND_CONFIG_JSON_ENV_INT}';
  214. $config = new Zend_Config_Json($json);
  215. $this->assertEquals(ZEND_CONFIG_JSON_ENV, $config->env);
  216. $this->assertEquals(ZEND_CONFIG_JSON_ENV_PATH . '/tests', $config->path);
  217. $this->assertEquals(ZEND_CONFIG_JSON_ENV_INT, $config->int);
  218. }
  219. public function testCanIgnoreConstantsWhenParsing()
  220. {
  221. if (!defined('ZEND_CONFIG_JSON_ENV')) {
  222. define('ZEND_CONFIG_JSON_ENV', 'testing');
  223. }
  224. $json = '{"env":"ZEND_CONFIG_JSON_ENV"}';
  225. $config = new Zend_Config_Json($json, null, array('ignore_constants' => true));
  226. $this->assertEquals('ZEND_CONFIG_JSON_ENV', $config->env);
  227. }
  228. public function testIgnoringConstantsCanLeadToParseErrors()
  229. {
  230. if (!defined('ZEND_CONFIG_JSON_ENV')) {
  231. define('ZEND_CONFIG_JSON_ENV', 'testing');
  232. }
  233. if (!defined('ZEND_CONFIG_JSON_ENV_PATH')) {
  234. define('ZEND_CONFIG_JSON_ENV_PATH', dirname(__FILE__));
  235. }
  236. if (!defined('ZEND_CONFIG_JSON_ENV_INT')) {
  237. define('ZEND_CONFIG_JSON_ENV_INT', 42);
  238. }
  239. $json = '{"env":"ZEND_CONFIG_JSON_ENV","path":"ZEND_CONFIG_JSON_ENV_PATH/tests","int":ZEND_CONFIG_JSON_ENV_INT}';
  240. $this->setExpectedException('Zend_Config_Exception', 'Error parsing JSON data');
  241. $config = new Zend_Config_Json($json, null, array('ignore_constants' => true));
  242. }
  243. }