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 .
2010-03-18 07:48:03 +00:00
* ( c ) Fabien Potencier < fabien . potencier @ symfony - project . com >
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
2010-06-28 08:31:54 +01:00
namespace Symfony\Tests\Components\Yaml ;
2010-03-18 07:48:03 +00:00
use Symfony\Components\Yaml\Yaml ;
use Symfony\Components\Yaml\Parser ;
use Symfony\Components\Yaml\ParserException ;
class ParserTest extends \PHPUnit_Framework_TestCase
{
2010-05-06 12:25:53 +01:00
protected $parser ;
protected $path ;
2010-03-18 07:48:03 +00:00
2010-05-06 12:25:53 +01:00
static public function setUpBeforeClass ()
{
Yaml :: setSpecVersion ( '1.1' );
}
2010-03-18 07:48:03 +00:00
2010-05-06 12:25:53 +01:00
public function setUp ()
2010-03-18 07:48:03 +00:00
{
2010-05-06 12:25:53 +01:00
$this -> parser = new Parser ();
2010-06-28 08:31:54 +01:00
$this -> path = __DIR__ . '/Fixtures' ;
2010-05-06 12:25:53 +01:00
}
2010-03-18 07:48:03 +00:00
2010-05-06 12:25:53 +01:00
public function testSpecifications ()
{
$files = $this -> parser -> parse ( file_get_contents ( $this -> path . '/index.yml' ));
2010-05-07 15:09:11 +01:00
foreach ( $files as $file ) {
2010-05-06 12:25:53 +01:00
$yamls = file_get_contents ( $this -> 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 ;
}
$test = $this -> 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
2010-05-06 12:25:53 +01:00
$this -> assertEquals ( $expected , var_export ( $this -> parser -> parse ( $test [ 'yaml' ]), true ), $test [ 'test' ]);
}
}
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' );
$this -> assertEquals ( 'A YAML file cannot contain tabs as indentation at line 2 (' . strpbrk ( $yaml , " \t " ) . ').' , $e -> getMessage (), 'YAML files must not contain tabs' );
}
}
2010-03-18 07:48:03 +00:00
}
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-06-28 08:31:54 +01:00
foo : !! php / object : O : 31 : " Symfony \T ests \ Components \ 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' );
}
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
}