InlineTest.php 17 KB

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