ParserTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  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\Yaml;
  12. use Symfony\Component\Yaml\Parser;
  13. class ParserTest extends \PHPUnit_Framework_TestCase
  14. {
  15. protected $parser;
  16. protected function setUp()
  17. {
  18. $this->parser = new Parser();
  19. }
  20. protected function tearDown()
  21. {
  22. $this->parser = null;
  23. }
  24. /**
  25. * @dataProvider getDataFormSpecifications
  26. */
  27. public function testSpecifications($file, $expected, $yaml, $comment)
  28. {
  29. $this->assertEquals($expected, var_export($this->parser->parse($yaml), true), $comment);
  30. }
  31. public function getDataFormSpecifications()
  32. {
  33. $parser = new Parser();
  34. $path = __DIR__.'/Fixtures';
  35. $tests = array();
  36. $files = $parser->parse(file_get_contents($path.'/index.yml'));
  37. foreach ($files as $file) {
  38. $yamls = file_get_contents($path.'/'.$file.'.yml');
  39. // split YAMLs documents
  40. foreach (preg_split('/^---( %YAML\:1\.0)?/m', $yamls) as $yaml) {
  41. if (!$yaml) {
  42. continue;
  43. }
  44. $test = $parser->parse($yaml);
  45. if (isset($test['todo']) && $test['todo']) {
  46. // TODO
  47. } else {
  48. eval('$expected = '.trim($test['php']).';');
  49. $tests[] = array($file, var_export($expected, true), $test['yaml'], $test['test']);
  50. }
  51. }
  52. }
  53. return $tests;
  54. }
  55. public function testTabsInYaml()
  56. {
  57. // test tabs in YAML
  58. $yamls = array(
  59. "foo:\n bar",
  60. "foo:\n bar",
  61. "foo:\n bar",
  62. "foo:\n bar",
  63. );
  64. foreach ($yamls as $yaml) {
  65. try {
  66. $content = $this->parser->parse($yaml);
  67. $this->fail('YAML files must not contain tabs');
  68. } catch (\Exception $e) {
  69. $this->assertInstanceOf('\Exception', $e, 'YAML files must not contain tabs');
  70. $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');
  71. }
  72. }
  73. }
  74. public function testEndOfTheDocumentMarker()
  75. {
  76. $yaml = <<<EOF
  77. --- %YAML:1.0
  78. foo
  79. ...
  80. EOF;
  81. $this->assertEquals('foo', $this->parser->parse($yaml));
  82. }
  83. public function getBlockChompingTests()
  84. {
  85. $tests = array();
  86. $yaml = <<<'EOF'
  87. foo: |-
  88. one
  89. two
  90. bar: |-
  91. one
  92. two
  93. EOF;
  94. $expected = array(
  95. 'foo' => "one\ntwo",
  96. 'bar' => "one\ntwo",
  97. );
  98. $tests['Literal block chomping strip with single trailing newline'] = array($expected, $yaml);
  99. $yaml = <<<'EOF'
  100. foo: |-
  101. one
  102. two
  103. bar: |-
  104. one
  105. two
  106. EOF;
  107. $expected = array(
  108. 'foo' => "one\ntwo",
  109. 'bar' => "one\ntwo",
  110. );
  111. $tests['Literal block chomping strip with multiple trailing newlines'] = array($expected, $yaml);
  112. $yaml = <<<'EOF'
  113. {}
  114. EOF;
  115. $expected = array();
  116. $tests['Literal block chomping strip with multiple trailing newlines after a 1-liner'] = array($expected, $yaml);
  117. $yaml = <<<'EOF'
  118. foo: |-
  119. one
  120. two
  121. bar: |-
  122. one
  123. two
  124. EOF;
  125. $expected = array(
  126. 'foo' => "one\ntwo",
  127. 'bar' => "one\ntwo",
  128. );
  129. $tests['Literal block chomping strip without trailing newline'] = array($expected, $yaml);
  130. $yaml = <<<'EOF'
  131. foo: |
  132. one
  133. two
  134. bar: |
  135. one
  136. two
  137. EOF;
  138. $expected = array(
  139. 'foo' => "one\ntwo\n",
  140. 'bar' => "one\ntwo\n",
  141. );
  142. $tests['Literal block chomping clip with single trailing newline'] = array($expected, $yaml);
  143. $yaml = <<<'EOF'
  144. foo: |
  145. one
  146. two
  147. bar: |
  148. one
  149. two
  150. EOF;
  151. $expected = array(
  152. 'foo' => "one\ntwo\n",
  153. 'bar' => "one\ntwo\n",
  154. );
  155. $tests['Literal block chomping clip with multiple trailing newlines'] = array($expected, $yaml);
  156. $yaml = <<<'EOF'
  157. foo: |
  158. one
  159. two
  160. bar: |
  161. one
  162. two
  163. EOF;
  164. $expected = array(
  165. 'foo' => "one\ntwo\n",
  166. 'bar' => "one\ntwo",
  167. );
  168. $tests['Literal block chomping clip without trailing newline'] = array($expected, $yaml);
  169. $yaml = <<<'EOF'
  170. foo: |+
  171. one
  172. two
  173. bar: |+
  174. one
  175. two
  176. EOF;
  177. $expected = array(
  178. 'foo' => "one\ntwo\n",
  179. 'bar' => "one\ntwo\n",
  180. );
  181. $tests['Literal block chomping keep with single trailing newline'] = array($expected, $yaml);
  182. $yaml = <<<'EOF'
  183. foo: |+
  184. one
  185. two
  186. bar: |+
  187. one
  188. two
  189. EOF;
  190. $expected = array(
  191. 'foo' => "one\ntwo\n\n",
  192. 'bar' => "one\ntwo\n\n",
  193. );
  194. $tests['Literal block chomping keep with multiple trailing newlines'] = array($expected, $yaml);
  195. $yaml = <<<'EOF'
  196. foo: |+
  197. one
  198. two
  199. bar: |+
  200. one
  201. two
  202. EOF;
  203. $expected = array(
  204. 'foo' => "one\ntwo\n",
  205. 'bar' => "one\ntwo",
  206. );
  207. $tests['Literal block chomping keep without trailing newline'] = array($expected, $yaml);
  208. $yaml = <<<'EOF'
  209. foo: >-
  210. one
  211. two
  212. bar: >-
  213. one
  214. two
  215. EOF;
  216. $expected = array(
  217. 'foo' => 'one two',
  218. 'bar' => 'one two',
  219. );
  220. $tests['Folded block chomping strip with single trailing newline'] = array($expected, $yaml);
  221. $yaml = <<<'EOF'
  222. foo: >-
  223. one
  224. two
  225. bar: >-
  226. one
  227. two
  228. EOF;
  229. $expected = array(
  230. 'foo' => 'one two',
  231. 'bar' => 'one two',
  232. );
  233. $tests['Folded block chomping strip with multiple trailing newlines'] = array($expected, $yaml);
  234. $yaml = <<<'EOF'
  235. foo: >-
  236. one
  237. two
  238. bar: >-
  239. one
  240. two
  241. EOF;
  242. $expected = array(
  243. 'foo' => 'one two',
  244. 'bar' => 'one two',
  245. );
  246. $tests['Folded block chomping strip without trailing newline'] = array($expected, $yaml);
  247. $yaml = <<<'EOF'
  248. foo: >
  249. one
  250. two
  251. bar: >
  252. one
  253. two
  254. EOF;
  255. $expected = array(
  256. 'foo' => "one two\n",
  257. 'bar' => "one two\n",
  258. );
  259. $tests['Folded block chomping clip with single trailing newline'] = array($expected, $yaml);
  260. $yaml = <<<'EOF'
  261. foo: >
  262. one
  263. two
  264. bar: >
  265. one
  266. two
  267. EOF;
  268. $expected = array(
  269. 'foo' => "one two\n",
  270. 'bar' => "one two\n",
  271. );
  272. $tests['Folded block chomping clip with multiple trailing newlines'] = array($expected, $yaml);
  273. $yaml = <<<'EOF'
  274. foo: >
  275. one
  276. two
  277. bar: >
  278. one
  279. two
  280. EOF;
  281. $expected = array(
  282. 'foo' => "one two\n",
  283. 'bar' => 'one two',
  284. );
  285. $tests['Folded block chomping clip without trailing newline'] = array($expected, $yaml);
  286. $yaml = <<<'EOF'
  287. foo: >+
  288. one
  289. two
  290. bar: >+
  291. one
  292. two
  293. EOF;
  294. $expected = array(
  295. 'foo' => "one two\n",
  296. 'bar' => "one two\n",
  297. );
  298. $tests['Folded block chomping keep with single trailing newline'] = array($expected, $yaml);
  299. $yaml = <<<'EOF'
  300. foo: >+
  301. one
  302. two
  303. bar: >+
  304. one
  305. two
  306. EOF;
  307. $expected = array(
  308. 'foo' => "one two\n\n",
  309. 'bar' => "one two\n\n",
  310. );
  311. $tests['Folded block chomping keep with multiple trailing newlines'] = array($expected, $yaml);
  312. $yaml = <<<'EOF'
  313. foo: >+
  314. one
  315. two
  316. bar: >+
  317. one
  318. two
  319. EOF;
  320. $expected = array(
  321. 'foo' => "one two\n",
  322. 'bar' => 'one two',
  323. );
  324. $tests['Folded block chomping keep without trailing newline'] = array($expected, $yaml);
  325. return $tests;
  326. }
  327. /**
  328. * @dataProvider getBlockChompingTests
  329. */
  330. public function testBlockChomping($expected, $yaml)
  331. {
  332. $this->assertSame($expected, $this->parser->parse($yaml));
  333. }
  334. /**
  335. * Regression test for issue #7989.
  336. *
  337. * @see https://github.com/symfony/symfony/issues/7989
  338. */
  339. public function testBlockLiteralWithLeadingNewlines()
  340. {
  341. $yaml = <<<'EOF'
  342. foo: |-
  343. bar
  344. EOF;
  345. $expected = array(
  346. 'foo' => "\n\nbar",
  347. );
  348. $this->assertSame($expected, $this->parser->parse($yaml));
  349. }
  350. public function testObjectSupportEnabled()
  351. {
  352. $input = <<<EOF
  353. foo: !!php/object:O:30:"Symfony\Component\Yaml\Tests\B":1:{s:1:"b";s:3:"foo";}
  354. bar: 1
  355. EOF;
  356. $this->assertEquals(array('foo' => new B(), 'bar' => 1), $this->parser->parse($input, false, true), '->parse() is able to parse objects');
  357. }
  358. public function testObjectSupportDisabledButNoExceptions()
  359. {
  360. $input = <<<EOF
  361. foo: !!php/object:O:30:"Symfony\Tests\Component\Yaml\B":1:{s:1:"b";s:3:"foo";}
  362. bar: 1
  363. EOF;
  364. $this->assertEquals(array('foo' => null, 'bar' => 1), $this->parser->parse($input), '->parse() does not parse objects');
  365. }
  366. /**
  367. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  368. */
  369. public function testObjectsSupportDisabledWithExceptions()
  370. {
  371. $this->parser->parse('foo: !!php/object:O:30:"Symfony\Tests\Component\Yaml\B":1:{s:1:"b";s:3:"foo";}', true, false);
  372. }
  373. public function testNonUtf8Exception()
  374. {
  375. if (!function_exists('iconv')) {
  376. $this->markTestSkipped('Exceptions for non-utf8 charsets require the iconv() function.');
  377. return;
  378. }
  379. $yamls = array(
  380. iconv('UTF-8', 'ISO-8859-1', "foo: 'äöüß'"),
  381. iconv('UTF-8', 'ISO-8859-15', "euro: '€'"),
  382. iconv('UTF-8', 'CP1252', "cp1252: '©ÉÇáñ'"),
  383. );
  384. foreach ($yamls as $yaml) {
  385. try {
  386. $this->parser->parse($yaml);
  387. $this->fail('charsets other than UTF-8 are rejected.');
  388. } catch (\Exception $e) {
  389. $this->assertInstanceOf('Symfony\Component\Yaml\Exception\ParseException', $e, 'charsets other than UTF-8 are rejected.');
  390. }
  391. }
  392. }
  393. /**
  394. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  395. */
  396. public function testUnindentedCollectionException()
  397. {
  398. $yaml = <<<EOF
  399. collection:
  400. -item1
  401. -item2
  402. -item3
  403. EOF;
  404. $this->parser->parse($yaml);
  405. }
  406. /**
  407. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  408. */
  409. public function testShortcutKeyUnindentedCollectionException()
  410. {
  411. $yaml = <<<EOF
  412. collection:
  413. - key: foo
  414. foo: bar
  415. EOF;
  416. $this->parser->parse($yaml);
  417. }
  418. /**
  419. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  420. * @expectedExceptionMessage Multiple documents are not supported.
  421. */
  422. public function testMultipleDocumentsNotSupportedException()
  423. {
  424. Yaml::parse(<<<EOL
  425. # Ranking of 1998 home runs
  426. ---
  427. - Mark McGwire
  428. - Sammy Sosa
  429. - Ken Griffey
  430. # Team ranking
  431. ---
  432. - Chicago Cubs
  433. - St Louis Cardinals
  434. EOL
  435. );
  436. }
  437. /**
  438. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  439. */
  440. public function testSequenceInAMapping()
  441. {
  442. Yaml::parse(<<<EOF
  443. yaml:
  444. hash: me
  445. - array stuff
  446. EOF
  447. );
  448. }
  449. /**
  450. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  451. */
  452. public function testMappingInASequence()
  453. {
  454. Yaml::parse(<<<EOF
  455. yaml:
  456. - array stuff
  457. hash: me
  458. EOF
  459. );
  460. }
  461. /**
  462. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  463. * @expectedExceptionMessage missing colon
  464. */
  465. public function testScalarInSequence()
  466. {
  467. Yaml::parse(<<<EOF
  468. foo:
  469. - bar
  470. "missing colon"
  471. foo: bar
  472. EOF
  473. );
  474. }
  475. /**
  476. * > It is an error for two equal keys to appear in the same mapping node.
  477. * > In such a case the YAML processor may continue, ignoring the second
  478. * > `key: value` pair and issuing an appropriate warning. This strategy
  479. * > preserves a consistent information model for one-pass and random access
  480. * > applications.
  481. *
  482. * @see http://yaml.org/spec/1.2/spec.html#id2759572
  483. * @see http://yaml.org/spec/1.1/#id932806
  484. *
  485. * @covers \Symfony\Component\Yaml\Parser::parse
  486. */
  487. public function testMappingDuplicateKeyBlock()
  488. {
  489. $input = <<<EOD
  490. parent:
  491. child: first
  492. child: duplicate
  493. parent:
  494. child: duplicate
  495. child: duplicate
  496. EOD;
  497. $expected = array(
  498. 'parent' => array(
  499. 'child' => 'first',
  500. ),
  501. );
  502. $this->assertSame($expected, Yaml::parse($input));
  503. }
  504. /**
  505. * @covers \Symfony\Component\Yaml\Inline::parseMapping
  506. */
  507. public function testMappingDuplicateKeyFlow()
  508. {
  509. $input = <<<EOD
  510. parent: { child: first, child: duplicate }
  511. parent: { child: duplicate, child: duplicate }
  512. EOD;
  513. $expected = array(
  514. 'parent' => array(
  515. 'child' => 'first',
  516. ),
  517. );
  518. $this->assertSame($expected, Yaml::parse($input));
  519. }
  520. public function testEmptyValue()
  521. {
  522. $input = <<<EOF
  523. hash:
  524. EOF;
  525. $this->assertEquals(array('hash' => null), Yaml::parse($input));
  526. }
  527. public function testStringBlockWithComments()
  528. {
  529. $this->assertEquals(array('content' => <<<EOT
  530. # comment 1
  531. header
  532. # comment 2
  533. <body>
  534. <h1>title</h1>
  535. </body>
  536. footer # comment3
  537. EOT
  538. ), Yaml::parse(<<<EOF
  539. content: |
  540. # comment 1
  541. header
  542. # comment 2
  543. <body>
  544. <h1>title</h1>
  545. </body>
  546. footer # comment3
  547. EOF
  548. ));
  549. }
  550. public function testFoldedStringBlockWithComments()
  551. {
  552. $this->assertEquals(array(array('content' => <<<EOT
  553. # comment 1
  554. header
  555. # comment 2
  556. <body>
  557. <h1>title</h1>
  558. </body>
  559. footer # comment3
  560. EOT
  561. )), Yaml::parse(<<<EOF
  562. -
  563. content: |
  564. # comment 1
  565. header
  566. # comment 2
  567. <body>
  568. <h1>title</h1>
  569. </body>
  570. footer # comment3
  571. EOF
  572. ));
  573. }
  574. public function testNestedFoldedStringBlockWithComments()
  575. {
  576. $this->assertEquals(array(array(
  577. 'title' => 'some title',
  578. 'content' => <<<EOT
  579. # comment 1
  580. header
  581. # comment 2
  582. <body>
  583. <h1>title</h1>
  584. </body>
  585. footer # comment3
  586. EOT
  587. )), Yaml::parse(<<<EOF
  588. -
  589. title: some title
  590. content: |
  591. # comment 1
  592. header
  593. # comment 2
  594. <body>
  595. <h1>title</h1>
  596. </body>
  597. footer # comment3
  598. EOF
  599. ));
  600. }
  601. public function testReferenceResolvingInInlineStrings()
  602. {
  603. $this->assertEquals(array(
  604. 'var' => 'var-value',
  605. 'scalar' => 'var-value',
  606. 'list' => array('var-value'),
  607. 'list_in_list' => array(array('var-value')),
  608. 'map_in_list' => array(array('key' => 'var-value')),
  609. 'embedded_mapping' => array(array('key' => 'var-value')),
  610. 'map' => array('key' => 'var-value'),
  611. 'list_in_map' => array('key' => array('var-value')),
  612. 'map_in_map' => array('foo' => array('bar' => 'var-value')),
  613. ), Yaml::parse(<<<EOF
  614. var: &var var-value
  615. scalar: *var
  616. list: [ *var ]
  617. list_in_list: [[ *var ]]
  618. map_in_list: [ { key: *var } ]
  619. embedded_mapping: [ key: *var ]
  620. map: { key: *var }
  621. list_in_map: { key: [*var] }
  622. map_in_map: { foo: { bar: *var } }
  623. EOF
  624. ));
  625. }
  626. public function testYamlDirective()
  627. {
  628. $yaml = <<<EOF
  629. %YAML 1.2
  630. ---
  631. foo: 1
  632. bar: 2
  633. EOF;
  634. $this->assertEquals(array('foo' => 1, 'bar' => 2), $this->parser->parse($yaml));
  635. }
  636. public function testFloatKeys()
  637. {
  638. $yaml = <<<EOF
  639. foo:
  640. 1.2: "bar"
  641. 1.3: "baz"
  642. EOF;
  643. $expected = array(
  644. 'foo' => array(
  645. '1.2' => 'bar',
  646. '1.3' => 'baz',
  647. ),
  648. );
  649. $this->assertEquals($expected, $this->parser->parse($yaml));
  650. }
  651. }
  652. class B
  653. {
  654. public $b = 'foo';
  655. }