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
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
class ParserTest extends \PHPUnit_Framework_TestCase
{
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
{
2012-04-19 18:28:53 +01:00
if ( 'escapedCharacters' == $file ) {
if ( ! function_exists ( 'iconv' ) && ! function_exists ( 'mb_convert_encoding' )) {
$this -> markTestSkipped ( 'The iconv and mbstring extensions are not available.' );
}
}
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 ()
{
$yaml = <<< EOF
--- % 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
$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 );
$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 (
'foo' => " one two " ,
'bar' => " one two " ,
);
$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 (
'foo' => " one two " ,
'bar' => " one two " ,
);
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 (
'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 " ,
);
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 " ,
'bar' => " one two " ,
);
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 " ,
2013-03-23 01:54:33 +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 (
'foo' => " \n \n bar "
);
$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' );
}
public function testObjectSupportDisabledButNoExceptions ()
{
$input = <<< EOF
foo : !! php / object : O : 30 : " Symfony \T ests \ Component \ Yaml \ B " : 1 : { s : 1 : " b " ; s : 3 : " foo " ;}
bar : 1
EOF ;
$this -> assertEquals ( array ( 'foo' => null , 'bar' => 1 ), $this -> parser -> parse ( $input ), '->parse() does not parse objects' );
}
/**
* @ expectedException \Symfony\Component\Yaml\Exception\ParseException
*/
public function testObjectsSupportDisabledWithExceptions ()
{
$this -> parser -> parse ( 'foo: !!php/object:O:30:"Symfony\Tests\Component\Yaml\B":1:{s:1:"b";s:3:"foo";}' , true , false );
2010-05-06 12:25:53 +01:00
}
2011-05-12 21:02:56 +01:00
public function testNonUtf8Exception ()
{
2012-04-20 11:33:18 +01:00
if ( ! function_exists ( 'mb_detect_encoding' ) || ! function_exists ( 'iconv' )) {
$this -> markTestSkipped ( 'Exceptions for non-utf8 charsets require the mb_detect_encoding() and iconv() functions.' );
2011-05-12 21:02:56 +01:00
return ;
}
$yamls = array (
iconv ( " UTF-8 " , " ISO-8859-1 " , " foo: 'äöüß' " ),
iconv ( " UTF-8 " , " ISO-8859-15 " , " euro: '€' " ),
iconv ( " UTF-8 " , " CP1252 " , " cp1252: '©ÉÇáñ' " )
);
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 ) {
2011-06-14 11:44:54 +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 ()
{
$yaml = <<< EOF
collection :
- item1
- item2
- item3
EOF ;
$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 testSequenceInAMapping ()
{
Yaml :: parse ( <<< EOF
yaml :
hash : me
- array stuff
EOF
);
}
/**
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 ()
{
Yaml :: parse ( <<< EOF
yaml :
- array stuff
hash : me
EOF
);
}
2013-04-12 10:29:40 +01:00
public function testEmptyValue ()
{
$input = <<< EOF
hash :
EOF ;
$this -> assertEquals ( array ( 'hash' => null ), Yaml :: parse ( $input ));
}
2013-07-11 10:21:24 +01:00
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
));
}
2013-08-24 07:35:53 +01:00
public function testFoldedStringBlockWithComments ()
2013-07-11 10:21:24 +01:00
{
$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
2013-08-24 07:35:53 +01:00
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
2013-07-11 10:21:24 +01:00
EOF
));
}
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
}