YamlTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. $this->_yamlInlineCommentsConfig = dirname(__FILE__) . '/_files/inlinecomments.yaml';
  48. $this->_yamlIndentedCommentsConfig = dirname(__FILE__) . '/_files/indentedcomments.yaml';
  49. $this->_yamlListConstantsConfig = dirname(__FILE__) . '/_files/listconstants.yaml';
  50. $this->_listBooleansConfig = dirname(__FILE__) . '/_files/listbooleans.yaml';
  51. $this->_yamlSingleQuotedString = dirname(__FILE__) . '/_files/zf11934.yaml';
  52. }
  53. public function testLoadSingleSection()
  54. {
  55. $config = new Zend_Config_Yaml($this->_iniFileConfig, 'all');
  56. $this->assertEquals('all', $config->hostname);
  57. $this->assertEquals('live', $config->db->name);
  58. $this->assertEquals('multi', $config->one->two->three);
  59. $this->assertNull(@$config->nonexistent); // property doesn't exist
  60. }
  61. public function testSectionInclude()
  62. {
  63. $config = new Zend_Config_Yaml($this->_iniFileConfig, 'staging');
  64. $this->assertEquals('', $config->debug); // only in staging
  65. $this->assertEquals('thisname', $config->name); // only in all
  66. $this->assertEquals('username', $config->db->user); // only in all (nested version)
  67. $this->assertEquals('staging', $config->hostname); // inherited and overridden
  68. $this->assertEquals('dbstaging', $config->db->name); // inherited and overridden
  69. }
  70. public function testTrueValues()
  71. {
  72. $config = new Zend_Config_Yaml($this->_iniFileConfig, 'debug');
  73. $this->assertType('string', $config->debug);
  74. $this->assertEquals('1', $config->debug);
  75. $this->assertType('string', $config->values->changed);
  76. $this->assertEquals('1', $config->values->changed);
  77. }
  78. public function testEmptyValues()
  79. {
  80. $config = new Zend_Config_Yaml($this->_iniFileConfig, 'debug');
  81. $this->assertType('string', $config->special->no);
  82. $this->assertEquals('', $config->special->no);
  83. $this->assertType('string', $config->special->null);
  84. $this->assertEquals('', $config->special->null);
  85. $this->assertType('string', $config->special->false);
  86. $this->assertEquals('', $config->special->false);
  87. $this->assertType('string', $config->special->zero);
  88. $this->assertEquals('0', $config->special->zero);
  89. }
  90. public function testMultiDepthExtends()
  91. {
  92. $config = new Zend_Config_Yaml($this->_iniFileConfig, 'other_staging');
  93. $this->assertEquals('otherStaging', $config->only_in); // only in other_staging
  94. $this->assertEquals('', $config->debug); // 1 level down: only in staging
  95. $this->assertEquals('thisname', $config->name); // 2 levels down: only in all
  96. $this->assertEquals('username', $config->db->user); // 2 levels down: only in all (nested version)
  97. $this->assertEquals('staging', $config->hostname); // inherited from two to one and overridden
  98. $this->assertEquals('dbstaging', $config->db->name); // inherited from two to one and overridden
  99. $this->assertEquals('anotherpwd', $config->db->pass); // inherited from two to other_staging and overridden
  100. }
  101. public function testErrorNoExtendsSection()
  102. {
  103. try {
  104. $config = new Zend_Config_Yaml($this->_iniFileConfig, 'extendserror');
  105. $this->fail('An expected Zend_Config_Exception has not been raised');
  106. } catch (Zend_Config_Exception $expected) {
  107. $this->assertContains('cannot be found', $expected->getMessage());
  108. }
  109. }
  110. public function testZF413_MultiSections()
  111. {
  112. $config = new Zend_Config_Yaml($this->_iniFileAllSectionsConfig, array('staging','other_staging'));
  113. $this->assertEquals('otherStaging', $config->only_in);
  114. $this->assertEquals('staging', $config->hostname);
  115. }
  116. public function testZF413_AllSections()
  117. {
  118. $config = new Zend_Config_Yaml($this->_iniFileAllSectionsConfig, null);
  119. $this->assertEquals('otherStaging', $config->other_staging->only_in);
  120. $this->assertEquals('staging', $config->staging->hostname);
  121. }
  122. public function testZF414()
  123. {
  124. $config = new Zend_Config_Yaml($this->_iniFileAllSectionsConfig, null);
  125. $this->assertEquals(null, $config->getSectionName());
  126. $this->assertEquals(true, $config->areAllSectionsLoaded());
  127. $config = new Zend_Config_Yaml($this->_iniFileAllSectionsConfig, 'all');
  128. $this->assertEquals('all', $config->getSectionName());
  129. $this->assertEquals(false, $config->areAllSectionsLoaded());
  130. $config = new Zend_Config_Yaml($this->_iniFileAllSectionsConfig, array('staging','other_staging'));
  131. $this->assertEquals(array('staging','other_staging'), $config->getSectionName());
  132. $this->assertEquals(false, $config->areAllSectionsLoaded());
  133. }
  134. public function testZF415()
  135. {
  136. try {
  137. $config = new Zend_Config_Yaml($this->_iniFileCircularConfig, null);
  138. $this->fail('An expected Zend_Config_Exception has not been raised');
  139. } catch (Zend_Config_Exception $expected) {
  140. $this->assertContains('circular inheritance', $expected->getMessage());
  141. }
  142. }
  143. public function testErrorNoFile()
  144. {
  145. try {
  146. $config = new Zend_Config_Yaml('','');
  147. $this->fail('An expected Zend_Config_Exception has not been raised');
  148. } catch (Zend_Config_Exception $expected) {
  149. $this->assertContains('Filename is not set', $expected->getMessage());
  150. }
  151. }
  152. public function testErrorNoSectionFound()
  153. {
  154. try {
  155. $config = new Zend_Config_Yaml($this->_iniFileConfig,array('all', 'notthere'));
  156. $this->fail('An expected Zend_Config_Exception has not been raised');
  157. } catch (Zend_Config_Exception $expected) {
  158. $this->assertContains('cannot be found', $expected->getMessage());
  159. }
  160. try {
  161. $config = new Zend_Config_Yaml($this->_iniFileConfig,'notthere');
  162. $this->fail('An expected Zend_Config_Exception has not been raised');
  163. } catch (Zend_Config_Exception $expected) {
  164. $this->assertContains('cannot be found', $expected->getMessage());
  165. }
  166. }
  167. public function testZF3196_InvalidIniFile()
  168. {
  169. try {
  170. $config = new Zend_Config_Yaml($this->_iniFileInvalid);
  171. $this->fail('An expected Zend_Config_Exception has not been raised');
  172. } catch (Zend_Config_Exception $expected) {
  173. $this->assertRegexp('/(Error parsing|syntax error, unexpected)/', $expected->getMessage());
  174. }
  175. }
  176. public function testZF2285_MultipleKeysOfTheSameName()
  177. {
  178. $config = new Zend_Config_Yaml($this->_iniFileSameNameKeysConfig, null);
  179. $this->assertEquals('2a', $config->one->two->{0});
  180. $this->assertEquals('2b', $config->one->two->{1});
  181. $this->assertEquals('4', $config->three->four->{1});
  182. $this->assertEquals('5', $config->three->four->{0}->five);
  183. }
  184. public function testZF2437_ArraysWithMultipleChildren()
  185. {
  186. $config = new Zend_Config_Yaml($this->_iniFileSameNameKeysConfig, null);
  187. $this->assertEquals('1', $config->six->seven->{0}->eight);
  188. $this->assertEquals('2', $config->six->seven->{1}->eight);
  189. $this->assertEquals('3', $config->six->seven->{2}->eight);
  190. $this->assertEquals('1', $config->six->seven->{0}->nine);
  191. $this->assertEquals('2', $config->six->seven->{1}->nine);
  192. $this->assertEquals('3', $config->six->seven->{2}->nine);
  193. }
  194. public function yamlDecoder($string)
  195. {
  196. return Zend_Config_Yaml::decode($string);
  197. }
  198. public function testHonorsOptionsProvidedToConstructor()
  199. {
  200. $config = new Zend_Config_Yaml($this->_iniFileAllSectionsConfig, 'debug', array(
  201. 'allow_modifications' => true,
  202. 'skip_extends' => true,
  203. 'yaml_decoder' => array($this, 'yamlDecoder'),
  204. 'foo' => 'bar', // ignored
  205. ));
  206. $this->assertNull($config->name); // verifies extends were skipped
  207. $config->foo = 'bar';
  208. $this->assertEquals('bar', $config->foo); // verifies allows modifications
  209. $this->assertEquals(array($this, 'yamlDecoder'), $config->getYamlDecoder());
  210. }
  211. public function testConstructorRaisesExceptionWhenUnableToLoadFile()
  212. {
  213. $this->setExpectedException('Zend_Config_Exception', 'file_get_contents');
  214. $config = new Zend_Config_Yaml('__foo__');
  215. }
  216. public function testBadIndentationRaisesException()
  217. {
  218. $this->setExpectedException('Zend_Config_Exception', 'unsupported syntax');
  219. $config = new Zend_Config_Yaml($this->_badIndentationConfig, 'all');
  220. }
  221. public function testPassingBadYamlDecoderRaisesException()
  222. {
  223. $this->setExpectedException('Zend_Config_Exception', 'must be callable');
  224. $config = new Zend_Config_Yaml($this->_iniFileAllSectionsConfig, 'debug', array(
  225. 'yaml_decoder' => '__foo__',
  226. ));
  227. }
  228. public function testParsesBooleansAccordingToOneDotOneSpecification()
  229. {
  230. $config = new Zend_Config_Yaml($this->_booleansConfig, 'production');
  231. $this->assertTrue($config->usingLowerCasedYes);
  232. $this->assertTrue($config->usingTitleCasedYes);
  233. $this->assertTrue($config->usingCapitalYes);
  234. $this->assertTrue($config->usingLowerY);
  235. $this->assertTrue($config->usingUpperY);
  236. $this->assertFalse($config->usingLowerCasedNo);
  237. $this->assertFalse($config->usingTitleCasedNo);
  238. $this->assertFalse($config->usingCapitalNo);
  239. $this->assertFalse($config->usingLowerN);
  240. $this->assertFalse($config->usingUpperN);
  241. $this->assertTrue($config->usingLowerCasedTrue);
  242. $this->assertTrue($config->usingTitleCasedTrue);
  243. $this->assertTrue($config->usingCapitalTrue);
  244. $this->assertFalse($config->usingLowerCasedFalse);
  245. $this->assertFalse($config->usingTitleCasedFalse);
  246. $this->assertFalse($config->usingCapitalFalse);
  247. $this->assertTrue($config->usingLowerCasedOn);
  248. $this->assertTrue($config->usingTitleCasedOn);
  249. $this->assertTrue($config->usingCapitalOn);
  250. $this->assertFalse($config->usingLowerCasedOff);
  251. $this->assertFalse($config->usingTitleCasedOff);
  252. $this->assertFalse($config->usingCapitalOff);
  253. }
  254. /**
  255. * @group ZF-12277
  256. */
  257. public function testParsesTypesAccordingToOneDotTwoSpecification()
  258. {
  259. $config = new Zend_Config_Yaml($this->_booleansConfig, 'production');
  260. $this->assertNull($config->usingLowerCasedNull);
  261. $this->assertNull($config->usingTitleCasedNull);
  262. $this->assertNull($config->usingCapitalNull);
  263. }
  264. public function testHonorsPhpConstants()
  265. {
  266. if (!defined('ZEND_CONFIG_YAML_ENV')) {
  267. define('ZEND_CONFIG_YAML_ENV', 'testing');
  268. }
  269. if (!defined('ZEND_CONFIG_YAML_ENV_PATH')) {
  270. define('ZEND_CONFIG_YAML_ENV_PATH', dirname(__FILE__));
  271. }
  272. $config = new Zend_Config_Yaml($this->_constantsConfig, 'production');
  273. $this->assertEquals(ZEND_CONFIG_YAML_ENV, $config->env);
  274. $this->assertEquals(ZEND_CONFIG_YAML_ENV_PATH . '/test/this', $config->path);
  275. }
  276. public function testAllowsIgnoringConstantStrings()
  277. {
  278. if (!defined('ZEND_CONFIG_YAML_ENV')) {
  279. define('ZEND_CONFIG_YAML_ENV', 'testing');
  280. }
  281. if (!defined('ZEND_CONFIG_YAML_ENV_PATH')) {
  282. define('ZEND_CONFIG_YAML_ENV_PATH', dirname(__FILE__));
  283. }
  284. $config = new Zend_Config_Yaml(
  285. $this->_constantsConfig, 'production', array('ignore_constants' => true)
  286. );
  287. $this->assertEquals('ZEND_CONFIG_YAML_ENV', $config->env);
  288. $this->assertEquals('ZEND_CONFIG_YAML_ENV_PATH/test/this', $config->path);
  289. }
  290. /**
  291. * @group ZF-11329
  292. */
  293. public function testAllowsInlineCommentsInValuesUsingHash()
  294. {
  295. $config = new Zend_Config_Yaml($this->_yamlInlineCommentsConfig, null);
  296. $this->assertType('Zend_Config', $config->resources);
  297. $this->assertType('Zend_Config', $config->resources->frontController);
  298. $this->assertType(
  299. 'string',
  300. $config->resources->frontController->controllerDirectory
  301. );
  302. $this->assertSame(
  303. 'APPLICATION_PATH/controllers',
  304. $config->resources->frontController->controllerDirectory
  305. );
  306. }
  307. /**
  308. * @group ZF-11384
  309. */
  310. public function testAllowsIndentedCommentsUsingHash()
  311. {
  312. $config = new Zend_Config_Yaml($this->_yamlIndentedCommentsConfig, null);
  313. $this->assertType('Zend_Config', $config->resources);
  314. $this->assertType('Zend_Config', $config->resources->frontController);
  315. $this->assertType(
  316. 'string',
  317. $config->resources->frontController->controllerDirectory
  318. );
  319. $this->assertSame(
  320. 'APPLICATION_PATH/controllers',
  321. $config->resources->frontController->controllerDirectory
  322. );
  323. }
  324. /**
  325. * @group ZF-11702
  326. */
  327. public function testAllowsConstantsInLists()
  328. {
  329. if (!defined('ZEND_CONFIG_YAML_TEST_PATH')) {
  330. define('ZEND_CONFIG_YAML_TEST_PATH', 'testing');
  331. }
  332. $config = new Zend_Config_Yaml($this->_yamlListConstantsConfig, 'production');
  333. $this->assertEquals(ZEND_CONFIG_YAML_TEST_PATH, $config->paths->{0});
  334. $this->assertEquals(ZEND_CONFIG_YAML_TEST_PATH . '/library/test', $config->paths->{1});
  335. }
  336. /**
  337. * @group ZF-11702
  338. */
  339. public function testAllowsBooleansInLists()
  340. {
  341. $config = new Zend_Config_Yaml($this->_listBooleansConfig, 'production');
  342. $this->assertTrue($config->usingLowerCasedYes->{0});
  343. $this->assertTrue($config->usingTitleCasedYes->{0});
  344. $this->assertTrue($config->usingCapitalYes->{0});
  345. $this->assertTrue($config->usingLowerY->{0});
  346. $this->assertTrue($config->usingUpperY->{0});
  347. $this->assertFalse($config->usingLowerCasedNo->{0});
  348. $this->assertFalse($config->usingTitleCasedNo->{0});
  349. $this->assertFalse($config->usingCapitalNo->{0});
  350. $this->assertFalse($config->usingLowerN->{0});
  351. $this->assertFalse($config->usingUpperN->{0});
  352. $this->assertTrue($config->usingLowerCasedTrue->{0});
  353. $this->assertTrue($config->usingTitleCasedTrue->{0});
  354. $this->assertTrue($config->usingCapitalTrue->{0});
  355. $this->assertFalse($config->usingLowerCasedFalse->{0});
  356. $this->assertFalse($config->usingTitleCasedFalse->{0});
  357. $this->assertFalse($config->usingCapitalFalse->{0});
  358. $this->assertTrue($config->usingLowerCasedOn->{0});
  359. $this->assertTrue($config->usingTitleCasedOn->{0});
  360. $this->assertTrue($config->usingCapitalOn->{0});
  361. $this->assertFalse($config->usingLowerCasedOff->{0});
  362. $this->assertFalse($config->usingTitleCasedOff->{0});
  363. $this->assertFalse($config->usingCapitalOff->{0});
  364. }
  365. /**
  366. * @group ZF-11934
  367. */
  368. public function testAllowsSingleQuotedStringValues()
  369. {
  370. $config = new Zend_Config_Yaml($this->_yamlSingleQuotedString);
  371. $this->assertEquals('two', $config->one);
  372. }
  373. /**
  374. * @group ZF-11363
  375. */
  376. public function testAllowsDashesInLists()
  377. {
  378. $config = new Zend_Config_Yaml($this->_iniFileSameNameKeysConfig, null);
  379. $this->assertEquals('101112', $config->{'te-n'}->{'ele-ven'}->{0}->{'twel-ve'});
  380. $this->assertEquals('101112', $config->{'te-n'}->{'ele-ven'}->{0}->twelve);
  381. $this->assertEquals('thir-teen', $config->{'te-n'}->{'ele-ven'}->{1});
  382. }
  383. }