2010-03-18 07:48:03 +00:00
< ? php
/*
2010-04-25 16:06:54 +01:00
* This file is part of the Symfony package .
2011-01-15 13:29:43 +00:00
*
2011-03-06 11:40:06 +00:00
* ( c ) Fabien Potencier < fabien @ symfony . com >
2010-03-18 07:48:03 +00:00
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
2012-03-28 14:43:52 +01:00
namespace Symfony\Component\Yaml\Tests ;
2010-03-18 07:48:03 +00:00
2017-02-08 07:24:27 +00:00
use PHPUnit\Framework\TestCase ;
2010-08-20 22:09:55 +01:00
use Symfony\Component\Yaml\Yaml ;
use Symfony\Component\Yaml\Parser ;
2010-03-18 07:48:03 +00:00
2017-02-08 07:24:27 +00:00
class ParserTest extends TestCase
2010-03-18 07:48:03 +00:00
{
2010-05-06 12:25:53 +01:00
protected $parser ;
2010-03-18 07:48:03 +00:00
2010-12-06 14:45:37 +00:00
protected function setUp ()
2010-03-18 07:48:03 +00:00
{
2010-05-06 12:25:53 +01:00
$this -> parser = new Parser ();
}
2010-03-18 07:48:03 +00:00
2011-06-14 09:40:42 +01:00
protected function tearDown ()
{
$this -> parser = null ;
}
2010-06-29 16:45:09 +01:00
/**
* @ dataProvider getDataFormSpecifications
*/
2012-04-19 18:28:53 +01:00
public function testSpecifications ( $file , $expected , $yaml , $comment )
2010-05-06 12:25:53 +01:00
{
2010-06-29 16:45:09 +01:00
$this -> assertEquals ( $expected , var_export ( $this -> parser -> parse ( $yaml ), true ), $comment );
}
public function getDataFormSpecifications ()
{
$parser = new Parser ();
$path = __DIR__ . '/Fixtures' ;
$tests = array ();
$files = $parser -> parse ( file_get_contents ( $path . '/index.yml' ));
2010-05-07 15:09:11 +01:00
foreach ( $files as $file ) {
2010-06-29 16:45:09 +01:00
$yamls = file_get_contents ( $path . '/' . $file . '.yml' );
2010-03-18 07:48:03 +00:00
2010-05-06 12:25:53 +01:00
// split YAMLs documents
2010-05-07 15:09:11 +01:00
foreach ( preg_split ( '/^---( %YAML\:1\.0)?/m' , $yamls ) as $yaml ) {
2010-05-08 14:32:30 +01:00
if ( ! $yaml ) {
2010-05-06 12:25:53 +01:00
continue ;
}
2010-06-29 16:45:09 +01:00
$test = $parser -> parse ( $yaml );
2010-05-07 15:09:11 +01:00
if ( isset ( $test [ 'todo' ]) && $test [ 'todo' ]) {
2010-05-06 12:25:53 +01:00
// TODO
2010-05-07 15:09:11 +01:00
} else {
2014-03-04 16:04:39 +00:00
eval ( '$expected = ' . trim ( $test [ 'php' ]) . ';' );
2010-03-18 07:48:03 +00:00
2014-03-04 16:04:39 +00:00
$tests [] = array ( $file , var_export ( $expected , true ), $test [ 'yaml' ], $test [ 'test' ]);
2010-05-06 12:25:53 +01:00
}
}
2010-03-18 07:48:03 +00:00
}
2010-06-29 16:45:09 +01:00
return $tests ;
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
public function testTabsInYaml ()
2010-03-18 07:48:03 +00:00
{
2010-05-06 12:25:53 +01:00
// test tabs in YAML
$yamls = array (
" foo: \n bar " ,
" foo: \n bar " ,
" foo: \n bar " ,
" foo: \n bar " ,
);
2010-05-07 15:09:11 +01:00
foreach ( $yamls as $yaml ) {
2010-05-08 14:32:30 +01:00
try {
2010-05-06 12:25:53 +01:00
$content = $this -> parser -> parse ( $yaml );
2010-03-18 07:48:03 +00:00
2010-05-06 12:25:53 +01:00
$this -> fail ( 'YAML files must not contain tabs' );
2010-05-07 15:09:11 +01:00
} catch ( \Exception $e ) {
2010-05-06 12:25:53 +01:00
$this -> assertInstanceOf ( '\Exception' , $e , 'YAML files must not contain tabs' );
2011-06-14 11:44:54 +01:00
$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' );
2010-05-06 12:25:53 +01:00
}
}
2010-03-18 07:48:03 +00:00
}
2010-06-29 16:51:05 +01:00
public function testEndOfTheDocumentMarker ()
{
2015-12-21 11:01:57 +00:00
$yaml = <<< 'EOF'
2010-06-29 16:51:05 +01:00
--- % YAML : 1.0
foo
...
EOF ;
$this -> assertEquals ( 'foo' , $this -> parser -> parse ( $yaml ));
}
2013-01-17 23:42:00 +00:00
public function getBlockChompingTests ()
{
$tests = array ();
$yaml = <<< 'EOF'
foo : |-
one
two
2013-03-23 01:54:33 +00:00
bar : |-
one
two
EOF ;
$expected = array (
'foo' => " one \n two " ,
'bar' => " one \n two " ,
);
$tests [ 'Literal block chomping strip with single trailing newline' ] = array ( $expected , $yaml );
$yaml = <<< 'EOF'
foo : |-
one
two
2013-01-17 23:42:00 +00:00
bar : |-
one
two
2013-03-23 01:54:33 +00:00
2013-01-17 23:42:00 +00:00
EOF ;
$expected = array (
'foo' => " one \n two " ,
'bar' => " one \n two " ,
);
2013-03-23 01:54:33 +00:00
$tests [ 'Literal block chomping strip with multiple trailing newlines' ] = array ( $expected , $yaml );
2013-01-17 23:42:00 +00:00
2015-01-12 11:05:05 +00:00
$yaml = <<< 'EOF'
{}
EOF ;
$expected = array ();
$tests [ 'Literal block chomping strip with multiple trailing newlines after a 1-liner' ] = array ( $expected , $yaml );
2013-01-17 23:42:00 +00:00
$yaml = <<< 'EOF'
foo : |-
one
two
bar : |-
one
two
EOF ;
$expected = array (
'foo' => " one \n two " ,
'bar' => " one \n two " ,
);
$tests [ 'Literal block chomping strip without trailing newline' ] = array ( $expected , $yaml );
$yaml = <<< 'EOF'
foo : |
one
two
bar : |
one
two
EOF ;
$expected = array (
'foo' => " one \n two \n " ,
'bar' => " one \n two \n " ,
);
2013-03-23 01:54:33 +00:00
$tests [ 'Literal block chomping clip with single trailing newline' ] = array ( $expected , $yaml );
2013-01-17 23:42:00 +00:00
$yaml = <<< 'EOF'
foo : |
one
two
2013-03-23 01:54:33 +00:00
2013-01-17 23:42:00 +00:00
bar : |
one
two
2013-03-23 01:54:33 +00:00
2013-01-17 23:42:00 +00:00
EOF ;
$expected = array (
'foo' => " one \n two \n " ,
'bar' => " one \n two \n " ,
);
2013-03-23 01:54:33 +00:00
$tests [ 'Literal block chomping clip with multiple trailing newlines' ] = array ( $expected , $yaml );
2016-12-28 18:13:31 +00:00
$yaml = <<< 'EOF'
foo :
- bar : |
one
two
EOF ;
$expected = array (
'foo' => array (
array (
'bar' => " one \n \n two " ,
),
),
);
$tests [ 'Literal block chomping clip with embedded blank line inside unindented collection' ] = array ( $expected , $yaml );
2013-03-23 01:54:33 +00:00
$yaml = <<< 'EOF'
foo : |
one
two
bar : |
one
two
EOF ;
$expected = array (
'foo' => " one \n two \n " ,
'bar' => " one \n two " ,
);
2013-01-17 23:42:00 +00:00
$tests [ 'Literal block chomping clip without trailing newline' ] = array ( $expected , $yaml );
$yaml = <<< 'EOF'
foo : |+
one
two
2013-03-23 01:54:33 +00:00
bar : |+
one
two
EOF ;
$expected = array (
'foo' => " one \n two \n " ,
'bar' => " one \n two \n " ,
);
$tests [ 'Literal block chomping keep with single trailing newline' ] = array ( $expected , $yaml );
$yaml = <<< 'EOF'
foo : |+
one
two
2013-01-17 23:42:00 +00:00
bar : |+
one
two
2013-03-23 01:54:33 +00:00
2013-01-17 23:42:00 +00:00
EOF ;
$expected = array (
'foo' => " one \n two \n \n " ,
'bar' => " one \n two \n \n " ,
);
2013-03-23 01:54:33 +00:00
$tests [ 'Literal block chomping keep with multiple trailing newlines' ] = array ( $expected , $yaml );
2013-01-17 23:42:00 +00:00
$yaml = <<< 'EOF'
foo : |+
one
two
bar : |+
one
two
EOF ;
$expected = array (
'foo' => " one \n two \n " ,
2013-03-23 01:54:33 +00:00
'bar' => " one \n two " ,
2013-01-17 23:42:00 +00:00
);
$tests [ 'Literal block chomping keep without trailing newline' ] = array ( $expected , $yaml );
$yaml = <<< 'EOF'
foo : >-
one
two
2013-03-23 01:54:33 +00:00
bar : >-
one
two
EOF ;
$expected = array (
2015-03-21 10:51:07 +00:00
'foo' => 'one two' ,
'bar' => 'one two' ,
2013-03-23 01:54:33 +00:00
);
$tests [ 'Folded block chomping strip with single trailing newline' ] = array ( $expected , $yaml );
$yaml = <<< 'EOF'
foo : >-
one
two
2013-01-17 23:42:00 +00:00
bar : >-
one
two
2013-03-23 01:54:33 +00:00
2013-01-17 23:42:00 +00:00
EOF ;
$expected = array (
2015-03-21 10:51:07 +00:00
'foo' => 'one two' ,
'bar' => 'one two' ,
2013-01-17 23:42:00 +00:00
);
2013-03-23 01:54:33 +00:00
$tests [ 'Folded block chomping strip with multiple trailing newlines' ] = array ( $expected , $yaml );
2013-01-17 23:42:00 +00:00
$yaml = <<< 'EOF'
foo : >-
one
two
bar : >-
one
two
EOF ;
$expected = array (
2015-03-21 10:51:07 +00:00
'foo' => 'one two' ,
'bar' => 'one two' ,
2013-01-17 23:42:00 +00:00
);
$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 " ,
);
2013-03-23 01:54:33 +00:00
$tests [ 'Folded block chomping clip with single trailing newline' ] = array ( $expected , $yaml );
2013-01-17 23:42:00 +00:00
$yaml = <<< 'EOF'
foo : >
one
two
2013-03-23 01:54:33 +00:00
2013-01-17 23:42:00 +00:00
bar : >
one
two
2013-03-23 01:54:33 +00:00
2013-01-17 23:42:00 +00:00
EOF ;
$expected = array (
'foo' => " one two \n " ,
'bar' => " one two \n " ,
);
2013-03-23 01:54:33 +00:00
$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 " ,
2015-03-21 10:51:07 +00:00
'bar' => 'one two' ,
2013-03-23 01:54:33 +00:00
);
2013-01-17 23:42:00 +00:00
$tests [ 'Folded block chomping clip without trailing newline' ] = array ( $expected , $yaml );
$yaml = <<< 'EOF'
foo : >+
one
two
2013-03-23 01:54:33 +00:00
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
2013-01-17 23:42:00 +00:00
bar : >+
one
two
2013-03-23 01:54:33 +00:00
2013-01-17 23:42:00 +00:00
EOF ;
$expected = array (
'foo' => " one two \n \n " ,
'bar' => " one two \n \n " ,
);
2013-03-23 01:54:33 +00:00
$tests [ 'Folded block chomping keep with multiple trailing newlines' ] = array ( $expected , $yaml );
2013-01-17 23:42:00 +00:00
$yaml = <<< 'EOF'
foo : >+
one
two
bar : >+
one
two
EOF ;
$expected = array (
'foo' => " one two \n " ,
2015-03-21 10:51:07 +00:00
'bar' => 'one two' ,
2013-01-17 23:42:00 +00:00
);
$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 ));
}
2013-05-10 01:09:46 +01:00
/**
* Regression test for issue #7989.
*
* @ see https :// github . com / symfony / symfony / issues / 7989
*/
public function testBlockLiteralWithLeadingNewlines ()
{
$yaml = <<< 'EOF'
foo : |-
bar
EOF ;
$expected = array (
2014-09-21 19:53:12 +01:00
'foo' => " \n \n bar " ,
2013-05-10 01:09:46 +01:00
);
$this -> assertSame ( $expected , $this -> parser -> parse ( $yaml ));
}
added a way to enable/disable object support when parsing/dumping
By default, object support is disabled, and instead of throwing an
exception when an object is handled, null is returned.
If you do need object support, enable it via:
Yaml::dump($data, false, true);
If you want an exception to be thrown in case an invalid type is handled
(a PHP resource or a PHP object), pass true as the second argument:
Yaml::dump($data, true, true);
The same can be done when parsing:
Yaml::parse($data, 2, false, true);
2013-01-17 08:34:45 +00:00
public function testObjectSupportEnabled ()
2010-05-06 12:25:53 +01:00
{
added a way to enable/disable object support when parsing/dumping
By default, object support is disabled, and instead of throwing an
exception when an object is handled, null is returned.
If you do need object support, enable it via:
Yaml::dump($data, false, true);
If you want an exception to be thrown in case an invalid type is handled
(a PHP resource or a PHP object), pass true as the second argument:
Yaml::dump($data, true, true);
The same can be done when parsing:
Yaml::parse($data, 2, false, true);
2013-01-17 08:34:45 +00:00
$input = <<< EOF
2012-03-28 14:43:52 +01:00
foo : !! php / object : O : 30 : " Symfony \ Component \ Yaml \T ests \ B " : 1 : { s : 1 : " b " ; s : 3 : " foo " ;}
2010-03-18 07:48:03 +00:00
bar : 1
added a way to enable/disable object support when parsing/dumping
By default, object support is disabled, and instead of throwing an
exception when an object is handled, null is returned.
If you do need object support, enable it via:
Yaml::dump($data, false, true);
If you want an exception to be thrown in case an invalid type is handled
(a PHP resource or a PHP object), pass true as the second argument:
Yaml::dump($data, true, true);
The same can be done when parsing:
Yaml::parse($data, 2, false, true);
2013-01-17 08:34:45 +00:00
EOF ;
$this -> assertEquals ( array ( 'foo' => new B (), 'bar' => 1 ), $this -> parser -> parse ( $input , false , true ), '->parse() is able to parse objects' );
$input = <<< EOF
2016-01-20 19:11:57 +00:00
foo : ! php / object : O : 30 : " Symfony \ Component \ Yaml \T ests \ B " : 1 : { s : 1 : " b " ; s : 3 : " foo " ;}
added a way to enable/disable object support when parsing/dumping
By default, object support is disabled, and instead of throwing an
exception when an object is handled, null is returned.
If you do need object support, enable it via:
Yaml::dump($data, false, true);
If you want an exception to be thrown in case an invalid type is handled
(a PHP resource or a PHP object), pass true as the second argument:
Yaml::dump($data, true, true);
The same can be done when parsing:
Yaml::parse($data, 2, false, true);
2013-01-17 08:34:45 +00:00
bar : 1
EOF ;
2016-01-20 19:11:57 +00:00
$this -> assertEquals ( array ( 'foo' => new B (), 'bar' => 1 ), $this -> parser -> parse ( $input , false , true ), '->parse() is able to parse objects' );
}
added a way to enable/disable object support when parsing/dumping
By default, object support is disabled, and instead of throwing an
exception when an object is handled, null is returned.
If you do need object support, enable it via:
Yaml::dump($data, false, true);
If you want an exception to be thrown in case an invalid type is handled
(a PHP resource or a PHP object), pass true as the second argument:
Yaml::dump($data, true, true);
The same can be done when parsing:
Yaml::parse($data, 2, false, true);
2013-01-17 08:34:45 +00:00
2016-01-20 19:11:57 +00:00
/**
* @ dataProvider invalidDumpedObjectProvider
*/
public function testObjectSupportDisabledButNoExceptions ( $input )
{
added a way to enable/disable object support when parsing/dumping
By default, object support is disabled, and instead of throwing an
exception when an object is handled, null is returned.
If you do need object support, enable it via:
Yaml::dump($data, false, true);
If you want an exception to be thrown in case an invalid type is handled
(a PHP resource or a PHP object), pass true as the second argument:
Yaml::dump($data, true, true);
The same can be done when parsing:
Yaml::parse($data, 2, false, true);
2013-01-17 08:34:45 +00:00
$this -> assertEquals ( array ( 'foo' => null , 'bar' => 1 ), $this -> parser -> parse ( $input ), '->parse() does not parse objects' );
}
2015-12-20 11:25:49 +00:00
public function testObjectForMapEnabledWithMapping ()
{
2016-10-30 09:34:06 +00:00
$yaml = <<< 'EOF'
2015-12-20 11:25:49 +00:00
foo :
fiz : [ cat ]
EOF ;
$result = $this -> parser -> parse ( $yaml , false , false , true );
$this -> assertInstanceOf ( 'stdClass' , $result );
$this -> assertInstanceOf ( 'stdClass' , $result -> foo );
$this -> assertEquals ( array ( 'cat' ), $result -> foo -> fiz );
}
public function testObjectForMapEnabledWithInlineMapping ()
{
$result = $this -> parser -> parse ( '{ "foo": "bar", "fiz": "cat" }' , false , false , true );
$this -> assertInstanceOf ( 'stdClass' , $result );
$this -> assertEquals ( 'bar' , $result -> foo );
$this -> assertEquals ( 'cat' , $result -> fiz );
}
added a way to enable/disable object support when parsing/dumping
By default, object support is disabled, and instead of throwing an
exception when an object is handled, null is returned.
If you do need object support, enable it via:
Yaml::dump($data, false, true);
If you want an exception to be thrown in case an invalid type is handled
(a PHP resource or a PHP object), pass true as the second argument:
Yaml::dump($data, true, true);
The same can be done when parsing:
Yaml::parse($data, 2, false, true);
2013-01-17 08:34:45 +00:00
/**
2016-01-20 19:11:57 +00:00
* @ dataProvider invalidDumpedObjectProvider
added a way to enable/disable object support when parsing/dumping
By default, object support is disabled, and instead of throwing an
exception when an object is handled, null is returned.
If you do need object support, enable it via:
Yaml::dump($data, false, true);
If you want an exception to be thrown in case an invalid type is handled
(a PHP resource or a PHP object), pass true as the second argument:
Yaml::dump($data, true, true);
The same can be done when parsing:
Yaml::parse($data, 2, false, true);
2013-01-17 08:34:45 +00:00
* @ expectedException \Symfony\Component\Yaml\Exception\ParseException
*/
2016-01-20 19:11:57 +00:00
public function testObjectsSupportDisabledWithExceptions ( $yaml )
{
$this -> parser -> parse ( $yaml , true , false );
}
public function invalidDumpedObjectProvider ()
added a way to enable/disable object support when parsing/dumping
By default, object support is disabled, and instead of throwing an
exception when an object is handled, null is returned.
If you do need object support, enable it via:
Yaml::dump($data, false, true);
If you want an exception to be thrown in case an invalid type is handled
(a PHP resource or a PHP object), pass true as the second argument:
Yaml::dump($data, true, true);
The same can be done when parsing:
Yaml::parse($data, 2, false, true);
2013-01-17 08:34:45 +00:00
{
2016-01-20 19:11:57 +00:00
$yamlTag = <<< EOF
foo : !! php / object : O : 30 : " Symfony \T ests \ Component \ Yaml \ B " : 1 : { s : 1 : " b " ; s : 3 : " foo " ;}
bar : 1
EOF ;
$localTag = <<< EOF
foo : ! php / object : O : 30 : " Symfony \T ests \ Component \ Yaml \ B " : 1 : { s : 1 : " b " ; s : 3 : " foo " ;}
bar : 1
EOF ;
return array (
'yaml-tag' => array ( $yamlTag ),
'local-tag' => array ( $localTag ),
);
2010-05-06 12:25:53 +01:00
}
2011-05-12 21:02:56 +01:00
2015-10-08 10:41:54 +01:00
/**
* @ requires extension iconv
*/
2011-05-12 21:02:56 +01:00
public function testNonUtf8Exception ()
{
$yamls = array (
2015-03-21 10:51:07 +00:00
iconv ( 'UTF-8' , 'ISO-8859-1' , " foo: 'äöüß' " ),
iconv ( 'UTF-8' , 'ISO-8859-15' , " euro: '€' " ),
iconv ( 'UTF-8' , 'CP1252' , " cp1252: '©ÉÇáñ' " ),
2011-05-12 21:02:56 +01:00
);
2011-05-13 00:08:37 +01:00
foreach ( $yamls as $yaml ) {
2011-05-12 21:02:56 +01:00
try {
$this -> parser -> parse ( $yaml );
$this -> fail ( 'charsets other than UTF-8 are rejected.' );
2011-05-13 00:08:37 +01:00
} catch ( \Exception $e ) {
2014-09-21 19:53:12 +01:00
$this -> assertInstanceOf ( 'Symfony\Component\Yaml\Exception\ParseException' , $e , 'charsets other than UTF-8 are rejected.' );
2011-05-12 21:02:56 +01:00
}
}
}
2012-04-26 18:34:19 +01:00
/**
2013-01-05 17:52:40 +00:00
* @ expectedException \Symfony\Component\Yaml\Exception\ParseException
2012-04-26 18:34:19 +01:00
*/
public function testUnindentedCollectionException ()
{
2015-12-21 11:01:57 +00:00
$yaml = <<< 'EOF'
2012-04-26 18:34:19 +01:00
collection :
- item1
- item2
- item3
2015-01-10 08:51:00 +00:00
EOF ;
$this -> parser -> parse ( $yaml );
}
/**
* @ expectedException \Symfony\Component\Yaml\Exception\ParseException
*/
public function testShortcutKeyUnindentedCollectionException ()
{
2015-12-21 11:01:57 +00:00
$yaml = <<< 'EOF'
2015-01-10 08:51:00 +00:00
collection :
- key : foo
foo : bar
2012-04-26 18:34:19 +01:00
EOF ;
$this -> parser -> parse ( $yaml );
}
2014-11-01 12:21:10 +00:00
/**
* @ expectedException \Symfony\Component\Yaml\Exception\ParseException
2016-05-23 10:57:38 +01:00
* @ expectedExceptionMessageRegExp /^ Multiple documents are not supported .+/
2014-11-01 12:21:10 +00:00
*/
public function testMultipleDocumentsNotSupportedException ()
{
2015-12-21 11:01:57 +00:00
Yaml :: parse ( <<< 'EOL'
2014-11-01 12:21:10 +00:00
# Ranking of 1998 home runs
---
- Mark McGwire
- Sammy Sosa
- Ken Griffey
# Team ranking
---
- Chicago Cubs
- St Louis Cardinals
EOL
);
}
2012-07-01 10:19:53 +01:00
/**
2013-01-05 17:52:40 +00:00
* @ expectedException \Symfony\Component\Yaml\Exception\ParseException
2012-07-01 10:19:53 +01:00
*/
public function testSequenceInAMapping ()
{
2015-12-21 11:01:57 +00:00
Yaml :: parse ( <<< 'EOF'
2012-07-01 10:19:53 +01:00
yaml :
hash : me
- array stuff
EOF
);
}
2016-05-22 17:02:36 +01:00
public function testSequenceInMappingStartedBySingleDashLine ()
{
2016-10-30 09:34:06 +00:00
$yaml = <<< 'EOT'
2016-05-22 17:02:36 +01:00
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 ));
}
2016-06-10 22:57:14 +01:00
public function testSequenceFollowedByCommentEmbeddedInMapping ()
{
2016-10-30 09:34:06 +00:00
$yaml = <<< 'EOT'
2016-06-10 22:57:14 +01:00
a :
b :
- c
# comment
d : e
EOT ;
$expected = array (
'a' => array (
'b' => array ( 'c' ),
'd' => 'e' ,
),
);
$this -> assertSame ( $expected , $this -> parser -> parse ( $yaml ));
}
2012-07-01 10:19:53 +01:00
/**
2013-01-05 17:52:40 +00:00
* @ expectedException \Symfony\Component\Yaml\Exception\ParseException
2012-07-01 10:19:53 +01:00
*/
public function testMappingInASequence ()
{
2015-12-21 11:01:57 +00:00
Yaml :: parse ( <<< 'EOF'
2012-07-01 10:19:53 +01:00
yaml :
- array stuff
hash : me
EOF
);
}
2013-04-12 10:29:40 +01:00
2015-07-22 07:30:32 +01:00
/**
* @ expectedException \Symfony\Component\Yaml\Exception\ParseException
* @ expectedExceptionMessage missing colon
*/
public function testScalarInSequence ()
{
2016-10-30 09:34:06 +00:00
Yaml :: parse ( <<< 'EOF'
2015-07-22 07:30:32 +01:00
foo :
- bar
" missing colon "
foo : bar
EOF
);
}
2014-05-15 01:05:21 +01:00
/**
* > 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
*/
public function testMappingDuplicateKeyBlock ()
{
2016-10-30 09:34:06 +00:00
$input = <<< 'EOD'
2014-05-15 01:05:21 +01:00
parent :
child : first
child : duplicate
parent :
child : duplicate
child : duplicate
EOD ;
$expected = array (
'parent' => array (
'child' => 'first' ,
),
);
$this -> assertSame ( $expected , Yaml :: parse ( $input ));
}
public function testMappingDuplicateKeyFlow ()
{
2016-10-30 09:34:06 +00:00
$input = <<< 'EOD'
2014-05-15 01:05:21 +01:00
parent : { child : first , child : duplicate }
parent : { child : duplicate , child : duplicate }
EOD ;
$expected = array (
'parent' => array (
'child' => 'first' ,
),
);
$this -> assertSame ( $expected , Yaml :: parse ( $input ));
}
2013-04-12 10:29:40 +01:00
public function testEmptyValue ()
{
2015-12-21 11:01:57 +00:00
$input = <<< 'EOF'
2013-04-12 10:29:40 +01:00
hash :
EOF ;
$this -> assertEquals ( array ( 'hash' => null ), Yaml :: parse ( $input ));
}
2013-07-11 10:21:24 +01:00
2015-10-02 12:13:24 +01:00
public function testCommentAtTheRootIndent ()
{
$this -> assertEquals ( array (
'services' => array (
'app.foo_service' => array (
'class' => 'Foo' ,
),
'app/bar_service' => array (
'class' => 'Bar' ,
),
),
2015-12-21 11:01:57 +00:00
), Yaml :: parse ( <<< 'EOF'
2015-10-02 12:13:24 +01:00
# comment 1
services :
# comment 2
# comment 3
app . foo_service :
class : Foo
# comment 4
# comment 5
app / bar_service :
class : Bar
EOF
));
}
2013-07-11 10:21:24 +01:00
public function testStringBlockWithComments ()
{
2015-12-21 11:01:57 +00:00
$this -> assertEquals ( array ( 'content' => <<< 'EOT'
2013-07-11 10:21:24 +01:00
# comment 1
header
# comment 2
< body >
< h1 > title </ h1 >
</ body >
footer # comment3
EOT
2015-12-21 11:01:57 +00:00
), Yaml :: parse ( <<< 'EOF'
2013-07-11 10:21:24 +01:00
content : |
# comment 1
header
# comment 2
< body >
< h1 > title </ h1 >
</ body >
footer # comment3
EOF
));
}
2013-08-24 07:35:53 +01:00
public function testFoldedStringBlockWithComments ()
2013-07-11 10:21:24 +01:00
{
2015-12-21 11:01:57 +00:00
$this -> assertEquals ( array ( array ( 'content' => <<< 'EOT'
2013-07-11 10:21:24 +01:00
# comment 1
header
# comment 2
< body >
< h1 > title </ h1 >
</ body >
footer # comment3
EOT
2015-12-21 11:01:57 +00:00
)), Yaml :: parse ( <<< 'EOF'
2013-07-11 10:21:24 +01:00
-
content : |
# comment 1
header
# comment 2
< body >
< h1 > title </ h1 >
</ body >
footer # comment3
2013-08-24 07:35:53 +01:00
EOF
));
}
public function testNestedFoldedStringBlockWithComments ()
{
$this -> assertEquals ( array ( array (
2014-10-22 19:27:13 +01:00
'title' => 'some title' ,
2015-12-21 11:01:57 +00:00
'content' => <<< 'EOT'
2013-08-24 07:35:53 +01:00
# comment 1
header
# comment 2
< body >
< h1 > title </ h1 >
</ body >
footer # comment3
EOT
2015-12-21 11:01:57 +00:00
)), Yaml :: parse ( <<< 'EOF'
2013-08-24 07:35:53 +01:00
-
title : some title
content : |
# comment 1
header
# comment 2
< body >
< h1 > title </ h1 >
</ body >
footer # comment3
2014-08-15 13:16:41 +01:00
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' )),
2015-12-21 11:01:57 +00:00
), Yaml :: parse ( <<< 'EOF'
2014-08-15 13:16:41 +01:00
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 } }
2013-07-11 10:21:24 +01:00
EOF
));
}
2015-01-08 15:45:16 +00:00
public function testYamlDirective ()
{
2015-12-21 11:01:57 +00:00
$yaml = <<< 'EOF'
2015-01-08 15:45:16 +00:00
% YAML 1.2
---
foo : 1
bar : 2
EOF ;
$this -> assertEquals ( array ( 'foo' => 1 , 'bar' => 2 ), $this -> parser -> parse ( $yaml ));
}
2015-09-12 16:58:59 +01:00
public function testFloatKeys ()
{
2015-12-21 11:01:57 +00:00
$yaml = <<< 'EOF'
2015-09-12 16:58:59 +01:00
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 ));
}
2015-12-05 10:55:16 +00:00
/**
* @ dataProvider getCommentLikeStringInScalarBlockData
*/
public function testCommentLikeStringsAreNotStrippedInBlockScalars ( $yaml , $expectedParserResult )
{
$this -> assertSame ( $expectedParserResult , $this -> parser -> parse ( $yaml ));
}
public function getCommentLikeStringInScalarBlockData ()
{
2015-12-28 14:55:02 +00:00
$tests = array ();
$yaml = <<< 'EOT'
2015-12-05 10:55:16 +00:00
pages :
-
title : some title
content : |
# comment 1
header
# comment 2
< body >
< h1 > title </ h1 >
</ body >
footer # comment3
EOT ;
2015-12-28 14:55:02 +00:00
$expected = array (
2015-12-05 10:55:16 +00:00
'pages' => array (
array (
'title' => 'some title' ,
2015-12-21 11:01:57 +00:00
'content' => <<< 'EOT'
2015-12-05 10:55:16 +00:00
# comment 1
header
# comment 2
< body >
< h1 > title </ h1 >
</ body >
footer # comment3
EOT
,
),
),
);
2015-12-28 14:55:02 +00:00
$tests [] = array ( $yaml , $expected );
2015-12-05 10:55:16 +00:00
2015-12-28 14:55:02 +00:00
$yaml = <<< 'EOT'
2015-12-05 10:55:16 +00:00
test : |
foo
# bar
baz
collection :
- one : |
foo
# bar
baz
- two : |
foo
# bar
baz
EOT ;
2015-12-28 14:55:02 +00:00
$expected = array (
2015-12-21 11:01:57 +00:00
'test' => <<< 'EOT'
2015-12-05 10:55:16 +00:00
foo
# bar
baz
EOT
,
'collection' => array (
array (
2015-12-21 11:01:57 +00:00
'one' => <<< 'EOT'
2015-12-05 10:55:16 +00:00
foo
# bar
baz
2016-05-14 16:57:37 +01:00
2015-12-05 10:55:16 +00:00
EOT
,
),
array (
2015-12-21 11:01:57 +00:00
'two' => <<< 'EOT'
2015-12-05 10:55:16 +00:00
foo
# bar
baz
EOT
,
),
),
);
2015-12-28 14:55:02 +00:00
$tests [] = array ( $yaml , $expected );
2015-12-05 10:55:16 +00:00
2016-10-30 09:34:06 +00:00
$yaml = <<< 'EOT'
2015-12-28 14:55:02 +00:00
foo :
bar :
scalar - block : >
line1
line2 >
baz :
# comment
foobar : ~
EOT ;
$expected = array (
'foo' => array (
'bar' => array (
2016-05-14 16:57:37 +01:00
'scalar-block' => " line1 line2> \n " ,
2015-12-28 14:55:02 +00:00
),
'baz' => array (
'foobar' => null ,
),
),
2015-12-05 10:55:16 +00:00
);
2015-12-28 14:55:02 +00:00
$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 ;
2015-12-05 10:55:16 +00:00
}
2015-12-05 16:57:30 +00:00
public function testBlankLinesAreParsedAsNewLinesInFoldedBlocks ()
{
2016-10-30 09:34:06 +00:00
$yaml = <<< 'EOT'
2015-12-05 16:57:30 +00:00
test : >
< h2 > A heading </ h2 >
< ul >
< li > a list </ li >
< li > may be a good example </ li >
</ ul >
EOT ;
$this -> assertSame (
array (
2016-10-30 09:34:06 +00:00
'test' => <<< 'EOT'
2015-12-05 16:57:30 +00:00
< 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 ()
{
2016-10-30 09:34:06 +00:00
$yaml = <<< 'EOT'
2015-12-05 16:57:30 +00:00
test : >
< h2 > A heading </ h2 >
< ul >
< li > a list </ li >
< li > may be a good example </ li >
</ ul >
EOT ;
$this -> assertSame (
array (
2016-10-30 09:34:06 +00:00
'test' => <<< 'EOT'
2015-12-05 16:57:30 +00:00
< h2 > A heading </ h2 >
< ul >
< li > a list </ li >
< li > may be a good example </ li >
</ ul >
EOT
,
),
$this -> parser -> parse ( $yaml )
);
}
2016-02-08 23:53:32 +00:00
/**
* @ 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 ,
2016-10-30 09:34:06 +00:00
<<< 'YAML'
2016-02-08 23:53:32 +00:00
foo :
-
# bar
bar : " 123 " ,
YAML
),
array (
5 ,
2016-10-30 09:34:06 +00:00
<<< 'YAML'
2016-02-08 23:53:32 +00:00
foo :
-
# bar
# bar
bar : " 123 " ,
YAML
),
array (
8 ,
2016-10-30 09:34:06 +00:00
<<< 'YAML'
2016-02-08 23:53:32 +00:00
foo :
-
# foobar
baz : 123
bar :
-
# bar
bar : " 123 " ,
YAML
),
array (
10 ,
2016-10-30 09:34:06 +00:00
<<< 'YAML'
2016-02-08 23:53:32 +00:00
foo :
-
# foobar
# foobar
baz : 123
bar :
-
# bar
# bar
bar : " 123 " ,
YAML
),
);
}
2010-03-18 07:48:03 +00:00
}
class B
{
2010-05-06 12:25:53 +01:00
public $b = 'foo' ;
2010-03-18 07:48:03 +00:00
}