Geen omschrijving

InlineTest.php 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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. use Symfony\Component\Yaml\Yaml;
  13. class InlineTest extends \PHPUnit_Framework_TestCase
  14. {
  15. /**
  16. * @dataProvider getTestsForParse
  17. */
  18. public function testParse($yaml, $value)
  19. {
  20. $this->assertSame($value, Inline::parse($yaml), sprintf('::parse() converts an inline YAML to a PHP structure (%s)', $yaml));
  21. }
  22. /**
  23. * @dataProvider getTestsForParseWithMapObjects
  24. */
  25. public function testParseWithMapObjects($yaml, $value)
  26. {
  27. $actual = Inline::parse($yaml, Yaml::PARSE_OBJECT_FOR_MAP);
  28. $this->assertSame(serialize($value), serialize($actual));
  29. }
  30. /**
  31. * @dataProvider getTestsForParsePhpConstants
  32. */
  33. public function testParsePhpConstants($yaml, $value)
  34. {
  35. $actual = Inline::parse($yaml, Yaml::PARSE_CONSTANT);
  36. $this->assertSame($value, $actual);
  37. }
  38. public function getTestsForParsePhpConstants()
  39. {
  40. return array(
  41. array('!php/const:Symfony\Component\Yaml\Yaml::PARSE_CONSTANT', Yaml::PARSE_CONSTANT),
  42. array('!php/const:PHP_INT_MAX', PHP_INT_MAX),
  43. array('[!php/const:PHP_INT_MAX]', array(PHP_INT_MAX)),
  44. array('{ foo: !php/const:PHP_INT_MAX }', array('foo' => PHP_INT_MAX)),
  45. );
  46. }
  47. /**
  48. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  49. * @expectedExceptionMessage The constant "WRONG_CONSTANT" is not defined
  50. */
  51. public function testParsePhpConstantThrowsExceptionWhenUndefined()
  52. {
  53. Inline::parse('!php/const:WRONG_CONSTANT', Yaml::PARSE_CONSTANT);
  54. }
  55. /**
  56. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  57. * @expectedExceptionMessageRegExp #The string "!php/const:PHP_INT_MAX" could not be parsed as a constant.*#
  58. */
  59. public function testParsePhpConstantThrowsExceptionOnInvalidType()
  60. {
  61. Inline::parse('!php/const:PHP_INT_MAX', Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE);
  62. }
  63. /**
  64. * @group legacy
  65. * @dataProvider getTestsForParseWithMapObjects
  66. */
  67. public function testParseWithMapObjectsPassingTrue($yaml, $value)
  68. {
  69. $actual = Inline::parse($yaml, false, false, true);
  70. $this->assertSame(serialize($value), serialize($actual));
  71. }
  72. /**
  73. * @dataProvider getTestsForDump
  74. */
  75. public function testDump($yaml, $value)
  76. {
  77. $this->assertEquals($yaml, Inline::dump($value), sprintf('::dump() converts a PHP structure to an inline YAML (%s)', $yaml));
  78. $this->assertSame($value, Inline::parse(Inline::dump($value)), 'check consistency');
  79. }
  80. public function testDumpNumericValueWithLocale()
  81. {
  82. $locale = setlocale(LC_NUMERIC, 0);
  83. if (false === $locale) {
  84. $this->markTestSkipped('Your platform does not support locales.');
  85. }
  86. try {
  87. $requiredLocales = array('fr_FR.UTF-8', 'fr_FR.UTF8', 'fr_FR.utf-8', 'fr_FR.utf8', 'French_France.1252');
  88. if (false === setlocale(LC_NUMERIC, $requiredLocales)) {
  89. $this->markTestSkipped('Could not set any of required locales: '.implode(', ', $requiredLocales));
  90. }
  91. $this->assertEquals('1.2', Inline::dump(1.2));
  92. $this->assertContains('fr', strtolower(setlocale(LC_NUMERIC, 0)));
  93. } finally {
  94. setlocale(LC_NUMERIC, $locale);
  95. }
  96. }
  97. public function testHashStringsResemblingExponentialNumericsShouldNotBeChangedToINF()
  98. {
  99. $value = '686e444';
  100. $this->assertSame($value, Inline::parse(Inline::dump($value)));
  101. }
  102. /**
  103. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  104. * @expectedExceptionMessage Found unknown escape character "\V".
  105. */
  106. public function testParseScalarWithNonEscapedBlackslashShouldThrowException()
  107. {
  108. Inline::parse('"Foo\Var"');
  109. }
  110. /**
  111. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  112. */
  113. public function testParseScalarWithNonEscapedBlackslashAtTheEndShouldThrowException()
  114. {
  115. Inline::parse('"Foo\\"');
  116. }
  117. /**
  118. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  119. */
  120. public function testParseScalarWithIncorrectlyQuotedStringShouldThrowException()
  121. {
  122. $value = "'don't do somthin' like that'";
  123. Inline::parse($value);
  124. }
  125. /**
  126. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  127. */
  128. public function testParseScalarWithIncorrectlyDoubleQuotedStringShouldThrowException()
  129. {
  130. $value = '"don"t do somthin" like that"';
  131. Inline::parse($value);
  132. }
  133. /**
  134. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  135. */
  136. public function testParseInvalidMappingKeyShouldThrowException()
  137. {
  138. $value = '{ "foo " bar": "bar" }';
  139. Inline::parse($value);
  140. }
  141. /**
  142. * @group legacy
  143. * @expectedDeprecation Using a colon that is not followed by an indication character (i.e. " ", ",", "[", "]", "{", "}" is deprecated since version 3.2 and will throw a ParseException in 4.0.
  144. * throws \Symfony\Component\Yaml\Exception\ParseException in 4.0
  145. */
  146. public function testParseMappingKeyWithColonNotFollowedBySpace()
  147. {
  148. Inline::parse('{1:""}');
  149. }
  150. /**
  151. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  152. */
  153. public function testParseInvalidMappingShouldThrowException()
  154. {
  155. Inline::parse('[foo] bar');
  156. }
  157. /**
  158. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  159. */
  160. public function testParseInvalidSequenceShouldThrowException()
  161. {
  162. Inline::parse('{ foo: bar } bar');
  163. }
  164. public function testParseScalarWithCorrectlyQuotedStringShouldReturnString()
  165. {
  166. $value = "'don''t do somthin'' like that'";
  167. $expect = "don't do somthin' like that";
  168. $this->assertSame($expect, Inline::parseScalar($value));
  169. }
  170. /**
  171. * @dataProvider getDataForParseReferences
  172. */
  173. public function testParseReferences($yaml, $expected)
  174. {
  175. $this->assertSame($expected, Inline::parse($yaml, 0, array('var' => 'var-value')));
  176. }
  177. /**
  178. * @group legacy
  179. * @dataProvider getDataForParseReferences
  180. */
  181. public function testParseReferencesAsFifthArgument($yaml, $expected)
  182. {
  183. $this->assertSame($expected, Inline::parse($yaml, false, false, false, array('var' => 'var-value')));
  184. }
  185. public function getDataForParseReferences()
  186. {
  187. return array(
  188. 'scalar' => array('*var', 'var-value'),
  189. 'list' => array('[ *var ]', array('var-value')),
  190. 'list-in-list' => array('[[ *var ]]', array(array('var-value'))),
  191. 'map-in-list' => array('[ { key: *var } ]', array(array('key' => 'var-value'))),
  192. 'embedded-mapping-in-list' => array('[ key: *var ]', array(array('key' => 'var-value'))),
  193. 'map' => array('{ key: *var }', array('key' => 'var-value')),
  194. 'list-in-map' => array('{ key: [*var] }', array('key' => array('var-value'))),
  195. 'map-in-map' => array('{ foo: { bar: *var } }', array('foo' => array('bar' => 'var-value'))),
  196. );
  197. }
  198. public function testParseMapReferenceInSequence()
  199. {
  200. $foo = array(
  201. 'a' => 'Steve',
  202. 'b' => 'Clark',
  203. 'c' => 'Brian',
  204. );
  205. $this->assertSame(array($foo), Inline::parse('[*foo]', 0, array('foo' => $foo)));
  206. }
  207. /**
  208. * @group legacy
  209. */
  210. public function testParseMapReferenceInSequenceAsFifthArgument()
  211. {
  212. $foo = array(
  213. 'a' => 'Steve',
  214. 'b' => 'Clark',
  215. 'c' => 'Brian',
  216. );
  217. $this->assertSame(array($foo), Inline::parse('[*foo]', false, false, false, array('foo' => $foo)));
  218. }
  219. /**
  220. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  221. * @expectedExceptionMessage A reference must contain at least one character.
  222. */
  223. public function testParseUnquotedAsterisk()
  224. {
  225. Inline::parse('{ foo: * }');
  226. }
  227. /**
  228. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  229. * @expectedExceptionMessage A reference must contain at least one character.
  230. */
  231. public function testParseUnquotedAsteriskFollowedByAComment()
  232. {
  233. Inline::parse('{ foo: * #foo }');
  234. }
  235. /**
  236. * @dataProvider getReservedIndicators
  237. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  238. * @expectedExceptionMessage cannot start a plain scalar; you need to quote the scalar.
  239. */
  240. public function testParseUnquotedScalarStartingWithReservedIndicator($indicator)
  241. {
  242. Inline::parse(sprintf('{ foo: %sfoo }', $indicator));
  243. }
  244. public function getReservedIndicators()
  245. {
  246. return array(array('@'), array('`'));
  247. }
  248. /**
  249. * @dataProvider getScalarIndicators
  250. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  251. * @expectedExceptionMessage cannot start a plain scalar; you need to quote the scalar.
  252. */
  253. public function testParseUnquotedScalarStartingWithScalarIndicator($indicator)
  254. {
  255. Inline::parse(sprintf('{ foo: %sfoo }', $indicator));
  256. }
  257. public function getScalarIndicators()
  258. {
  259. return array(array('|'), array('>'));
  260. }
  261. /**
  262. * @group legacy
  263. * @expectedDeprecation Not quoting the scalar "%bar " starting with the "%" indicator character is deprecated since Symfony 3.1 and will throw a ParseException in 4.0.
  264. * throws \Symfony\Component\Yaml\Exception\ParseException in 4.0
  265. */
  266. public function testParseUnquotedScalarStartingWithPercentCharacter()
  267. {
  268. Inline::parse('{ foo: %bar }');
  269. }
  270. /**
  271. * @dataProvider getDataForIsHash
  272. */
  273. public function testIsHash($array, $expected)
  274. {
  275. $this->assertSame($expected, Inline::isHash($array));
  276. }
  277. public function getDataForIsHash()
  278. {
  279. return array(
  280. array(array(), false),
  281. array(array(1, 2, 3), false),
  282. array(array(2 => 1, 1 => 2, 0 => 3), true),
  283. array(array('foo' => 1, 'bar' => 2), true),
  284. );
  285. }
  286. public function getTestsForParse()
  287. {
  288. return array(
  289. array('', ''),
  290. array('null', null),
  291. array('false', false),
  292. array('true', true),
  293. array('12', 12),
  294. array('-12', -12),
  295. array('1_2', 12),
  296. array('_12', '_12'),
  297. array('12_', 12),
  298. array('"quoted string"', 'quoted string'),
  299. array("'quoted string'", 'quoted string'),
  300. array('12.30e+02', 12.30e+02),
  301. array('123.45_67', 123.4567),
  302. array('0x4D2', 0x4D2),
  303. array('0x_4_D_2_', 0x4D2),
  304. array('02333', 02333),
  305. array('0_2_3_3_3', 02333),
  306. array('.Inf', -log(0)),
  307. array('-.Inf', log(0)),
  308. array("'686e444'", '686e444'),
  309. array('686e444', 646e444),
  310. array('123456789123456789123456789123456789', '123456789123456789123456789123456789'),
  311. array('"foo\r\nbar"', "foo\r\nbar"),
  312. array("'foo#bar'", 'foo#bar'),
  313. array("'foo # bar'", 'foo # bar'),
  314. array("'#cfcfcf'", '#cfcfcf'),
  315. array('::form_base.html.twig', '::form_base.html.twig'),
  316. // Pre-YAML-1.2 booleans
  317. array("'y'", 'y'),
  318. array("'n'", 'n'),
  319. array("'yes'", 'yes'),
  320. array("'no'", 'no'),
  321. array("'on'", 'on'),
  322. array("'off'", 'off'),
  323. array('2007-10-30', gmmktime(0, 0, 0, 10, 30, 2007)),
  324. array('2007-10-30T02:59:43Z', gmmktime(2, 59, 43, 10, 30, 2007)),
  325. array('2007-10-30 02:59:43 Z', gmmktime(2, 59, 43, 10, 30, 2007)),
  326. array('1960-10-30 02:59:43 Z', gmmktime(2, 59, 43, 10, 30, 1960)),
  327. array('1730-10-30T02:59:43Z', gmmktime(2, 59, 43, 10, 30, 1730)),
  328. array('"a \\"string\\" with \'quoted strings inside\'"', 'a "string" with \'quoted strings inside\''),
  329. array("'a \"string\" with ''quoted strings inside'''", 'a "string" with \'quoted strings inside\''),
  330. // sequences
  331. // urls are no key value mapping. see #3609. Valid yaml "key: value" mappings require a space after the colon
  332. array('[foo, http://urls.are/no/mappings, false, null, 12]', array('foo', 'http://urls.are/no/mappings', false, null, 12)),
  333. array('[ foo , bar , false , null , 12 ]', array('foo', 'bar', false, null, 12)),
  334. array('[\'foo,bar\', \'foo bar\']', array('foo,bar', 'foo bar')),
  335. // mappings
  336. array('{foo: bar,bar: foo,false: false,null: null,integer: 12}', array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12)),
  337. array('{ foo : bar, bar : foo, false : false, null : null, integer : 12 }', array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12)),
  338. array('{foo: \'bar\', bar: \'foo: bar\'}', array('foo' => 'bar', 'bar' => 'foo: bar')),
  339. array('{\'foo\': \'bar\', "bar": \'foo: bar\'}', array('foo' => 'bar', 'bar' => 'foo: bar')),
  340. array('{\'foo\'\'\': \'bar\', "bar\"": \'foo: bar\'}', array('foo\'' => 'bar', 'bar"' => 'foo: bar')),
  341. array('{\'foo: \': \'bar\', "bar: ": \'foo: bar\'}', array('foo: ' => 'bar', 'bar: ' => 'foo: bar')),
  342. // nested sequences and mappings
  343. array('[foo, [bar, foo]]', array('foo', array('bar', 'foo'))),
  344. array('[foo, {bar: foo}]', array('foo', array('bar' => 'foo'))),
  345. array('{ foo: {bar: foo} }', array('foo' => array('bar' => 'foo'))),
  346. array('{ foo: [bar, foo] }', array('foo' => array('bar', 'foo'))),
  347. array('{ foo:{bar: foo} }', array('foo' => array('bar' => 'foo'))),
  348. array('{ foo:[bar, foo] }', array('foo' => array('bar', 'foo'))),
  349. array('[ foo, [ bar, foo ] ]', array('foo', array('bar', 'foo'))),
  350. array('[{ foo: {bar: foo} }]', array(array('foo' => array('bar' => 'foo')))),
  351. array('[foo, [bar, [foo, [bar, foo]], foo]]', array('foo', array('bar', array('foo', array('bar', 'foo')), 'foo'))),
  352. 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')))),
  353. array('[foo, bar: { foo: bar }]', array('foo', '1' => array('bar' => array('foo' => 'bar')))),
  354. 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')),
  355. );
  356. }
  357. public function getTestsForParseWithMapObjects()
  358. {
  359. return array(
  360. array('', ''),
  361. array('null', null),
  362. array('false', false),
  363. array('true', true),
  364. array('12', 12),
  365. array('-12', -12),
  366. array('"quoted string"', 'quoted string'),
  367. array("'quoted string'", 'quoted string'),
  368. array('12.30e+02', 12.30e+02),
  369. array('0x4D2', 0x4D2),
  370. array('02333', 02333),
  371. array('.Inf', -log(0)),
  372. array('-.Inf', log(0)),
  373. array("'686e444'", '686e444'),
  374. array('686e444', 646e444),
  375. array('123456789123456789123456789123456789', '123456789123456789123456789123456789'),
  376. array('"foo\r\nbar"', "foo\r\nbar"),
  377. array("'foo#bar'", 'foo#bar'),
  378. array("'foo # bar'", 'foo # bar'),
  379. array("'#cfcfcf'", '#cfcfcf'),
  380. array('::form_base.html.twig', '::form_base.html.twig'),
  381. array('2007-10-30', gmmktime(0, 0, 0, 10, 30, 2007)),
  382. array('2007-10-30T02:59:43Z', gmmktime(2, 59, 43, 10, 30, 2007)),
  383. array('2007-10-30 02:59:43 Z', gmmktime(2, 59, 43, 10, 30, 2007)),
  384. array('1960-10-30 02:59:43 Z', gmmktime(2, 59, 43, 10, 30, 1960)),
  385. array('1730-10-30T02:59:43Z', gmmktime(2, 59, 43, 10, 30, 1730)),
  386. array('"a \\"string\\" with \'quoted strings inside\'"', 'a "string" with \'quoted strings inside\''),
  387. array("'a \"string\" with ''quoted strings inside'''", 'a "string" with \'quoted strings inside\''),
  388. // sequences
  389. // urls are no key value mapping. see #3609. Valid yaml "key: value" mappings require a space after the colon
  390. array('[foo, http://urls.are/no/mappings, false, null, 12]', array('foo', 'http://urls.are/no/mappings', false, null, 12)),
  391. array('[ foo , bar , false , null , 12 ]', array('foo', 'bar', false, null, 12)),
  392. array('[\'foo,bar\', \'foo bar\']', array('foo,bar', 'foo bar')),
  393. // mappings
  394. array('{foo: bar,bar: foo,false: false,null: null,integer: 12}', (object) array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12)),
  395. array('{ foo : bar, bar : foo, false : false, null : null, integer : 12 }', (object) array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12)),
  396. array('{foo: \'bar\', bar: \'foo: bar\'}', (object) array('foo' => 'bar', 'bar' => 'foo: bar')),
  397. array('{\'foo\': \'bar\', "bar": \'foo: bar\'}', (object) array('foo' => 'bar', 'bar' => 'foo: bar')),
  398. array('{\'foo\'\'\': \'bar\', "bar\"": \'foo: bar\'}', (object) array('foo\'' => 'bar', 'bar"' => 'foo: bar')),
  399. array('{\'foo: \': \'bar\', "bar: ": \'foo: bar\'}', (object) array('foo: ' => 'bar', 'bar: ' => 'foo: bar')),
  400. // nested sequences and mappings
  401. array('[foo, [bar, foo]]', array('foo', array('bar', 'foo'))),
  402. array('[foo, {bar: foo}]', array('foo', (object) array('bar' => 'foo'))),
  403. array('{ foo: {bar: foo} }', (object) array('foo' => (object) array('bar' => 'foo'))),
  404. array('{ foo: [bar, foo] }', (object) array('foo' => array('bar', 'foo'))),
  405. array('[ foo, [ bar, foo ] ]', array('foo', array('bar', 'foo'))),
  406. array('[{ foo: {bar: foo} }]', array((object) array('foo' => (object) array('bar' => 'foo')))),
  407. array('[foo, [bar, [foo, [bar, foo]], foo]]', array('foo', array('bar', array('foo', array('bar', 'foo')), 'foo'))),
  408. 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')))),
  409. array('[foo, bar: { foo: bar }]', array('foo', '1' => (object) array('bar' => (object) array('foo' => 'bar')))),
  410. 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')),
  411. array('{}', new \stdClass()),
  412. array('{ foo : bar, bar : {} }', (object) array('foo' => 'bar', 'bar' => new \stdClass())),
  413. array('{ foo : [], bar : {} }', (object) array('foo' => array(), 'bar' => new \stdClass())),
  414. array('{foo: \'bar\', bar: {} }', (object) array('foo' => 'bar', 'bar' => new \stdClass())),
  415. array('{\'foo\': \'bar\', "bar": {}}', (object) array('foo' => 'bar', 'bar' => new \stdClass())),
  416. array('{\'foo\': \'bar\', "bar": \'{}\'}', (object) array('foo' => 'bar', 'bar' => '{}')),
  417. array('[foo, [{}, {}]]', array('foo', array(new \stdClass(), new \stdClass()))),
  418. array('[foo, [[], {}]]', array('foo', array(array(), new \stdClass()))),
  419. array('[foo, [[{}, {}], {}]]', array('foo', array(array(new \stdClass(), new \stdClass()), new \stdClass()))),
  420. array('[foo, {bar: {}}]', array('foo', '1' => (object) array('bar' => new \stdClass()))),
  421. );
  422. }
  423. public function getTestsForDump()
  424. {
  425. return array(
  426. array('null', null),
  427. array('false', false),
  428. array('true', true),
  429. array('12', 12),
  430. array("'1_2'", '1_2'),
  431. array('_12', '_12'),
  432. array("'12_'", '12_'),
  433. array("'quoted string'", 'quoted string'),
  434. array('!!float 1230', 12.30e+02),
  435. array('1234', 0x4D2),
  436. array('1243', 02333),
  437. array("'0x_4_D_2_'", '0x_4_D_2_'),
  438. array("'0_2_3_3_3'", '0_2_3_3_3'),
  439. array('.Inf', -log(0)),
  440. array('-.Inf', log(0)),
  441. array("'686e444'", '686e444'),
  442. array('"foo\r\nbar"', "foo\r\nbar"),
  443. array("'foo#bar'", 'foo#bar'),
  444. array("'foo # bar'", 'foo # bar'),
  445. array("'#cfcfcf'", '#cfcfcf'),
  446. array("'a \"string\" with ''quoted strings inside'''", 'a "string" with \'quoted strings inside\''),
  447. array("'-dash'", '-dash'),
  448. array("'-'", '-'),
  449. // Pre-YAML-1.2 booleans
  450. array("'y'", 'y'),
  451. array("'n'", 'n'),
  452. array("'yes'", 'yes'),
  453. array("'no'", 'no'),
  454. array("'on'", 'on'),
  455. array("'off'", 'off'),
  456. // sequences
  457. array('[foo, bar, false, null, 12]', array('foo', 'bar', false, null, 12)),
  458. array('[\'foo,bar\', \'foo bar\']', array('foo,bar', 'foo bar')),
  459. // mappings
  460. array('{ foo: bar, bar: foo, \'false\': false, \'null\': null, integer: 12 }', array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12)),
  461. array('{ foo: bar, bar: \'foo: bar\' }', array('foo' => 'bar', 'bar' => 'foo: bar')),
  462. // nested sequences and mappings
  463. array('[foo, [bar, foo]]', array('foo', array('bar', 'foo'))),
  464. array('[foo, [bar, [foo, [bar, foo]], foo]]', array('foo', array('bar', array('foo', array('bar', 'foo')), 'foo'))),
  465. array('{ foo: { bar: foo } }', array('foo' => array('bar' => 'foo'))),
  466. array('[foo, { bar: foo }]', array('foo', array('bar' => 'foo'))),
  467. 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')))),
  468. 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')),
  469. array('{ foo: { bar: { 1: 2, baz: 3 } } }', array('foo' => array('bar' => array(1 => 2, 'baz' => 3)))),
  470. );
  471. }
  472. /**
  473. * @dataProvider getTimestampTests
  474. */
  475. public function testParseTimestampAsUnixTimestampByDefault($yaml, $year, $month, $day, $hour, $minute, $second)
  476. {
  477. $this->assertSame(gmmktime($hour, $minute, $second, $month, $day, $year), Inline::parse($yaml));
  478. }
  479. /**
  480. * @dataProvider getTimestampTests
  481. */
  482. public function testParseTimestampAsDateTimeObject($yaml, $year, $month, $day, $hour, $minute, $second, $timezone)
  483. {
  484. $expected = new \DateTime($yaml);
  485. $expected->setTimeZone(new \DateTimeZone('UTC'));
  486. $expected->setDate($year, $month, $day);
  487. if (PHP_VERSION_ID >= 70100) {
  488. $expected->setTime($hour, $minute, $second, 1000000 * ($second - (int) $second));
  489. } else {
  490. $expected->setTime($hour, $minute, $second);
  491. }
  492. $date = Inline::parse($yaml, Yaml::PARSE_DATETIME);
  493. $this->assertEquals($expected, $date);
  494. $this->assertSame($timezone, $date->format('O'));
  495. }
  496. public function getTimestampTests()
  497. {
  498. return array(
  499. 'canonical' => array('2001-12-15T02:59:43.1Z', 2001, 12, 15, 2, 59, 43.1, '+0000'),
  500. 'ISO-8601' => array('2001-12-15t21:59:43.10-05:00', 2001, 12, 16, 2, 59, 43.1, '-0500'),
  501. 'spaced' => array('2001-12-15 21:59:43.10 -5', 2001, 12, 16, 2, 59, 43.1, '-0500'),
  502. 'date' => array('2001-12-15', 2001, 12, 15, 0, 0, 0, '+0000'),
  503. );
  504. }
  505. /**
  506. * @dataProvider getTimestampTests
  507. */
  508. public function testParseNestedTimestampListAsDateTimeObject($yaml, $year, $month, $day, $hour, $minute, $second)
  509. {
  510. $expected = new \DateTime($yaml);
  511. $expected->setTimeZone(new \DateTimeZone('UTC'));
  512. $expected->setDate($year, $month, $day);
  513. if (PHP_VERSION_ID >= 70100) {
  514. $expected->setTime($hour, $minute, $second, 1000000 * ($second - (int) $second));
  515. } else {
  516. $expected->setTime($hour, $minute, $second);
  517. }
  518. $expectedNested = array('nested' => array($expected));
  519. $yamlNested = "{nested: [$yaml]}";
  520. $this->assertEquals($expectedNested, Inline::parse($yamlNested, Yaml::PARSE_DATETIME));
  521. }
  522. /**
  523. * @dataProvider getDateTimeDumpTests
  524. */
  525. public function testDumpDateTime($dateTime, $expected)
  526. {
  527. $this->assertSame($expected, Inline::dump($dateTime));
  528. }
  529. public function getDateTimeDumpTests()
  530. {
  531. $tests = array();
  532. $dateTime = new \DateTime('2001-12-15 21:59:43', new \DateTimeZone('UTC'));
  533. $tests['date-time-utc'] = array($dateTime, '2001-12-15T21:59:43+00:00');
  534. $dateTime = new \DateTimeImmutable('2001-07-15 21:59:43', new \DateTimeZone('Europe/Berlin'));
  535. $tests['immutable-date-time-europe-berlin'] = array($dateTime, '2001-07-15T21:59:43+02:00');
  536. return $tests;
  537. }
  538. /**
  539. * @dataProvider getBinaryData
  540. */
  541. public function testParseBinaryData($data)
  542. {
  543. $this->assertSame('Hello world', Inline::parse($data));
  544. }
  545. public function getBinaryData()
  546. {
  547. return array(
  548. 'enclosed with double quotes' => array('!!binary "SGVsbG8gd29ybGQ="'),
  549. 'enclosed with single quotes' => array("!!binary 'SGVsbG8gd29ybGQ='"),
  550. 'containing spaces' => array('!!binary "SGVs bG8gd 29ybGQ="'),
  551. );
  552. }
  553. /**
  554. * @dataProvider getInvalidBinaryData
  555. */
  556. public function testParseInvalidBinaryData($data, $expectedMessage)
  557. {
  558. $this->setExpectedExceptionRegExp('\Symfony\Component\Yaml\Exception\ParseException', $expectedMessage);
  559. Inline::parse($data);
  560. }
  561. public function getInvalidBinaryData()
  562. {
  563. return array(
  564. 'length not a multiple of four' => array('!!binary "SGVsbG8d29ybGQ="', '/The normalized base64 encoded data \(data without whitespace characters\) length must be a multiple of four \(\d+ bytes given\)/'),
  565. 'invalid characters' => array('!!binary "SGVsbG8#d29ybGQ="', '/The base64 encoded data \(.*\) contains invalid characters/'),
  566. 'too many equals characters' => array('!!binary "SGVsbG8gd29yb==="', '/The base64 encoded data \(.*\) contains invalid characters/'),
  567. 'misplaced equals character' => array('!!binary "SGVsbG8gd29ybG=Q"', '/The base64 encoded data \(.*\) contains invalid characters/'),
  568. );
  569. }
  570. /**
  571. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  572. * @expectedExceptionMessage Malformed inline YAML string: {this, is not, supported}.
  573. */
  574. public function testNotSupportedMissingValue()
  575. {
  576. Inline::parse('{this, is not, supported}');
  577. }
  578. public function testVeryLongQuotedStrings()
  579. {
  580. $longStringWithQuotes = str_repeat("x\r\n\\\"x\"x", 1000);
  581. $yamlString = Inline::dump(array('longStringWithQuotes' => $longStringWithQuotes));
  582. $arrayFromYaml = Inline::parse($yamlString);
  583. $this->assertEquals($longStringWithQuotes, $arrayFromYaml['longStringWithQuotes']);
  584. }
  585. }