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;
17 use Symfony\Component\Yaml\Yaml;
19 class DumperTest extends TestCase
25 protected $array = array(
28 'foo\'bar' => array(),
29 'bar' => array(1, 'foo'),
32 'bar' => array(1, 'foo'),
35 'bar' => array(1, 'foo'),
40 protected function setUp()
42 $this->parser = new Parser();
43 $this->dumper = new Dumper();
44 $this->path = __DIR__.'/Fixtures';
47 protected function tearDown()
55 public function testIndentationInConstructor()
57 $dumper = new Dumper(7);
77 $this->assertEquals($expected, $dumper->dump($this->array, 4, 0));
83 public function testSetIndentation()
85 $this->dumper->setIndentation(7);
106 $this->assertEquals($expected, $this->dumper->dump($this->array, 4, 0));
109 public function testSpecifications()
111 $files = $this->parser->parse(file_get_contents($this->path.'/index.yml'));
112 foreach ($files as $file) {
113 $yamls = file_get_contents($this->path.'/'.$file.'.yml');
115 // split YAMLs documents
116 foreach (preg_split('/^---( %YAML\:1\.0)?/m', $yamls) as $yaml) {
121 $test = $this->parser->parse($yaml);
122 if (isset($test['dump_skip']) && $test['dump_skip']) {
124 } elseif (isset($test['todo']) && $test['todo']) {
127 eval('$expected = '.trim($test['php']).';');
128 $this->assertSame($expected, $this->parser->parse($this->dumper->dump($expected, 10)), $test['test']);
134 public function testInlineLevel()
137 { '': bar, foo: '#bar', 'foo''bar': { }, bar: [1, foo], foobar: { foo: bar, bar: [1, foo], foobar: { foo: bar, bar: [1, foo] } } }
139 $this->assertEquals($expected, $this->dumper->dump($this->array, -10), '->dump() takes an inline level argument');
140 $this->assertEquals($expected, $this->dumper->dump($this->array, 0), '->dump() takes an inline level argument');
147 foobar: { foo: bar, bar: [1, foo], foobar: { foo: bar, bar: [1, foo] } }
150 $this->assertEquals($expected, $this->dumper->dump($this->array, 1), '->dump() takes an inline level argument');
162 foobar: { foo: bar, bar: [1, foo] }
165 $this->assertEquals($expected, $this->dumper->dump($this->array, 2), '->dump() takes an inline level argument');
184 $this->assertEquals($expected, $this->dumper->dump($this->array, 3), '->dump() takes an inline level argument');
205 $this->assertEquals($expected, $this->dumper->dump($this->array, 4), '->dump() takes an inline level argument');
206 $this->assertEquals($expected, $this->dumper->dump($this->array, 10), '->dump() takes an inline level argument');
209 public function testObjectSupportEnabled()
211 $dump = $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, Yaml::DUMP_OBJECT);
213 $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');
219 public function testObjectSupportEnabledPassingTrue()
221 $dump = $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, false, true);
223 $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');
226 public function testObjectSupportDisabledButNoExceptions()
228 $dump = $this->dumper->dump(array('foo' => new A(), 'bar' => 1));
230 $this->assertEquals('{ foo: null, bar: 1 }', $dump, '->dump() does not dump objects when disabled');
234 * @expectedException \Symfony\Component\Yaml\Exception\DumpException
236 public function testObjectSupportDisabledWithExceptions()
238 $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE);
243 * @expectedException \Symfony\Component\Yaml\Exception\DumpException
245 public function testObjectSupportDisabledWithExceptionsPassingTrue()
247 $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, true);
250 public function testEmptyArray()
252 $dump = $this->dumper->dump(array());
253 $this->assertEquals('{ }', $dump);
255 $dump = $this->dumper->dump(array(), 0, 0, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE);
256 $this->assertEquals('[]', $dump);
258 $dump = $this->dumper->dump(array(), 9, 0, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE);
259 $this->assertEquals('[]', $dump);
261 $dump = $this->dumper->dump(new \ArrayObject(), 0, 0, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE | Yaml::DUMP_OBJECT_AS_MAP);
262 $this->assertEquals('{ }', $dump);
264 $dump = $this->dumper->dump(new \stdClass(), 0, 0, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE | Yaml::DUMP_OBJECT_AS_MAP);
265 $this->assertEquals('{ }', $dump);
269 * @dataProvider getEscapeSequences
271 public function testEscapedEscapeSequencesInQuotedScalar($input, $expected)
273 $this->assertEquals($expected, $this->dumper->dump($input));
276 public function getEscapeSequences()
279 'empty string' => array('', "''"),
280 'null' => array("\x0", '"\\0"'),
281 'bell' => array("\x7", '"\\a"'),
282 'backspace' => array("\x8", '"\\b"'),
283 'horizontal-tab' => array("\t", '"\\t"'),
284 'line-feed' => array("\n", '"\\n"'),
285 'vertical-tab' => array("\v", '"\\v"'),
286 'form-feed' => array("\xC", '"\\f"'),
287 'carriage-return' => array("\r", '"\\r"'),
288 'escape' => array("\x1B", '"\\e"'),
289 'space' => array(' ', "' '"),
290 'double-quote' => array('"', "'\"'"),
291 'slash' => array('/', '/'),
292 'backslash' => array('\\', '\\'),
293 'next-line' => array("\xC2\x85", '"\\N"'),
294 'non-breaking-space' => array("\xc2\xa0", '"\\_"'),
295 'line-separator' => array("\xE2\x80\xA8", '"\\L"'),
296 'paragraph-separator' => array("\xE2\x80\xA9", '"\\P"'),
297 'colon' => array(':', "':'"),
301 public function testBinaryDataIsDumpedBase64Encoded()
303 $binaryData = file_get_contents(__DIR__.'/Fixtures/arrow.gif');
304 $expected = '{ data: !!binary '.base64_encode($binaryData).' }';
306 $this->assertSame($expected, $this->dumper->dump(array('data' => $binaryData)));
309 public function testNonUtf8DataIsDumpedBase64Encoded()
311 // "für" (ISO-8859-1 encoded)
312 $this->assertSame('!!binary ZsM/cg==', $this->dumper->dump("f\xc3\x3fr"));
316 * @dataProvider objectAsMapProvider
318 public function testDumpObjectAsMap($object, $expected)
320 $yaml = $this->dumper->dump($object, 0, 0, Yaml::DUMP_OBJECT_AS_MAP);
322 $this->assertEquals($expected, Yaml::parse($yaml, Yaml::PARSE_OBJECT_FOR_MAP));
325 public function objectAsMapProvider()
329 $bar = new \stdClass();
330 $bar->class = 'classBar';
331 $bar->args = array('bar');
332 $zar = new \stdClass();
333 $foo = new \stdClass();
336 $object = new \stdClass();
338 $tests['stdClass'] = array($object, $object);
340 $arrayObject = new \ArrayObject();
341 $arrayObject['foo'] = 'bar';
342 $arrayObject['baz'] = 'foobar';
343 $parsedArrayObject = new \stdClass();
344 $parsedArrayObject->foo = 'bar';
345 $parsedArrayObject->baz = 'foobar';
346 $tests['ArrayObject'] = array($arrayObject, $parsedArrayObject);
349 $tests['arbitrary-object'] = array($a, null);
354 public function testDumpingArrayObjectInstancesRespectsInlineLevel()
356 $deep = new \ArrayObject(array('deep1' => 'd', 'deep2' => 'e'));
357 $inner = new \ArrayObject(array('inner1' => 'b', 'inner2' => 'c', 'inner3' => $deep));
358 $outer = new \ArrayObject(array('outer1' => 'a', 'outer2' => $inner));
360 $yaml = $this->dumper->dump($outer, 2, 0, Yaml::DUMP_OBJECT_AS_MAP);
367 inner3: { deep1: d, deep2: e }
370 $this->assertSame($expected, $yaml);
373 public function testDumpingArrayObjectInstancesWithNumericKeysInlined()
375 $deep = new \ArrayObject(array('d', 'e'));
376 $inner = new \ArrayObject(array('b', 'c', $deep));
377 $outer = new \ArrayObject(array('a', $inner));
379 $yaml = $this->dumper->dump($outer, 0, 0, Yaml::DUMP_OBJECT_AS_MAP);
381 { 0: a, 1: { 0: b, 1: c, 2: { 0: d, 1: e } } }
383 $this->assertSame($expected, $yaml);
386 public function testDumpingArrayObjectInstancesWithNumericKeysRespectsInlineLevel()
388 $deep = new \ArrayObject(array('d', 'e'));
389 $inner = new \ArrayObject(array('b', 'c', $deep));
390 $outer = new \ArrayObject(array('a', $inner));
391 $yaml = $this->dumper->dump($outer, 2, 0, Yaml::DUMP_OBJECT_AS_MAP);
400 $this->assertEquals($expected, $yaml);
403 public function testDumpEmptyArrayObjectInstanceAsMap()
405 $this->assertSame('{ }', $this->dumper->dump(new \ArrayObject(), 2, 0, Yaml::DUMP_OBJECT_AS_MAP));
408 public function testDumpEmptyStdClassInstanceAsMap()
410 $this->assertSame('{ }', $this->dumper->dump(new \stdClass(), 2, 0, Yaml::DUMP_OBJECT_AS_MAP));
413 public function testDumpingStdClassInstancesRespectsInlineLevel()
415 $deep = new \stdClass();
419 $inner = new \stdClass();
420 $inner->inner1 = 'b';
421 $inner->inner2 = 'c';
422 $inner->inner3 = $deep;
424 $outer = new \stdClass();
425 $outer->outer1 = 'a';
426 $outer->outer2 = $inner;
428 $yaml = $this->dumper->dump($outer, 2, 0, Yaml::DUMP_OBJECT_AS_MAP);
435 inner3: { deep1: d, deep2: e }
438 $this->assertSame($expected, $yaml);
441 public function testDumpMultiLineStringAsScalarBlock()
445 'single_line' => 'foo bar baz',
446 'multi_line' => "foo\nline with trailing spaces:\n \nbar\ninteger like line:\n123456789\nempty line:\n\nbaz",
447 'multi_line_with_carriage_return' => "foo\nbar\r\nbaz",
448 'nested_inlined_multi_line_string' => array(
449 'inlined_multi_line' => "foo\nbar\r\nempty line:\n\nbaz",
454 $this->assertSame(file_get_contents(__DIR__.'/Fixtures/multiple_lines_as_literal_block.yml'), $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
457 public function testDumpMultiLineStringAsScalarBlockWhenFirstLineHasLeadingSpace()
461 'multi_line' => " the first line has leading spaces\nThe second line does not.",
465 $this->assertSame(file_get_contents(__DIR__.'/Fixtures/multiple_lines_as_literal_block_leading_space_in_first_line.yml'), $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
468 public function testCarriageReturnIsMaintainedWhenDumpingAsMultiLineLiteralBlock()
470 $this->assertSame("- \"a\\r\\nb\\nc\"\n", $this->dumper->dump(array("a\r\nb\nc"), 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
474 * @expectedException \InvalidArgumentException
475 * @expectedExceptionMessage The indentation must be greater than zero
477 public function testZeroIndentationThrowsException()
483 * @expectedException \InvalidArgumentException
484 * @expectedExceptionMessage The indentation must be greater than zero
486 public function testNegativeIndentationThrowsException()