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 PHPUnit\Framework\TestCase;
15 use Symfony\Component\Yaml\Parser;
16 use Symfony\Component\Yaml\Dumper;
18 class DumperTest extends 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 eval('$expected = '.trim($test['php']).';');
99 $this->assertSame($expected, $this->parser->parse($this->dumper->dump($expected, 10)), $test['test']);
105 public function testInlineLevel()
108 { '': bar, foo: '#bar', 'foo''bar': { }, bar: [1, foo], foobar: { foo: bar, bar: [1, foo], foobar: { foo: bar, bar: [1, foo] } } }
110 $this->assertEquals($expected, $this->dumper->dump($this->array, -10), '->dump() takes an inline level argument');
111 $this->assertEquals($expected, $this->dumper->dump($this->array, 0), '->dump() takes an inline level argument');
118 foobar: { foo: bar, bar: [1, foo], foobar: { foo: bar, bar: [1, foo] } }
121 $this->assertEquals($expected, $this->dumper->dump($this->array, 1), '->dump() takes an inline level argument');
133 foobar: { foo: bar, bar: [1, foo] }
136 $this->assertEquals($expected, $this->dumper->dump($this->array, 2), '->dump() takes an inline level argument');
155 $this->assertEquals($expected, $this->dumper->dump($this->array, 3), '->dump() takes an inline level argument');
176 $this->assertEquals($expected, $this->dumper->dump($this->array, 4), '->dump() takes an inline level argument');
177 $this->assertEquals($expected, $this->dumper->dump($this->array, 10), '->dump() takes an inline level argument');
180 public function testObjectSupportEnabled()
182 $dump = $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, false, true);
184 $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');
187 public function testObjectSupportDisabledButNoExceptions()
189 $dump = $this->dumper->dump(array('foo' => new A(), 'bar' => 1));
191 $this->assertEquals('{ foo: null, bar: 1 }', $dump, '->dump() does not dump objects when disabled');
195 * @expectedException \Symfony\Component\Yaml\Exception\DumpException
197 public function testObjectSupportDisabledWithExceptions()
199 $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, true, false);
203 * @dataProvider getEscapeSequences
205 public function testEscapedEscapeSequencesInQuotedScalar($input, $expected)
207 $this->assertEquals($expected, $this->dumper->dump($input));
210 public function getEscapeSequences()
213 'empty string' => array('', "''"),
214 'null' => array("\x0", '"\\0"'),
215 'bell' => array("\x7", '"\\a"'),
216 'backspace' => array("\x8", '"\\b"'),
217 'horizontal-tab' => array("\t", '"\\t"'),
218 'line-feed' => array("\n", '"\\n"'),
219 'vertical-tab' => array("\v", '"\\v"'),
220 'form-feed' => array("\xC", '"\\f"'),
221 'carriage-return' => array("\r", '"\\r"'),
222 'escape' => array("\x1B", '"\\e"'),
223 'space' => array(' ', "' '"),
224 'double-quote' => array('"', "'\"'"),
225 'slash' => array('/', '/'),
226 'backslash' => array('\\', '\\'),
227 'next-line' => array("\xC2\x85", '"\\N"'),
228 'non-breaking-space' => array("\xc2\xa0", '"\\_"'),
229 'line-separator' => array("\xE2\x80\xA8", '"\\L"'),
230 'paragraph-separator' => array("\xE2\x80\xA9", '"\\P"'),
231 'colon' => array(':', "':'"),
236 * @expectedException \InvalidArgumentException
237 * @expectedExceptionMessage The indentation must be greater than zero
239 public function testZeroIndentationThrowsException()
241 $this->dumper->setIndentation(0);
245 * @expectedException \InvalidArgumentException
246 * @expectedExceptionMessage The indentation must be greater than zero
248 public function testNegativeIndentationThrowsException()
250 $this->dumper->setIndentation(-4);