YamlTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. $this->assertInternalType('string', $config->special->zero);
  83. $this->assertEquals('0', $config->special->zero);
  84. }
  85. public function testMultiDepthExtends()
  86. {
  87. $config = new Zend_Config_Yaml($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 testErrorNoExtendsSection()
  97. {
  98. try {
  99. $config = new Zend_Config_Yaml($this->_iniFileConfig, 'extendserror');
  100. $this->fail('An expected Zend_Config_Exception has not been raised');
  101. } catch (Zend_Config_Exception $expected) {
  102. $this->assertContains('cannot be found', $expected->getMessage());
  103. }
  104. }
  105. public function testZF413_MultiSections()
  106. {
  107. $config = new Zend_Config_Yaml($this->_iniFileAllSectionsConfig, array('staging','other_staging'));
  108. $this->assertEquals('otherStaging', $config->only_in);
  109. $this->assertEquals('staging', $config->hostname);
  110. }
  111. public function testZF413_AllSections()
  112. {
  113. $config = new Zend_Config_Yaml($this->_iniFileAllSectionsConfig, null);
  114. $this->assertEquals('otherStaging', $config->other_staging->only_in);
  115. $this->assertEquals('staging', $config->staging->hostname);
  116. }
  117. public function testZF414()
  118. {
  119. $config = new Zend_Config_Yaml($this->_iniFileAllSectionsConfig, null);
  120. $this->assertEquals(null, $config->getSectionName());
  121. $this->assertEquals(true, $config->areAllSectionsLoaded());
  122. $config = new Zend_Config_Yaml($this->_iniFileAllSectionsConfig, 'all');
  123. $this->assertEquals('all', $config->getSectionName());
  124. $this->assertEquals(false, $config->areAllSectionsLoaded());
  125. $config = new Zend_Config_Yaml($this->_iniFileAllSectionsConfig, array('staging','other_staging'));
  126. $this->assertEquals(array('staging','other_staging'), $config->getSectionName());
  127. $this->assertEquals(false, $config->areAllSectionsLoaded());
  128. }
  129. public function testZF415()
  130. {
  131. try {
  132. $config = new Zend_Config_Yaml($this->_iniFileCircularConfig, null);
  133. $this->fail('An expected Zend_Config_Exception has not been raised');
  134. } catch (Zend_Config_Exception $expected) {
  135. $this->assertContains('circular inheritance', $expected->getMessage());
  136. }
  137. }
  138. public function testErrorNoFile()
  139. {
  140. try {
  141. $config = new Zend_Config_Yaml('','');
  142. $this->fail('An expected Zend_Config_Exception has not been raised');
  143. } catch (Zend_Config_Exception $expected) {
  144. $this->assertContains('Filename is not set', $expected->getMessage());
  145. }
  146. }
  147. public function testErrorNoSectionFound()
  148. {
  149. try {
  150. $config = new Zend_Config_Yaml($this->_iniFileConfig,array('all', 'notthere'));
  151. $this->fail('An expected Zend_Config_Exception has not been raised');
  152. } catch (Zend_Config_Exception $expected) {
  153. $this->assertContains('cannot be found', $expected->getMessage());
  154. }
  155. try {
  156. $config = new Zend_Config_Yaml($this->_iniFileConfig,'notthere');
  157. $this->fail('An expected Zend_Config_Exception has not been raised');
  158. } catch (Zend_Config_Exception $expected) {
  159. $this->assertContains('cannot be found', $expected->getMessage());
  160. }
  161. }
  162. public function testZF3196_InvalidIniFile()
  163. {
  164. try {
  165. $config = new Zend_Config_Yaml($this->_iniFileInvalid);
  166. $this->fail('An expected Zend_Config_Exception has not been raised');
  167. } catch (Zend_Config_Exception $expected) {
  168. $this->assertRegexp('/(Error parsing|syntax error, unexpected)/', $expected->getMessage());
  169. }
  170. }
  171. public function testZF2285_MultipleKeysOfTheSameName()
  172. {
  173. $config = new Zend_Config_Yaml($this->_iniFileSameNameKeysConfig, null);
  174. $this->assertEquals('2a', $config->one->two->{0});
  175. $this->assertEquals('2b', $config->one->two->{1});
  176. $this->assertEquals('4', $config->three->four->{1});
  177. $this->assertEquals('5', $config->three->four->{0}->five);
  178. }
  179. public function testZF2437_ArraysWithMultipleChildren()
  180. {
  181. $config = new Zend_Config_Yaml($this->_iniFileSameNameKeysConfig, null);
  182. $this->assertEquals('1', $config->six->seven->{0}->eight);
  183. $this->assertEquals('2', $config->six->seven->{1}->eight);
  184. $this->assertEquals('3', $config->six->seven->{2}->eight);
  185. $this->assertEquals('1', $config->six->seven->{0}->nine);
  186. $this->assertEquals('2', $config->six->seven->{1}->nine);
  187. $this->assertEquals('3', $config->six->seven->{2}->nine);
  188. }
  189. public function yamlDecoder($string)
  190. {
  191. return Zend_Config_Yaml::decode($string);
  192. }
  193. public function testHonorsOptionsProvidedToConstructor()
  194. {
  195. $config = new Zend_Config_Yaml($this->_iniFileAllSectionsConfig, 'debug', array(
  196. 'allow_modifications' => true,
  197. 'skip_extends' => true,
  198. 'yaml_decoder' => array($this, 'yamlDecoder'),
  199. 'foo' => 'bar', // ignored
  200. ));
  201. $this->assertNull($config->name); // verifies extends were skipped
  202. $config->foo = 'bar';
  203. $this->assertEquals('bar', $config->foo); // verifies allows modifications
  204. $this->assertEquals(array($this, 'yamlDecoder'), $config->getYamlDecoder());
  205. }
  206. public function testConstructorRaisesExceptionWhenUnableToLoadFile()
  207. {
  208. $this->setExpectedException('Zend_Config_Exception', 'file_get_contents');
  209. $config = new Zend_Config_Yaml('__foo__');
  210. }
  211. public function testBadIndentationRaisesException()
  212. {
  213. $this->setExpectedException('Zend_Config_Exception', 'unsupported syntax');
  214. $config = new Zend_Config_Yaml($this->_badIndentationConfig, 'all');
  215. }
  216. public function testPassingBadYamlDecoderRaisesException()
  217. {
  218. $this->setExpectedException('Zend_Config_Exception', 'must be callable');
  219. $config = new Zend_Config_Yaml($this->_iniFileAllSectionsConfig, 'debug', array(
  220. 'yaml_decoder' => '__foo__',
  221. ));
  222. }
  223. public function testParsesBooleansAccordingToOneDotOneSpecification()
  224. {
  225. $config = new Zend_Config_Yaml($this->_booleansConfig, 'production');
  226. $this->assertTrue($config->usingLowerCasedYes);
  227. $this->assertTrue($config->usingTitleCasedYes);
  228. $this->assertTrue($config->usingCapitalYes);
  229. $this->assertTrue($config->usingLowerY);
  230. $this->assertTrue($config->usingUpperY);
  231. $this->assertFalse($config->usingLowerCasedNo);
  232. $this->assertFalse($config->usingTitleCasedNo);
  233. $this->assertFalse($config->usingCapitalNo);
  234. $this->assertFalse($config->usingLowerN);
  235. $this->assertFalse($config->usingUpperN);
  236. $this->assertTrue($config->usingLowerCasedTrue);
  237. $this->assertTrue($config->usingTitleCasedTrue);
  238. $this->assertTrue($config->usingCapitalTrue);
  239. $this->assertFalse($config->usingLowerCasedFalse);
  240. $this->assertFalse($config->usingTitleCasedFalse);
  241. $this->assertFalse($config->usingCapitalFalse);
  242. $this->assertTrue($config->usingLowerCasedOn);
  243. $this->assertTrue($config->usingTitleCasedOn);
  244. $this->assertTrue($config->usingCapitalOn);
  245. $this->assertFalse($config->usingLowerCasedOff);
  246. $this->assertFalse($config->usingTitleCasedOff);
  247. $this->assertFalse($config->usingCapitalOff);
  248. }
  249. public function testHonorsPhpConstants()
  250. {
  251. if (!defined('ZEND_CONFIG_YAML_ENV')) {
  252. define('ZEND_CONFIG_YAML_ENV', 'testing');
  253. }
  254. if (!defined('ZEND_CONFIG_YAML_ENV_PATH')) {
  255. define('ZEND_CONFIG_YAML_ENV_PATH', dirname(__FILE__));
  256. }
  257. $config = new Zend_Config_Yaml($this->_constantsConfig, 'production');
  258. $this->assertEquals(ZEND_CONFIG_YAML_ENV, $config->env);
  259. $this->assertEquals(ZEND_CONFIG_YAML_ENV_PATH . '/test/this', $config->path);
  260. }
  261. public function testAllowsIgnoringConstantStrings()
  262. {
  263. if (!defined('ZEND_CONFIG_YAML_ENV')) {
  264. define('ZEND_CONFIG_YAML_ENV', 'testing');
  265. }
  266. if (!defined('ZEND_CONFIG_YAML_ENV_PATH')) {
  267. define('ZEND_CONFIG_YAML_ENV_PATH', dirname(__FILE__));
  268. }
  269. $config = new Zend_Config_Yaml(
  270. $this->_constantsConfig, 'production', array('ignore_constants' => true)
  271. );
  272. $this->assertEquals('ZEND_CONFIG_YAML_ENV', $config->env);
  273. $this->assertEquals('ZEND_CONFIG_YAML_ENV_PATH/test/this', $config->path);
  274. }
  275. }