YamlTest.php 16 KB

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