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 Symfony\Component\Yaml\Yaml;
15
use Symfony\Component\Yaml\Parser;
17
class ParserTest extends \PHPUnit_Framework_TestCase
21
protected function setUp()
23
$this->parser = new Parser();
26
protected function tearDown()
32
* @dataProvider getDataFormSpecifications
34
public function testSpecifications($file, $expected, $yaml, $comment)
36
$this->assertEquals($expected, var_export($this->parser->parse($yaml), true), $comment);
39
public function getDataFormSpecifications()
41
$parser = new Parser();
42
$path = __DIR__.'/Fixtures';
45
$files = $parser->parse(file_get_contents($path.'/index.yml'));
46
foreach ($files as $file) {
47
$yamls = file_get_contents($path.'/'.$file.'.yml');
49
// split YAMLs documents
50
foreach (preg_split('/^---( %YAML\:1\.0)?/m', $yamls) as $yaml) {
55
$test = $parser->parse($yaml);
56
if (isset($test['todo']) && $test['todo']) {
59
eval('$expected = '.trim($test['php']).';');
61
$tests[] = array($file, var_export($expected, true), $test['yaml'], $test['test']);
69
public function testTabsInYaml()
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');
91
public function testEndOfTheDocumentMarker()
99
$this->assertEquals('foo', $this->parser->parse($yaml));
102
public function getBlockChompingTests()
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);
150
$tests['Literal block chomping strip without trailing newline'] = array($expected, $yaml);
162
'foo' => "one\ntwo\n",
163
'bar' => "one\ntwo\n",
165
$tests['Literal block chomping clip with single trailing newline'] = array($expected, $yaml);
179
'foo' => "one\ntwo\n",
180
'bar' => "one\ntwo\n",
182
$tests['Literal block chomping clip with multiple trailing newlines'] = array($expected, $yaml);
193
'foo' => "one\ntwo\n",
196
$tests['Literal block chomping clip without trailing newline'] = array($expected, $yaml);
208
'foo' => "one\ntwo\n",
209
'bar' => "one\ntwo\n",
211
$tests['Literal block chomping keep with single trailing newline'] = array($expected, $yaml);
225
'foo' => "one\ntwo\n\n",
226
'bar' => "one\ntwo\n\n",
228
$tests['Literal block chomping keep with multiple trailing newlines'] = array($expected, $yaml);
239
'foo' => "one\ntwo\n",
242
$tests['Literal block chomping keep without trailing newline'] = array($expected, $yaml);
257
$tests['Folded block chomping strip with single trailing newline'] = array($expected, $yaml);
274
$tests['Folded block chomping strip with multiple trailing newlines'] = array($expected, $yaml);
288
$tests['Folded block chomping strip without trailing newline'] = array($expected, $yaml);
300
'foo' => "one two\n",
301
'bar' => "one two\n",
303
$tests['Folded block chomping clip with single trailing newline'] = array($expected, $yaml);
317
'foo' => "one two\n",
318
'bar' => "one two\n",
320
$tests['Folded block chomping clip with multiple trailing newlines'] = array($expected, $yaml);
331
'foo' => "one two\n",
334
$tests['Folded block chomping clip without trailing newline'] = array($expected, $yaml);
346
'foo' => "one two\n",
347
'bar' => "one two\n",
349
$tests['Folded block chomping keep with single trailing newline'] = array($expected, $yaml);
363
'foo' => "one two\n\n",
364
'bar' => "one two\n\n",
366
$tests['Folded block chomping keep with multiple trailing newlines'] = array($expected, $yaml);
377
'foo' => "one two\n",
380
$tests['Folded block chomping keep without trailing newline'] = array($expected, $yaml);
386
* @dataProvider getBlockChompingTests
388
public function testBlockChomping($expected, $yaml)
390
$this->assertSame($expected, $this->parser->parse($yaml));
394
* Regression test for issue #7989.
396
* @see https://github.com/symfony/symfony/issues/7989
398
public function testBlockLiteralWithLeadingNewlines()
411
$this->assertSame($expected, $this->parser->parse($yaml));
414
public function testObjectSupportEnabled()
417
foo: !!php/object:O:30:"Symfony\Component\Yaml\Tests\B":1:{s:1:"b";s:3:"foo";}
420
$this->assertEquals(array('foo' => new B(), 'bar' => 1), $this->parser->parse($input, false, true), '->parse() is able to parse objects');
423
public function testObjectSupportDisabledButNoExceptions()
426
foo: !!php/object:O:30:"Symfony\Tests\Component\Yaml\B":1:{s:1:"b";s:3:"foo";}
430
$this->assertEquals(array('foo' => null, 'bar' => 1), $this->parser->parse($input), '->parse() does not parse objects');
434
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
436
public function testObjectsSupportDisabledWithExceptions()
438
$this->parser->parse('foo: !!php/object:O:30:"Symfony\Tests\Component\Yaml\B":1:{s:1:"b";s:3:"foo";}', true, false);
441
public function testNonUtf8Exception()
443
if (!function_exists('iconv')) {
444
$this->markTestSkipped('Exceptions for non-utf8 charsets require the iconv() function.');
450
iconv("UTF-8", "ISO-8859-1", "foo: 'äöüß'"),
451
iconv("UTF-8", "ISO-8859-15", "euro: '€'"),
452
iconv("UTF-8", "CP1252", "cp1252: '©ÉÇáñ'"),
455
foreach ($yamls as $yaml) {
457
$this->parser->parse($yaml);
459
$this->fail('charsets other than UTF-8 are rejected.');
460
} catch (\Exception $e) {
461
$this->assertInstanceOf('Symfony\Component\Yaml\Exception\ParseException', $e, 'charsets other than UTF-8 are rejected.');
467
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
469
public function testUnindentedCollectionException()
480
$this->parser->parse($yaml);
484
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
486
public function testShortcutKeyUnindentedCollectionException()
496
$this->parser->parse($yaml);
500
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
501
* @expectedExceptionMessage Multiple documents are not supported.
503
public function testMultipleDocumentsNotSupportedException()
506
# Ranking of 1998 home runs
521
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
523
public function testSequenceInAMapping()
534
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
536
public function testMappingInASequence()
547
* > It is an error for two equal keys to appear in the same mapping node.
548
* > In such a case the YAML processor may continue, ignoring the second
549
* > `key: value` pair and issuing an appropriate warning. This strategy
550
* > preserves a consistent information model for one-pass and random access
553
* @see http://yaml.org/spec/1.2/spec.html#id2759572
554
* @see http://yaml.org/spec/1.1/#id932806
556
* @covers \Symfony\Component\Yaml\Parser::parse
558
public function testMappingDuplicateKeyBlock()
573
$this->assertSame($expected, Yaml::parse($input));
577
* @covers \Symfony\Component\Yaml\Inline::parseMapping
579
public function testMappingDuplicateKeyFlow()
582
parent: { child: first, child: duplicate }
583
parent: { child: duplicate, child: duplicate }
590
$this->assertSame($expected, Yaml::parse($input));
593
public function testEmptyValue()
599
$this->assertEquals(array('hash' => null), Yaml::parse($input));
602
public function testStringBlockWithComments()
604
$this->assertEquals(array('content' => <<<EOT
615
), Yaml::parse(<<<EOF
630
public function testFoldedStringBlockWithComments()
632
$this->assertEquals(array(array('content' => <<<EOT
643
)), Yaml::parse(<<<EOF
659
public function testNestedFoldedStringBlockWithComments()
661
$this->assertEquals(array(array(
662
'title' => 'some title',
674
)), Yaml::parse(<<<EOF
691
public function testReferenceResolvingInInlineStrings()
693
$this->assertEquals(array(
694
'var' => 'var-value',
695
'scalar' => 'var-value',
696
'list' => array('var-value'),
697
'list_in_list' => array(array('var-value')),
698
'map_in_list' => array(array('key' => 'var-value')),
699
'embedded_mapping' => array(array('key' => 'var-value')),
700
'map' => array('key' => 'var-value'),
701
'list_in_map' => array('key' => array('var-value')),
702
'map_in_map' => array('foo' => array('bar' => 'var-value')),
703
), Yaml::parse(<<<EOF
707
list_in_list: [[ *var ]]
708
map_in_list: [ { key: *var } ]
709
embedded_mapping: [ key: *var ]
711
list_in_map: { key: [*var] }
712
map_in_map: { foo: { bar: *var } }
717
public function testYamlDirective()
725
$this->assertEquals(array('foo' => 1, 'bar' => 2), $this->parser->parse($yaml));