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 .
*/
2010-08-20 22:09:55 +01:00
namespace Symfony\Tests\Component\Yaml ;
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 ;
2011-06-14 11:44:54 +01:00
use Symfony\Component\Yaml\Exception\ParseException ;
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 {
2010-05-06 12:25:53 +01:00
$expected = var_export ( eval ( 'return ' . trim ( $test [ 'php' ]) . ';' ), true );
2010-03-18 07:48:03 +00:00
2012-04-19 18:28:53 +01:00
$tests [] = array ( $file , $expected , $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 ));
}
2010-05-06 12:25:53 +01:00
public function testObjectsSupport ()
{
$b = array ( 'foo' => new B (), 'bar' => 1 );
$this -> assertEquals ( $this -> parser -> parse ( <<< EOF
2010-08-20 22:09:55 +01:00
foo : !! php / object : O : 30 : " Symfony \T ests \ Component \ Yaml \ B " : 1 : { s : 1 : " b " ; s : 3 : " foo " ;}
2010-03-18 07:48:03 +00:00
bar : 1
EOF
2010-05-06 12:25:53 +01:00
), $b , '->parse() is able to dump objects' );
}
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
}
}
}
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
}