4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Yaml\Tests
;
14 use Symfony\Component\Yaml\Yaml
;
15 use Symfony\Component\Yaml\Parser
;
16 use Symfony\Component\Yaml\Dumper
;
18 class DumperTest
extends \PHPUnit_Framework_TestCase
24 protected $array = array(
27 'foo\'bar' => array(),
28 'bar' => array(1, 'foo'),
31 'bar' => array(1, 'foo'),
34 'bar' => array(1, 'foo'),
39 protected function setUp()
41 $this->parser
= new Parser();
42 $this->dumper
= new Dumper();
43 $this->path
= __DIR__
.'/Fixtures';
46 protected function tearDown()
54 public function testSetIndentation()
56 $this->dumper
->setIndentation(7);
77 $this->assertEquals($expected, $this->dumper
->dump($this->array, 4, 0));
80 public function testSpecifications()
82 $files = $this->parser
->parse(file_get_contents($this->path
.'/index.yml'));
83 foreach ($files as $file) {
84 $yamls = file_get_contents($this->path
.'/'.$file.'.yml');
86 // split YAMLs documents
87 foreach (preg_split('/^---( %YAML\:1\.0)?/m', $yamls) as $yaml) {
92 $test = $this->parser
->parse($yaml);
93 if (isset($test['dump_skip']) && $test['dump_skip']) {
95 } elseif (isset($test['todo']) && $test['todo']) {
98 $expected = eval('return '.trim($test['php']).';');
100 $this->assertEquals($expected, $this->parser
->parse($this->dumper
->dump($expected, 10)), $test['test']);
106 public function testInlineLevel()
109 { '': bar, foo: '#bar', 'foo''bar': { }, bar: [1, foo], foobar: { foo: bar, bar: [1, foo], foobar: { foo: bar, bar: [1, foo] } } }
111 $this->assertEquals($expected, $this->dumper
->dump($this->array, -10), '->dump() takes an inline level argument');
112 $this->assertEquals($expected, $this->dumper
->dump($this->array, 0), '->dump() takes an inline level argument');
119 foobar: { foo: bar, bar: [1, foo], foobar: { foo: bar, bar: [1, foo] } }
122 $this->assertEquals($expected, $this->dumper
->dump($this->array, 1), '->dump() takes an inline level argument');
134 foobar: { foo: bar, bar: [1, foo] }
137 $this->assertEquals($expected, $this->dumper
->dump($this->array, 2), '->dump() takes an inline level argument');
156 $this->assertEquals($expected, $this->dumper
->dump($this->array, 3), '->dump() takes an inline level argument');
177 $this->assertEquals($expected, $this->dumper
->dump($this->array, 4), '->dump() takes an inline level argument');
178 $this->assertEquals($expected, $this->dumper
->dump($this->array, 10), '->dump() takes an inline level argument');
181 public function testObjectSupportEnabled()
183 $dump = $this->dumper
->dump(array('foo' => new A(), 'bar' => 1), 0, 0, false, true);
185 $this->assertEquals('{ foo: !!php/object:O:30:"Symfony\Component\Yaml\Tests\A":1:{s:1:"a";s:3:"foo";}, bar: 1 }', $dump, '->dump() is able to dump objects');
188 public function testObjectSupportDisabledButNoExceptions()
190 $dump = $this->dumper
->dump(array('foo' => new A(), 'bar' => 1));
192 $this->assertEquals('{ foo: null, bar: 1 }', $dump, '->dump() does not dump objects when disabled');
196 * @expectedException \Symfony\Component\Yaml\Exception\DumpException
198 public function testObjectSupportDisabledWithExceptions()
200 $this->dumper
->dump(array('foo' => new A(), 'bar' => 1), 0, 0, true, false);