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-04-07 02:07:59 +01:00
*
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 ;
use Symfony\Component\Yaml\Dumper ;
2010-03-18 07:48:03 +00:00
class DumperTest extends \PHPUnit_Framework_TestCase
{
2010-05-06 12:25:53 +01:00
protected $parser ;
protected $dumper ;
protected $path ;
2010-12-06 14:45:37 +00:00
protected function setUp ()
2010-05-06 12:25:53 +01:00
{
$this -> parser = new Parser ();
$this -> dumper = new Dumper ();
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
2011-06-14 09:44:38 +01:00
protected function tearDown ()
{
$this -> parser = null ;
$this -> dumper = null ;
$this -> path = null ;
}
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' );
// 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 [ 'dump_skip' ]) && $test [ 'dump_skip' ]) {
2010-05-06 12:25:53 +01:00
continue ;
2011-12-18 13:42:59 +00:00
} elseif ( 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 = eval ( 'return ' . trim ( $test [ 'php' ]) . ';' );
$this -> assertEquals ( $expected , $this -> parser -> parse ( $this -> dumper -> dump ( $expected , 10 )), $test [ 'test' ]);
}
}
2010-03-18 07:48:03 +00:00
}
}
2010-05-06 12:25:53 +01:00
public function testInlineLevel ()
{
// inline level
$array = array (
'' => 'bar' ,
'foo' => '#bar' ,
'foo\'bar' => array (),
'bar' => array ( 1 , 'foo' ),
'foobar' => array (
'foo' => 'bar' ,
'bar' => array ( 1 , 'foo' ),
'foobar' => array (
'foo' => 'bar' ,
'bar' => array ( 1 , 'foo' ),
),
),
);
$expected = <<< EOF
2010-03-18 07:48:03 +00:00
{ '' : bar , foo : '#bar' , 'foo' 'bar' : { }, bar : [ 1 , foo ], foobar : { foo : bar , bar : [ 1 , foo ], foobar : { foo : bar , bar : [ 1 , foo ] } } }
EOF ;
2010-03-19 14:04:37 +00:00
$this -> assertEquals ( $expected , $this -> dumper -> dump ( $array , - 10 ), '->dump() takes an inline level argument' );
$this -> assertEquals ( $expected , $this -> dumper -> dump ( $array , 0 ), '->dump() takes an inline level argument' );
2010-03-18 07:48:03 +00:00
$expected = <<< EOF
'' : bar
foo : '#bar'
'foo' 'bar' : { }
bar : [ 1 , foo ]
foobar : { foo : bar , bar : [ 1 , foo ], foobar : { foo : bar , bar : [ 1 , foo ] } }
EOF ;
2010-05-06 12:25:53 +01:00
$this -> assertEquals ( $expected , $this -> dumper -> dump ( $array , 1 ), '->dump() takes an inline level argument' );
2010-03-18 07:48:03 +00:00
2010-05-06 12:25:53 +01:00
$expected = <<< EOF
2010-03-18 07:48:03 +00:00
'' : bar
foo : '#bar'
'foo' 'bar' : { }
bar :
- 1
- foo
foobar :
foo : bar
bar : [ 1 , foo ]
foobar : { foo : bar , bar : [ 1 , foo ] }
EOF ;
2010-05-06 12:25:53 +01:00
$this -> assertEquals ( $expected , $this -> dumper -> dump ( $array , 2 ), '->dump() takes an inline level argument' );
2010-03-18 07:48:03 +00:00
2010-05-06 12:25:53 +01:00
$expected = <<< EOF
2010-03-18 07:48:03 +00:00
'' : bar
foo : '#bar'
'foo' 'bar' : { }
bar :
- 1
- foo
foobar :
foo : bar
bar :
- 1
- foo
foobar :
foo : bar
bar : [ 1 , foo ]
EOF ;
2010-05-06 12:25:53 +01:00
$this -> assertEquals ( $expected , $this -> dumper -> dump ( $array , 3 ), '->dump() takes an inline level argument' );
2010-03-18 07:48:03 +00:00
2010-05-06 12:25:53 +01:00
$expected = <<< EOF
2010-03-18 07:48:03 +00:00
'' : bar
foo : '#bar'
'foo' 'bar' : { }
bar :
- 1
- foo
foobar :
foo : bar
bar :
- 1
- foo
foobar :
foo : bar
bar :
- 1
- foo
EOF ;
2010-05-06 12:25:53 +01:00
$this -> assertEquals ( $expected , $this -> dumper -> dump ( $array , 4 ), '->dump() takes an inline level argument' );
$this -> assertEquals ( $expected , $this -> dumper -> dump ( $array , 10 ), '->dump() takes an inline level argument' );
}
2010-03-18 07:48:03 +00: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
public function testObjectSupportEnabled ()
2010-05-06 12:25:53 +01:00
{
2013-01-17 21:16:19 +00:00
$dump = $this -> dumper -> dump ( array ( 'foo' => new A (), 'bar' => 1 ), 0 , 0 , false , true );
2010-03-18 07:48:03 +00: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
$this -> assertEquals ( '{ foo: !!php/object:O:30:"Symfony\Tests\Component\Yaml\A":1:{s:1:"a";s:3:"foo";}, bar: 1 }' , $dump , '->dump() is able to dump objects' );
}
public function testObjectSupportDisabledButNoExceptions ()
{
$dump = $this -> dumper -> dump ( array ( 'foo' => new A (), 'bar' => 1 ));
$this -> assertEquals ( '{ foo: null, bar: 1 }' , $dump , '->dump() does not dump objects when disabled' );
}
/**
* @ expectedException \Symfony\Component\Yaml\Exception\DumpException
*/
public function testObjectSupportDisabledWithExceptions ()
{
2013-01-17 21:16:19 +00:00
$this -> dumper -> dump ( array ( 'foo' => new A (), 'bar' => 1 ), 0 , 0 , true , false );
2010-05-06 12:25:53 +01:00
}
2010-03-18 07:48:03 +00:00
}
class A
{
2010-05-06 12:25:53 +01:00
public $a = 'foo' ;
2010-03-18 07:48:03 +00:00
}