1<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\Yaml\Tests;
13
14use PHPUnit\Framework\TestCase;
15use Symfony\Component\Yaml\Dumper;
16use Symfony\Component\Yaml\Parser;
17use Symfony\Component\Yaml\Yaml;
18
19class DumperTest extends TestCase
20{
21    protected $parser;
22    protected $dumper;
23    protected $path;
24
25    protected $array = [
26        '' => 'bar',
27        'foo' => '#bar',
28        'foo\'bar' => [],
29        'bar' => [1, 'foo'],
30        'foobar' => [
31            'foo' => 'bar',
32            'bar' => [1, 'foo'],
33            'foobar' => [
34                'foo' => 'bar',
35                'bar' => [1, 'foo'],
36            ],
37        ],
38    ];
39
40    protected function setUp()
41    {
42        $this->parser = new Parser();
43        $this->dumper = new Dumper();
44        $this->path = __DIR__.'/Fixtures';
45    }
46
47    protected function tearDown()
48    {
49        $this->parser = null;
50        $this->dumper = null;
51        $this->path = null;
52        $this->array = null;
53    }
54
55    public function testIndentationInConstructor()
56    {
57        $dumper = new Dumper(7);
58        $expected = <<<'EOF'
59'': bar
60foo: '#bar'
61'foo''bar': {  }
62bar:
63       - 1
64       - foo
65foobar:
66       foo: bar
67       bar:
68              - 1
69              - foo
70       foobar:
71              foo: bar
72              bar:
73                     - 1
74                     - foo
75
76EOF;
77        $this->assertEquals($expected, $dumper->dump($this->array, 4, 0));
78    }
79
80    public function testSpecifications()
81    {
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');
85
86            // split YAMLs documents
87            foreach (preg_split('/^---( %YAML\:1\.0)?/m', $yamls) as $yaml) {
88                if (!$yaml) {
89                    continue;
90                }
91
92                $test = $this->parser->parse($yaml);
93                if (isset($test['dump_skip']) && $test['dump_skip']) {
94                    continue;
95                } elseif (isset($test['todo']) && $test['todo']) {
96                    // TODO
97                } else {
98                    eval('$expected = '.trim($test['php']).';');
99                    $this->assertSame($expected, $this->parser->parse($this->dumper->dump($expected, 10)), $test['test']);
100                }
101            }
102        }
103    }
104
105    public function testInlineLevel()
106    {
107        $expected = <<<'EOF'
108{ '': bar, foo: '#bar', 'foo''bar': {  }, bar: [1, foo], foobar: { foo: bar, bar: [1, foo], foobar: { foo: bar, bar: [1, foo] } } }
109EOF;
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');
112
113        $expected = <<<'EOF'
114'': bar
115foo: '#bar'
116'foo''bar': {  }
117bar: [1, foo]
118foobar: { foo: bar, bar: [1, foo], foobar: { foo: bar, bar: [1, foo] } }
119
120EOF;
121        $this->assertEquals($expected, $this->dumper->dump($this->array, 1), '->dump() takes an inline level argument');
122
123        $expected = <<<'EOF'
124'': bar
125foo: '#bar'
126'foo''bar': {  }
127bar:
128    - 1
129    - foo
130foobar:
131    foo: bar
132    bar: [1, foo]
133    foobar: { foo: bar, bar: [1, foo] }
134
135EOF;
136        $this->assertEquals($expected, $this->dumper->dump($this->array, 2), '->dump() takes an inline level argument');
137
138        $expected = <<<'EOF'
139'': bar
140foo: '#bar'
141'foo''bar': {  }
142bar:
143    - 1
144    - foo
145foobar:
146    foo: bar
147    bar:
148        - 1
149        - foo
150    foobar:
151        foo: bar
152        bar: [1, foo]
153
154EOF;
155        $this->assertEquals($expected, $this->dumper->dump($this->array, 3), '->dump() takes an inline level argument');
156
157        $expected = <<<'EOF'
158'': bar
159foo: '#bar'
160'foo''bar': {  }
161bar:
162    - 1
163    - foo
164foobar:
165    foo: bar
166    bar:
167        - 1
168        - foo
169    foobar:
170        foo: bar
171        bar:
172            - 1
173            - foo
174
175EOF;
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');
178    }
179
180    public function testObjectSupportEnabled()
181    {
182        $dump = $this->dumper->dump(['foo' => new A(), 'bar' => 1], 0, 0, Yaml::DUMP_OBJECT);
183
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');
185    }
186
187    public function testObjectSupportDisabledButNoExceptions()
188    {
189        $dump = $this->dumper->dump(['foo' => new A(), 'bar' => 1]);
190
191        $this->assertEquals('{ foo: null, bar: 1 }', $dump, '->dump() does not dump objects when disabled');
192    }
193
194    /**
195     * @expectedException \Symfony\Component\Yaml\Exception\DumpException
196     */
197    public function testObjectSupportDisabledWithExceptions()
198    {
199        $this->dumper->dump(['foo' => new A(), 'bar' => 1], 0, 0, Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE);
200    }
201
202    /**
203     * @dataProvider getEscapeSequences
204     */
205    public function testEscapedEscapeSequencesInQuotedScalar($input, $expected)
206    {
207        $this->assertEquals($expected, $this->dumper->dump($input));
208    }
209
210    public function getEscapeSequences()
211    {
212        return [
213            'empty string' => ['', "''"],
214            'null' => ["\x0", '"\\0"'],
215            'bell' => ["\x7", '"\\a"'],
216            'backspace' => ["\x8", '"\\b"'],
217            'horizontal-tab' => ["\t", '"\\t"'],
218            'line-feed' => ["\n", '"\\n"'],
219            'vertical-tab' => ["\v", '"\\v"'],
220            'form-feed' => ["\xC", '"\\f"'],
221            'carriage-return' => ["\r", '"\\r"'],
222            'escape' => ["\x1B", '"\\e"'],
223            'space' => [' ', "' '"],
224            'double-quote' => ['"', "'\"'"],
225            'slash' => ['/', '/'],
226            'backslash' => ['\\', '\\'],
227            'next-line' => ["\xC2\x85", '"\\N"'],
228            'non-breaking-space' => ["\xc2\xa0", '"\\_"'],
229            'line-separator' => ["\xE2\x80\xA8", '"\\L"'],
230            'paragraph-separator' => ["\xE2\x80\xA9", '"\\P"'],
231            'colon' => [':', "':'"],
232        ];
233    }
234
235    public function testBinaryDataIsDumpedBase64Encoded()
236    {
237        $binaryData = file_get_contents(__DIR__.'/Fixtures/arrow.gif');
238        $expected = '{ data: !!binary '.base64_encode($binaryData).' }';
239
240        $this->assertSame($expected, $this->dumper->dump(['data' => $binaryData]));
241    }
242
243    public function testNonUtf8DataIsDumpedBase64Encoded()
244    {
245        // "für" (ISO-8859-1 encoded)
246        $this->assertSame('!!binary ZsM/cg==', $this->dumper->dump("f\xc3\x3fr"));
247    }
248
249    /**
250     * @dataProvider objectAsMapProvider
251     */
252    public function testDumpObjectAsMap($object, $expected)
253    {
254        $yaml = $this->dumper->dump($object, 0, 0, Yaml::DUMP_OBJECT_AS_MAP);
255
256        $this->assertEquals($expected, Yaml::parse($yaml, Yaml::PARSE_OBJECT_FOR_MAP));
257    }
258
259    public function objectAsMapProvider()
260    {
261        $tests = [];
262
263        $bar = new \stdClass();
264        $bar->class = 'classBar';
265        $bar->args = ['bar'];
266        $zar = new \stdClass();
267        $foo = new \stdClass();
268        $foo->bar = $bar;
269        $foo->zar = $zar;
270        $object = new \stdClass();
271        $object->foo = $foo;
272        $tests['stdClass'] = [$object, $object];
273
274        $arrayObject = new \ArrayObject();
275        $arrayObject['foo'] = 'bar';
276        $arrayObject['baz'] = 'foobar';
277        $parsedArrayObject = new \stdClass();
278        $parsedArrayObject->foo = 'bar';
279        $parsedArrayObject->baz = 'foobar';
280        $tests['ArrayObject'] = [$arrayObject, $parsedArrayObject];
281
282        $a = new A();
283        $tests['arbitrary-object'] = [$a, null];
284
285        return $tests;
286    }
287
288    public function testDumpingArrayObjectInstancesRespectsInlineLevel()
289    {
290        $deep = new \ArrayObject(['deep1' => 'd', 'deep2' => 'e']);
291        $inner = new \ArrayObject(['inner1' => 'b', 'inner2' => 'c', 'inner3' => $deep]);
292        $outer = new \ArrayObject(['outer1' => 'a', 'outer2' => $inner]);
293
294        $yaml = $this->dumper->dump($outer, 2, 0, Yaml::DUMP_OBJECT_AS_MAP);
295
296        $expected = <<<YAML
297outer1: a
298outer2:
299    inner1: b
300    inner2: c
301    inner3: { deep1: d, deep2: e }
302
303YAML;
304        $this->assertSame($expected, $yaml);
305    }
306
307    public function testDumpingArrayObjectInstancesWithNumericKeysInlined()
308    {
309        $deep = new \ArrayObject(['d', 'e']);
310        $inner = new \ArrayObject(['b', 'c', $deep]);
311        $outer = new \ArrayObject(['a', $inner]);
312
313        $yaml = $this->dumper->dump($outer, 0, 0, Yaml::DUMP_OBJECT_AS_MAP);
314        $expected = <<<YAML
315{ 0: a, 1: { 0: b, 1: c, 2: { 0: d, 1: e } } }
316YAML;
317        $this->assertSame($expected, $yaml);
318    }
319
320    public function testDumpingArrayObjectInstancesWithNumericKeysRespectsInlineLevel()
321    {
322        $deep = new \ArrayObject(['d', 'e']);
323        $inner = new \ArrayObject(['b', 'c', $deep]);
324        $outer = new \ArrayObject(['a', $inner]);
325        $yaml = $this->dumper->dump($outer, 2, 0, Yaml::DUMP_OBJECT_AS_MAP);
326        $expected = <<<YAML
3270: a
3281:
329    0: b
330    1: c
331    2: { 0: d, 1: e }
332
333YAML;
334        $this->assertEquals($expected, $yaml);
335    }
336
337    public function testDumpEmptyArrayObjectInstanceAsMap()
338    {
339        $this->assertSame('{  }', $this->dumper->dump(new \ArrayObject(), 2, 0, Yaml::DUMP_OBJECT_AS_MAP));
340    }
341
342    public function testDumpEmptyStdClassInstanceAsMap()
343    {
344        $this->assertSame('{  }', $this->dumper->dump(new \stdClass(), 2, 0, Yaml::DUMP_OBJECT_AS_MAP));
345    }
346
347    public function testDumpingStdClassInstancesRespectsInlineLevel()
348    {
349        $deep = new \stdClass();
350        $deep->deep1 = 'd';
351        $deep->deep2 = 'e';
352
353        $inner = new \stdClass();
354        $inner->inner1 = 'b';
355        $inner->inner2 = 'c';
356        $inner->inner3 = $deep;
357
358        $outer = new \stdClass();
359        $outer->outer1 = 'a';
360        $outer->outer2 = $inner;
361
362        $yaml = $this->dumper->dump($outer, 2, 0, Yaml::DUMP_OBJECT_AS_MAP);
363
364        $expected = <<<YAML
365outer1: a
366outer2:
367    inner1: b
368    inner2: c
369    inner3: { deep1: d, deep2: e }
370
371YAML;
372        $this->assertSame($expected, $yaml);
373    }
374
375    public function testDumpMultiLineStringAsScalarBlock()
376    {
377        $data = [
378            'data' => [
379                'single_line' => 'foo bar baz',
380                'multi_line' => "foo\nline with trailing spaces:\n  \nbar\ninteger like line:\n123456789\nempty line:\n\nbaz",
381                'multi_line_with_carriage_return' => "foo\nbar\r\nbaz",
382                'nested_inlined_multi_line_string' => [
383                    'inlined_multi_line' => "foo\nbar\r\nempty line:\n\nbaz",
384                ],
385            ],
386        ];
387
388        $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));
389    }
390
391    public function testDumpMultiLineStringAsScalarBlockWhenFirstLineHasLeadingSpace()
392    {
393        $data = [
394            'data' => [
395                'multi_line' => "    the first line has leading spaces\nThe second line does not.",
396            ],
397        ];
398
399        $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));
400    }
401
402    public function testCarriageReturnIsMaintainedWhenDumpingAsMultiLineLiteralBlock()
403    {
404        $this->assertSame("- \"a\\r\\nb\\nc\"\n", $this->dumper->dump(["a\r\nb\nc"], 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
405    }
406
407    /**
408     * @expectedException \InvalidArgumentException
409     * @expectedExceptionMessage The indentation must be greater than zero
410     */
411    public function testZeroIndentationThrowsException()
412    {
413        new Dumper(0);
414    }
415
416    /**
417     * @expectedException \InvalidArgumentException
418     * @expectedExceptionMessage The indentation must be greater than zero
419     */
420    public function testNegativeIndentationThrowsException()
421    {
422        new Dumper(-4);
423    }
424}
425
426class A
427{
428    public $a = 'foo';
429}
430