Updated Drupal to 8.6. This goes with the following updates because it's possible...
[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\Dumper;
16 use Symfony\Component\Yaml\Parser;
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     public function testEmptyArray()
251     {
252         $dump = $this->dumper->dump(array());
253         $this->assertEquals('{  }', $dump);
254
255         $dump = $this->dumper->dump(array(), 0, 0, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE);
256         $this->assertEquals('[]', $dump);
257
258         $dump = $this->dumper->dump(array(), 9, 0, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE);
259         $this->assertEquals('[]', $dump);
260
261         $dump = $this->dumper->dump(new \ArrayObject(), 0, 0, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE | Yaml::DUMP_OBJECT_AS_MAP);
262         $this->assertEquals('{  }', $dump);
263
264         $dump = $this->dumper->dump(new \stdClass(), 0, 0, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE | Yaml::DUMP_OBJECT_AS_MAP);
265         $this->assertEquals('{  }', $dump);
266     }
267
268     /**
269      * @dataProvider getEscapeSequences
270      */
271     public function testEscapedEscapeSequencesInQuotedScalar($input, $expected)
272     {
273         $this->assertEquals($expected, $this->dumper->dump($input));
274     }
275
276     public function getEscapeSequences()
277     {
278         return array(
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(':', "':'"),
298         );
299     }
300
301     public function testBinaryDataIsDumpedBase64Encoded()
302     {
303         $binaryData = file_get_contents(__DIR__.'/Fixtures/arrow.gif');
304         $expected = '{ data: !!binary '.base64_encode($binaryData).' }';
305
306         $this->assertSame($expected, $this->dumper->dump(array('data' => $binaryData)));
307     }
308
309     public function testNonUtf8DataIsDumpedBase64Encoded()
310     {
311         // "für" (ISO-8859-1 encoded)
312         $this->assertSame('!!binary ZsM/cg==', $this->dumper->dump("f\xc3\x3fr"));
313     }
314
315     /**
316      * @dataProvider objectAsMapProvider
317      */
318     public function testDumpObjectAsMap($object, $expected)
319     {
320         $yaml = $this->dumper->dump($object, 0, 0, Yaml::DUMP_OBJECT_AS_MAP);
321
322         $this->assertEquals($expected, Yaml::parse($yaml, Yaml::PARSE_OBJECT_FOR_MAP));
323     }
324
325     public function objectAsMapProvider()
326     {
327         $tests = array();
328
329         $bar = new \stdClass();
330         $bar->class = 'classBar';
331         $bar->args = array('bar');
332         $zar = new \stdClass();
333         $foo = new \stdClass();
334         $foo->bar = $bar;
335         $foo->zar = $zar;
336         $object = new \stdClass();
337         $object->foo = $foo;
338         $tests['stdClass'] = array($object, $object);
339
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);
347
348         $a = new A();
349         $tests['arbitrary-object'] = array($a, null);
350
351         return $tests;
352     }
353
354     public function testDumpingArrayObjectInstancesRespectsInlineLevel()
355     {
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));
359
360         $yaml = $this->dumper->dump($outer, 2, 0, Yaml::DUMP_OBJECT_AS_MAP);
361
362         $expected = <<<YAML
363 outer1: a
364 outer2:
365     inner1: b
366     inner2: c
367     inner3: { deep1: d, deep2: e }
368
369 YAML;
370         $this->assertSame($expected, $yaml);
371     }
372
373     public function testDumpingArrayObjectInstancesWithNumericKeysInlined()
374     {
375         $deep = new \ArrayObject(array('d', 'e'));
376         $inner = new \ArrayObject(array('b', 'c', $deep));
377         $outer = new \ArrayObject(array('a', $inner));
378
379         $yaml = $this->dumper->dump($outer, 0, 0, Yaml::DUMP_OBJECT_AS_MAP);
380         $expected = <<<YAML
381 { 0: a, 1: { 0: b, 1: c, 2: { 0: d, 1: e } } }
382 YAML;
383         $this->assertSame($expected, $yaml);
384     }
385
386     public function testDumpingArrayObjectInstancesWithNumericKeysRespectsInlineLevel()
387     {
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);
392         $expected = <<<YAML
393 0: a
394 1:
395     0: b
396     1: c
397     2: { 0: d, 1: e }
398
399 YAML;
400         $this->assertEquals($expected, $yaml);
401     }
402
403     public function testDumpEmptyArrayObjectInstanceAsMap()
404     {
405         $this->assertSame('{  }', $this->dumper->dump(new \ArrayObject(), 2, 0, Yaml::DUMP_OBJECT_AS_MAP));
406     }
407
408     public function testDumpEmptyStdClassInstanceAsMap()
409     {
410         $this->assertSame('{  }', $this->dumper->dump(new \stdClass(), 2, 0, Yaml::DUMP_OBJECT_AS_MAP));
411     }
412
413     public function testDumpingStdClassInstancesRespectsInlineLevel()
414     {
415         $deep = new \stdClass();
416         $deep->deep1 = 'd';
417         $deep->deep2 = 'e';
418
419         $inner = new \stdClass();
420         $inner->inner1 = 'b';
421         $inner->inner2 = 'c';
422         $inner->inner3 = $deep;
423
424         $outer = new \stdClass();
425         $outer->outer1 = 'a';
426         $outer->outer2 = $inner;
427
428         $yaml = $this->dumper->dump($outer, 2, 0, Yaml::DUMP_OBJECT_AS_MAP);
429
430         $expected = <<<YAML
431 outer1: a
432 outer2:
433     inner1: b
434     inner2: c
435     inner3: { deep1: d, deep2: e }
436
437 YAML;
438         $this->assertSame($expected, $yaml);
439     }
440
441     public function testDumpMultiLineStringAsScalarBlock()
442     {
443         $data = array(
444             'data' => array(
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",
450                 ),
451             ),
452         );
453
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));
455     }
456
457     public function testDumpMultiLineStringAsScalarBlockWhenFirstLineHasLeadingSpace()
458     {
459         $data = array(
460             'data' => array(
461                 'multi_line' => "    the first line has leading spaces\nThe second line does not.",
462             ),
463         );
464
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));
466     }
467
468     public function testCarriageReturnIsMaintainedWhenDumpingAsMultiLineLiteralBlock()
469     {
470         $this->assertSame("- \"a\\r\\nb\\nc\"\n", $this->dumper->dump(array("a\r\nb\nc"), 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
471     }
472
473     /**
474      * @expectedException \InvalidArgumentException
475      * @expectedExceptionMessage The indentation must be greater than zero
476      */
477     public function testZeroIndentationThrowsException()
478     {
479         new Dumper(0);
480     }
481
482     /**
483      * @expectedException \InvalidArgumentException
484      * @expectedExceptionMessage The indentation must be greater than zero
485      */
486     public function testNegativeIndentationThrowsException()
487     {
488         new Dumper(-4);
489     }
490 }
491
492 class A
493 {
494     public $a = 'foo';
495 }