123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Component\Yaml\Tests;
- use Symfony\Component\Yaml\Yaml;
- use Symfony\Component\Yaml\Parser;
- class ParserTest extends \PHPUnit_Framework_TestCase
- {
- protected $parser;
- protected function setUp()
- {
- $this->parser = new Parser();
- }
- protected function tearDown()
- {
- $this->parser = null;
- }
- /**
- * @dataProvider getDataFormSpecifications
- */
- public function testSpecifications($file, $expected, $yaml, $comment, $deprecated)
- {
- $deprecations = array();
- if ($deprecated) {
- set_error_handler(function ($type, $msg) use (&$deprecations) {
- if (E_USER_DEPRECATED !== $type) {
- restore_error_handler();
- return call_user_func_array('PHPUnit_Util_ErrorHandler::handleError', func_get_args());
- }
- $deprecations[] = $msg;
- });
- }
- $this->assertEquals($expected, var_export($this->parser->parse($yaml), true), $comment);
- if ($deprecated) {
- restore_error_handler();
- $this->assertCount(1, $deprecations);
- $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]);
- }
- }
- public function getDataFormSpecifications()
- {
- $parser = new Parser();
- $path = __DIR__.'/Fixtures';
- $tests = array();
- $files = $parser->parse(file_get_contents($path.'/index.yml'));
- foreach ($files as $file) {
- $yamls = file_get_contents($path.'/'.$file.'.yml');
- // split YAMLs documents
- foreach (preg_split('/^---( %YAML\:1\.0)?/m', $yamls) as $yaml) {
- if (!$yaml) {
- continue;
- }
- $test = $parser->parse($yaml);
- if (isset($test['todo']) && $test['todo']) {
- // TODO
- } else {
- eval('$expected = '.trim($test['php']).';');
- $tests[] = array($file, var_export($expected, true), $test['yaml'], $test['test'], isset($test['deprecated']) ? $test['deprecated'] : false);
- }
- }
- }
- return $tests;
- }
- public function testTabsInYaml()
- {
- // test tabs in YAML
- $yamls = array(
- "foo:\n bar",
- "foo:\n bar",
- "foo:\n bar",
- "foo:\n bar",
- );
- foreach ($yamls as $yaml) {
- try {
- $content = $this->parser->parse($yaml);
- $this->fail('YAML files must not contain tabs');
- } catch (\Exception $e) {
- $this->assertInstanceOf('\Exception', $e, 'YAML files must not contain tabs');
- $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');
- }
- }
- }
- public function testEndOfTheDocumentMarker()
- {
- $yaml = <<<'EOF'
- --- %YAML:1.0
- foo
- ...
- EOF;
- $this->assertEquals('foo', $this->parser->parse($yaml));
- }
- public function getBlockChompingTests()
- {
- $tests = array();
- $yaml = <<<'EOF'
- foo: |-
- one
- two
- bar: |-
- one
- two
- EOF;
- $expected = array(
- 'foo' => "one\ntwo",
- 'bar' => "one\ntwo",
- );
- $tests['Literal block chomping strip with single trailing newline'] = array($expected, $yaml);
- $yaml = <<<'EOF'
- foo: |-
- one
- two
- bar: |-
- one
- two
- EOF;
- $expected = array(
- 'foo' => "one\ntwo",
- 'bar' => "one\ntwo",
- );
- $tests['Literal block chomping strip with multiple trailing newlines'] = array($expected, $yaml);
- $yaml = <<<'EOF'
- {}
- EOF;
- $expected = array();
- $tests['Literal block chomping strip with multiple trailing newlines after a 1-liner'] = array($expected, $yaml);
- $yaml = <<<'EOF'
- foo: |-
- one
- two
- bar: |-
- one
- two
- EOF;
- $expected = array(
- 'foo' => "one\ntwo",
- 'bar' => "one\ntwo",
- );
- $tests['Literal block chomping strip without trailing newline'] = array($expected, $yaml);
- $yaml = <<<'EOF'
- foo: |
- one
- two
- bar: |
- one
- two
- EOF;
- $expected = array(
- 'foo' => "one\ntwo\n",
- 'bar' => "one\ntwo\n",
- );
- $tests['Literal block chomping clip with single trailing newline'] = array($expected, $yaml);
- $yaml = <<<'EOF'
- foo: |
- one
- two
- bar: |
- one
- two
- EOF;
- $expected = array(
- 'foo' => "one\ntwo\n",
- 'bar' => "one\ntwo\n",
- );
- $tests['Literal block chomping clip with multiple trailing newlines'] = array($expected, $yaml);
- $yaml = <<<'EOF'
- foo:
- - bar: |
- one
- two
- EOF;
- $expected = array(
- 'foo' => array(
- array(
- 'bar' => "one\n\ntwo",
- ),
- ),
- );
- $tests['Literal block chomping clip with embedded blank line inside unindented collection'] = array($expected, $yaml);
- $yaml = <<<'EOF'
- foo: |
- one
- two
- bar: |
- one
- two
- EOF;
- $expected = array(
- 'foo' => "one\ntwo\n",
- 'bar' => "one\ntwo",
- );
- $tests['Literal block chomping clip without trailing newline'] = array($expected, $yaml);
- $yaml = <<<'EOF'
- foo: |+
- one
- two
- bar: |+
- one
- two
- EOF;
- $expected = array(
- 'foo' => "one\ntwo\n",
- 'bar' => "one\ntwo\n",
- );
- $tests['Literal block chomping keep with single trailing newline'] = array($expected, $yaml);
- $yaml = <<<'EOF'
- foo: |+
- one
- two
- bar: |+
- one
- two
- EOF;
- $expected = array(
- 'foo' => "one\ntwo\n\n",
- 'bar' => "one\ntwo\n\n",
- );
- $tests['Literal block chomping keep with multiple trailing newlines'] = array($expected, $yaml);
- $yaml = <<<'EOF'
- foo: |+
- one
- two
- bar: |+
- one
- two
- EOF;
- $expected = array(
- 'foo' => "one\ntwo\n",
- 'bar' => "one\ntwo",
- );
- $tests['Literal block chomping keep without trailing newline'] = array($expected, $yaml);
- $yaml = <<<'EOF'
- foo: >-
- one
- two
- bar: >-
- one
- two
- EOF;
- $expected = array(
- 'foo' => 'one two',
- 'bar' => 'one two',
- );
- $tests['Folded block chomping strip with single trailing newline'] = array($expected, $yaml);
- $yaml = <<<'EOF'
- foo: >-
- one
- two
- bar: >-
- one
- two
- EOF;
- $expected = array(
- 'foo' => 'one two',
- 'bar' => 'one two',
- );
- $tests['Folded block chomping strip with multiple trailing newlines'] = array($expected, $yaml);
- $yaml = <<<'EOF'
- foo: >-
- one
- two
- bar: >-
- one
- two
- EOF;
- $expected = array(
- 'foo' => 'one two',
- 'bar' => 'one two',
- );
- $tests['Folded block chomping strip without trailing newline'] = array($expected, $yaml);
- $yaml = <<<'EOF'
- foo: >
- one
- two
- bar: >
- one
- two
- EOF;
- $expected = array(
- 'foo' => "one two\n",
- 'bar' => "one two\n",
- );
- $tests['Folded block chomping clip with single trailing newline'] = array($expected, $yaml);
- $yaml = <<<'EOF'
- foo: >
- one
- two
- bar: >
- one
- two
- EOF;
- $expected = array(
- 'foo' => "one two\n",
- 'bar' => "one two\n",
- );
- $tests['Folded block chomping clip with multiple trailing newlines'] = array($expected, $yaml);
- $yaml = <<<'EOF'
- foo: >
- one
- two
- bar: >
- one
- two
- EOF;
- $expected = array(
- 'foo' => "one two\n",
- 'bar' => 'one two',
- );
- $tests['Folded block chomping clip without trailing newline'] = array($expected, $yaml);
- $yaml = <<<'EOF'
- foo: >+
- one
- two
- bar: >+
- one
- two
- EOF;
- $expected = array(
- 'foo' => "one two\n",
- 'bar' => "one two\n",
- );
- $tests['Folded block chomping keep with single trailing newline'] = array($expected, $yaml);
- $yaml = <<<'EOF'
- foo: >+
- one
- two
- bar: >+
- one
- two
- EOF;
- $expected = array(
- 'foo' => "one two\n\n",
- 'bar' => "one two\n\n",
- );
- $tests['Folded block chomping keep with multiple trailing newlines'] = array($expected, $yaml);
- $yaml = <<<'EOF'
- foo: >+
- one
- two
- bar: >+
- one
- two
- EOF;
- $expected = array(
- 'foo' => "one two\n",
- 'bar' => 'one two',
- );
- $tests['Folded block chomping keep without trailing newline'] = array($expected, $yaml);
- return $tests;
- }
- /**
- * @dataProvider getBlockChompingTests
- */
- public function testBlockChomping($expected, $yaml)
- {
- $this->assertSame($expected, $this->parser->parse($yaml));
- }
- /**
- * Regression test for issue #7989.
- *
- * @see https://github.com/symfony/symfony/issues/7989
- */
- public function testBlockLiteralWithLeadingNewlines()
- {
- $yaml = <<<'EOF'
- foo: |-
- bar
- EOF;
- $expected = array(
- 'foo' => "\n\nbar",
- );
- $this->assertSame($expected, $this->parser->parse($yaml));
- }
- public function testObjectSupportEnabled()
- {
- $input = <<<'EOF'
- foo: !php/object:O:30:"Symfony\Component\Yaml\Tests\B":1:{s:1:"b";s:3:"foo";}
- bar: 1
- EOF;
- $this->assertEquals(array('foo' => new B(), 'bar' => 1), $this->parser->parse($input, Yaml::PARSE_OBJECT), '->parse() is able to parse objects');
- }
- /**
- * @group legacy
- */
- public function testObjectSupportEnabledPassingTrue()
- {
- $input = <<<'EOF'
- foo: !php/object:O:30:"Symfony\Component\Yaml\Tests\B":1:{s:1:"b";s:3:"foo";}
- bar: 1
- EOF;
- $this->assertEquals(array('foo' => new B(), 'bar' => 1), $this->parser->parse($input, false, true), '->parse() is able to parse objects');
- }
- /**
- * @group legacy
- */
- public function testObjectSupportEnabledWithDeprecatedTag()
- {
- $input = <<<'EOF'
- foo: !!php/object:O:30:"Symfony\Component\Yaml\Tests\B":1:{s:1:"b";s:3:"foo";}
- bar: 1
- EOF;
- $this->assertEquals(array('foo' => new B(), 'bar' => 1), $this->parser->parse($input, Yaml::PARSE_OBJECT), '->parse() is able to parse objects');
- }
- /**
- * @dataProvider invalidDumpedObjectProvider
- */
- public function testObjectSupportDisabledButNoExceptions($input)
- {
- $this->assertEquals(array('foo' => null, 'bar' => 1), $this->parser->parse($input), '->parse() does not parse objects');
- }
- /**
- * @dataProvider getObjectForMapTests
- */
- public function testObjectForMap($yaml, $expected)
- {
- $this->assertEquals($expected, $this->parser->parse($yaml, Yaml::PARSE_OBJECT_FOR_MAP));
- }
- /**
- * @group legacy
- * @dataProvider getObjectForMapTests
- */
- public function testObjectForMapEnabledWithMappingUsingBooleanToggles($yaml, $expected)
- {
- $this->assertEquals($expected, $this->parser->parse($yaml, false, false, true));
- }
- public function getObjectForMapTests()
- {
- $tests = array();
- $yaml = <<<'EOF'
- foo:
- fiz: [cat]
- EOF;
- $expected = new \stdClass();
- $expected->foo = new \stdClass();
- $expected->foo->fiz = array('cat');
- $tests['mapping'] = array($yaml, $expected);
- $yaml = '{ "foo": "bar", "fiz": "cat" }';
- $expected = new \stdClass();
- $expected->foo = 'bar';
- $expected->fiz = 'cat';
- $tests['inline-mapping'] = array($yaml, $expected);
- $yaml = "foo: bar\nbaz: foobar";
- $expected = new \stdClass();
- $expected->foo = 'bar';
- $expected->baz = 'foobar';
- $tests['object-for-map-is-applied-after-parsing'] = array($yaml, $expected);
- $yaml = <<<'EOT'
- array:
- - key: one
- - key: two
- EOT;
- $expected = new \stdClass();
- $expected->array = array();
- $expected->array[0] = new \stdClass();
- $expected->array[0]->key = 'one';
- $expected->array[1] = new \stdClass();
- $expected->array[1]->key = 'two';
- $tests['nest-map-and-sequence'] = array($yaml, $expected);
- $yaml = <<<'YAML'
- map:
- 1: one
- 2: two
- YAML;
- $expected = new \stdClass();
- $expected->map = new \stdClass();
- $expected->map->{1} = 'one';
- $expected->map->{2} = 'two';
- $tests['numeric-keys'] = array($yaml, $expected);
- $yaml = <<<'YAML'
- map:
- 0: one
- 1: two
- YAML;
- $expected = new \stdClass();
- $expected->map = new \stdClass();
- $expected->map->{0} = 'one';
- $expected->map->{1} = 'two';
- $tests['zero-indexed-numeric-keys'] = array($yaml, $expected);
- return $tests;
- }
- /**
- * @dataProvider invalidDumpedObjectProvider
- * @expectedException \Symfony\Component\Yaml\Exception\ParseException
- */
- public function testObjectsSupportDisabledWithExceptions($yaml)
- {
- $this->parser->parse($yaml, Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE);
- }
- /**
- * @group legacy
- * @dataProvider invalidDumpedObjectProvider
- * @expectedException \Symfony\Component\Yaml\Exception\ParseException
- */
- public function testObjectsSupportDisabledWithExceptionsUsingBooleanToggles($yaml)
- {
- $this->parser->parse($yaml, true);
- }
- public function invalidDumpedObjectProvider()
- {
- $yamlTag = <<<'EOF'
- foo: !!php/object:O:30:"Symfony\Tests\Component\Yaml\B":1:{s:1:"b";s:3:"foo";}
- bar: 1
- EOF;
- $localTag = <<<'EOF'
- foo: !php/object:O:30:"Symfony\Tests\Component\Yaml\B":1:{s:1:"b";s:3:"foo";}
- bar: 1
- EOF;
- return array(
- 'yaml-tag' => array($yamlTag),
- 'local-tag' => array($localTag),
- );
- }
- /**
- * @requires extension iconv
- */
- public function testNonUtf8Exception()
- {
- $yamls = array(
- iconv('UTF-8', 'ISO-8859-1', "foo: 'äöüß'"),
- iconv('UTF-8', 'ISO-8859-15', "euro: '€'"),
- iconv('UTF-8', 'CP1252', "cp1252: '©ÉÇáñ'"),
- );
- foreach ($yamls as $yaml) {
- try {
- $this->parser->parse($yaml);
- $this->fail('charsets other than UTF-8 are rejected.');
- } catch (\Exception $e) {
- $this->assertInstanceOf('Symfony\Component\Yaml\Exception\ParseException', $e, 'charsets other than UTF-8 are rejected.');
- }
- }
- }
- /**
- * @expectedException \Symfony\Component\Yaml\Exception\ParseException
- */
- public function testUnindentedCollectionException()
- {
- $yaml = <<<'EOF'
- collection:
- -item1
- -item2
- -item3
- EOF;
- $this->parser->parse($yaml);
- }
- /**
- * @expectedException \Symfony\Component\Yaml\Exception\ParseException
- */
- public function testShortcutKeyUnindentedCollectionException()
- {
- $yaml = <<<'EOF'
- collection:
- - key: foo
- foo: bar
- EOF;
- $this->parser->parse($yaml);
- }
- /**
- * @expectedException \Symfony\Component\Yaml\Exception\ParseException
- * @expectedExceptionMessageRegExp /^Multiple documents are not supported.+/
- */
- public function testMultipleDocumentsNotSupportedException()
- {
- Yaml::parse(<<<'EOL'
- # Ranking of 1998 home runs
- ---
- - Mark McGwire
- - Sammy Sosa
- - Ken Griffey
- # Team ranking
- ---
- - Chicago Cubs
- - St Louis Cardinals
- EOL
- );
- }
- /**
- * @expectedException \Symfony\Component\Yaml\Exception\ParseException
- */
- public function testSequenceInAMapping()
- {
- Yaml::parse(<<<'EOF'
- yaml:
- hash: me
- - array stuff
- EOF
- );
- }
- public function testSequenceInMappingStartedBySingleDashLine()
- {
- $yaml = <<<'EOT'
- a:
- -
- b:
- -
- bar: baz
- - foo
- d: e
- EOT;
- $expected = array(
- 'a' => array(
- array(
- 'b' => array(
- array(
- 'bar' => 'baz',
- ),
- ),
- ),
- 'foo',
- ),
- 'd' => 'e',
- );
- $this->assertSame($expected, $this->parser->parse($yaml));
- }
- public function testSequenceFollowedByCommentEmbeddedInMapping()
- {
- $yaml = <<<'EOT'
- a:
- b:
- - c
- # comment
- d: e
- EOT;
- $expected = array(
- 'a' => array(
- 'b' => array('c'),
- 'd' => 'e',
- ),
- );
- $this->assertSame($expected, $this->parser->parse($yaml));
- }
- /**
- * @expectedException \Symfony\Component\Yaml\Exception\ParseException
- */
- public function testMappingInASequence()
- {
- Yaml::parse(<<<'EOF'
- yaml:
- - array stuff
- hash: me
- EOF
- );
- }
- /**
- * @expectedException \Symfony\Component\Yaml\Exception\ParseException
- * @expectedExceptionMessage missing colon
- */
- public function testScalarInSequence()
- {
- Yaml::parse(<<<'EOF'
- foo:
- - bar
- "missing colon"
- foo: bar
- EOF
- );
- }
- /**
- * > It is an error for two equal keys to appear in the same mapping node.
- * > In such a case the YAML processor may continue, ignoring the second
- * > `key: value` pair and issuing an appropriate warning. This strategy
- * > preserves a consistent information model for one-pass and random access
- * > applications.
- *
- * @see http://yaml.org/spec/1.2/spec.html#id2759572
- * @see http://yaml.org/spec/1.1/#id932806
- * @group legacy
- */
- public function testMappingDuplicateKeyBlock()
- {
- $input = <<<'EOD'
- parent:
- child: first
- child: duplicate
- parent:
- child: duplicate
- child: duplicate
- EOD;
- $expected = array(
- 'parent' => array(
- 'child' => 'first',
- ),
- );
- $this->assertSame($expected, Yaml::parse($input));
- }
- /**
- * @group legacy
- */
- public function testMappingDuplicateKeyFlow()
- {
- $input = <<<'EOD'
- parent: { child: first, child: duplicate }
- parent: { child: duplicate, child: duplicate }
- EOD;
- $expected = array(
- 'parent' => array(
- 'child' => 'first',
- ),
- );
- $this->assertSame($expected, Yaml::parse($input));
- }
- /**
- * @group legacy
- * @dataProvider getParseExceptionOnDuplicateData
- * @expectedDeprecation Duplicate key "%s" detected on line %d whilst parsing YAML. Silent handling of duplicate mapping keys in YAML is deprecated %s.
- * throws \Symfony\Component\Yaml\Exception\ParseException in 4.0
- */
- public function testParseExceptionOnDuplicate($input, $duplicateKey, $lineNumber)
- {
- Yaml::parse($input);
- }
- public function getParseExceptionOnDuplicateData()
- {
- $tests = array();
- $yaml = <<<EOD
- parent: { child: first, child: duplicate }
- EOD;
- $tests[] = array($yaml, 'child', 1);
- $yaml = <<<EOD
- parent:
- child: first,
- child: duplicate
- EOD;
- $tests[] = array($yaml, 'child', 3);
- $yaml = <<<EOD
- parent: { child: foo }
- parent: { child: bar }
- EOD;
- $tests[] = array($yaml, 'parent', 2);
- $yaml = <<<EOD
- parent: { child_mapping: { value: bar}, child_mapping: { value: bar} }
- EOD;
- $tests[] = array($yaml, 'child_mapping', 1);
- $yaml = <<<EOD
- parent:
- child_mapping:
- value: bar
- child_mapping:
- value: bar
- EOD;
- $tests[] = array($yaml, 'child_mapping', 4);
- $yaml = <<<EOD
- parent: { child_sequence: ['key1', 'key2', 'key3'], child_sequence: ['key1', 'key2', 'key3'] }
- EOD;
- $tests[] = array($yaml, 'child_sequence', 1);
- $yaml = <<<EOD
- parent:
- child_sequence:
- - key1
- - key2
- - key3
- child_sequence:
- - key1
- - key2
- - key3
- EOD;
- $tests[] = array($yaml, 'child_sequence', 6);
- return $tests;
- }
- public function testEmptyValue()
- {
- $input = <<<'EOF'
- hash:
- EOF;
- $this->assertEquals(array('hash' => null), Yaml::parse($input));
- }
- public function testCommentAtTheRootIndent()
- {
- $this->assertEquals(array(
- 'services' => array(
- 'app.foo_service' => array(
- 'class' => 'Foo',
- ),
- 'app/bar_service' => array(
- 'class' => 'Bar',
- ),
- ),
- ), Yaml::parse(<<<'EOF'
- # comment 1
- services:
- # comment 2
- # comment 3
- app.foo_service:
- class: Foo
- # comment 4
- # comment 5
- app/bar_service:
- class: Bar
- EOF
- ));
- }
- public function testStringBlockWithComments()
- {
- $this->assertEquals(array('content' => <<<'EOT'
- # comment 1
- header
- # comment 2
- <body>
- <h1>title</h1>
- </body>
- footer # comment3
- EOT
- ), Yaml::parse(<<<'EOF'
- content: |
- # comment 1
- header
- # comment 2
- <body>
- <h1>title</h1>
- </body>
- footer # comment3
- EOF
- ));
- }
- public function testFoldedStringBlockWithComments()
- {
- $this->assertEquals(array(array('content' => <<<'EOT'
- # comment 1
- header
- # comment 2
- <body>
- <h1>title</h1>
- </body>
- footer # comment3
- EOT
- )), Yaml::parse(<<<'EOF'
- -
- content: |
- # comment 1
- header
- # comment 2
- <body>
- <h1>title</h1>
- </body>
- footer # comment3
- EOF
- ));
- }
- public function testNestedFoldedStringBlockWithComments()
- {
- $this->assertEquals(array(array(
- 'title' => 'some title',
- 'content' => <<<'EOT'
- # comment 1
- header
- # comment 2
- <body>
- <h1>title</h1>
- </body>
- footer # comment3
- EOT
- )), Yaml::parse(<<<'EOF'
- -
- title: some title
- content: |
- # comment 1
- header
- # comment 2
- <body>
- <h1>title</h1>
- </body>
- footer # comment3
- EOF
- ));
- }
- public function testReferenceResolvingInInlineStrings()
- {
- $this->assertEquals(array(
- 'var' => 'var-value',
- 'scalar' => 'var-value',
- 'list' => array('var-value'),
- 'list_in_list' => array(array('var-value')),
- 'map_in_list' => array(array('key' => 'var-value')),
- 'embedded_mapping' => array(array('key' => 'var-value')),
- 'map' => array('key' => 'var-value'),
- 'list_in_map' => array('key' => array('var-value')),
- 'map_in_map' => array('foo' => array('bar' => 'var-value')),
- ), Yaml::parse(<<<'EOF'
- var: &var var-value
- scalar: *var
- list: [ *var ]
- list_in_list: [[ *var ]]
- map_in_list: [ { key: *var } ]
- embedded_mapping: [ key: *var ]
- map: { key: *var }
- list_in_map: { key: [*var] }
- map_in_map: { foo: { bar: *var } }
- EOF
- ));
- }
- public function testYamlDirective()
- {
- $yaml = <<<'EOF'
- %YAML 1.2
- ---
- foo: 1
- bar: 2
- EOF;
- $this->assertEquals(array('foo' => 1, 'bar' => 2), $this->parser->parse($yaml));
- }
- public function testFloatKeys()
- {
- $yaml = <<<'EOF'
- foo:
- 1.2: "bar"
- 1.3: "baz"
- EOF;
- $expected = array(
- 'foo' => array(
- '1.2' => 'bar',
- '1.3' => 'baz',
- ),
- );
- $this->assertEquals($expected, $this->parser->parse($yaml));
- }
- /**
- * @expectedException \Symfony\Component\Yaml\Exception\ParseException
- * @expectedExceptionMessage A colon cannot be used in an unquoted mapping value
- */
- public function testColonInMappingValueException()
- {
- $yaml = <<<'EOF'
- foo: bar: baz
- EOF;
- $this->parser->parse($yaml);
- }
- public function testColonInMappingValueExceptionNotTriggeredByColonInComment()
- {
- $yaml = <<<'EOT'
- foo:
- bar: foobar # Note: a comment after a colon
- EOT;
- $this->assertSame(array('foo' => array('bar' => 'foobar')), $this->parser->parse($yaml));
- }
- /**
- * @dataProvider getCommentLikeStringInScalarBlockData
- */
- public function testCommentLikeStringsAreNotStrippedInBlockScalars($yaml, $expectedParserResult)
- {
- $this->assertSame($expectedParserResult, $this->parser->parse($yaml));
- }
- public function getCommentLikeStringInScalarBlockData()
- {
- $tests = array();
- $yaml = <<<'EOT'
- pages:
- -
- title: some title
- content: |
- # comment 1
- header
- # comment 2
- <body>
- <h1>title</h1>
- </body>
- footer # comment3
- EOT;
- $expected = array(
- 'pages' => array(
- array(
- 'title' => 'some title',
- 'content' => <<<'EOT'
- # comment 1
- header
- # comment 2
- <body>
- <h1>title</h1>
- </body>
- footer # comment3
- EOT
- ,
- ),
- ),
- );
- $tests[] = array($yaml, $expected);
- $yaml = <<<'EOT'
- test: |
- foo
- # bar
- baz
- collection:
- - one: |
- foo
- # bar
- baz
- - two: |
- foo
- # bar
- baz
- EOT;
- $expected = array(
- 'test' => <<<'EOT'
- foo
- # bar
- baz
- EOT
- ,
- 'collection' => array(
- array(
- 'one' => <<<'EOT'
- foo
- # bar
- baz
- EOT
- ,
- ),
- array(
- 'two' => <<<'EOT'
- foo
- # bar
- baz
- EOT
- ,
- ),
- ),
- );
- $tests[] = array($yaml, $expected);
- $yaml = <<<'EOT'
- foo:
- bar:
- scalar-block: >
- line1
- line2>
- baz:
- # comment
- foobar: ~
- EOT;
- $expected = array(
- 'foo' => array(
- 'bar' => array(
- 'scalar-block' => "line1 line2>\n",
- ),
- 'baz' => array(
- 'foobar' => null,
- ),
- ),
- );
- $tests[] = array($yaml, $expected);
- $yaml = <<<'EOT'
- a:
- b: hello
- # c: |
- # first row
- # second row
- d: hello
- EOT;
- $expected = array(
- 'a' => array(
- 'b' => 'hello',
- 'd' => 'hello',
- ),
- );
- $tests[] = array($yaml, $expected);
- return $tests;
- }
- public function testBlankLinesAreParsedAsNewLinesInFoldedBlocks()
- {
- $yaml = <<<'EOT'
- test: >
- <h2>A heading</h2>
- <ul>
- <li>a list</li>
- <li>may be a good example</li>
- </ul>
- EOT;
- $this->assertSame(
- array(
- 'test' => <<<'EOT'
- <h2>A heading</h2>
- <ul> <li>a list</li> <li>may be a good example</li> </ul>
- EOT
- ,
- ),
- $this->parser->parse($yaml)
- );
- }
- public function testAdditionallyIndentedLinesAreParsedAsNewLinesInFoldedBlocks()
- {
- $yaml = <<<'EOT'
- test: >
- <h2>A heading</h2>
- <ul>
- <li>a list</li>
- <li>may be a good example</li>
- </ul>
- EOT;
- $this->assertSame(
- array(
- 'test' => <<<'EOT'
- <h2>A heading</h2>
- <ul>
- <li>a list</li>
- <li>may be a good example</li>
- </ul>
- EOT
- ,
- ),
- $this->parser->parse($yaml)
- );
- }
- /**
- * @dataProvider getBinaryData
- */
- public function testParseBinaryData($data)
- {
- $this->assertSame(array('data' => 'Hello world'), $this->parser->parse($data));
- }
- public function getBinaryData()
- {
- return array(
- 'enclosed with double quotes' => array('data: !!binary "SGVsbG8gd29ybGQ="'),
- 'enclosed with single quotes' => array("data: !!binary 'SGVsbG8gd29ybGQ='"),
- 'containing spaces' => array('data: !!binary "SGVs bG8gd 29ybGQ="'),
- 'in block scalar' => array(
- <<<'EOT'
- data: !!binary |
- SGVsbG8gd29ybGQ=
- EOT
- ),
- 'containing spaces in block scalar' => array(
- <<<'EOT'
- data: !!binary |
- SGVs bG8gd 29ybGQ=
- EOT
- ),
- );
- }
- /**
- * @dataProvider getInvalidBinaryData
- */
- public function testParseInvalidBinaryData($data, $expectedMessage)
- {
- $this->setExpectedExceptionRegExp('\Symfony\Component\Yaml\Exception\ParseException', $expectedMessage);
- $this->parser->parse($data);
- }
- public function getInvalidBinaryData()
- {
- return array(
- '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\)/'),
- 'invalid characters' => array('!!binary "SGVsbG8#d29ybGQ="', '/The base64 encoded data \(.*\) contains invalid characters/'),
- 'too many equals characters' => array('data: !!binary "SGVsbG8gd29yb==="', '/The base64 encoded data \(.*\) contains invalid characters/'),
- 'misplaced equals character' => array('data: !!binary "SGVsbG8gd29ybG=Q"', '/The base64 encoded data \(.*\) contains invalid characters/'),
- 'length not a multiple of four in block scalar' => array(
- <<<'EOT'
- data: !!binary |
- SGVsbG8d29ybGQ=
- EOT
- ,
- '/The normalized base64 encoded data \(data without whitespace characters\) length must be a multiple of four \(\d+ bytes given\)/',
- ),
- 'invalid characters in block scalar' => array(
- <<<'EOT'
- data: !!binary |
- SGVsbG8#d29ybGQ=
- EOT
- ,
- '/The base64 encoded data \(.*\) contains invalid characters/',
- ),
- 'too many equals characters in block scalar' => array(
- <<<'EOT'
- data: !!binary |
- SGVsbG8gd29yb===
- EOT
- ,
- '/The base64 encoded data \(.*\) contains invalid characters/',
- ),
- 'misplaced equals character in block scalar' => array(
- <<<'EOT'
- data: !!binary |
- SGVsbG8gd29ybG=Q
- EOT
- ,
- '/The base64 encoded data \(.*\) contains invalid characters/',
- ),
- );
- }
- public function testParseDateAsMappingValue()
- {
- $yaml = <<<'EOT'
- date: 2002-12-14
- EOT;
- $expectedDate = new \DateTime();
- $expectedDate->setTimeZone(new \DateTimeZone('UTC'));
- $expectedDate->setDate(2002, 12, 14);
- $expectedDate->setTime(0, 0, 0);
- $this->assertEquals(array('date' => $expectedDate), $this->parser->parse($yaml, Yaml::PARSE_DATETIME));
- }
- /**
- * @param $lineNumber
- * @param $yaml
- * @dataProvider parserThrowsExceptionWithCorrectLineNumberProvider
- */
- public function testParserThrowsExceptionWithCorrectLineNumber($lineNumber, $yaml)
- {
- $this->setExpectedException(
- '\Symfony\Component\Yaml\Exception\ParseException',
- sprintf('Unexpected characters near "," at line %d (near "bar: "123",").', $lineNumber)
- );
- $this->parser->parse($yaml);
- }
- public function parserThrowsExceptionWithCorrectLineNumberProvider()
- {
- return array(
- array(
- 4,
- <<<'YAML'
- foo:
- -
- # bar
- bar: "123",
- YAML
- ),
- array(
- 5,
- <<<'YAML'
- foo:
- -
- # bar
- # bar
- bar: "123",
- YAML
- ),
- array(
- 8,
- <<<'YAML'
- foo:
- -
- # foobar
- baz: 123
- bar:
- -
- # bar
- bar: "123",
- YAML
- ),
- array(
- 10,
- <<<'YAML'
- foo:
- -
- # foobar
- # foobar
- baz: 123
- bar:
- -
- # bar
- # bar
- bar: "123",
- YAML
- ),
- );
- }
- public function testParseMultiLineQuotedString()
- {
- $yaml = <<<EOT
- foo: "bar
- baz
- foobar
- foo"
- bar: baz
- EOT;
- $this->assertSame(array('foo' => 'bar baz foobar foo', 'bar' => 'baz'), $this->parser->parse($yaml));
- }
- public function testParseMultiLineUnquotedString()
- {
- $yaml = <<<EOT
- foo: bar
- baz
- foobar
- foo
- bar: baz
- EOT;
- $this->assertSame(array('foo' => 'bar baz foobar foo', 'bar' => 'baz'), $this->parser->parse($yaml));
- }
- }
- class B
- {
- public $b = 'foo';
- }
|