Completed
Push — master ( ac6391...9b00f4 )
by Michael
12:56
created

ParserTest::testMappingDuplicateKeyBlock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 8

Duplication

Lines 17
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 17
loc 17
rs 9.4285
c 1
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <[email protected]>
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 Symfony\Bridge\PhpUnit\ErrorAssert;
15
use Symfony\Component\Yaml\Yaml;
16
use Symfony\Component\Yaml\Parser;
17
18
class ParserTest extends \PHPUnit_Framework_TestCase
19
{
20
    protected $parser;
21
22
    protected function setUp()
23
    {
24
        $this->parser = new Parser();
25
    }
26
27
    protected function tearDown()
28
    {
29
        $this->parser = null;
30
    }
31
32
    /**
33
     * @dataProvider getDataFormSpecifications
34
     */
35
    public function testSpecifications($file, $expected, $yaml, $comment)
36
    {
37
        $this->assertEquals($expected, var_export($this->parser->parse($yaml), true), $comment);
38
    }
39
40
    public function getDataFormSpecifications()
41
    {
42
        $parser = new Parser();
43
        $path = __DIR__.'/Fixtures';
44
45
        $tests = array();
46
        $files = $parser->parse(file_get_contents($path.'/index.yml'));
47
        foreach ($files as $file) {
0 ignored issues
show
Bug introduced by
The expression $files of type array|string|object<stdClass> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
48
            $yamls = file_get_contents($path.'/'.$file.'.yml');
49
50
            // split YAMLs documents
51
            foreach (preg_split('/^---( %YAML\:1\.0)?/m', $yamls) as $yaml) {
52
                if (!$yaml) {
53
                    continue;
54
                }
55
56
                $test = $parser->parse($yaml);
57
                if (isset($test['todo']) && $test['todo']) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
58
                    // TODO
59
                } else {
60
                    eval('$expected = '.trim($test['php']).';');
0 ignored issues
show
Coding Style introduced by
It is generally not recommended to use eval unless absolutely required.

On one hand, eval might be exploited by malicious users if they somehow manage to inject dynamic content. On the other hand, with the emergence of faster PHP runtimes like the HHVM, eval prevents some optimization that they perform.

Loading history...
61
62
                    $tests[] = array($file, var_export($expected, true), $test['yaml'], $test['test']);
0 ignored issues
show
Bug introduced by
The variable $expected does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
63
                }
64
            }
65
        }
66
67
        return $tests;
68
    }
69
70
    public function testTabsInYaml()
71
    {
72
        // test tabs in YAML
73
        $yamls = array(
74
            "foo:\n	bar",
75
            "foo:\n 	bar",
76
            "foo:\n	 bar",
77
            "foo:\n 	 bar",
78
        );
79
80
        foreach ($yamls as $yaml) {
81
            try {
82
                $content = $this->parser->parse($yaml);
0 ignored issues
show
Unused Code introduced by
$content is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
83
84
                $this->fail('YAML files must not contain tabs');
85
            } catch (\Exception $e) {
86
                $this->assertInstanceOf('\Exception', $e, 'YAML files must not contain tabs');
87
                $this->assertEquals('A YAML file cannot contain tabs as indentation at line 2 (near "'.strpbrk($yaml, "\t").'").', $e->getMessage(), 'YAML files must not contain tabs');
88
            }
89
        }
90
    }
91
92
    public function testEndOfTheDocumentMarker()
93
    {
94
        $yaml = <<<'EOF'
95
--- %YAML:1.0
96
foo
97
...
98
EOF;
99
100
        $this->assertEquals('foo', $this->parser->parse($yaml));
101
    }
102
103
    public function getBlockChompingTests()
104
    {
105
        $tests = array();
106
107
        $yaml = <<<'EOF'
108
foo: |-
109
    one
110
    two
111
bar: |-
112
    one
113
    two
114
115
EOF;
116
        $expected = array(
117
            'foo' => "one\ntwo",
118
            'bar' => "one\ntwo",
119
        );
120
        $tests['Literal block chomping strip with single trailing newline'] = array($expected, $yaml);
121
122
        $yaml = <<<'EOF'
123
foo: |-
124
    one
125
    two
126
127
bar: |-
128
    one
129
    two
130
131
132
EOF;
133
        $expected = array(
134
            'foo' => "one\ntwo",
135
            'bar' => "one\ntwo",
136
        );
137
        $tests['Literal block chomping strip with multiple trailing newlines'] = array($expected, $yaml);
138
139
        $yaml = <<<'EOF'
140
{}
141
142
143
EOF;
144
        $expected = array();
145
        $tests['Literal block chomping strip with multiple trailing newlines after a 1-liner'] = array($expected, $yaml);
146
147
        $yaml = <<<'EOF'
148
foo: |-
149
    one
150
    two
151
bar: |-
152
    one
153
    two
154
EOF;
155
        $expected = array(
156
            'foo' => "one\ntwo",
157
            'bar' => "one\ntwo",
158
        );
159
        $tests['Literal block chomping strip without trailing newline'] = array($expected, $yaml);
160
161
        $yaml = <<<'EOF'
162
foo: |
163
    one
164
    two
165
bar: |
166
    one
167
    two
168
169
EOF;
170
        $expected = array(
171
            'foo' => "one\ntwo\n",
172
            'bar' => "one\ntwo\n",
173
        );
174
        $tests['Literal block chomping clip with single trailing newline'] = array($expected, $yaml);
175
176
        $yaml = <<<'EOF'
177
foo: |
178
    one
179
    two
180
181
bar: |
182
    one
183
    two
184
185
186
EOF;
187
        $expected = array(
188
            'foo' => "one\ntwo\n",
189
            'bar' => "one\ntwo\n",
190
        );
191
        $tests['Literal block chomping clip with multiple trailing newlines'] = array($expected, $yaml);
192
193
        $yaml = <<<'EOF'
194
foo: |
195
    one
196
    two
197
bar: |
198
    one
199
    two
200
EOF;
201
        $expected = array(
202
            'foo' => "one\ntwo\n",
203
            'bar' => "one\ntwo",
204
        );
205
        $tests['Literal block chomping clip without trailing newline'] = array($expected, $yaml);
206
207
        $yaml = <<<'EOF'
208
foo: |+
209
    one
210
    two
211
bar: |+
212
    one
213
    two
214
215
EOF;
216
        $expected = array(
217
            'foo' => "one\ntwo\n",
218
            'bar' => "one\ntwo\n",
219
        );
220
        $tests['Literal block chomping keep with single trailing newline'] = array($expected, $yaml);
221
222
        $yaml = <<<'EOF'
223
foo: |+
224
    one
225
    two
226
227
bar: |+
228
    one
229
    two
230
231
232
EOF;
233
        $expected = array(
234
            'foo' => "one\ntwo\n\n",
235
            'bar' => "one\ntwo\n\n",
236
        );
237
        $tests['Literal block chomping keep with multiple trailing newlines'] = array($expected, $yaml);
238
239
        $yaml = <<<'EOF'
240
foo: |+
241
    one
242
    two
243
bar: |+
244
    one
245
    two
246
EOF;
247
        $expected = array(
248
            'foo' => "one\ntwo\n",
249
            'bar' => "one\ntwo",
250
        );
251
        $tests['Literal block chomping keep without trailing newline'] = array($expected, $yaml);
252
253
        $yaml = <<<'EOF'
254
foo: >-
255
    one
256
    two
257
bar: >-
258
    one
259
    two
260
261
EOF;
262
        $expected = array(
263
            'foo' => 'one two',
264
            'bar' => 'one two',
265
        );
266
        $tests['Folded block chomping strip with single trailing newline'] = array($expected, $yaml);
267
268
        $yaml = <<<'EOF'
269
foo: >-
270
    one
271
    two
272
273
bar: >-
274
    one
275
    two
276
277
278
EOF;
279
        $expected = array(
280
            'foo' => 'one two',
281
            'bar' => 'one two',
282
        );
283
        $tests['Folded block chomping strip with multiple trailing newlines'] = array($expected, $yaml);
284
285
        $yaml = <<<'EOF'
286
foo: >-
287
    one
288
    two
289
bar: >-
290
    one
291
    two
292
EOF;
293
        $expected = array(
294
            'foo' => 'one two',
295
            'bar' => 'one two',
296
        );
297
        $tests['Folded block chomping strip without trailing newline'] = array($expected, $yaml);
298
299
        $yaml = <<<'EOF'
300
foo: >
301
    one
302
    two
303
bar: >
304
    one
305
    two
306
307
EOF;
308
        $expected = array(
309
            'foo' => "one two\n",
310
            'bar' => "one two\n",
311
        );
312
        $tests['Folded block chomping clip with single trailing newline'] = array($expected, $yaml);
313
314
        $yaml = <<<'EOF'
315
foo: >
316
    one
317
    two
318
319
bar: >
320
    one
321
    two
322
323
324
EOF;
325
        $expected = array(
326
            'foo' => "one two\n",
327
            'bar' => "one two\n",
328
        );
329
        $tests['Folded block chomping clip with multiple trailing newlines'] = array($expected, $yaml);
330
331
        $yaml = <<<'EOF'
332
foo: >
333
    one
334
    two
335
bar: >
336
    one
337
    two
338
EOF;
339
        $expected = array(
340
            'foo' => "one two\n",
341
            'bar' => 'one two',
342
        );
343
        $tests['Folded block chomping clip without trailing newline'] = array($expected, $yaml);
344
345
        $yaml = <<<'EOF'
346
foo: >+
347
    one
348
    two
349
bar: >+
350
    one
351
    two
352
353
EOF;
354
        $expected = array(
355
            'foo' => "one two\n",
356
            'bar' => "one two\n",
357
        );
358
        $tests['Folded block chomping keep with single trailing newline'] = array($expected, $yaml);
359
360
        $yaml = <<<'EOF'
361
foo: >+
362
    one
363
    two
364
365
bar: >+
366
    one
367
    two
368
369
370
EOF;
371
        $expected = array(
372
            'foo' => "one two\n\n",
373
            'bar' => "one two\n\n",
374
        );
375
        $tests['Folded block chomping keep with multiple trailing newlines'] = array($expected, $yaml);
376
377
        $yaml = <<<'EOF'
378
foo: >+
379
    one
380
    two
381
bar: >+
382
    one
383
    two
384
EOF;
385
        $expected = array(
386
            'foo' => "one two\n",
387
            'bar' => 'one two',
388
        );
389
        $tests['Folded block chomping keep without trailing newline'] = array($expected, $yaml);
390
391
        return $tests;
392
    }
393
394
    /**
395
     * @dataProvider getBlockChompingTests
396
     */
397
    public function testBlockChomping($expected, $yaml)
398
    {
399
        $this->assertSame($expected, $this->parser->parse($yaml));
400
    }
401
402
    /**
403
     * Regression test for issue #7989.
404
     *
405
     * @see https://github.com/symfony/symfony/issues/7989
406
     */
407
    public function testBlockLiteralWithLeadingNewlines()
408
    {
409
        $yaml = <<<'EOF'
410
foo: |-
411
412
413
    bar
414
415
EOF;
416
        $expected = array(
417
            'foo' => "\n\nbar",
418
        );
419
420
        $this->assertSame($expected, $this->parser->parse($yaml));
421
    }
422
423
    public function testObjectSupportEnabled()
424
    {
425
        $input = <<<EOF
426
foo: !!php/object:O:30:"Symfony\Component\Yaml\Tests\B":1:{s:1:"b";s:3:"foo";}
427
bar: 1
428
EOF;
429
        $this->assertEquals(array('foo' => new B(), 'bar' => 1), $this->parser->parse($input, false, true), '->parse() is able to parse objects');
430
431
        $input = <<<EOF
432
foo: !php/object:O:30:"Symfony\Component\Yaml\Tests\B":1:{s:1:"b";s:3:"foo";}
433
bar: 1
434
EOF;
435
        $this->assertEquals(array('foo' => new B(), 'bar' => 1), $this->parser->parse($input, false, true), '->parse() is able to parse objects');
436
    }
437
438
    /**
439
     * @dataProvider invalidDumpedObjectProvider
440
     */
441
    public function testObjectSupportDisabledButNoExceptions($input)
442
    {
443
        $this->assertEquals(array('foo' => null, 'bar' => 1), $this->parser->parse($input), '->parse() does not parse objects');
444
    }
445
446
    /**
447
     * @dataProvider getObjectForMapTests
448
     */
449
    public function testObjectForMap($yaml, $expected)
450
    {
451
        $this->assertEquals($expected, $this->parser->parse($yaml, false, false, true));
452
    }
453
454
    public function getObjectForMapTests()
455
    {
456
        $tests = array();
457
458
        $yaml = <<<EOF
459
foo:
460
    fiz: [cat]
461
EOF;
462
        $expected = new \stdClass();
463
        $expected->foo = new \stdClass();
464
        $expected->foo->fiz = array('cat');
465
        $tests['mapping'] = array($yaml, $expected);
466
467
        $yaml = '{ "foo": "bar", "fiz": "cat" }';
468
        $expected = new \stdClass();
469
        $expected->foo = 'bar';
470
        $expected->fiz = 'cat';
471
        $tests['inline-mapping'] = array($yaml, $expected);
472
473
        $yaml = "foo: bar\nbaz: foobar";
474
        $expected = new \stdClass();
475
        $expected->foo = 'bar';
476
        $expected->baz = 'foobar';
477
        $tests['object-for-map-is-applied-after-parsing'] = array($yaml, $expected);
478
479
        $yaml = <<<EOT
480
array:
481
  - key: one
482
  - key: two
483
EOT;
484
        $expected = new \stdClass();
485
        $expected->array = array();
486
        $expected->array[0] = new \stdClass();
487
        $expected->array[0]->key = 'one';
488
        $expected->array[1] = new \stdClass();
489
        $expected->array[1]->key = 'two';
490
        $tests['nest-map-and-sequence'] = array($yaml, $expected);
491
492
        $yaml = <<<YAML
493
map:
494
  1: one
495
  2: two
496
YAML;
497
        $expected = new \stdClass();
498
        $expected->map = new \stdClass();
499
        $expected->map->{1} = 'one';
500
        $expected->map->{2} = 'two';
501
        $tests['numeric-keys'] = array($yaml, $expected);
502
503
        $yaml = <<<YAML
504
map:
505
  0: one
506
  1: two
507
YAML;
508
        $expected = new \stdClass();
509
        $expected->map = new \stdClass();
510
        $expected->map->{0} = 'one';
511
        $expected->map->{1} = 'two';
512
        $tests['zero-indexed-numeric-keys'] = array($yaml, $expected);
513
514
        return $tests;
515
    }
516
517
    /**
518
     * @dataProvider invalidDumpedObjectProvider
519
     * @expectedException \Symfony\Component\Yaml\Exception\ParseException
520
     */
521
    public function testObjectsSupportDisabledWithExceptions($yaml)
522
    {
523
        $this->parser->parse($yaml, true, false);
524
    }
525
526
    public function invalidDumpedObjectProvider()
527
    {
528
        $yamlTag = <<<EOF
529
foo: !!php/object:O:30:"Symfony\Tests\Component\Yaml\B":1:{s:1:"b";s:3:"foo";}
530
bar: 1
531
EOF;
532
        $localTag = <<<EOF
533
foo: !php/object:O:30:"Symfony\Tests\Component\Yaml\B":1:{s:1:"b";s:3:"foo";}
534
bar: 1
535
EOF;
536
537
        return array(
538
            'yaml-tag' => array($yamlTag),
539
            'local-tag' => array($localTag),
540
        );
541
    }
542
543
    /**
544
     * @requires extension iconv
545
     */
546
    public function testNonUtf8Exception()
547
    {
548
        $yamls = array(
549
            iconv('UTF-8', 'ISO-8859-1', "foo: 'äöüß'"),
550
            iconv('UTF-8', 'ISO-8859-15', "euro: '€'"),
551
            iconv('UTF-8', 'CP1252', "cp1252: '©ÉÇáñ'"),
552
        );
553
554
        foreach ($yamls as $yaml) {
555
            try {
556
                $this->parser->parse($yaml);
557
558
                $this->fail('charsets other than UTF-8 are rejected.');
559
            } catch (\Exception $e) {
560
                $this->assertInstanceOf('Symfony\Component\Yaml\Exception\ParseException', $e, 'charsets other than UTF-8 are rejected.');
561
            }
562
        }
563
    }
564
565
    /**
566
     * @expectedException \Symfony\Component\Yaml\Exception\ParseException
567
     */
568
    public function testUnindentedCollectionException()
569
    {
570
        $yaml = <<<'EOF'
571
572
collection:
573
-item1
574
-item2
575
-item3
576
577
EOF;
578
579
        $this->parser->parse($yaml);
580
    }
581
582
    /**
583
     * @expectedException \Symfony\Component\Yaml\Exception\ParseException
584
     */
585
    public function testShortcutKeyUnindentedCollectionException()
586
    {
587
        $yaml = <<<'EOF'
588
589
collection:
590
-  key: foo
591
  foo: bar
592
593
EOF;
594
595
        $this->parser->parse($yaml);
596
    }
597
598
    /**
599
     * @expectedException \Symfony\Component\Yaml\Exception\ParseException
600
     * @expectedExceptionMessageRegExp /^Multiple documents are not supported.+/
601
     */
602
    public function testMultipleDocumentsNotSupportedException()
603
    {
604
        Yaml::parse(<<<'EOL'
605
# Ranking of 1998 home runs
606
---
607
- Mark McGwire
608
- Sammy Sosa
609
- Ken Griffey
610
611
# Team ranking
612
---
613
- Chicago Cubs
614
- St Louis Cardinals
615
EOL
616
        );
617
    }
618
619
    /**
620
     * @expectedException \Symfony\Component\Yaml\Exception\ParseException
621
     */
622
    public function testSequenceInAMapping()
623
    {
624
        Yaml::parse(<<<'EOF'
625
yaml:
626
  hash: me
627
  - array stuff
628
EOF
629
        );
630
    }
631
632
    public function testSequenceInMappingStartedBySingleDashLine()
633
    {
634
        $yaml = <<<EOT
635
a:
636
-
637
  b:
638
  -
639
    bar: baz
640
- foo
641
d: e
642
EOT;
643
        $expected = array(
644
            'a' => array(
645
                array(
646
                    'b' => array(
647
                        array(
648
                            'bar' => 'baz',
649
                        ),
650
                    ),
651
                ),
652
                'foo',
653
            ),
654
            'd' => 'e',
655
        );
656
657
        $this->assertSame($expected, $this->parser->parse($yaml));
658
    }
659
660
    public function testSequenceFollowedByCommentEmbeddedInMapping()
661
    {
662
        $yaml = <<<EOT
663
a:
664
    b:
665
        - c
666
# comment
667
    d: e
668
EOT;
669
        $expected = array(
670
            'a' => array(
671
                'b' => array('c'),
672
                'd' => 'e',
673
            ),
674
        );
675
676
        $this->assertSame($expected, $this->parser->parse($yaml));
677
    }
678
679
    /**
680
     * @expectedException \Symfony\Component\Yaml\Exception\ParseException
681
     */
682
    public function testMappingInASequence()
683
    {
684
        Yaml::parse(<<<'EOF'
685
yaml:
686
  - array stuff
687
  hash: me
688
EOF
689
        );
690
    }
691
692
    /**
693
     * @expectedException \Symfony\Component\Yaml\Exception\ParseException
694
     * @expectedExceptionMessage missing colon
695
     */
696
    public function testScalarInSequence()
697
    {
698
        Yaml::parse(<<<EOF
699
foo:
700
    - bar
701
"missing colon"
702
    foo: bar
703
EOF
704
        );
705
    }
706
707
    /**
708
     * > It is an error for two equal keys to appear in the same mapping node.
709
     * > In such a case the YAML processor may continue, ignoring the second
710
     * > `key: value` pair and issuing an appropriate warning. This strategy
711
     * > preserves a consistent information model for one-pass and random access
712
     * > applications.
713
     *
714
     * @see http://yaml.org/spec/1.2/spec.html#id2759572
715
     * @see http://yaml.org/spec/1.1/#id932806
716
     */
717 View Code Duplication
    public function testMappingDuplicateKeyBlock()
718
    {
719
        $input = <<<EOD
720
parent:
721
    child: first
722
    child: duplicate
723
parent:
724
    child: duplicate
725
    child: duplicate
726
EOD;
727
        $expected = array(
728
            'parent' => array(
729
                'child' => 'first',
730
            ),
731
        );
732
        $this->assertSame($expected, Yaml::parse($input));
733
    }
734
735 View Code Duplication
    public function testMappingDuplicateKeyFlow()
736
    {
737
        $input = <<<EOD
738
parent: { child: first, child: duplicate }
739
parent: { child: duplicate, child: duplicate }
740
EOD;
741
        $expected = array(
742
            'parent' => array(
743
                'child' => 'first',
744
            ),
745
        );
746
        $this->assertSame($expected, Yaml::parse($input));
747
    }
748
749
    public function testEmptyValue()
750
    {
751
        $input = <<<'EOF'
752
hash:
753
EOF;
754
755
        $this->assertEquals(array('hash' => null), Yaml::parse($input));
756
    }
757
758
    public function testCommentAtTheRootIndent()
759
    {
760
        $this->assertEquals(array(
761
            'services' => array(
762
                'app.foo_service' => array(
763
                    'class' => 'Foo',
764
                ),
765
                'app/bar_service' => array(
766
                    'class' => 'Bar',
767
                ),
768
            ),
769
        ), Yaml::parse(<<<'EOF'
770
# comment 1
771
services:
772
# comment 2
773
    # comment 3
774
    app.foo_service:
775
        class: Foo
776
# comment 4
777
    # comment 5
778
    app/bar_service:
779
        class: Bar
780
EOF
781
        ));
782
    }
783
784
    public function testStringBlockWithComments()
785
    {
786
        $this->assertEquals(array('content' => <<<'EOT'
787
# comment 1
788
header
789
790
    # comment 2
791
    <body>
792
        <h1>title</h1>
793
    </body>
794
795
footer # comment3
796
EOT
797
        ), Yaml::parse(<<<'EOF'
798
content: |
799
    # comment 1
800
    header
801
802
        # comment 2
803
        <body>
804
            <h1>title</h1>
805
        </body>
806
807
    footer # comment3
808
EOF
809
        ));
810
    }
811
812
    public function testFoldedStringBlockWithComments()
813
    {
814
        $this->assertEquals(array(array('content' => <<<'EOT'
815
# comment 1
816
header
817
818
    # comment 2
819
    <body>
820
        <h1>title</h1>
821
    </body>
822
823
footer # comment3
824
EOT
825
        )), Yaml::parse(<<<'EOF'
826
-
827
    content: |
828
        # comment 1
829
        header
830
831
            # comment 2
832
            <body>
833
                <h1>title</h1>
834
            </body>
835
836
        footer # comment3
837
EOF
838
        ));
839
    }
840
841
    public function testNestedFoldedStringBlockWithComments()
842
    {
843
        $this->assertEquals(array(array(
844
            'title' => 'some title',
845
            'content' => <<<'EOT'
846
# comment 1
847
header
848
849
    # comment 2
850
    <body>
851
        <h1>title</h1>
852
    </body>
853
854
footer # comment3
855
EOT
856
        )), Yaml::parse(<<<'EOF'
857
-
858
    title: some title
859
    content: |
860
        # comment 1
861
        header
862
863
            # comment 2
864
            <body>
865
                <h1>title</h1>
866
            </body>
867
868
        footer # comment3
869
EOF
870
        ));
871
    }
872
873
    public function testReferenceResolvingInInlineStrings()
874
    {
875
        $this->assertEquals(array(
876
            'var' => 'var-value',
877
            'scalar' => 'var-value',
878
            'list' => array('var-value'),
879
            'list_in_list' => array(array('var-value')),
880
            'map_in_list' => array(array('key' => 'var-value')),
881
            'embedded_mapping' => array(array('key' => 'var-value')),
882
            'map' => array('key' => 'var-value'),
883
            'list_in_map' => array('key' => array('var-value')),
884
            'map_in_map' => array('foo' => array('bar' => 'var-value')),
885
        ), Yaml::parse(<<<'EOF'
886
var:  &var var-value
887
scalar: *var
888
list: [ *var ]
889
list_in_list: [[ *var ]]
890
map_in_list: [ { key: *var } ]
891
embedded_mapping: [ key: *var ]
892
map: { key: *var }
893
list_in_map: { key: [*var] }
894
map_in_map: { foo: { bar: *var } }
895
EOF
896
        ));
897
    }
898
899 View Code Duplication
    public function testYamlDirective()
900
    {
901
        $yaml = <<<'EOF'
902
%YAML 1.2
903
---
904
foo: 1
905
bar: 2
906
EOF;
907
        $this->assertEquals(array('foo' => 1, 'bar' => 2), $this->parser->parse($yaml));
908
    }
909
910 View Code Duplication
    public function testFloatKeys()
911
    {
912
        $yaml = <<<'EOF'
913
foo:
914
    1.2: "bar"
915
    1.3: "baz"
916
EOF;
917
918
        $expected = array(
919
            'foo' => array(
920
                '1.2' => 'bar',
921
                '1.3' => 'baz',
922
            ),
923
        );
924
925
        $this->assertEquals($expected, $this->parser->parse($yaml));
926
    }
927
928
    /**
929
     * @group legacy
930
     * throw ParseException in Symfony 3.0
931
     * @requires function Symfony\Bridge\PhpUnit\ErrorAssert::assertDeprecationsAreTriggered
932
     */
933
    public function testColonInMappingValueException()
934
    {
935
        $parser = $this->parser;
936
937
        ErrorAssert::assertDeprecationsAreTriggered('Using a colon in the unquoted mapping value "bar: baz" in line 1 is deprecated since Symfony 2.8 and will throw a ParseException in 3.0.', function () use ($parser) {
938
            $yaml = <<<EOF
939
foo: bar: baz
940
EOF;
941
            $parser->parse($yaml);
942
        });
943
    }
944
945
    public function testColonInMappingValueExceptionNotTriggeredByColonInComment()
946
    {
947
        $yaml = <<<EOT
948
foo:
949
    bar: foobar # Note: a comment after a colon
950
EOT;
951
952
        $this->assertSame(array('foo' => array('bar' => 'foobar')), $this->parser->parse($yaml));
953
    }
954
955
    /**
956
     * @dataProvider getCommentLikeStringInScalarBlockData
957
     */
958
    public function testCommentLikeStringsAreNotStrippedInBlockScalars($yaml, $expectedParserResult)
959
    {
960
        $this->assertSame($expectedParserResult, $this->parser->parse($yaml));
961
    }
962
963
    public function getCommentLikeStringInScalarBlockData()
964
    {
965
        $tests = array();
966
967
        $yaml = <<<'EOT'
968
pages:
969
    -
970
        title: some title
971
        content: |
972
            # comment 1
973
            header
974
975
                # comment 2
976
                <body>
977
                    <h1>title</h1>
978
                </body>
979
980
            footer # comment3
981
EOT;
982
        $expected = array(
983
            'pages' => array(
984
                array(
985
                    'title' => 'some title',
986
                    'content' => <<<'EOT'
987
# comment 1
988
header
989
990
    # comment 2
991
    <body>
992
        <h1>title</h1>
993
    </body>
994
995
footer # comment3
996
EOT
997
                    ,
998
                ),
999
            ),
1000
        );
1001
        $tests[] = array($yaml, $expected);
1002
1003
        $yaml = <<<'EOT'
1004
test: |
1005
    foo
1006
    # bar
1007
    baz
1008
collection:
1009
    - one: |
1010
        foo
1011
        # bar
1012
        baz
1013
    - two: |
1014
        foo
1015
        # bar
1016
        baz
1017
EOT;
1018
        $expected = array(
1019
            'test' => <<<'EOT'
1020
foo
1021
# bar
1022
baz
1023
1024
EOT
1025
            ,
1026
            'collection' => array(
1027
                array(
1028
                    'one' => <<<'EOT'
1029
foo
1030
# bar
1031
baz
1032
1033
EOT
1034
                    ,
1035
                ),
1036
                array(
1037
                    'two' => <<<'EOT'
1038
foo
1039
# bar
1040
baz
1041
EOT
1042
                    ,
1043
                ),
1044
            ),
1045
        );
1046
        $tests[] = array($yaml, $expected);
1047
1048
        $yaml = <<<EOT
1049
foo:
1050
  bar:
1051
    scalar-block: >
1052
      line1
1053
      line2>
1054
  baz:
1055
# comment
1056
    foobar: ~
1057
EOT;
1058
        $expected = array(
1059
            'foo' => array(
1060
                'bar' => array(
1061
                    'scalar-block' => "line1 line2>\n",
1062
                ),
1063
                'baz' => array(
1064
                    'foobar' => null,
1065
                ),
1066
            ),
1067
        );
1068
        $tests[] = array($yaml, $expected);
1069
1070
        $yaml = <<<'EOT'
1071
a:
1072
    b: hello
1073
#    c: |
1074
#        first row
1075
#        second row
1076
    d: hello
1077
EOT;
1078
        $expected = array(
1079
            'a' => array(
1080
                'b' => 'hello',
1081
                'd' => 'hello',
1082
            ),
1083
        );
1084
        $tests[] = array($yaml, $expected);
1085
1086
        return $tests;
1087
    }
1088
1089
    public function testBlankLinesAreParsedAsNewLinesInFoldedBlocks()
1090
    {
1091
        $yaml = <<<EOT
1092
test: >
1093
    <h2>A heading</h2>
1094
1095
    <ul>
1096
    <li>a list</li>
1097
    <li>may be a good example</li>
1098
    </ul>
1099
EOT;
1100
1101
        $this->assertSame(
1102
            array(
1103
                'test' => <<<EOT
1104
<h2>A heading</h2>
1105
<ul> <li>a list</li> <li>may be a good example</li> </ul>
1106
EOT
1107
                ,
1108
            ),
1109
            $this->parser->parse($yaml)
1110
        );
1111
    }
1112
1113
    public function testAdditionallyIndentedLinesAreParsedAsNewLinesInFoldedBlocks()
1114
    {
1115
        $yaml = <<<EOT
1116
test: >
1117
    <h2>A heading</h2>
1118
1119
    <ul>
1120
      <li>a list</li>
1121
      <li>may be a good example</li>
1122
    </ul>
1123
EOT;
1124
1125
        $this->assertSame(
1126
            array(
1127
                'test' => <<<EOT
1128
<h2>A heading</h2>
1129
<ul>
1130
  <li>a list</li>
1131
  <li>may be a good example</li>
1132
</ul>
1133
EOT
1134
                ,
1135
            ),
1136
            $this->parser->parse($yaml)
1137
        );
1138
    }
1139
1140
    /**
1141
     * @param $lineNumber
1142
     * @param $yaml
1143
     * @dataProvider parserThrowsExceptionWithCorrectLineNumberProvider
1144
     */
1145
    public function testParserThrowsExceptionWithCorrectLineNumber($lineNumber, $yaml)
1146
    {
1147
        $this->setExpectedException(
1148
            '\Symfony\Component\Yaml\Exception\ParseException',
1149
            sprintf('Unexpected characters near "," at line %d (near "bar: "123",").', $lineNumber)
1150
        );
1151
1152
        $this->parser->parse($yaml);
1153
    }
1154
1155
    public function parserThrowsExceptionWithCorrectLineNumberProvider()
1156
    {
1157
        return array(
1158
            array(
1159
                4,
1160
                <<<YAML
1161
foo:
1162
    -
1163
        # bar
1164
        bar: "123",
1165
YAML
1166
            ),
1167
            array(
1168
                5,
1169
                <<<YAML
1170
foo:
1171
    -
1172
        # bar
1173
        # bar
1174
        bar: "123",
1175
YAML
1176
            ),
1177
            array(
1178
                8,
1179
                <<<YAML
1180
foo:
1181
    -
1182
        # foobar
1183
        baz: 123
1184
bar:
1185
    -
1186
        # bar
1187
        bar: "123",
1188
YAML
1189
            ),
1190
            array(
1191
                10,
1192
                <<<YAML
1193
foo:
1194
    -
1195
        # foobar
1196
        # foobar
1197
        baz: 123
1198
bar:
1199
    -
1200
        # bar
1201
        # bar
1202
        bar: "123",
1203
YAML
1204
            ),
1205
        );
1206
    }
1207
}
1208
1209
class B
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
1210
{
1211
    public $b = 'foo';
1212
}
1213