ParserTest.php 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486
  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 PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Yaml\Exception\ParseException;
  13. use Symfony\Component\Yaml\Yaml;
  14. use Symfony\Component\Yaml\Parser;
  15. class ParserTest extends TestCase
  16. {
  17. protected $parser;
  18. protected function setUp()
  19. {
  20. $this->parser = new Parser();
  21. }
  22. protected function tearDown()
  23. {
  24. $this->parser = null;
  25. }
  26. /**
  27. * @dataProvider getDataFormSpecifications
  28. */
  29. public function testSpecifications($file, $expected, $yaml, $comment, $deprecated)
  30. {
  31. $deprecations = array();
  32. if ($deprecated) {
  33. set_error_handler(function ($type, $msg) use (&$deprecations) {
  34. if (E_USER_DEPRECATED !== $type) {
  35. restore_error_handler();
  36. if (class_exists('PHPUnit_Util_ErrorHandler')) {
  37. return call_user_func_array('PHPUnit_Util_ErrorHandler::handleError', func_get_args());
  38. }
  39. return call_user_func_array('PHPUnit\Util\ErrorHandler::handleError', func_get_args());
  40. }
  41. $deprecations[] = $msg;
  42. });
  43. }
  44. $this->assertEquals($expected, var_export($this->parser->parse($yaml), true), $comment);
  45. if ($deprecated) {
  46. restore_error_handler();
  47. $this->assertCount(1, $deprecations);
  48. $this->assertContains('Using the comma as a group separator for floats is deprecated since version 3.2 and will be removed in 4.0.', $deprecations[0]);
  49. }
  50. }
  51. public function getDataFormSpecifications()
  52. {
  53. $parser = new Parser();
  54. $path = __DIR__.'/Fixtures';
  55. $tests = array();
  56. $files = $parser->parse(file_get_contents($path.'/index.yml'));
  57. foreach ($files as $file) {
  58. $yamls = file_get_contents($path.'/'.$file.'.yml');
  59. // split YAMLs documents
  60. foreach (preg_split('/^---( %YAML\:1\.0)?/m', $yamls) as $yaml) {
  61. if (!$yaml) {
  62. continue;
  63. }
  64. $test = $parser->parse($yaml);
  65. if (isset($test['todo']) && $test['todo']) {
  66. // TODO
  67. } else {
  68. eval('$expected = '.trim($test['php']).';');
  69. $tests[] = array($file, var_export($expected, true), $test['yaml'], $test['test'], isset($test['deprecated']) ? $test['deprecated'] : false);
  70. }
  71. }
  72. }
  73. return $tests;
  74. }
  75. public function testTabsInYaml()
  76. {
  77. // test tabs in YAML
  78. $yamls = array(
  79. "foo:\n bar",
  80. "foo:\n bar",
  81. "foo:\n bar",
  82. "foo:\n bar",
  83. );
  84. foreach ($yamls as $yaml) {
  85. try {
  86. $content = $this->parser->parse($yaml);
  87. $this->fail('YAML files must not contain tabs');
  88. } catch (\Exception $e) {
  89. $this->assertInstanceOf('\Exception', $e, 'YAML files must not contain tabs');
  90. $this->assertEquals('A YAML file cannot contain tabs as indentation at line 2 (near "'.strpbrk($yaml, "\t").'").', $e->getMessage(), 'YAML files must not contain tabs');
  91. }
  92. }
  93. }
  94. public function testEndOfTheDocumentMarker()
  95. {
  96. $yaml = <<<'EOF'
  97. --- %YAML:1.0
  98. foo
  99. ...
  100. EOF;
  101. $this->assertEquals('foo', $this->parser->parse($yaml));
  102. }
  103. public function getBlockChompingTests()
  104. {
  105. $tests = array();
  106. $yaml = <<<'EOF'
  107. foo: |-
  108. one
  109. two
  110. bar: |-
  111. one
  112. two
  113. EOF;
  114. $expected = array(
  115. 'foo' => "one\ntwo",
  116. 'bar' => "one\ntwo",
  117. );
  118. $tests['Literal block chomping strip with single trailing newline'] = array($expected, $yaml);
  119. $yaml = <<<'EOF'
  120. foo: |-
  121. one
  122. two
  123. bar: |-
  124. one
  125. two
  126. EOF;
  127. $expected = array(
  128. 'foo' => "one\ntwo",
  129. 'bar' => "one\ntwo",
  130. );
  131. $tests['Literal block chomping strip with multiple trailing newlines'] = array($expected, $yaml);
  132. $yaml = <<<'EOF'
  133. {}
  134. EOF;
  135. $expected = array();
  136. $tests['Literal block chomping strip with multiple trailing newlines after a 1-liner'] = array($expected, $yaml);
  137. $yaml = <<<'EOF'
  138. foo: |-
  139. one
  140. two
  141. bar: |-
  142. one
  143. two
  144. EOF;
  145. $expected = array(
  146. 'foo' => "one\ntwo",
  147. 'bar' => "one\ntwo",
  148. );
  149. $tests['Literal block chomping strip without trailing newline'] = array($expected, $yaml);
  150. $yaml = <<<'EOF'
  151. foo: |
  152. one
  153. two
  154. bar: |
  155. one
  156. two
  157. EOF;
  158. $expected = array(
  159. 'foo' => "one\ntwo\n",
  160. 'bar' => "one\ntwo\n",
  161. );
  162. $tests['Literal block chomping clip with single trailing newline'] = array($expected, $yaml);
  163. $yaml = <<<'EOF'
  164. foo: |
  165. one
  166. two
  167. bar: |
  168. one
  169. two
  170. EOF;
  171. $expected = array(
  172. 'foo' => "one\ntwo\n",
  173. 'bar' => "one\ntwo\n",
  174. );
  175. $tests['Literal block chomping clip with multiple trailing newlines'] = array($expected, $yaml);
  176. $yaml = <<<'EOF'
  177. foo:
  178. - bar: |
  179. one
  180. two
  181. EOF;
  182. $expected = array(
  183. 'foo' => array(
  184. array(
  185. 'bar' => "one\n\ntwo",
  186. ),
  187. ),
  188. );
  189. $tests['Literal block chomping clip with embedded blank line inside unindented collection'] = array($expected, $yaml);
  190. $yaml = <<<'EOF'
  191. foo: |
  192. one
  193. two
  194. bar: |
  195. one
  196. two
  197. EOF;
  198. $expected = array(
  199. 'foo' => "one\ntwo\n",
  200. 'bar' => "one\ntwo",
  201. );
  202. $tests['Literal block chomping clip without trailing newline'] = array($expected, $yaml);
  203. $yaml = <<<'EOF'
  204. foo: |+
  205. one
  206. two
  207. bar: |+
  208. one
  209. two
  210. EOF;
  211. $expected = array(
  212. 'foo' => "one\ntwo\n",
  213. 'bar' => "one\ntwo\n",
  214. );
  215. $tests['Literal block chomping keep with single trailing newline'] = array($expected, $yaml);
  216. $yaml = <<<'EOF'
  217. foo: |+
  218. one
  219. two
  220. bar: |+
  221. one
  222. two
  223. EOF;
  224. $expected = array(
  225. 'foo' => "one\ntwo\n\n",
  226. 'bar' => "one\ntwo\n\n",
  227. );
  228. $tests['Literal block chomping keep with multiple trailing newlines'] = array($expected, $yaml);
  229. $yaml = <<<'EOF'
  230. foo: |+
  231. one
  232. two
  233. bar: |+
  234. one
  235. two
  236. EOF;
  237. $expected = array(
  238. 'foo' => "one\ntwo\n",
  239. 'bar' => "one\ntwo",
  240. );
  241. $tests['Literal block chomping keep without trailing newline'] = array($expected, $yaml);
  242. $yaml = <<<'EOF'
  243. foo: >-
  244. one
  245. two
  246. bar: >-
  247. one
  248. two
  249. EOF;
  250. $expected = array(
  251. 'foo' => 'one two',
  252. 'bar' => 'one two',
  253. );
  254. $tests['Folded block chomping strip with single trailing newline'] = array($expected, $yaml);
  255. $yaml = <<<'EOF'
  256. foo: >-
  257. one
  258. two
  259. bar: >-
  260. one
  261. two
  262. EOF;
  263. $expected = array(
  264. 'foo' => 'one two',
  265. 'bar' => 'one two',
  266. );
  267. $tests['Folded block chomping strip with multiple trailing newlines'] = array($expected, $yaml);
  268. $yaml = <<<'EOF'
  269. foo: >-
  270. one
  271. two
  272. bar: >-
  273. one
  274. two
  275. EOF;
  276. $expected = array(
  277. 'foo' => 'one two',
  278. 'bar' => 'one two',
  279. );
  280. $tests['Folded block chomping strip without trailing newline'] = array($expected, $yaml);
  281. $yaml = <<<'EOF'
  282. foo: >
  283. one
  284. two
  285. bar: >
  286. one
  287. two
  288. EOF;
  289. $expected = array(
  290. 'foo' => "one two\n",
  291. 'bar' => "one two\n",
  292. );
  293. $tests['Folded block chomping clip with single trailing newline'] = array($expected, $yaml);
  294. $yaml = <<<'EOF'
  295. foo: >
  296. one
  297. two
  298. bar: >
  299. one
  300. two
  301. EOF;
  302. $expected = array(
  303. 'foo' => "one two\n",
  304. 'bar' => "one two\n",
  305. );
  306. $tests['Folded block chomping clip with multiple trailing newlines'] = array($expected, $yaml);
  307. $yaml = <<<'EOF'
  308. foo: >
  309. one
  310. two
  311. bar: >
  312. one
  313. two
  314. EOF;
  315. $expected = array(
  316. 'foo' => "one two\n",
  317. 'bar' => 'one two',
  318. );
  319. $tests['Folded block chomping clip without trailing newline'] = array($expected, $yaml);
  320. $yaml = <<<'EOF'
  321. foo: >+
  322. one
  323. two
  324. bar: >+
  325. one
  326. two
  327. EOF;
  328. $expected = array(
  329. 'foo' => "one two\n",
  330. 'bar' => "one two\n",
  331. );
  332. $tests['Folded block chomping keep with single trailing newline'] = array($expected, $yaml);
  333. $yaml = <<<'EOF'
  334. foo: >+
  335. one
  336. two
  337. bar: >+
  338. one
  339. two
  340. EOF;
  341. $expected = array(
  342. 'foo' => "one two\n\n",
  343. 'bar' => "one two\n\n",
  344. );
  345. $tests['Folded block chomping keep with multiple trailing newlines'] = array($expected, $yaml);
  346. $yaml = <<<'EOF'
  347. foo: >+
  348. one
  349. two
  350. bar: >+
  351. one
  352. two
  353. EOF;
  354. $expected = array(
  355. 'foo' => "one two\n",
  356. 'bar' => 'one two',
  357. );
  358. $tests['Folded block chomping keep without trailing newline'] = array($expected, $yaml);
  359. return $tests;
  360. }
  361. /**
  362. * @dataProvider getBlockChompingTests
  363. */
  364. public function testBlockChomping($expected, $yaml)
  365. {
  366. $this->assertSame($expected, $this->parser->parse($yaml));
  367. }
  368. /**
  369. * Regression test for issue #7989.
  370. *
  371. * @see https://github.com/symfony/symfony/issues/7989
  372. */
  373. public function testBlockLiteralWithLeadingNewlines()
  374. {
  375. $yaml = <<<'EOF'
  376. foo: |-
  377. bar
  378. EOF;
  379. $expected = array(
  380. 'foo' => "\n\nbar",
  381. );
  382. $this->assertSame($expected, $this->parser->parse($yaml));
  383. }
  384. public function testObjectSupportEnabled()
  385. {
  386. $input = <<<'EOF'
  387. foo: !php/object:O:30:"Symfony\Component\Yaml\Tests\B":1:{s:1:"b";s:3:"foo";}
  388. bar: 1
  389. EOF;
  390. $this->assertEquals(array('foo' => new B(), 'bar' => 1), $this->parser->parse($input, Yaml::PARSE_OBJECT), '->parse() is able to parse objects');
  391. }
  392. /**
  393. * @group legacy
  394. */
  395. public function testObjectSupportEnabledPassingTrue()
  396. {
  397. $input = <<<'EOF'
  398. foo: !php/object:O:30:"Symfony\Component\Yaml\Tests\B":1:{s:1:"b";s:3:"foo";}
  399. bar: 1
  400. EOF;
  401. $this->assertEquals(array('foo' => new B(), 'bar' => 1), $this->parser->parse($input, false, true), '->parse() is able to parse objects');
  402. }
  403. /**
  404. * @group legacy
  405. */
  406. public function testObjectSupportEnabledWithDeprecatedTag()
  407. {
  408. $input = <<<'EOF'
  409. foo: !!php/object:O:30:"Symfony\Component\Yaml\Tests\B":1:{s:1:"b";s:3:"foo";}
  410. bar: 1
  411. EOF;
  412. $this->assertEquals(array('foo' => new B(), 'bar' => 1), $this->parser->parse($input, Yaml::PARSE_OBJECT), '->parse() is able to parse objects');
  413. }
  414. /**
  415. * @dataProvider invalidDumpedObjectProvider
  416. */
  417. public function testObjectSupportDisabledButNoExceptions($input)
  418. {
  419. $this->assertEquals(array('foo' => null, 'bar' => 1), $this->parser->parse($input), '->parse() does not parse objects');
  420. }
  421. /**
  422. * @dataProvider getObjectForMapTests
  423. */
  424. public function testObjectForMap($yaml, $expected)
  425. {
  426. $this->assertEquals($expected, $this->parser->parse($yaml, Yaml::PARSE_OBJECT_FOR_MAP));
  427. }
  428. /**
  429. * @group legacy
  430. * @dataProvider getObjectForMapTests
  431. */
  432. public function testObjectForMapEnabledWithMappingUsingBooleanToggles($yaml, $expected)
  433. {
  434. $this->assertEquals($expected, $this->parser->parse($yaml, false, false, true));
  435. }
  436. public function getObjectForMapTests()
  437. {
  438. $tests = array();
  439. $yaml = <<<'EOF'
  440. foo:
  441. fiz: [cat]
  442. EOF;
  443. $expected = new \stdClass();
  444. $expected->foo = new \stdClass();
  445. $expected->foo->fiz = array('cat');
  446. $tests['mapping'] = array($yaml, $expected);
  447. $yaml = '{ "foo": "bar", "fiz": "cat" }';
  448. $expected = new \stdClass();
  449. $expected->foo = 'bar';
  450. $expected->fiz = 'cat';
  451. $tests['inline-mapping'] = array($yaml, $expected);
  452. $yaml = "foo: bar\nbaz: foobar";
  453. $expected = new \stdClass();
  454. $expected->foo = 'bar';
  455. $expected->baz = 'foobar';
  456. $tests['object-for-map-is-applied-after-parsing'] = array($yaml, $expected);
  457. $yaml = <<<'EOT'
  458. array:
  459. - key: one
  460. - key: two
  461. EOT;
  462. $expected = new \stdClass();
  463. $expected->array = array();
  464. $expected->array[0] = new \stdClass();
  465. $expected->array[0]->key = 'one';
  466. $expected->array[1] = new \stdClass();
  467. $expected->array[1]->key = 'two';
  468. $tests['nest-map-and-sequence'] = array($yaml, $expected);
  469. $yaml = <<<'YAML'
  470. map:
  471. 1: one
  472. 2: two
  473. YAML;
  474. $expected = new \stdClass();
  475. $expected->map = new \stdClass();
  476. $expected->map->{1} = 'one';
  477. $expected->map->{2} = 'two';
  478. $tests['numeric-keys'] = array($yaml, $expected);
  479. $yaml = <<<'YAML'
  480. map:
  481. 0: one
  482. 1: two
  483. YAML;
  484. $expected = new \stdClass();
  485. $expected->map = new \stdClass();
  486. $expected->map->{0} = 'one';
  487. $expected->map->{1} = 'two';
  488. $tests['zero-indexed-numeric-keys'] = array($yaml, $expected);
  489. return $tests;
  490. }
  491. /**
  492. * @dataProvider invalidDumpedObjectProvider
  493. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  494. */
  495. public function testObjectsSupportDisabledWithExceptions($yaml)
  496. {
  497. $this->parser->parse($yaml, Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE);
  498. }
  499. /**
  500. * @group legacy
  501. * @dataProvider invalidDumpedObjectProvider
  502. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  503. */
  504. public function testObjectsSupportDisabledWithExceptionsUsingBooleanToggles($yaml)
  505. {
  506. $this->parser->parse($yaml, true);
  507. }
  508. public function invalidDumpedObjectProvider()
  509. {
  510. $yamlTag = <<<'EOF'
  511. foo: !!php/object:O:30:"Symfony\Tests\Component\Yaml\B":1:{s:1:"b";s:3:"foo";}
  512. bar: 1
  513. EOF;
  514. $localTag = <<<'EOF'
  515. foo: !php/object:O:30:"Symfony\Tests\Component\Yaml\B":1:{s:1:"b";s:3:"foo";}
  516. bar: 1
  517. EOF;
  518. return array(
  519. 'yaml-tag' => array($yamlTag),
  520. 'local-tag' => array($localTag),
  521. );
  522. }
  523. /**
  524. * @requires extension iconv
  525. */
  526. public function testNonUtf8Exception()
  527. {
  528. $yamls = array(
  529. iconv('UTF-8', 'ISO-8859-1', "foo: 'äöüß'"),
  530. iconv('UTF-8', 'ISO-8859-15', "euro: '€'"),
  531. iconv('UTF-8', 'CP1252', "cp1252: '©ÉÇáñ'"),
  532. );
  533. foreach ($yamls as $yaml) {
  534. try {
  535. $this->parser->parse($yaml);
  536. $this->fail('charsets other than UTF-8 are rejected.');
  537. } catch (\Exception $e) {
  538. $this->assertInstanceOf('Symfony\Component\Yaml\Exception\ParseException', $e, 'charsets other than UTF-8 are rejected.');
  539. }
  540. }
  541. }
  542. /**
  543. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  544. */
  545. public function testUnindentedCollectionException()
  546. {
  547. $yaml = <<<'EOF'
  548. collection:
  549. -item1
  550. -item2
  551. -item3
  552. EOF;
  553. $this->parser->parse($yaml);
  554. }
  555. /**
  556. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  557. */
  558. public function testShortcutKeyUnindentedCollectionException()
  559. {
  560. $yaml = <<<'EOF'
  561. collection:
  562. - key: foo
  563. foo: bar
  564. EOF;
  565. $this->parser->parse($yaml);
  566. }
  567. /**
  568. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  569. * @expectedExceptionMessageRegExp /^Multiple documents are not supported.+/
  570. */
  571. public function testMultipleDocumentsNotSupportedException()
  572. {
  573. Yaml::parse(<<<'EOL'
  574. # Ranking of 1998 home runs
  575. ---
  576. - Mark McGwire
  577. - Sammy Sosa
  578. - Ken Griffey
  579. # Team ranking
  580. ---
  581. - Chicago Cubs
  582. - St Louis Cardinals
  583. EOL
  584. );
  585. }
  586. /**
  587. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  588. */
  589. public function testSequenceInAMapping()
  590. {
  591. Yaml::parse(<<<'EOF'
  592. yaml:
  593. hash: me
  594. - array stuff
  595. EOF
  596. );
  597. }
  598. public function testSequenceInMappingStartedBySingleDashLine()
  599. {
  600. $yaml = <<<'EOT'
  601. a:
  602. -
  603. b:
  604. -
  605. bar: baz
  606. - foo
  607. d: e
  608. EOT;
  609. $expected = array(
  610. 'a' => array(
  611. array(
  612. 'b' => array(
  613. array(
  614. 'bar' => 'baz',
  615. ),
  616. ),
  617. ),
  618. 'foo',
  619. ),
  620. 'd' => 'e',
  621. );
  622. $this->assertSame($expected, $this->parser->parse($yaml));
  623. }
  624. public function testSequenceFollowedByCommentEmbeddedInMapping()
  625. {
  626. $yaml = <<<'EOT'
  627. a:
  628. b:
  629. - c
  630. # comment
  631. d: e
  632. EOT;
  633. $expected = array(
  634. 'a' => array(
  635. 'b' => array('c'),
  636. 'd' => 'e',
  637. ),
  638. );
  639. $this->assertSame($expected, $this->parser->parse($yaml));
  640. }
  641. /**
  642. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  643. */
  644. public function testMappingInASequence()
  645. {
  646. Yaml::parse(<<<'EOF'
  647. yaml:
  648. - array stuff
  649. hash: me
  650. EOF
  651. );
  652. }
  653. /**
  654. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  655. * @expectedExceptionMessage missing colon
  656. */
  657. public function testScalarInSequence()
  658. {
  659. Yaml::parse(<<<'EOF'
  660. foo:
  661. - bar
  662. "missing colon"
  663. foo: bar
  664. EOF
  665. );
  666. }
  667. /**
  668. * > It is an error for two equal keys to appear in the same mapping node.
  669. * > In such a case the YAML processor may continue, ignoring the second
  670. * > `key: value` pair and issuing an appropriate warning. This strategy
  671. * > preserves a consistent information model for one-pass and random access
  672. * > applications.
  673. *
  674. * @see http://yaml.org/spec/1.2/spec.html#id2759572
  675. * @see http://yaml.org/spec/1.1/#id932806
  676. * @group legacy
  677. */
  678. public function testMappingDuplicateKeyBlock()
  679. {
  680. $input = <<<'EOD'
  681. parent:
  682. child: first
  683. child: duplicate
  684. parent:
  685. child: duplicate
  686. child: duplicate
  687. EOD;
  688. $expected = array(
  689. 'parent' => array(
  690. 'child' => 'first',
  691. ),
  692. );
  693. $this->assertSame($expected, Yaml::parse($input));
  694. }
  695. /**
  696. * @group legacy
  697. */
  698. public function testMappingDuplicateKeyFlow()
  699. {
  700. $input = <<<'EOD'
  701. parent: { child: first, child: duplicate }
  702. parent: { child: duplicate, child: duplicate }
  703. EOD;
  704. $expected = array(
  705. 'parent' => array(
  706. 'child' => 'first',
  707. ),
  708. );
  709. $this->assertSame($expected, Yaml::parse($input));
  710. }
  711. /**
  712. * @group legacy
  713. * @dataProvider getParseExceptionOnDuplicateData
  714. * @expectedDeprecation Duplicate key "%s" detected on line %d whilst parsing YAML. Silent handling of duplicate mapping keys in YAML is deprecated %s.
  715. * throws \Symfony\Component\Yaml\Exception\ParseException in 4.0
  716. */
  717. public function testParseExceptionOnDuplicate($input, $duplicateKey, $lineNumber)
  718. {
  719. Yaml::parse($input);
  720. }
  721. public function getParseExceptionOnDuplicateData()
  722. {
  723. $tests = array();
  724. $yaml = <<<EOD
  725. parent: { child: first, child: duplicate }
  726. EOD;
  727. $tests[] = array($yaml, 'child', 1);
  728. $yaml = <<<EOD
  729. parent:
  730. child: first,
  731. child: duplicate
  732. EOD;
  733. $tests[] = array($yaml, 'child', 3);
  734. $yaml = <<<EOD
  735. parent: { child: foo }
  736. parent: { child: bar }
  737. EOD;
  738. $tests[] = array($yaml, 'parent', 2);
  739. $yaml = <<<EOD
  740. parent: { child_mapping: { value: bar}, child_mapping: { value: bar} }
  741. EOD;
  742. $tests[] = array($yaml, 'child_mapping', 1);
  743. $yaml = <<<EOD
  744. parent:
  745. child_mapping:
  746. value: bar
  747. child_mapping:
  748. value: bar
  749. EOD;
  750. $tests[] = array($yaml, 'child_mapping', 4);
  751. $yaml = <<<EOD
  752. parent: { child_sequence: ['key1', 'key2', 'key3'], child_sequence: ['key1', 'key2', 'key3'] }
  753. EOD;
  754. $tests[] = array($yaml, 'child_sequence', 1);
  755. $yaml = <<<EOD
  756. parent:
  757. child_sequence:
  758. - key1
  759. - key2
  760. - key3
  761. child_sequence:
  762. - key1
  763. - key2
  764. - key3
  765. EOD;
  766. $tests[] = array($yaml, 'child_sequence', 6);
  767. return $tests;
  768. }
  769. public function testEmptyValue()
  770. {
  771. $input = <<<'EOF'
  772. hash:
  773. EOF;
  774. $this->assertEquals(array('hash' => null), Yaml::parse($input));
  775. }
  776. public function testCommentAtTheRootIndent()
  777. {
  778. $this->assertEquals(array(
  779. 'services' => array(
  780. 'app.foo_service' => array(
  781. 'class' => 'Foo',
  782. ),
  783. 'app/bar_service' => array(
  784. 'class' => 'Bar',
  785. ),
  786. ),
  787. ), Yaml::parse(<<<'EOF'
  788. # comment 1
  789. services:
  790. # comment 2
  791. # comment 3
  792. app.foo_service:
  793. class: Foo
  794. # comment 4
  795. # comment 5
  796. app/bar_service:
  797. class: Bar
  798. EOF
  799. ));
  800. }
  801. public function testStringBlockWithComments()
  802. {
  803. $this->assertEquals(array('content' => <<<'EOT'
  804. # comment 1
  805. header
  806. # comment 2
  807. <body>
  808. <h1>title</h1>
  809. </body>
  810. footer # comment3
  811. EOT
  812. ), Yaml::parse(<<<'EOF'
  813. content: |
  814. # comment 1
  815. header
  816. # comment 2
  817. <body>
  818. <h1>title</h1>
  819. </body>
  820. footer # comment3
  821. EOF
  822. ));
  823. }
  824. public function testFoldedStringBlockWithComments()
  825. {
  826. $this->assertEquals(array(array('content' => <<<'EOT'
  827. # comment 1
  828. header
  829. # comment 2
  830. <body>
  831. <h1>title</h1>
  832. </body>
  833. footer # comment3
  834. EOT
  835. )), Yaml::parse(<<<'EOF'
  836. -
  837. content: |
  838. # comment 1
  839. header
  840. # comment 2
  841. <body>
  842. <h1>title</h1>
  843. </body>
  844. footer # comment3
  845. EOF
  846. ));
  847. }
  848. public function testNestedFoldedStringBlockWithComments()
  849. {
  850. $this->assertEquals(array(array(
  851. 'title' => 'some title',
  852. 'content' => <<<'EOT'
  853. # comment 1
  854. header
  855. # comment 2
  856. <body>
  857. <h1>title</h1>
  858. </body>
  859. footer # comment3
  860. EOT
  861. )), Yaml::parse(<<<'EOF'
  862. -
  863. title: some title
  864. content: |
  865. # comment 1
  866. header
  867. # comment 2
  868. <body>
  869. <h1>title</h1>
  870. </body>
  871. footer # comment3
  872. EOF
  873. ));
  874. }
  875. public function testReferenceResolvingInInlineStrings()
  876. {
  877. $this->assertEquals(array(
  878. 'var' => 'var-value',
  879. 'scalar' => 'var-value',
  880. 'list' => array('var-value'),
  881. 'list_in_list' => array(array('var-value')),
  882. 'map_in_list' => array(array('key' => 'var-value')),
  883. 'embedded_mapping' => array(array('key' => 'var-value')),
  884. 'map' => array('key' => 'var-value'),
  885. 'list_in_map' => array('key' => array('var-value')),
  886. 'map_in_map' => array('foo' => array('bar' => 'var-value')),
  887. ), Yaml::parse(<<<'EOF'
  888. var: &var var-value
  889. scalar: *var
  890. list: [ *var ]
  891. list_in_list: [[ *var ]]
  892. map_in_list: [ { key: *var } ]
  893. embedded_mapping: [ key: *var ]
  894. map: { key: *var }
  895. list_in_map: { key: [*var] }
  896. map_in_map: { foo: { bar: *var } }
  897. EOF
  898. ));
  899. }
  900. public function testYamlDirective()
  901. {
  902. $yaml = <<<'EOF'
  903. %YAML 1.2
  904. ---
  905. foo: 1
  906. bar: 2
  907. EOF;
  908. $this->assertEquals(array('foo' => 1, 'bar' => 2), $this->parser->parse($yaml));
  909. }
  910. public function testFloatKeys()
  911. {
  912. $yaml = <<<'EOF'
  913. foo:
  914. 1.2: "bar"
  915. 1.3: "baz"
  916. EOF;
  917. $expected = array(
  918. 'foo' => array(
  919. '1.2' => 'bar',
  920. '1.3' => 'baz',
  921. ),
  922. );
  923. $this->assertEquals($expected, $this->parser->parse($yaml));
  924. }
  925. /**
  926. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  927. * @expectedExceptionMessage A colon cannot be used in an unquoted mapping value
  928. */
  929. public function testColonInMappingValueException()
  930. {
  931. $yaml = <<<'EOF'
  932. foo: bar: baz
  933. EOF;
  934. $this->parser->parse($yaml);
  935. }
  936. public function testColonInMappingValueExceptionNotTriggeredByColonInComment()
  937. {
  938. $yaml = <<<'EOT'
  939. foo:
  940. bar: foobar # Note: a comment after a colon
  941. EOT;
  942. $this->assertSame(array('foo' => array('bar' => 'foobar')), $this->parser->parse($yaml));
  943. }
  944. /**
  945. * @dataProvider getCommentLikeStringInScalarBlockData
  946. */
  947. public function testCommentLikeStringsAreNotStrippedInBlockScalars($yaml, $expectedParserResult)
  948. {
  949. $this->assertSame($expectedParserResult, $this->parser->parse($yaml));
  950. }
  951. public function getCommentLikeStringInScalarBlockData()
  952. {
  953. $tests = array();
  954. $yaml = <<<'EOT'
  955. pages:
  956. -
  957. title: some title
  958. content: |
  959. # comment 1
  960. header
  961. # comment 2
  962. <body>
  963. <h1>title</h1>
  964. </body>
  965. footer # comment3
  966. EOT;
  967. $expected = array(
  968. 'pages' => array(
  969. array(
  970. 'title' => 'some title',
  971. 'content' => <<<'EOT'
  972. # comment 1
  973. header
  974. # comment 2
  975. <body>
  976. <h1>title</h1>
  977. </body>
  978. footer # comment3
  979. EOT
  980. ,
  981. ),
  982. ),
  983. );
  984. $tests[] = array($yaml, $expected);
  985. $yaml = <<<'EOT'
  986. test: |
  987. foo
  988. # bar
  989. baz
  990. collection:
  991. - one: |
  992. foo
  993. # bar
  994. baz
  995. - two: |
  996. foo
  997. # bar
  998. baz
  999. EOT;
  1000. $expected = array(
  1001. 'test' => <<<'EOT'
  1002. foo
  1003. # bar
  1004. baz
  1005. EOT
  1006. ,
  1007. 'collection' => array(
  1008. array(
  1009. 'one' => <<<'EOT'
  1010. foo
  1011. # bar
  1012. baz
  1013. EOT
  1014. ,
  1015. ),
  1016. array(
  1017. 'two' => <<<'EOT'
  1018. foo
  1019. # bar
  1020. baz
  1021. EOT
  1022. ,
  1023. ),
  1024. ),
  1025. );
  1026. $tests[] = array($yaml, $expected);
  1027. $yaml = <<<'EOT'
  1028. foo:
  1029. bar:
  1030. scalar-block: >
  1031. line1
  1032. line2>
  1033. baz:
  1034. # comment
  1035. foobar: ~
  1036. EOT;
  1037. $expected = array(
  1038. 'foo' => array(
  1039. 'bar' => array(
  1040. 'scalar-block' => "line1 line2>\n",
  1041. ),
  1042. 'baz' => array(
  1043. 'foobar' => null,
  1044. ),
  1045. ),
  1046. );
  1047. $tests[] = array($yaml, $expected);
  1048. $yaml = <<<'EOT'
  1049. a:
  1050. b: hello
  1051. # c: |
  1052. # first row
  1053. # second row
  1054. d: hello
  1055. EOT;
  1056. $expected = array(
  1057. 'a' => array(
  1058. 'b' => 'hello',
  1059. 'd' => 'hello',
  1060. ),
  1061. );
  1062. $tests[] = array($yaml, $expected);
  1063. return $tests;
  1064. }
  1065. public function testBlankLinesAreParsedAsNewLinesInFoldedBlocks()
  1066. {
  1067. $yaml = <<<'EOT'
  1068. test: >
  1069. <h2>A heading</h2>
  1070. <ul>
  1071. <li>a list</li>
  1072. <li>may be a good example</li>
  1073. </ul>
  1074. EOT;
  1075. $this->assertSame(
  1076. array(
  1077. 'test' => <<<'EOT'
  1078. <h2>A heading</h2>
  1079. <ul> <li>a list</li> <li>may be a good example</li> </ul>
  1080. EOT
  1081. ,
  1082. ),
  1083. $this->parser->parse($yaml)
  1084. );
  1085. }
  1086. public function testAdditionallyIndentedLinesAreParsedAsNewLinesInFoldedBlocks()
  1087. {
  1088. $yaml = <<<'EOT'
  1089. test: >
  1090. <h2>A heading</h2>
  1091. <ul>
  1092. <li>a list</li>
  1093. <li>may be a good example</li>
  1094. </ul>
  1095. EOT;
  1096. $this->assertSame(
  1097. array(
  1098. 'test' => <<<'EOT'
  1099. <h2>A heading</h2>
  1100. <ul>
  1101. <li>a list</li>
  1102. <li>may be a good example</li>
  1103. </ul>
  1104. EOT
  1105. ,
  1106. ),
  1107. $this->parser->parse($yaml)
  1108. );
  1109. }
  1110. /**
  1111. * @dataProvider getBinaryData
  1112. */
  1113. public function testParseBinaryData($data)
  1114. {
  1115. $this->assertSame(array('data' => 'Hello world'), $this->parser->parse($data));
  1116. }
  1117. public function getBinaryData()
  1118. {
  1119. return array(
  1120. 'enclosed with double quotes' => array('data: !!binary "SGVsbG8gd29ybGQ="'),
  1121. 'enclosed with single quotes' => array("data: !!binary 'SGVsbG8gd29ybGQ='"),
  1122. 'containing spaces' => array('data: !!binary "SGVs bG8gd 29ybGQ="'),
  1123. 'in block scalar' => array(
  1124. <<<'EOT'
  1125. data: !!binary |
  1126. SGVsbG8gd29ybGQ=
  1127. EOT
  1128. ),
  1129. 'containing spaces in block scalar' => array(
  1130. <<<'EOT'
  1131. data: !!binary |
  1132. SGVs bG8gd 29ybGQ=
  1133. EOT
  1134. ),
  1135. );
  1136. }
  1137. /**
  1138. * @dataProvider getInvalidBinaryData
  1139. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  1140. */
  1141. public function testParseInvalidBinaryData($data, $expectedMessage)
  1142. {
  1143. if (method_exists($this, 'expectException')) {
  1144. $this->expectExceptionMessageRegExp($expectedMessage);
  1145. } else {
  1146. $this->setExpectedExceptionRegExp(ParseException::class, $expectedMessage);
  1147. }
  1148. $this->parser->parse($data);
  1149. }
  1150. public function getInvalidBinaryData()
  1151. {
  1152. return array(
  1153. 'length not a multiple of four' => array('data: !!binary "SGVsbG8d29ybGQ="', '/The normalized base64 encoded data \(data without whitespace characters\) length must be a multiple of four \(\d+ bytes given\)/'),
  1154. 'invalid characters' => array('!!binary "SGVsbG8#d29ybGQ="', '/The base64 encoded data \(.*\) contains invalid characters/'),
  1155. 'too many equals characters' => array('data: !!binary "SGVsbG8gd29yb==="', '/The base64 encoded data \(.*\) contains invalid characters/'),
  1156. 'misplaced equals character' => array('data: !!binary "SGVsbG8gd29ybG=Q"', '/The base64 encoded data \(.*\) contains invalid characters/'),
  1157. 'length not a multiple of four in block scalar' => array(
  1158. <<<'EOT'
  1159. data: !!binary |
  1160. SGVsbG8d29ybGQ=
  1161. EOT
  1162. ,
  1163. '/The normalized base64 encoded data \(data without whitespace characters\) length must be a multiple of four \(\d+ bytes given\)/',
  1164. ),
  1165. 'invalid characters in block scalar' => array(
  1166. <<<'EOT'
  1167. data: !!binary |
  1168. SGVsbG8#d29ybGQ=
  1169. EOT
  1170. ,
  1171. '/The base64 encoded data \(.*\) contains invalid characters/',
  1172. ),
  1173. 'too many equals characters in block scalar' => array(
  1174. <<<'EOT'
  1175. data: !!binary |
  1176. SGVsbG8gd29yb===
  1177. EOT
  1178. ,
  1179. '/The base64 encoded data \(.*\) contains invalid characters/',
  1180. ),
  1181. 'misplaced equals character in block scalar' => array(
  1182. <<<'EOT'
  1183. data: !!binary |
  1184. SGVsbG8gd29ybG=Q
  1185. EOT
  1186. ,
  1187. '/The base64 encoded data \(.*\) contains invalid characters/',
  1188. ),
  1189. );
  1190. }
  1191. public function testParseDateAsMappingValue()
  1192. {
  1193. $yaml = <<<'EOT'
  1194. date: 2002-12-14
  1195. EOT;
  1196. $expectedDate = new \DateTime();
  1197. $expectedDate->setTimeZone(new \DateTimeZone('UTC'));
  1198. $expectedDate->setDate(2002, 12, 14);
  1199. $expectedDate->setTime(0, 0, 0);
  1200. $this->assertEquals(array('date' => $expectedDate), $this->parser->parse($yaml, Yaml::PARSE_DATETIME));
  1201. }
  1202. /**
  1203. * @param $lineNumber
  1204. * @param $yaml
  1205. * @dataProvider parserThrowsExceptionWithCorrectLineNumberProvider
  1206. */
  1207. public function testParserThrowsExceptionWithCorrectLineNumber($lineNumber, $yaml)
  1208. {
  1209. if (method_exists($this, 'expectException')) {
  1210. $this->expectException('\Symfony\Component\Yaml\Exception\ParseException');
  1211. $this->expectExceptionMessage(sprintf('Unexpected characters near "," at line %d (near "bar: "123",").', $lineNumber));
  1212. } else {
  1213. $this->setExpectedException('\Symfony\Component\Yaml\Exception\ParseException', sprintf('Unexpected characters near "," at line %d (near "bar: "123",").', $lineNumber));
  1214. }
  1215. $this->parser->parse($yaml);
  1216. }
  1217. public function parserThrowsExceptionWithCorrectLineNumberProvider()
  1218. {
  1219. return array(
  1220. array(
  1221. 4,
  1222. <<<'YAML'
  1223. foo:
  1224. -
  1225. # bar
  1226. bar: "123",
  1227. YAML
  1228. ),
  1229. array(
  1230. 5,
  1231. <<<'YAML'
  1232. foo:
  1233. -
  1234. # bar
  1235. # bar
  1236. bar: "123",
  1237. YAML
  1238. ),
  1239. array(
  1240. 8,
  1241. <<<'YAML'
  1242. foo:
  1243. -
  1244. # foobar
  1245. baz: 123
  1246. bar:
  1247. -
  1248. # bar
  1249. bar: "123",
  1250. YAML
  1251. ),
  1252. array(
  1253. 10,
  1254. <<<'YAML'
  1255. foo:
  1256. -
  1257. # foobar
  1258. # foobar
  1259. baz: 123
  1260. bar:
  1261. -
  1262. # bar
  1263. # bar
  1264. bar: "123",
  1265. YAML
  1266. ),
  1267. );
  1268. }
  1269. public function testParseMultiLineQuotedString()
  1270. {
  1271. $yaml = <<<EOT
  1272. foo: "bar
  1273. baz
  1274. foobar
  1275. foo"
  1276. bar: baz
  1277. EOT;
  1278. $this->assertSame(array('foo' => 'bar baz foobar foo', 'bar' => 'baz'), $this->parser->parse($yaml));
  1279. }
  1280. public function testParseMultiLineUnquotedString()
  1281. {
  1282. $yaml = <<<EOT
  1283. foo: bar
  1284. baz
  1285. foobar
  1286. foo
  1287. bar: baz
  1288. EOT;
  1289. $this->assertSame(array('foo' => 'bar baz foobar foo', 'bar' => 'baz'), $this->parser->parse($yaml));
  1290. }
  1291. }
  1292. class B
  1293. {
  1294. public $b = 'foo';
  1295. }