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