No Description

InlineTest.php 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. $required_locales = array('fr_FR.UTF-8', 'fr_FR.UTF8', 'fr_FR.utf-8', 'fr_FR.utf8', 'French_France.1252');
  44. if (false === setlocale(LC_ALL, $required_locales)) {
  45. $this->markTestSkipped('Could not set any of required locales: '.implode(", ", $required_locales));
  46. }
  47. $this->assertEquals('1.2', Inline::dump(1.2));
  48. $this->assertContains('fr', strtolower(setlocale(LC_NUMERIC, 0)));
  49. setlocale(LC_ALL, $locale);
  50. }
  51. public function testHashStringsResemblingExponentialNumericsShouldNotBeChangedToINF()
  52. {
  53. $value = '686e444';
  54. $this->assertSame($value, Inline::parse(Inline::dump($value)));
  55. }
  56. /**
  57. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  58. */
  59. public function testParseScalarWithIncorrectlyQuotedStringShouldThrowException()
  60. {
  61. $value = "'don't do somthin' like that'";
  62. Inline::parse($value);
  63. }
  64. /**
  65. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  66. */
  67. public function testParseScalarWithIncorrectlyDoubleQuotedStringShouldThrowException()
  68. {
  69. $value = '"don"t do somthin" like that"';
  70. Inline::parse($value);
  71. }
  72. /**
  73. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  74. */
  75. public function testParseInvalidMappingKeyShouldThrowException()
  76. {
  77. $value = '{ "foo " bar": "bar" }';
  78. Inline::parse($value);
  79. }
  80. /**
  81. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  82. */
  83. public function testParseInvalidMappingShouldThrowException()
  84. {
  85. Inline::parse('[foo] bar');
  86. }
  87. /**
  88. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  89. */
  90. public function testParseInvalidSequenceShouldThrowException()
  91. {
  92. Inline::parse('{ foo: bar } bar');
  93. }
  94. public function testParseScalarWithCorrectlyQuotedStringShouldReturnString()
  95. {
  96. $value = "'don''t do somthin'' like that'";
  97. $expect = "don't do somthin' like that";
  98. $this->assertSame($expect, Inline::parseScalar($value));
  99. }
  100. /**
  101. * @dataProvider getDataForParseReferences
  102. */
  103. public function testParseReferences($yaml, $expected)
  104. {
  105. $this->assertSame($expected, Inline::parse($yaml, false, false, false, array('var' => 'var-value')));
  106. }
  107. public function getDataForParseReferences()
  108. {
  109. return array(
  110. 'scalar' => array('*var', 'var-value'),
  111. 'list' => array('[ *var ]', array('var-value')),
  112. 'list-in-list' => array('[[ *var ]]', array(array('var-value'))),
  113. 'map-in-list' => array('[ { key: *var } ]', array(array('key' => 'var-value'))),
  114. 'embedded-mapping-in-list' => array('[ key: *var ]', array(array('key' => 'var-value'))),
  115. 'map' => array('{ key: *var }', array('key' => 'var-value')),
  116. 'list-in-map' => array('{ key: [*var] }', array('key' => array('var-value'))),
  117. 'map-in-map' => array('{ foo: { bar: *var } }', array('foo' => array('bar' => 'var-value'))),
  118. );
  119. }
  120. public function testParseMapReferenceInSequence()
  121. {
  122. $foo = array(
  123. 'a' => 'Steve',
  124. 'b' => 'Clark',
  125. 'c' => 'Brian',
  126. );
  127. $this->assertSame(array($foo), Inline::parse('[*foo]', false, false, false, array('foo' => $foo)));
  128. }
  129. /**
  130. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  131. * @expectedExceptionMessage A reference must contain at least one character.
  132. */
  133. public function testParseUnquotedAsterisk()
  134. {
  135. Inline::parse('{ foo: * }');
  136. }
  137. /**
  138. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  139. * @expectedExceptionMessage A reference must contain at least one character.
  140. */
  141. public function testParseUnquotedAsteriskFollowedByAComment()
  142. {
  143. Inline::parse('{ foo: * #foo }');
  144. }
  145. public function getTestsForParse()
  146. {
  147. return array(
  148. array('', ''),
  149. array('null', null),
  150. array('false', false),
  151. array('true', true),
  152. array('12', 12),
  153. array('-12', -12),
  154. array('"quoted string"', 'quoted string'),
  155. array("'quoted string'", 'quoted string'),
  156. array('12.30e+02', 12.30e+02),
  157. array('0x4D2', 0x4D2),
  158. array('02333', 02333),
  159. array('.Inf', -log(0)),
  160. array('-.Inf', log(0)),
  161. array("'686e444'", '686e444'),
  162. array('686e444', 646e444),
  163. array('123456789123456789123456789123456789', '123456789123456789123456789123456789'),
  164. array('"foo\r\nbar"', "foo\r\nbar"),
  165. array("'foo#bar'", 'foo#bar'),
  166. array("'foo # bar'", 'foo # bar'),
  167. array("'#cfcfcf'", '#cfcfcf'),
  168. array('::form_base.html.twig', '::form_base.html.twig'),
  169. // Pre-YAML-1.2 booleans
  170. array("'y'", 'y'),
  171. array("'n'", 'n'),
  172. array("'yes'", 'yes'),
  173. array("'no'", 'no'),
  174. array("'on'", 'on'),
  175. array("'off'", 'off'),
  176. array('2007-10-30', mktime(0, 0, 0, 10, 30, 2007)),
  177. array('2007-10-30T02:59:43Z', gmmktime(2, 59, 43, 10, 30, 2007)),
  178. array('2007-10-30 02:59:43 Z', gmmktime(2, 59, 43, 10, 30, 2007)),
  179. array('1960-10-30 02:59:43 Z', gmmktime(2, 59, 43, 10, 30, 1960)),
  180. array('1730-10-30T02:59:43Z', gmmktime(2, 59, 43, 10, 30, 1730)),
  181. array('"a \\"string\\" with \'quoted strings inside\'"', 'a "string" with \'quoted strings inside\''),
  182. array("'a \"string\" with ''quoted strings inside'''", 'a "string" with \'quoted strings inside\''),
  183. // sequences
  184. // urls are no key value mapping. see #3609. Valid yaml "key: value" mappings require a space after the colon
  185. array('[foo, http://urls.are/no/mappings, false, null, 12]', array('foo', 'http://urls.are/no/mappings', false, null, 12)),
  186. array('[ foo , bar , false , null , 12 ]', array('foo', 'bar', false, null, 12)),
  187. array('[\'foo,bar\', \'foo bar\']', array('foo,bar', 'foo bar')),
  188. // mappings
  189. array('{foo:bar,bar:foo,false:false,null:null,integer:12}', array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12)),
  190. array('{ foo : bar, bar : foo, false : false, null : null, integer : 12 }', array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12)),
  191. array('{foo: \'bar\', bar: \'foo: bar\'}', array('foo' => 'bar', 'bar' => 'foo: bar')),
  192. array('{\'foo\': \'bar\', "bar": \'foo: bar\'}', array('foo' => 'bar', 'bar' => 'foo: bar')),
  193. array('{\'foo\'\'\': \'bar\', "bar\"": \'foo: bar\'}', array('foo\'' => 'bar', "bar\"" => 'foo: bar')),
  194. array('{\'foo: \': \'bar\', "bar: ": \'foo: bar\'}', array('foo: ' => 'bar', "bar: " => 'foo: bar')),
  195. // nested sequences and mappings
  196. array('[foo, [bar, foo]]', array('foo', array('bar', 'foo'))),
  197. array('[foo, {bar: foo}]', array('foo', array('bar' => 'foo'))),
  198. array('{ foo: {bar: foo} }', array('foo' => array('bar' => 'foo'))),
  199. array('{ foo: [bar, foo] }', array('foo' => array('bar', 'foo'))),
  200. array('[ foo, [ bar, foo ] ]', array('foo', array('bar', 'foo'))),
  201. array('[{ foo: {bar: foo} }]', array(array('foo' => array('bar' => 'foo')))),
  202. array('[foo, [bar, [foo, [bar, foo]], foo]]', array('foo', array('bar', array('foo', array('bar', 'foo')), 'foo'))),
  203. 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')))),
  204. array('[foo, bar: { foo: bar }]', array('foo', '1' => array('bar' => array('foo' => 'bar')))),
  205. 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')),
  206. );
  207. }
  208. public function getTestsForParseWithMapObjects()
  209. {
  210. return array(
  211. array('', ''),
  212. array('null', null),
  213. array('false', false),
  214. array('true', true),
  215. array('12', 12),
  216. array('-12', -12),
  217. array('"quoted string"', 'quoted string'),
  218. array("'quoted string'", 'quoted string'),
  219. array('12.30e+02', 12.30e+02),
  220. array('0x4D2', 0x4D2),
  221. array('02333', 02333),
  222. array('.Inf', -log(0)),
  223. array('-.Inf', log(0)),
  224. array("'686e444'", '686e444'),
  225. array('686e444', 646e444),
  226. array('123456789123456789123456789123456789', '123456789123456789123456789123456789'),
  227. array('"foo\r\nbar"', "foo\r\nbar"),
  228. array("'foo#bar'", 'foo#bar'),
  229. array("'foo # bar'", 'foo # bar'),
  230. array("'#cfcfcf'", '#cfcfcf'),
  231. array('::form_base.html.twig', '::form_base.html.twig'),
  232. array('2007-10-30', mktime(0, 0, 0, 10, 30, 2007)),
  233. array('2007-10-30T02:59:43Z', gmmktime(2, 59, 43, 10, 30, 2007)),
  234. array('2007-10-30 02:59:43 Z', gmmktime(2, 59, 43, 10, 30, 2007)),
  235. array('1960-10-30 02:59:43 Z', gmmktime(2, 59, 43, 10, 30, 1960)),
  236. array('1730-10-30T02:59:43Z', gmmktime(2, 59, 43, 10, 30, 1730)),
  237. array('"a \\"string\\" with \'quoted strings inside\'"', 'a "string" with \'quoted strings inside\''),
  238. array("'a \"string\" with ''quoted strings inside'''", 'a "string" with \'quoted strings inside\''),
  239. // sequences
  240. // urls are no key value mapping. see #3609. Valid yaml "key: value" mappings require a space after the colon
  241. array('[foo, http://urls.are/no/mappings, false, null, 12]', array('foo', 'http://urls.are/no/mappings', false, null, 12)),
  242. array('[ foo , bar , false , null , 12 ]', array('foo', 'bar', false, null, 12)),
  243. array('[\'foo,bar\', \'foo bar\']', array('foo,bar', 'foo bar')),
  244. // mappings
  245. array('{foo:bar,bar:foo,false:false,null:null,integer:12}', (object) array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12)),
  246. array('{ foo : bar, bar : foo, false : false, null : null, integer : 12 }', (object) array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12)),
  247. array('{foo: \'bar\', bar: \'foo: bar\'}', (object) array('foo' => 'bar', 'bar' => 'foo: bar')),
  248. array('{\'foo\': \'bar\', "bar": \'foo: bar\'}', (object) array('foo' => 'bar', 'bar' => 'foo: bar')),
  249. array('{\'foo\'\'\': \'bar\', "bar\"": \'foo: bar\'}', (object) array('foo\'' => 'bar', "bar\"" => 'foo: bar')),
  250. array('{\'foo: \': \'bar\', "bar: ": \'foo: bar\'}', (object) array('foo: ' => 'bar', "bar: " => 'foo: bar')),
  251. // nested sequences and mappings
  252. array('[foo, [bar, foo]]', array('foo', array('bar', 'foo'))),
  253. array('[foo, {bar: foo}]', array('foo', (object) array('bar' => 'foo'))),
  254. array('{ foo: {bar: foo} }', (object) array('foo' => (object) array('bar' => 'foo'))),
  255. array('{ foo: [bar, foo] }', (object) array('foo' => array('bar', 'foo'))),
  256. array('[ foo, [ bar, foo ] ]', array('foo', array('bar', 'foo'))),
  257. array('[{ foo: {bar: foo} }]', array((object) array('foo' => (object) array('bar' => 'foo')))),
  258. array('[foo, [bar, [foo, [bar, foo]], foo]]', array('foo', array('bar', array('foo', array('bar', 'foo')), 'foo'))),
  259. 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')))),
  260. array('[foo, bar: { foo: bar }]', array('foo', '1' => (object) array('bar' => (object) array('foo' => 'bar')))),
  261. 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')),
  262. array('{}', new \stdClass()),
  263. array('{ foo : bar, bar : {} }', (object) array('foo' => 'bar', 'bar' => new \stdClass())),
  264. array('{ foo : [], bar : {} }', (object) array('foo' => array(), 'bar' => new \stdClass())),
  265. array('{foo: \'bar\', bar: {} }', (object) array('foo' => 'bar', 'bar' => new \stdClass())),
  266. array('{\'foo\': \'bar\', "bar": {}}', (object) array('foo' => 'bar', 'bar' => new \stdClass())),
  267. array('{\'foo\': \'bar\', "bar": \'{}\'}', (object) array('foo' => 'bar', 'bar' => '{}')),
  268. array('[foo, [{}, {}]]', array('foo', array(new \stdClass(), new \stdClass()))),
  269. array('[foo, [[], {}]]', array('foo', array(array(), new \stdClass()))),
  270. array('[foo, [[{}, {}], {}]]', array('foo', array(array(new \stdClass(), new \stdClass()), new \stdClass()))),
  271. array('[foo, {bar: {}}]', array('foo', '1' => (object) array('bar' => new \stdClass()))),
  272. );
  273. }
  274. public function getTestsForDump()
  275. {
  276. return array(
  277. array('null', null),
  278. array('false', false),
  279. array('true', true),
  280. array('12', 12),
  281. array("'quoted string'", 'quoted string'),
  282. array('!!float 1230', 12.30e+02),
  283. array('1234', 0x4D2),
  284. array('1243', 02333),
  285. array('.Inf', -log(0)),
  286. array('-.Inf', log(0)),
  287. array("'686e444'", '686e444'),
  288. array('"foo\r\nbar"', "foo\r\nbar"),
  289. array("'foo#bar'", 'foo#bar'),
  290. array("'foo # bar'", 'foo # bar'),
  291. array("'#cfcfcf'", '#cfcfcf'),
  292. array("'a \"string\" with ''quoted strings inside'''", 'a "string" with \'quoted strings inside\''),
  293. array("'-dash'", '-dash'),
  294. array("'-'", '-'),
  295. // Pre-YAML-1.2 booleans
  296. array("'y'", 'y'),
  297. array("'n'", 'n'),
  298. array("'yes'", 'yes'),
  299. array("'no'", 'no'),
  300. array("'on'", 'on'),
  301. array("'off'", 'off'),
  302. // sequences
  303. array('[foo, bar, false, null, 12]', array('foo', 'bar', false, null, 12)),
  304. array('[\'foo,bar\', \'foo bar\']', array('foo,bar', 'foo bar')),
  305. // mappings
  306. array('{ foo: bar, bar: foo, \'false\': false, \'null\': null, integer: 12 }', array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12)),
  307. array('{ foo: bar, bar: \'foo: bar\' }', array('foo' => 'bar', 'bar' => 'foo: bar')),
  308. // nested sequences and mappings
  309. array('[foo, [bar, foo]]', array('foo', array('bar', 'foo'))),
  310. array('[foo, [bar, [foo, [bar, foo]], foo]]', array('foo', array('bar', array('foo', array('bar', 'foo')), 'foo'))),
  311. array('{ foo: { bar: foo } }', array('foo' => array('bar' => 'foo'))),
  312. array('[foo, { bar: foo }]', array('foo', array('bar' => 'foo'))),
  313. 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')))),
  314. 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')),
  315. );
  316. }
  317. }