4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Yaml\Tests;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Yaml\Yaml;
16 use Symfony\Component\Yaml\Parser;
18 class ParserTest extends TestCase
23 protected function setUp()
25 $this->parser = new Parser();
28 protected function tearDown()
34 * @dataProvider getDataFormSpecifications
36 public function testSpecifications($file, $expected, $yaml, $comment)
38 $this->assertEquals($expected, var_export($this->parser->parse($yaml), true), $comment);
41 public function getDataFormSpecifications()
43 $parser = new Parser();
44 $path = __DIR__.'/Fixtures';
47 $files = $parser->parse(file_get_contents($path.'/index.yml'));
48 foreach ($files as $file) {
49 $yamls = file_get_contents($path.'/'.$file.'.yml');
51 // split YAMLs documents
52 foreach (preg_split('/^---( %YAML\:1\.0)?/m', $yamls) as $yaml) {
57 $test = $parser->parse($yaml);
58 if (isset($test['todo']) && $test['todo']) {
61 eval('$expected = '.trim($test['php']).';');
63 $tests[] = array($file, var_export($expected, true), $test['yaml'], $test['test']);
71 public function testTabsInYaml()
81 foreach ($yamls as $yaml) {
83 $content = $this->parser->parse($yaml);
85 $this->fail('YAML files must not contain tabs');
86 } catch (\Exception $e) {
87 $this->assertInstanceOf('\Exception', $e, 'YAML files must not contain tabs');
88 $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');
93 public function testEndOfTheDocumentMarker()
101 $this->assertEquals('foo', $this->parser->parse($yaml));
104 public function getBlockChompingTests()
121 $tests['Literal block chomping strip with single trailing newline'] = array($expected, $yaml);
138 $tests['Literal block chomping strip with multiple trailing newlines'] = array($expected, $yaml);
146 $tests['Literal block chomping strip with multiple trailing newlines after a 1-liner'] = array($expected, $yaml);
160 $tests['Literal block chomping strip without trailing newline'] = array($expected, $yaml);
172 'foo' => "one\ntwo\n",
173 'bar' => "one\ntwo\n",
175 $tests['Literal block chomping clip with single trailing newline'] = array($expected, $yaml);
189 'foo' => "one\ntwo\n",
190 'bar' => "one\ntwo\n",
192 $tests['Literal block chomping clip with multiple trailing newlines'] = array($expected, $yaml);
204 'bar' => "one\n\ntwo",
208 $tests['Literal block chomping clip with embedded blank line inside unindented collection'] = array($expected, $yaml);
219 'foo' => "one\ntwo\n",
222 $tests['Literal block chomping clip without trailing newline'] = array($expected, $yaml);
234 'foo' => "one\ntwo\n",
235 'bar' => "one\ntwo\n",
237 $tests['Literal block chomping keep with single trailing newline'] = array($expected, $yaml);
251 'foo' => "one\ntwo\n\n",
252 'bar' => "one\ntwo\n\n",
254 $tests['Literal block chomping keep with multiple trailing newlines'] = array($expected, $yaml);
265 'foo' => "one\ntwo\n",
268 $tests['Literal block chomping keep without trailing newline'] = array($expected, $yaml);
283 $tests['Folded block chomping strip with single trailing newline'] = array($expected, $yaml);
300 $tests['Folded block chomping strip with multiple trailing newlines'] = array($expected, $yaml);
314 $tests['Folded block chomping strip without trailing newline'] = array($expected, $yaml);
326 'foo' => "one two\n",
327 'bar' => "one two\n",
329 $tests['Folded block chomping clip with single trailing newline'] = array($expected, $yaml);
343 'foo' => "one two\n",
344 'bar' => "one two\n",
346 $tests['Folded block chomping clip with multiple trailing newlines'] = array($expected, $yaml);
357 'foo' => "one two\n",
360 $tests['Folded block chomping clip without trailing newline'] = array($expected, $yaml);
372 'foo' => "one two\n",
373 'bar' => "one two\n",
375 $tests['Folded block chomping keep with single trailing newline'] = array($expected, $yaml);
389 'foo' => "one two\n\n",
390 'bar' => "one two\n\n",
392 $tests['Folded block chomping keep with multiple trailing newlines'] = array($expected, $yaml);
403 'foo' => "one two\n",
406 $tests['Folded block chomping keep without trailing newline'] = array($expected, $yaml);
412 * @dataProvider getBlockChompingTests
414 public function testBlockChomping($expected, $yaml)
416 $this->assertSame($expected, $this->parser->parse($yaml));
420 * Regression test for issue #7989.
422 * @see https://github.com/symfony/symfony/issues/7989
424 public function testBlockLiteralWithLeadingNewlines()
437 $this->assertSame($expected, $this->parser->parse($yaml));
440 public function testObjectSupportEnabled()
443 foo: !!php/object:O:30:"Symfony\Component\Yaml\Tests\B":1:{s:1:"b";s:3:"foo";}
446 $this->assertEquals(array('foo' => new B(), 'bar' => 1), $this->parser->parse($input, false, true), '->parse() is able to parse objects');
449 foo: !php/object:O:30:"Symfony\Component\Yaml\Tests\B":1:{s:1:"b";s:3:"foo";}
452 $this->assertEquals(array('foo' => new B(), 'bar' => 1), $this->parser->parse($input, false, true), '->parse() is able to parse objects');
456 * @dataProvider invalidDumpedObjectProvider
458 public function testObjectSupportDisabledButNoExceptions($input)
460 $this->assertEquals(array('foo' => null, 'bar' => 1), $this->parser->parse($input), '->parse() does not parse objects');
464 * @dataProvider getObjectForMapTests
466 public function testObjectForMap($yaml, $expected)
468 $this->assertEquals($expected, $this->parser->parse($yaml, false, false, true));
471 public function getObjectForMapTests()
479 $expected = new \stdClass();
480 $expected->foo = new \stdClass();
481 $expected->foo->fiz = array('cat');
482 $tests['mapping'] = array($yaml, $expected);
484 $yaml = '{ "foo": "bar", "fiz": "cat" }';
485 $expected = new \stdClass();
486 $expected->foo = 'bar';
487 $expected->fiz = 'cat';
488 $tests['inline-mapping'] = array($yaml, $expected);
490 $yaml = "foo: bar\nbaz: foobar";
491 $expected = new \stdClass();
492 $expected->foo = 'bar';
493 $expected->baz = 'foobar';
494 $tests['object-for-map-is-applied-after-parsing'] = array($yaml, $expected);
501 $expected = new \stdClass();
502 $expected->array = array();
503 $expected->array[0] = new \stdClass();
504 $expected->array[0]->key = 'one';
505 $expected->array[1] = new \stdClass();
506 $expected->array[1]->key = 'two';
507 $tests['nest-map-and-sequence'] = array($yaml, $expected);
514 $expected = new \stdClass();
515 $expected->map = new \stdClass();
516 $expected->map->{1} = 'one';
517 $expected->map->{2} = 'two';
518 $tests['numeric-keys'] = array($yaml, $expected);
525 $expected = new \stdClass();
526 $expected->map = new \stdClass();
527 $expected->map->{0} = 'one';
528 $expected->map->{1} = 'two';
529 $tests['zero-indexed-numeric-keys'] = array($yaml, $expected);
535 * @dataProvider invalidDumpedObjectProvider
536 * @expectedException \Symfony\Component\Yaml\Exception\ParseException
538 public function testObjectsSupportDisabledWithExceptions($yaml)
540 $this->parser->parse($yaml, true, false);
543 public function invalidDumpedObjectProvider()
546 foo: !!php/object:O:30:"Symfony\Tests\Component\Yaml\B":1:{s:1:"b";s:3:"foo";}
550 foo: !php/object:O:30:"Symfony\Tests\Component\Yaml\B":1:{s:1:"b";s:3:"foo";}
555 'yaml-tag' => array($yamlTag),
556 'local-tag' => array($localTag),
561 * @requires extension iconv
563 public function testNonUtf8Exception()
566 iconv('UTF-8', 'ISO-8859-1', "foo: 'äöüß'"),
567 iconv('UTF-8', 'ISO-8859-15', "euro: '€'"),
568 iconv('UTF-8', 'CP1252', "cp1252: '©ÉÇáñ'"),
571 foreach ($yamls as $yaml) {
573 $this->parser->parse($yaml);
575 $this->fail('charsets other than UTF-8 are rejected.');
576 } catch (\Exception $e) {
577 $this->assertInstanceOf('Symfony\Component\Yaml\Exception\ParseException', $e, 'charsets other than UTF-8 are rejected.');
583 * @expectedException \Symfony\Component\Yaml\Exception\ParseException
585 public function testUnindentedCollectionException()
596 $this->parser->parse($yaml);
600 * @expectedException \Symfony\Component\Yaml\Exception\ParseException
602 public function testShortcutKeyUnindentedCollectionException()
612 $this->parser->parse($yaml);
616 * @expectedException \Symfony\Component\Yaml\Exception\ParseException
617 * @expectedExceptionMessageRegExp /^Multiple documents are not supported.+/
619 public function testMultipleDocumentsNotSupportedException()
622 # Ranking of 1998 home runs
637 * @expectedException \Symfony\Component\Yaml\Exception\ParseException
639 public function testSequenceInAMapping()
649 public function testSequenceInMappingStartedBySingleDashLine()
674 $this->assertSame($expected, $this->parser->parse($yaml));
677 public function testSequenceFollowedByCommentEmbeddedInMapping()
693 $this->assertSame($expected, $this->parser->parse($yaml));
697 * @expectedException \Symfony\Component\Yaml\Exception\ParseException
699 public function testMappingInASequence()
710 * @expectedException \Symfony\Component\Yaml\Exception\ParseException
711 * @expectedExceptionMessage missing colon
713 public function testScalarInSequence()
725 * > It is an error for two equal keys to appear in the same mapping node.
726 * > In such a case the YAML processor may continue, ignoring the second
727 * > `key: value` pair and issuing an appropriate warning. This strategy
728 * > preserves a consistent information model for one-pass and random access
731 * @see http://yaml.org/spec/1.2/spec.html#id2759572
732 * @see http://yaml.org/spec/1.1/#id932806
734 public function testMappingDuplicateKeyBlock()
749 $this->assertSame($expected, Yaml::parse($input));
752 public function testMappingDuplicateKeyFlow()
755 parent: { child: first, child: duplicate }
756 parent: { child: duplicate, child: duplicate }
763 $this->assertSame($expected, Yaml::parse($input));
766 public function testEmptyValue()
772 $this->assertEquals(array('hash' => null), Yaml::parse($input));
775 public function testCommentAtTheRootIndent()
777 $this->assertEquals(array(
779 'app.foo_service' => array(
782 'app/bar_service' => array(
786 ), Yaml::parse(<<<'EOF'
801 public function testStringBlockWithComments()
803 $this->assertEquals(array('content' => <<<'EOT'
814 ), Yaml::parse(<<<'EOF'
829 public function testFoldedStringBlockWithComments()
831 $this->assertEquals(array(array('content' => <<<'EOT'
842 )), Yaml::parse(<<<'EOF'
858 public function testNestedFoldedStringBlockWithComments()
860 $this->assertEquals(array(array(
861 'title' => 'some title',
862 'content' => <<<'EOT'
873 )), Yaml::parse(<<<'EOF'
890 public function testReferenceResolvingInInlineStrings()
892 $this->assertEquals(array(
893 'var' => 'var-value',
894 'scalar' => 'var-value',
895 'list' => array('var-value'),
896 'list_in_list' => array(array('var-value')),
897 'map_in_list' => array(array('key' => 'var-value')),
898 'embedded_mapping' => array(array('key' => 'var-value')),
899 'map' => array('key' => 'var-value'),
900 'list_in_map' => array('key' => array('var-value')),
901 'map_in_map' => array('foo' => array('bar' => 'var-value')),
902 ), Yaml::parse(<<<'EOF'
906 list_in_list: [[ *var ]]
907 map_in_list: [ { key: *var } ]
908 embedded_mapping: [ key: *var ]
910 list_in_map: { key: [*var] }
911 map_in_map: { foo: { bar: *var } }
916 public function testYamlDirective()
924 $this->assertEquals(array('foo' => 1, 'bar' => 2), $this->parser->parse($yaml));
927 public function testFloatKeys()
942 $this->assertEquals($expected, $this->parser->parse($yaml));
947 * @expectedDeprecation Using a colon in the unquoted mapping value "bar: baz" in line 1 is deprecated since Symfony 2.8 and will throw a ParseException in 3.0.
948 * throw ParseException in Symfony 3.0
950 public function testColonInMappingValueException()
956 $this->parser->parse($yaml);
959 public function testColonInMappingValueExceptionNotTriggeredByColonInComment()
963 bar: foobar # Note: a comment after a colon
966 $this->assertSame(array('foo' => array('bar' => 'foobar')), $this->parser->parse($yaml));
970 * @dataProvider getCommentLikeStringInScalarBlockData
972 public function testCommentLikeStringsAreNotStrippedInBlockScalars($yaml, $expectedParserResult)
974 $this->assertSame($expectedParserResult, $this->parser->parse($yaml));
977 public function getCommentLikeStringInScalarBlockData()
999 'title' => 'some title',
1000 'content' => <<<'EOT'
1015 $tests[] = array($yaml, $expected);
1040 'collection' => array(
1060 $tests[] = array($yaml, $expected);
1075 'scalar-block' => "line1 line2>\n",
1082 $tests[] = array($yaml, $expected);
1098 $tests[] = array($yaml, $expected);
1103 public function testBlankLinesAreParsedAsNewLinesInFoldedBlocks()
1111 <li>may be a good example</li>
1119 <ul> <li>a list</li> <li>may be a good example</li> </ul>
1123 $this->parser->parse($yaml)
1127 public function testAdditionallyIndentedLinesAreParsedAsNewLinesInFoldedBlocks()
1135 <li>may be a good example</li>
1145 <li>may be a good example</li>
1150 $this->parser->parse($yaml)
1155 * @param $lineNumber
1157 * @dataProvider parserThrowsExceptionWithCorrectLineNumberProvider
1159 public function testParserThrowsExceptionWithCorrectLineNumber($lineNumber, $yaml)
1161 if (method_exists($this, 'expectException')) {
1162 $this->expectException('\Symfony\Component\Yaml\Exception\ParseException');
1163 $this->expectExceptionMessage(sprintf('Unexpected characters near "," at line %d (near "bar: "123",").', $lineNumber));
1165 $this->setExpectedException('\Symfony\Component\Yaml\Exception\ParseException', sprintf('Unexpected characters near "," at line %d (near "bar: "123",").', $lineNumber));
1168 $this->parser->parse($yaml);
1171 public function parserThrowsExceptionWithCorrectLineNumberProvider()
1224 public function testCanParseVeryLongValue()
1226 $longStringWithSpaces = str_repeat('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ', 20000);
1227 $trickyVal = array('x' => $longStringWithSpaces);
1229 $yamlString = Yaml::dump($trickyVal);
1230 $arrayFromYaml = $this->parser->parse($yamlString);
1232 $this->assertEquals($trickyVal, $arrayFromYaml);
1236 * @expectedException \Symfony\Component\Yaml\Exception\ParseException
1237 * @expectedExceptionMessage Reference "foo" does not exist at line 2
1239 public function testParserCleansUpReferencesBetweenRuns()
1247 $this->parser->parse($yaml);
1253 $this->parser->parse($yaml);