Completed
Push — master ( fe98f7...8d5106 )
by David
24s queued 11s
created

tests/Yaml/DumperTest.php (2 issues)

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