YamlTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: IniTest.php 18950 2009-11-12 15:37:56Z alexander $
  21. */
  22. /**
  23. * Test helper
  24. */
  25. require_once dirname(__FILE__) . '/../../TestHelper.php';
  26. /**
  27. * Zend_Config_Ini
  28. */
  29. require_once 'Zend/Config/Yaml.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Config
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Config
  37. */
  38. class Zend_Config_YamlTest extends PHPUnit_Framework_TestCase
  39. {
  40. public function setUp()
  41. {
  42. $this->_iniFileConfig = dirname(__FILE__) . '/_files/config.yaml';
  43. $this->_iniFileAllSectionsConfig = dirname(__FILE__) . '/_files/allsections.yaml';
  44. $this->_iniFileCircularConfig = dirname(__FILE__) . '/_files/circular.yaml';
  45. $this->_nonReadableConfig = dirname(__FILE__) . '/_files/nonreadable.yaml';
  46. $this->_iniFileInvalid = dirname(__FILE__) . '/_files/invalid.yaml';
  47. $this->_iniFileSameNameKeysConfig = dirname(__FILE__) . '/_files/array.yaml';
  48. $this->_badIndentationConfig = dirname(__FILE__) . '/_files/badindentation.yaml';
  49. $this->_booleansConfig = dirname(__FILE__) . '/_files/booleans.yaml';
  50. $this->_constantsConfig = dirname(__FILE__) . '/_files/constants.yaml';
  51. }
  52. public function testLoadSingleSection()
  53. {
  54. $config = new Zend_Config_Yaml($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_Yaml($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('staging', $config->hostname); // inherited and overridden
  67. $this->assertEquals('dbstaging', $config->db->name); // inherited and overridden
  68. }
  69. public function testTrueValues()
  70. {
  71. $config = new Zend_Config_Yaml($this->_iniFileConfig, 'debug');
  72. $this->assertType('string', $config->debug);
  73. $this->assertEquals('1', $config->debug);
  74. $this->assertType('string', $config->values->changed);
  75. $this->assertEquals('1', $config->values->changed);
  76. }
  77. public function testEmptyValues()
  78. {
  79. $config = new Zend_Config_Yaml($this->_iniFileConfig, 'debug');
  80. $this->assertType('string', $config->special->no);
  81. $this->assertEquals('', $config->special->no);
  82. $this->assertType('string', $config->special->null);
  83. $this->assertEquals('', $config->special->null);
  84. $this->assertType('string', $config->special->false);
  85. $this->assertEquals('', $config->special->false);
  86. }
  87. public function testMultiDepthExtends()
  88. {
  89. $config = new Zend_Config_Yaml($this->_iniFileConfig, 'other_staging');
  90. $this->assertEquals('otherStaging', $config->only_in); // only in other_staging
  91. $this->assertEquals('', $config->debug); // 1 level down: only in staging
  92. $this->assertEquals('thisname', $config->name); // 2 levels down: only in all
  93. $this->assertEquals('username', $config->db->user); // 2 levels down: only in all (nested version)
  94. $this->assertEquals('staging', $config->hostname); // inherited from two to one and overridden
  95. $this->assertEquals('dbstaging', $config->db->name); // inherited from two to one and overridden
  96. $this->assertEquals('anotherpwd', $config->db->pass); // inherited from two to other_staging and overridden
  97. }
  98. public function testErrorNoExtendsSection()
  99. {
  100. try {
  101. $config = new Zend_Config_Yaml($this->_iniFileConfig, 'extendserror');
  102. $this->fail('An expected Zend_Config_Exception has not been raised');
  103. } catch (Zend_Config_Exception $expected) {
  104. $this->assertContains('cannot be found', $expected->getMessage());
  105. }
  106. }
  107. public function testZF413_MultiSections()
  108. {
  109. $config = new Zend_Config_Yaml($this->_iniFileAllSectionsConfig, array('staging','other_staging'));
  110. $this->assertEquals('otherStaging', $config->only_in);
  111. $this->assertEquals('staging', $config->hostname);
  112. }
  113. public function testZF413_AllSections()
  114. {
  115. $config = new Zend_Config_Yaml($this->_iniFileAllSectionsConfig, null);
  116. $this->assertEquals('otherStaging', $config->other_staging->only_in);
  117. $this->assertEquals('staging', $config->staging->hostname);
  118. }
  119. public function testZF414()
  120. {
  121. $config = new Zend_Config_Yaml($this->_iniFileAllSectionsConfig, null);
  122. $this->assertEquals(null, $config->getSectionName());
  123. $this->assertEquals(true, $config->areAllSectionsLoaded());
  124. $config = new Zend_Config_Yaml($this->_iniFileAllSectionsConfig, 'all');
  125. $this->assertEquals('all', $config->getSectionName());
  126. $this->assertEquals(false, $config->areAllSectionsLoaded());
  127. $config = new Zend_Config_Yaml($this->_iniFileAllSectionsConfig, array('staging','other_staging'));
  128. $this->assertEquals(array('staging','other_staging'), $config->getSectionName());
  129. $this->assertEquals(false, $config->areAllSectionsLoaded());
  130. }
  131. public function testZF415()
  132. {
  133. try {
  134. $config = new Zend_Config_Yaml($this->_iniFileCircularConfig, null);
  135. $this->fail('An expected Zend_Config_Exception has not been raised');
  136. } catch (Zend_Config_Exception $expected) {
  137. $this->assertContains('circular inheritance', $expected->getMessage());
  138. }
  139. }
  140. public function testErrorNoFile()
  141. {
  142. try {
  143. $config = new Zend_Config_Yaml('','');
  144. $this->fail('An expected Zend_Config_Exception has not been raised');
  145. } catch (Zend_Config_Exception $expected) {
  146. $this->assertContains('Filename is not set', $expected->getMessage());
  147. }
  148. }
  149. public function testErrorNoSectionFound()
  150. {
  151. try {
  152. $config = new Zend_Config_Yaml($this->_iniFileConfig,array('all', 'notthere'));
  153. $this->fail('An expected Zend_Config_Exception has not been raised');
  154. } catch (Zend_Config_Exception $expected) {
  155. $this->assertContains('cannot be found', $expected->getMessage());
  156. }
  157. try {
  158. $config = new Zend_Config_Yaml($this->_iniFileConfig,'notthere');
  159. $this->fail('An expected Zend_Config_Exception has not been raised');
  160. } catch (Zend_Config_Exception $expected) {
  161. $this->assertContains('cannot be found', $expected->getMessage());
  162. }
  163. }
  164. public function testZF3196_InvalidIniFile()
  165. {
  166. try {
  167. $config = new Zend_Config_Yaml($this->_iniFileInvalid);
  168. $this->fail('An expected Zend_Config_Exception has not been raised');
  169. } catch (Zend_Config_Exception $expected) {
  170. $this->assertRegexp('/(Error parsing|syntax error, unexpected)/', $expected->getMessage());
  171. }
  172. }
  173. public function testZF2285_MultipleKeysOfTheSameName()
  174. {
  175. $config = new Zend_Config_Yaml($this->_iniFileSameNameKeysConfig, null);
  176. $this->assertEquals('2a', $config->one->two->{0});
  177. $this->assertEquals('2b', $config->one->two->{1});
  178. $this->assertEquals('4', $config->three->four->{1});
  179. $this->assertEquals('5', $config->three->four->{0}->five);
  180. }
  181. public function testZF2437_ArraysWithMultipleChildren()
  182. {
  183. $config = new Zend_Config_Yaml($this->_iniFileSameNameKeysConfig, null);
  184. $this->assertEquals('1', $config->six->seven->{0}->eight);
  185. $this->assertEquals('2', $config->six->seven->{1}->eight);
  186. $this->assertEquals('3', $config->six->seven->{2}->eight);
  187. $this->assertEquals('1', $config->six->seven->{0}->nine);
  188. $this->assertEquals('2', $config->six->seven->{1}->nine);
  189. $this->assertEquals('3', $config->six->seven->{2}->nine);
  190. }
  191. public function yamlDecoder($string)
  192. {
  193. return Zend_Config_Yaml::decode($string);
  194. }
  195. public function testHonorsOptionsProvidedToConstructor()
  196. {
  197. $config = new Zend_Config_Yaml($this->_iniFileAllSectionsConfig, 'debug', array(
  198. 'allow_modifications' => true,
  199. 'skip_extends' => true,
  200. 'yaml_decoder' => array($this, 'yamlDecoder'),
  201. 'foo' => 'bar', // ignored
  202. ));
  203. $this->assertNull($config->name); // verifies extends were skipped
  204. $config->foo = 'bar';
  205. $this->assertEquals('bar', $config->foo); // verifies allows modifications
  206. $this->assertEquals(array($this, 'yamlDecoder'), $config->getYamlDecoder());
  207. }
  208. public function testConstructorRaisesExceptionWhenUnableToLoadFile()
  209. {
  210. $this->setExpectedException('Zend_Config_Exception', 'file_get_contents');
  211. $config = new Zend_Config_Yaml('__foo__');
  212. }
  213. public function testBadIndentationRaisesException()
  214. {
  215. $this->setExpectedException('Zend_Config_Exception', 'unsupported syntax');
  216. $config = new Zend_Config_Yaml($this->_badIndentationConfig, 'all');
  217. }
  218. public function testPassingBadYamlDecoderRaisesException()
  219. {
  220. $this->setExpectedException('Zend_Config_Exception', 'must be callable');
  221. $config = new Zend_Config_Yaml($this->_iniFileAllSectionsConfig, 'debug', array(
  222. 'yaml_decoder' => '__foo__',
  223. ));
  224. }
  225. public function testParsesBooleansAccordingToOneDotOneSpecification()
  226. {
  227. $config = new Zend_Config_Yaml($this->_booleansConfig, 'production');
  228. $this->assertTrue($config->usingLowerCasedYes);
  229. $this->assertTrue($config->usingTitleCasedYes);
  230. $this->assertTrue($config->usingCapitalYes);
  231. $this->assertTrue($config->usingLowerY);
  232. $this->assertTrue($config->usingUpperY);
  233. $this->assertFalse($config->usingLowerCasedNo);
  234. $this->assertFalse($config->usingTitleCasedNo);
  235. $this->assertFalse($config->usingCapitalNo);
  236. $this->assertFalse($config->usingLowerN);
  237. $this->assertFalse($config->usingUpperN);
  238. $this->assertTrue($config->usingLowerCasedTrue);
  239. $this->assertTrue($config->usingTitleCasedTrue);
  240. $this->assertTrue($config->usingCapitalTrue);
  241. $this->assertFalse($config->usingLowerCasedFalse);
  242. $this->assertFalse($config->usingTitleCasedFalse);
  243. $this->assertFalse($config->usingCapitalFalse);
  244. $this->assertTrue($config->usingLowerCasedOn);
  245. $this->assertTrue($config->usingTitleCasedOn);
  246. $this->assertTrue($config->usingCapitalOn);
  247. $this->assertFalse($config->usingLowerCasedOff);
  248. $this->assertFalse($config->usingTitleCasedOff);
  249. $this->assertFalse($config->usingCapitalOff);
  250. }
  251. public function testHonorsPhpConstants()
  252. {
  253. if (!defined('ZEND_CONFIG_YAML_ENV')) {
  254. define('ZEND_CONFIG_YAML_ENV', 'testing');
  255. }
  256. if (!defined('ZEND_CONFIG_YAML_ENV_PATH')) {
  257. define('ZEND_CONFIG_YAML_ENV_PATH', dirname(__FILE__));
  258. }
  259. $config = new Zend_Config_Yaml($this->_constantsConfig, 'production');
  260. $this->assertEquals(ZEND_CONFIG_YAML_ENV, $config->env);
  261. $this->assertEquals(ZEND_CONFIG_YAML_ENV_PATH . '/test/this', $config->path);
  262. }
  263. public function testAllowsIgnoringConstantStrings()
  264. {
  265. if (!defined('ZEND_CONFIG_YAML_ENV')) {
  266. define('ZEND_CONFIG_YAML_ENV', 'testing');
  267. }
  268. if (!defined('ZEND_CONFIG_YAML_ENV_PATH')) {
  269. define('ZEND_CONFIG_YAML_ENV_PATH', dirname(__FILE__));
  270. }
  271. $config = new Zend_Config_Yaml(
  272. $this->_constantsConfig, 'production', array('ignore_constants' => true)
  273. );
  274. $this->assertEquals('ZEND_CONFIG_YAML_ENV', $config->env);
  275. $this->assertEquals('ZEND_CONFIG_YAML_ENV_PATH/test/this', $config->path);
  276. }
  277. }