InlineTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Yaml\Tests;
  11. use Symfony\Component\Yaml\Inline;
  12. class InlineTest extends \PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * @dataProvider getTestsForParse
  16. */
  17. public function testParse($yaml, $value)
  18. {
  19. $this->assertSame($value, Inline::parse($yaml), sprintf('::parse() converts an inline YAML to a PHP structure (%s)', $yaml));
  20. }
  21. /**
  22. * @dataProvider getTestsForParseWithMapObjects
  23. */
  24. public function testParseWithMapObjects($yaml, $value)
  25. {
  26. $actual = Inline::parse($yaml, false, false, true);
  27. $this->assertSame(serialize($value), serialize($actual));
  28. }
  29. /**
  30. * @dataProvider getTestsForDump
  31. */
  32. public function testDump($yaml, $value)
  33. {
  34. $this->assertEquals($yaml, Inline::dump($value), sprintf('::dump() converts a PHP structure to an inline YAML (%s)', $yaml));
  35. $this->assertSame($value, Inline::parse(Inline::dump($value)), 'check consistency');
  36. }
  37. public function testDumpNumericValueWithLocale()
  38. {
  39. $locale = setlocale(LC_NUMERIC, 0);
  40. if (false === $locale) {
  41. $this->markTestSkipped('Your platform does not support locales.');
  42. }
  43. try {
  44. $requiredLocales = array('fr_FR.UTF-8', 'fr_FR.UTF8', 'fr_FR.utf-8', 'fr_FR.utf8', 'French_France.1252');
  45. if (false === setlocale(LC_NUMERIC, $requiredLocales)) {
  46. $this->markTestSkipped('Could not set any of required locales: '.implode(', ', $requiredLocales));
  47. }
  48. $this->assertEquals('1.2', Inline::dump(1.2));
  49. $this->assertContains('fr', strtolower(setlocale(LC_NUMERIC, 0)));
  50. setlocale(LC_NUMERIC, $locale);
  51. } catch (\Exception $e) {
  52. setlocale(LC_NUMERIC, $locale);
  53. throw $e;
  54. }
  55. }
  56. public function testHashStringsResemblingExponentialNumericsShouldNotBeChangedToINF()
  57. {
  58. $value = '686e444';
  59. $this->assertSame($value, Inline::parse(Inline::dump($value)));
  60. }
  61. /**
  62. * @group legacy
  63. * throws \Symfony\Component\Yaml\Exception\ParseException in 3.0
  64. */
  65. public function testParseScalarWithNonEscapedBlackslashShouldThrowException()
  66. {
  67. $this->assertSame('Foo\Var', Inline::parse('"Foo\Var"'));
  68. }
  69. /**
  70. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  71. */
  72. public function testParseScalarWithNonEscapedBlackslashAtTheEndShouldThrowException()
  73. {
  74. Inline::parse('"Foo\\"');
  75. }
  76. /**
  77. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  78. */
  79. public function testParseScalarWithIncorrectlyQuotedStringShouldThrowException()
  80. {
  81. $value = "'don't do somthin' like that'";
  82. Inline::parse($value);
  83. }
  84. /**
  85. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  86. */
  87. public function testParseScalarWithIncorrectlyDoubleQuotedStringShouldThrowException()
  88. {
  89. $value = '"don"t do somthin" like that"';
  90. Inline::parse($value);
  91. }
  92. /**
  93. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  94. */
  95. public function testParseInvalidMappingKeyShouldThrowException()
  96. {
  97. $value = '{ "foo " bar": "bar" }';
  98. Inline::parse($value);
  99. }
  100. /**
  101. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  102. */
  103. public function testParseInvalidMappingShouldThrowException()
  104. {
  105. Inline::parse('[foo] bar');
  106. }
  107. /**
  108. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  109. */
  110. public function testParseInvalidSequenceShouldThrowException()
  111. {
  112. Inline::parse('{ foo: bar } bar');
  113. }
  114. public function testParseScalarWithCorrectlyQuotedStringShouldReturnString()
  115. {
  116. $value = "'don''t do somthin'' like that'";
  117. $expect = "don't do somthin' like that";
  118. $this->assertSame($expect, Inline::parseScalar($value));
  119. }
  120. /**
  121. * @dataProvider getDataForParseReferences
  122. */
  123. public function testParseReferences($yaml, $expected)
  124. {
  125. $this->assertSame($expected, Inline::parse($yaml, false, false, false, array('var' => 'var-value')));
  126. }
  127. public function getDataForParseReferences()
  128. {
  129. return array(
  130. 'scalar' => array('*var', 'var-value'),
  131. 'list' => array('[ *var ]', array('var-value')),
  132. 'list-in-list' => array('[[ *var ]]', array(array('var-value'))),
  133. 'map-in-list' => array('[ { key: *var } ]', array(array('key' => 'var-value'))),
  134. 'embedded-mapping-in-list' => array('[ key: *var ]', array(array('key' => 'var-value'))),
  135. 'map' => array('{ key: *var }', array('key' => 'var-value')),
  136. 'list-in-map' => array('{ key: [*var] }', array('key' => array('var-value'))),
  137. 'map-in-map' => array('{ foo: { bar: *var } }', array('foo' => array('bar' => 'var-value'))),
  138. );
  139. }
  140. public function testParseMapReferenceInSequence()
  141. {
  142. $foo = array(
  143. 'a' => 'Steve',
  144. 'b' => 'Clark',
  145. 'c' => 'Brian',
  146. );
  147. $this->assertSame(array($foo), Inline::parse('[*foo]', false, false, false, array('foo' => $foo)));
  148. }
  149. /**
  150. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  151. * @expectedExceptionMessage A reference must contain at least one character.
  152. */
  153. public function testParseUnquotedAsterisk()
  154. {
  155. Inline::parse('{ foo: * }');
  156. }
  157. /**
  158. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  159. * @expectedExceptionMessage A reference must contain at least one character.
  160. */
  161. public function testParseUnquotedAsteriskFollowedByAComment()
  162. {
  163. Inline::parse('{ foo: * #foo }');
  164. }
  165. /**
  166. * @group legacy
  167. * @dataProvider getReservedIndicators
  168. * throws \Symfony\Component\Yaml\Exception\ParseException in 3.0
  169. */
  170. public function testParseUnquotedScalarStartingWithReservedIndicator($indicator)
  171. {
  172. Inline::parse(sprintf('{ foo: %sfoo }', $indicator));
  173. }
  174. public function getReservedIndicators()
  175. {
  176. return array(array('@'), array('`'));
  177. }
  178. /**
  179. * @group legacy
  180. * @dataProvider getScalarIndicators
  181. * throws \Symfony\Component\Yaml\Exception\ParseException in 3.0
  182. */
  183. public function testParseUnquotedScalarStartingWithScalarIndicator($indicator)
  184. {
  185. Inline::parse(sprintf('{ foo: %sfoo }', $indicator));
  186. }
  187. public function getScalarIndicators()
  188. {
  189. return array(array('|'), array('>'));
  190. }
  191. /**
  192. * @dataProvider getDataForIsHash
  193. */
  194. public function testIsHash($array, $expected)
  195. {
  196. $this->assertSame($expected, Inline::isHash($array));
  197. }
  198. public function getDataForIsHash()
  199. {
  200. return array(
  201. array(array(), false),
  202. array(array(1, 2, 3), false),
  203. array(array(2 => 1, 1 => 2, 0 => 3), true),
  204. array(array('foo' => 1, 'bar' => 2), true),
  205. );
  206. }
  207. public function getTestsForParse()
  208. {
  209. return array(
  210. array('', ''),
  211. array('null', null),
  212. array('false', false),
  213. array('true', true),
  214. array('12', 12),
  215. array('-12', -12),
  216. array('"quoted string"', 'quoted string'),
  217. array("'quoted string'", 'quoted string'),
  218. array('12.30e+02', 12.30e+02),
  219. array('0x4D2', 0x4D2),
  220. array('02333', 02333),
  221. array('.Inf', -log(0)),
  222. array('-.Inf', log(0)),
  223. array("'686e444'", '686e444'),
  224. array('686e444', 646e444),
  225. array('123456789123456789123456789123456789', '123456789123456789123456789123456789'),
  226. array('"foo\r\nbar"', "foo\r\nbar"),
  227. array("'foo#bar'", 'foo#bar'),
  228. array("'foo # bar'", 'foo # bar'),
  229. array("'#cfcfcf'", '#cfcfcf'),
  230. array('::form_base.html.twig', '::form_base.html.twig'),
  231. // Pre-YAML-1.2 booleans
  232. array("'y'", 'y'),
  233. array("'n'", 'n'),
  234. array("'yes'", 'yes'),
  235. array("'no'", 'no'),
  236. array("'on'", 'on'),
  237. array("'off'", 'off'),
  238. array('2007-10-30', gmmktime(0, 0, 0, 10, 30, 2007)),
  239. array('2007-10-30T02:59:43Z', gmmktime(2, 59, 43, 10, 30, 2007)),
  240. array('2007-10-30 02:59:43 Z', gmmktime(2, 59, 43, 10, 30, 2007)),
  241. array('1960-10-30 02:59:43 Z', gmmktime(2, 59, 43, 10, 30, 1960)),
  242. array('1730-10-30T02:59:43Z', gmmktime(2, 59, 43, 10, 30, 1730)),
  243. array('"a \\"string\\" with \'quoted strings inside\'"', 'a "string" with \'quoted strings inside\''),
  244. array("'a \"string\" with ''quoted strings inside'''", 'a "string" with \'quoted strings inside\''),
  245. // sequences
  246. // urls are no key value mapping. see #3609. Valid yaml "key: value" mappings require a space after the colon
  247. array('[foo, http://urls.are/no/mappings, false, null, 12]', array('foo', 'http://urls.are/no/mappings', false, null, 12)),
  248. array('[ foo , bar , false , null , 12 ]', array('foo', 'bar', false, null, 12)),
  249. array('[\'foo,bar\', \'foo bar\']', array('foo,bar', 'foo bar')),
  250. // mappings
  251. array('{foo:bar,bar:foo,false:false,null:null,integer:12}', array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12)),
  252. array('{ foo : bar, bar : foo, false : false, null : null, integer : 12 }', array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12)),
  253. array('{foo: \'bar\', bar: \'foo: bar\'}', array('foo' => 'bar', 'bar' => 'foo: bar')),
  254. array('{\'foo\': \'bar\', "bar": \'foo: bar\'}', array('foo' => 'bar', 'bar' => 'foo: bar')),
  255. array('{\'foo\'\'\': \'bar\', "bar\"": \'foo: bar\'}', array('foo\'' => 'bar', 'bar"' => 'foo: bar')),
  256. array('{\'foo: \': \'bar\', "bar: ": \'foo: bar\'}', array('foo: ' => 'bar', 'bar: ' => 'foo: bar')),
  257. // nested sequences and mappings
  258. array('[foo, [bar, foo]]', array('foo', array('bar', 'foo'))),
  259. array('[foo, {bar: foo}]', array('foo', array('bar' => 'foo'))),
  260. array('{ foo: {bar: foo} }', array('foo' => array('bar' => 'foo'))),
  261. array('{ foo: [bar, foo] }', array('foo' => array('bar', 'foo'))),
  262. array('[ foo, [ bar, foo ] ]', array('foo', array('bar', 'foo'))),
  263. array('[{ foo: {bar: foo} }]', array(array('foo' => array('bar' => 'foo')))),
  264. array('[foo, [bar, [foo, [bar, foo]], foo]]', array('foo', array('bar', array('foo', array('bar', 'foo')), 'foo'))),
  265. array('[foo, {bar: foo, foo: [foo, {bar: foo}]}, [foo, {bar: foo}]]', array('foo', array('bar' => 'foo', 'foo' => array('foo', array('bar' => 'foo'))), array('foo', array('bar' => 'foo')))),
  266. array('[foo, bar: { foo: bar }]', array('foo', '1' => array('bar' => array('foo' => 'bar')))),
  267. array('[foo, \'@foo.baz\', { \'%foo%\': \'foo is %foo%\', bar: \'%foo%\' }, true, \'@service_container\']', array('foo', '@foo.baz', array('%foo%' => 'foo is %foo%', 'bar' => '%foo%'), true, '@service_container')),
  268. );
  269. }
  270. public function getTestsForParseWithMapObjects()
  271. {
  272. return array(
  273. array('', ''),
  274. array('null', null),
  275. array('false', false),
  276. array('true', true),
  277. array('12', 12),
  278. array('-12', -12),
  279. array('"quoted string"', 'quoted string'),
  280. array("'quoted string'", 'quoted string'),
  281. array('12.30e+02', 12.30e+02),
  282. array('0x4D2', 0x4D2),
  283. array('02333', 02333),
  284. array('.Inf', -log(0)),
  285. array('-.Inf', log(0)),
  286. array("'686e444'", '686e444'),
  287. array('686e444', 646e444),
  288. array('123456789123456789123456789123456789', '123456789123456789123456789123456789'),
  289. array('"foo\r\nbar"', "foo\r\nbar"),
  290. array("'foo#bar'", 'foo#bar'),
  291. array("'foo # bar'", 'foo # bar'),
  292. array("'#cfcfcf'", '#cfcfcf'),
  293. array('::form_base.html.twig', '::form_base.html.twig'),
  294. array('2007-10-30', gmmktime(0, 0, 0, 10, 30, 2007)),
  295. array('2007-10-30T02:59:43Z', gmmktime(2, 59, 43, 10, 30, 2007)),
  296. array('2007-10-30 02:59:43 Z', gmmktime(2, 59, 43, 10, 30, 2007)),
  297. array('1960-10-30 02:59:43 Z', gmmktime(2, 59, 43, 10, 30, 1960)),
  298. array('1730-10-30T02:59:43Z', gmmktime(2, 59, 43, 10, 30, 1730)),
  299. array('"a \\"string\\" with \'quoted strings inside\'"', 'a "string" with \'quoted strings inside\''),
  300. array("'a \"string\" with ''quoted strings inside'''", 'a "string" with \'quoted strings inside\''),
  301. // sequences
  302. // urls are no key value mapping. see #3609. Valid yaml "key: value" mappings require a space after the colon
  303. array('[foo, http://urls.are/no/mappings, false, null, 12]', array('foo', 'http://urls.are/no/mappings', false, null, 12)),
  304. array('[ foo , bar , false , null , 12 ]', array('foo', 'bar', false, null, 12)),
  305. array('[\'foo,bar\', \'foo bar\']', array('foo,bar', 'foo bar')),
  306. // mappings
  307. array('{foo:bar,bar:foo,false:false,null:null,integer:12}', (object) array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12)),
  308. array('{ foo : bar, bar : foo, false : false, null : null, integer : 12 }', (object) array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12)),
  309. array('{foo: \'bar\', bar: \'foo: bar\'}', (object) array('foo' => 'bar', 'bar' => 'foo: bar')),
  310. array('{\'foo\': \'bar\', "bar": \'foo: bar\'}', (object) array('foo' => 'bar', 'bar' => 'foo: bar')),
  311. array('{\'foo\'\'\': \'bar\', "bar\"": \'foo: bar\'}', (object) array('foo\'' => 'bar', 'bar"' => 'foo: bar')),
  312. array('{\'foo: \': \'bar\', "bar: ": \'foo: bar\'}', (object) array('foo: ' => 'bar', 'bar: ' => 'foo: bar')),
  313. // nested sequences and mappings
  314. array('[foo, [bar, foo]]', array('foo', array('bar', 'foo'))),
  315. array('[foo, {bar: foo}]', array('foo', (object) array('bar' => 'foo'))),
  316. array('{ foo: {bar: foo} }', (object) array('foo' => (object) array('bar' => 'foo'))),
  317. array('{ foo: [bar, foo] }', (object) array('foo' => array('bar', 'foo'))),
  318. array('[ foo, [ bar, foo ] ]', array('foo', array('bar', 'foo'))),
  319. array('[{ foo: {bar: foo} }]', array((object) array('foo' => (object) array('bar' => 'foo')))),
  320. array('[foo, [bar, [foo, [bar, foo]], foo]]', array('foo', array('bar', array('foo', array('bar', 'foo')), 'foo'))),
  321. array('[foo, {bar: foo, foo: [foo, {bar: foo}]}, [foo, {bar: foo}]]', array('foo', (object) array('bar' => 'foo', 'foo' => array('foo', (object) array('bar' => 'foo'))), array('foo', (object) array('bar' => 'foo')))),
  322. array('[foo, bar: { foo: bar }]', array('foo', '1' => (object) array('bar' => (object) array('foo' => 'bar')))),
  323. array('[foo, \'@foo.baz\', { \'%foo%\': \'foo is %foo%\', bar: \'%foo%\' }, true, \'@service_container\']', array('foo', '@foo.baz', (object) array('%foo%' => 'foo is %foo%', 'bar' => '%foo%'), true, '@service_container')),
  324. array('{}', new \stdClass()),
  325. array('{ foo : bar, bar : {} }', (object) array('foo' => 'bar', 'bar' => new \stdClass())),
  326. array('{ foo : [], bar : {} }', (object) array('foo' => array(), 'bar' => new \stdClass())),
  327. array('{foo: \'bar\', bar: {} }', (object) array('foo' => 'bar', 'bar' => new \stdClass())),
  328. array('{\'foo\': \'bar\', "bar": {}}', (object) array('foo' => 'bar', 'bar' => new \stdClass())),
  329. array('{\'foo\': \'bar\', "bar": \'{}\'}', (object) array('foo' => 'bar', 'bar' => '{}')),
  330. array('[foo, [{}, {}]]', array('foo', array(new \stdClass(), new \stdClass()))),
  331. array('[foo, [[], {}]]', array('foo', array(array(), new \stdClass()))),
  332. array('[foo, [[{}, {}], {}]]', array('foo', array(array(new \stdClass(), new \stdClass()), new \stdClass()))),
  333. array('[foo, {bar: {}}]', array('foo', '1' => (object) array('bar' => new \stdClass()))),
  334. );
  335. }
  336. public function getTestsForDump()
  337. {
  338. return array(
  339. array('null', null),
  340. array('false', false),
  341. array('true', true),
  342. array('12', 12),
  343. array("'quoted string'", 'quoted string'),
  344. array('!!float 1230', 12.30e+02),
  345. array('1234', 0x4D2),
  346. array('1243', 02333),
  347. array('.Inf', -log(0)),
  348. array('-.Inf', log(0)),
  349. array("'686e444'", '686e444'),
  350. array('"foo\r\nbar"', "foo\r\nbar"),
  351. array("'foo#bar'", 'foo#bar'),
  352. array("'foo # bar'", 'foo # bar'),
  353. array("'#cfcfcf'", '#cfcfcf'),
  354. array("'a \"string\" with ''quoted strings inside'''", 'a "string" with \'quoted strings inside\''),
  355. array("'-dash'", '-dash'),
  356. array("'-'", '-'),
  357. // Pre-YAML-1.2 booleans
  358. array("'y'", 'y'),
  359. array("'n'", 'n'),
  360. array("'yes'", 'yes'),
  361. array("'no'", 'no'),
  362. array("'on'", 'on'),
  363. array("'off'", 'off'),
  364. // sequences
  365. array('[foo, bar, false, null, 12]', array('foo', 'bar', false, null, 12)),
  366. array('[\'foo,bar\', \'foo bar\']', array('foo,bar', 'foo bar')),
  367. // mappings
  368. array('{ foo: bar, bar: foo, \'false\': false, \'null\': null, integer: 12 }', array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12)),
  369. array('{ foo: bar, bar: \'foo: bar\' }', array('foo' => 'bar', 'bar' => 'foo: bar')),
  370. // nested sequences and mappings
  371. array('[foo, [bar, foo]]', array('foo', array('bar', 'foo'))),
  372. array('[foo, [bar, [foo, [bar, foo]], foo]]', array('foo', array('bar', array('foo', array('bar', 'foo')), 'foo'))),
  373. array('{ foo: { bar: foo } }', array('foo' => array('bar' => 'foo'))),
  374. array('[foo, { bar: foo }]', array('foo', array('bar' => 'foo'))),
  375. array('[foo, { bar: foo, foo: [foo, { bar: foo }] }, [foo, { bar: foo }]]', array('foo', array('bar' => 'foo', 'foo' => array('foo', array('bar' => 'foo'))), array('foo', array('bar' => 'foo')))),
  376. array('[foo, \'@foo.baz\', { \'%foo%\': \'foo is %foo%\', bar: \'%foo%\' }, true, \'@service_container\']', array('foo', '@foo.baz', array('%foo%' => 'foo is %foo%', 'bar' => '%foo%'), true, '@service_container')),
  377. array('{ foo: { bar: { 1: 2, baz: 3 } } }', array('foo' => array('bar' => array(1 => 2, 'baz' => 3)))),
  378. );
  379. }
  380. }