23 $this->parser =
new Parser();
36 $this->assertEquals($expected, var_export($this->parser->parse($yaml),
true),
$comment);
42 $path = __DIR__.
'/Fixtures';
47 $yamls = file_get_contents(
$path.
'/'.
$file.
'.yml');
50 foreach (preg_split(
'/^---( %YAML\:1\.0)?/m', $yamls) as $yaml) {
59 eval(
'$expected = '.trim(
$test[
'php']).
';');
61 $tests[] = array(
$file, var_export($expected,
true),
$test[
'yaml'],
$test[
'test']);
79 foreach ($yamls as $yaml) {
81 $content = $this->parser->parse($yaml);
83 $this->fail(
'YAML files must not contain tabs');
84 }
catch (\Exception $e) {
85 $this->assertInstanceOf(
'\Exception', $e,
'YAML files must not contain tabs');
86 $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');
99 $this->assertEquals(
'foo', $this->parser->parse($yaml));
119 $tests[
'Literal block chomping strip with single trailing newline'] = array($expected, $yaml);
136 $tests[
'Literal block chomping strip with multiple trailing newlines'] = array($expected, $yaml);
144 $tests[
'Literal block chomping strip with multiple trailing newlines after a 1-liner'] = array($expected, $yaml);
158 $tests[
'Literal block chomping strip without trailing newline'] = array($expected, $yaml);
170 'foo' =>
"one\ntwo\n",
171 'bar' =>
"one\ntwo\n",
173 $tests[
'Literal block chomping clip with single trailing newline'] = array($expected, $yaml);
187 'foo' =>
"one\ntwo\n",
188 'bar' =>
"one\ntwo\n",
190 $tests[
'Literal block chomping clip with multiple trailing newlines'] = array($expected, $yaml);
201 'foo' =>
"one\ntwo\n",
204 $tests[
'Literal block chomping clip without trailing newline'] = array($expected, $yaml);
216 'foo' =>
"one\ntwo\n",
217 'bar' =>
"one\ntwo\n",
219 $tests[
'Literal block chomping keep with single trailing newline'] = array($expected, $yaml);
233 'foo' =>
"one\ntwo\n\n",
234 'bar' =>
"one\ntwo\n\n",
236 $tests[
'Literal block chomping keep with multiple trailing newlines'] = array($expected, $yaml);
247 'foo' =>
"one\ntwo\n",
250 $tests[
'Literal block chomping keep without trailing newline'] = array($expected, $yaml);
265 $tests[
'Folded block chomping strip with single trailing newline'] = array($expected, $yaml);
282 $tests[
'Folded block chomping strip with multiple trailing newlines'] = array($expected, $yaml);
296 $tests[
'Folded block chomping strip without trailing newline'] = array($expected, $yaml);
308 'foo' =>
"one two\n",
309 'bar' =>
"one two\n",
311 $tests[
'Folded block chomping clip with single trailing newline'] = array($expected, $yaml);
325 'foo' =>
"one two\n",
326 'bar' =>
"one two\n",
328 $tests[
'Folded block chomping clip with multiple trailing newlines'] = array($expected, $yaml);
339 'foo' =>
"one two\n",
342 $tests[
'Folded block chomping clip without trailing newline'] = array($expected, $yaml);
354 'foo' =>
"one two\n",
355 'bar' =>
"one two\n",
357 $tests[
'Folded block chomping keep with single trailing newline'] = array($expected, $yaml);
371 'foo' =>
"one two\n\n",
372 'bar' =>
"one two\n\n",
374 $tests[
'Folded block chomping keep with multiple trailing newlines'] = array($expected, $yaml);
385 'foo' =>
"one two\n",
388 $tests[
'Folded block chomping keep without trailing newline'] = array($expected, $yaml);
398 $this->assertSame($expected, $this->parser->parse($yaml));
419 $this->assertSame($expected, $this->parser->parse($yaml));
425foo: !
php/object:O:30:
"Symfony\Component\Yaml\Tests\B":1:{s:1:
"b";s:3:
"foo";}
428 $this->assertEquals(array(
'foo' =>
new B(),
'bar' => 1), $this->parser->parse($input,
Yaml::PARSE_OBJECT),
'->parse() is able to parse objects');
437foo: !
php/object:O:30:
"Symfony\Component\Yaml\Tests\B":1:{s:1:
"b";s:3:
"foo";}
440 $this->assertEquals(array(
'foo' =>
new B(),
'bar' => 1), $this->parser->parse($input,
false,
true),
'->parse() is able to parse objects');
449foo: !!
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,
Yaml::PARSE_OBJECT),
'->parse() is able to parse objects');
460 $this->assertEquals(array(
'foo' =>
null,
'bar' => 1), $this->parser->parse($input),
'->parse() does not parse objects');
477 $this->assertEquals($expected, $this->parser->parse($yaml,
false,
false,
true));
488 $expected = new \stdClass();
489 $expected->foo = new \stdClass();
490 $expected->foo->fiz = array(
'cat');
491 $tests[
'mapping'] = array($yaml, $expected);
493 $yaml =
'{ "foo": "bar", "fiz": "cat" }';
494 $expected = new \stdClass();
495 $expected->foo =
'bar';
496 $expected->fiz =
'cat';
497 $tests[
'inline-mapping'] = array($yaml, $expected);
499 $yaml =
"foo: bar\nbaz: foobar";
500 $expected = new \stdClass();
501 $expected->foo =
'bar';
502 $expected->baz =
'foobar';
503 $tests[
'object-for-map-is-applied-after-parsing'] = array($yaml, $expected);
510 $expected = new \stdClass();
511 $expected->array = array();
512 $expected->array[0] = new \stdClass();
513 $expected->array[0]->key =
'one';
514 $expected->array[1] = new \stdClass();
515 $expected->array[1]->key =
'two';
516 $tests[
'nest-map-and-sequence'] = array($yaml, $expected);
523 $expected = new \stdClass();
524 $expected->map = new \stdClass();
525 $expected->map->{1} =
'one';
526 $expected->map->{2} =
'two';
527 $tests[
'numeric-keys'] = array($yaml, $expected);
534 $expected = new \stdClass();
535 $expected->map = new \stdClass();
536 $expected->map->{0} =
'one';
537 $expected->map->{1} =
'two';
538 $tests[
'zero-indexed-numeric-keys'] = array($yaml, $expected);
549 $this->parser->parse($yaml, Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE);
559 $this->parser->parse($yaml,
true);
565foo: !!
php/object:O:30:
"Symfony\Tests\Component\Yaml\B":1:{s:1:
"b";s:3:
"foo";}
569foo: !
php/object:O:30:
"Symfony\Tests\Component\Yaml\B":1:{s:1:
"b";s:3:
"foo";}
574 'yaml-tag' => array($yamlTag),
575 'local-tag' => array($localTag),
585 iconv(
'UTF-8',
'ISO-8859-1',
"foo: 'äöüß'"),
586 iconv(
'UTF-8',
'ISO-8859-15',
"euro: '€'"),
587 iconv(
'UTF-8',
'CP1252',
"cp1252: '©ÉÇáñ'"),
590 foreach ($yamls as $yaml) {
592 $this->parser->parse($yaml);
594 $this->fail(
'charsets other than UTF-8 are rejected.');
595 }
catch (\Exception $e) {
596 $this->assertInstanceOf(
'Symfony\Component\Yaml\Exception\ParseException', $e,
'charsets other than UTF-8 are rejected.');
615 $this->parser->parse($yaml);
631 $this->parser->parse($yaml);
641# Ranking of 1998 home runs
693 $this->assertSame($expected, $this->parser->parse($yaml));
712 $this->assertSame($expected, $this->parser->parse($yaml));
768 $this->assertSame($expected, Yaml::parse($input));
774parent: { child: first, child: duplicate }
775parent: { child: duplicate, child: duplicate }
782 $this->assertSame($expected, Yaml::parse($input));
791 $this->assertEquals(array(
'hash' =>
null), Yaml::parse($input));
796 $this->assertEquals(array(
798 'app.foo_service' => array(
801 'app/bar_service' => array(
805 ), Yaml::parse(<<<
'EOF'
822 $this->assertEquals(array(
'content' => <<<
'EOT'
833 ), Yaml::parse(<<<
'EOF'
850 $this->assertEquals(array(array(
'content' => <<<
'EOT'
861 )), Yaml::parse(<<<
'EOF'
879 $this->assertEquals(array(array(
880 'title' =>
'some title',
881 'content' => <<<
'EOT'
892 )), Yaml::parse(<<<
'EOF'
911 $this->assertEquals(array(
912 'var' =>
'var-value',
913 'scalar' =>
'var-value',
914 'list' => array(
'var-value'),
915 'list_in_list' => array(array(
'var-value')),
916 'map_in_list' => array(array(
'key' =>
'var-value')),
917 'embedded_mapping' => array(array(
'key' =>
'var-value')),
918 'map' => array(
'key' =>
'var-value'),
919 'list_in_map' => array(
'key' => array(
'var-value')),
920 'map_in_map' => array(
'foo' => array(
'bar' =>
'var-value')),
921 ), Yaml::parse(<<<
'EOF'
925list_in_list: [[ *var ]]
926map_in_list: [ { key: *var } ]
927embedded_mapping: [ key: *var ]
929list_in_map: { key: [*var] }
930map_in_map: { foo: { bar: *var } }
943 $this->assertEquals(array(
'foo' => 1,
'bar' => 2), $this->parser->parse($yaml));
961 $this->assertEquals($expected, $this->parser->parse($yaml));
974 $this->parser->parse($yaml);
981 bar: foobar # Note: a
comment after a colon
984 $this->assertSame(array(
'foo' => array(
'bar' =>
'foobar')), $this->parser->parse($yaml));
992 $this->assertSame($expectedParserResult, $this->parser->parse($yaml));
1017 'title' =>
'some title',
1018 'content' => <<<
'EOT'
1033 $tests[] = array($yaml, $expected);
1058 'collection' => array(
1078 $tests[] = array($yaml, $expected);
1093 'scalar-block' =>
"line1 line2>\n",
1100 $tests[] = array($yaml, $expected);
1116 $tests[] = array($yaml, $expected);
1129 <li>may be a good example</li>
1137<ul> <li>a list</li> <li>may be a good example</li> </ul>
1141 $this->parser->parse($yaml)
1153 <li>may be a good example</li>
1163 <li>may be a good example</li>
1168 $this->parser->parse($yaml)
1177 $this->assertSame(array(
'data' =>
'Hello world'), $this->parser->parse(
$data));
1183 'enclosed with double quotes' => array(
'data: !!binary "SGVsbG8gd29ybGQ="'),
1184 'enclosed with single quotes' => array(
"data: !!binary 'SGVsbG8gd29ybGQ='"),
1185 'containing spaces' => array(
'data: !!binary "SGVs bG8gd 29ybGQ="'),
1186 'in block scalar' => array(
1192 'containing spaces in block scalar' => array(
1206 $this->setExpectedExceptionRegExp(
'\Symfony\Component\Yaml\Exception\ParseException', $expectedMessage);
1208 $this->parser->parse(
$data);
1214 '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\)/'),
1215 'invalid characters' => array(
'!!binary "SGVsbG8#d29ybGQ="',
'/The base64 encoded data \(.*\) contains invalid characters/'),
1216 'too many equals characters' => array(
'data: !!binary "SGVsbG8gd29yb==="',
'/The base64 encoded data \(.*\) contains invalid characters/'),
1217 'misplaced equals character' => array(
'data: !!binary "SGVsbG8gd29ybG=Q"',
'/The base64 encoded data \(.*\) contains invalid characters/'),
1218 'length not a multiple of four in block scalar' => array(
1224 '/The normalized base64 encoded data \(data without whitespace characters\) length must be a multiple of four \(\d+ bytes given\)/',
1226 'invalid characters in block scalar' => array(
1232 '/The base64 encoded data \(.*\) contains invalid characters/',
1234 'too many equals characters in block scalar' => array(
1240 '/The base64 encoded data \(.*\) contains invalid characters/',
1242 'misplaced equals character in block scalar' => array(
1248 '/The base64 encoded data \(.*\) contains invalid characters/',
1258 $expectedDate = new \DateTime();
1259 $expectedDate->setTimeZone(
new \DateTimeZone(
'UTC'));
1260 $expectedDate->setDate(2002, 12, 14);
1261 $expectedDate->setTime(0, 0, 0);
1263 $this->assertEquals(array(
'date' => $expectedDate), $this->parser->parse($yaml, Yaml::PARSE_DATETIME));
1273 $this->setExpectedException(
1274 '\Symfony\Component\Yaml\Exception\ParseException',
1275 sprintf(
'Unexpected characters near "," at line %d (near "bar: "123",").', $lineNumber)
1278 $this->parser->parse($yaml);
sprintf('%.4f', $callTime)
date( 'd-M-Y', $objPHPExcel->getProperties() ->getCreated())
const EOF
How fgetc() reports an End Of File.
An exception for terminatinating execution or to throw for unit testing.
Parser parses YAML strings to convert them to PHP arrays.
testObjectsSupportDisabledWithExceptions($yaml)
@dataProvider invalidDumpedObjectProvider @expectedException \Symfony\Component\Yaml\Exception\ParseE...
getDataFormSpecifications()
testObjectForMap($yaml, $expected)
@dataProvider getObjectForMapTests
testCommentAtTheRootIndent()
testNestedFoldedStringBlockWithComments()
invalidDumpedObjectProvider()
testMappingDuplicateKeyBlock()
It is an error for two equal keys to appear in the same mapping node.
testAdditionallyIndentedLinesAreParsedAsNewLinesInFoldedBlocks()
testParseBinaryData($data)
@dataProvider getBinaryData
testStringBlockWithComments()
testReferenceResolvingInInlineStrings()
testUnindentedCollectionException()
@expectedException \Symfony\Component\Yaml\Exception\ParseException
testMultipleDocumentsNotSupportedException()
@expectedException \Symfony\Component\Yaml\Exception\ParseException @expectedExceptionMessageRegExp /...
testParseInvalidBinaryData($data, $expectedMessage)
@dataProvider getInvalidBinaryData
testNonUtf8Exception()
@requires extension iconv
testSequenceInMappingStartedBySingleDashLine()
testCommentLikeStringsAreNotStrippedInBlockScalars($yaml, $expectedParserResult)
@dataProvider getCommentLikeStringInScalarBlockData
testBlockLiteralWithLeadingNewlines()
Regression test for issue #7989.
testObjectSupportEnabledWithDeprecatedTag()
@group legacy
testParserThrowsExceptionWithCorrectLineNumber($lineNumber, $yaml)
testObjectForMapEnabledWithMappingUsingBooleanToggles($yaml, $expected)
@group legacy @dataProvider getObjectForMapTests
testColonInMappingValueExceptionNotTriggeredByColonInComment()
testObjectSupportEnabled()
testObjectsSupportDisabledWithExceptionsUsingBooleanToggles($yaml)
@group legacy @dataProvider invalidDumpedObjectProvider @expectedException \Symfony\Component\Yaml\Ex...
testFoldedStringBlockWithComments()
getCommentLikeStringInScalarBlockData()
testObjectSupportDisabledButNoExceptions($input)
@dataProvider invalidDumpedObjectProvider
testMappingDuplicateKeyFlow()
testShortcutKeyUnindentedCollectionException()
@expectedException \Symfony\Component\Yaml\Exception\ParseException
testBlockChomping($expected, $yaml)
@dataProvider getBlockChompingTests
testObjectSupportEnabledPassingTrue()
@group legacy
testParseDateAsMappingValue()
testColonInMappingValueException()
@expectedException \Symfony\Component\Yaml\Exception\ParseException @expectedExceptionMessage A colon...
testEndOfTheDocumentMarker()
testScalarInSequence()
@expectedException \Symfony\Component\Yaml\Exception\ParseException @expectedExceptionMessage missing...
testBlankLinesAreParsedAsNewLinesInFoldedBlocks()
parserThrowsExceptionWithCorrectLineNumberProvider()
testSpecifications($file, $expected, $yaml, $comment)
@dataProvider getDataFormSpecifications
testMappingInASequence()
@expectedException \Symfony\Component\Yaml\Exception\ParseException
testSequenceFollowedByCommentEmbeddedInMapping()
testSequenceInAMapping()
@expectedException \Symfony\Component\Yaml\Exception\ParseException
Yaml offers convenience methods to load and dump YAML.
const PARSE_OBJECT_FOR_MAP
hash(StreamInterface $stream, $algo, $rawOutput=false)
Calculate a hash of a Stream.
if(!file_exists("$old.txt")) if( $old===$new) if(file_exists("$new.txt")) $file