YamlTest.php 13 KB

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