ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
ParserTest.php
Go to the documentation of this file.
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 
13 
16 
18 {
19  protected $parser;
20 
21  protected function setUp()
22  {
23  $this->parser = new Parser();
24  }
25 
26  protected function tearDown()
27  {
28  $this->parser = null;
29  }
30 
34  public function testSpecifications($file, $expected, $yaml, $comment)
35  {
36  $this->assertEquals($expected, var_export($this->parser->parse($yaml), true), $comment);
37  }
38 
39  public function getDataFormSpecifications()
40  {
41  $parser = new Parser();
42  $path = __DIR__.'/Fixtures';
43 
44  $tests = array();
45  $files = $parser->parse(file_get_contents($path.'/index.yml'));
46  foreach ($files as $file) {
47  $yamls = file_get_contents($path.'/'.$file.'.yml');
48 
49  // split YAMLs documents
50  foreach (preg_split('/^---( %YAML\:1\.0)?/m', $yamls) as $yaml) {
51  if (!$yaml) {
52  continue;
53  }
54 
55  $test = $parser->parse($yaml);
56  if (isset($test['todo']) && $test['todo']) {
57  // TODO
58  } else {
59  eval('$expected = '.trim($test['php']).';');
60 
61  $tests[] = array($file, var_export($expected, true), $test['yaml'], $test['test']);
62  }
63  }
64  }
65 
66  return $tests;
67  }
68 
69  public function testTabsInYaml()
70  {
71  // test tabs in YAML
72  $yamls = array(
73  "foo:\n bar",
74  "foo:\n bar",
75  "foo:\n bar",
76  "foo:\n bar",
77  );
78 
79  foreach ($yamls as $yaml) {
80  try {
81  $content = $this->parser->parse($yaml);
82 
83  $this->fail('YAML files must not contain tabs');
84  } catch (\Exception $e) {
85  $this->assertInstanceOf('\Exception', $e, 'YAML files must not contain tabs');
86  $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');
87  }
88  }
89  }
90 
91  public function testEndOfTheDocumentMarker()
92  {
93  $yaml = <<<'EOF'
94 --- %YAML:1.0
95 foo
96 ...
97 EOF;
98 
99  $this->assertEquals('foo', $this->parser->parse($yaml));
100  }
101 
102  public function getBlockChompingTests()
103  {
104  $tests = array();
105 
106  $yaml = <<<'EOF'
107 foo: |-
108  one
109  two
110 bar: |-
111  one
112  two
113 
114 EOF;
115  $expected = array(
116  'foo' => "one\ntwo",
117  'bar' => "one\ntwo",
118  );
119  $tests['Literal block chomping strip with single trailing newline'] = array($expected, $yaml);
120 
121  $yaml = <<<'EOF'
122 foo: |-
123  one
124  two
125 
126 bar: |-
127  one
128  two
129 
130 
131 EOF;
132  $expected = array(
133  'foo' => "one\ntwo",
134  'bar' => "one\ntwo",
135  );
136  $tests['Literal block chomping strip with multiple trailing newlines'] = array($expected, $yaml);
137 
138  $yaml = <<<'EOF'
139 {}
140 
141 
142 EOF;
143  $expected = array();
144  $tests['Literal block chomping strip with multiple trailing newlines after a 1-liner'] = array($expected, $yaml);
145 
146  $yaml = <<<'EOF'
147 foo: |-
148  one
149  two
150 bar: |-
151  one
152  two
153 EOF;
154  $expected = array(
155  'foo' => "one\ntwo",
156  'bar' => "one\ntwo",
157  );
158  $tests['Literal block chomping strip without trailing newline'] = array($expected, $yaml);
159 
160  $yaml = <<<'EOF'
161 foo: |
162  one
163  two
164 bar: |
165  one
166  two
167 
168 EOF;
169  $expected = array(
170  'foo' => "one\ntwo\n",
171  'bar' => "one\ntwo\n",
172  );
173  $tests['Literal block chomping clip with single trailing newline'] = array($expected, $yaml);
174 
175  $yaml = <<<'EOF'
176 foo: |
177  one
178  two
179 
180 bar: |
181  one
182  two
183 
184 
185 EOF;
186  $expected = array(
187  'foo' => "one\ntwo\n",
188  'bar' => "one\ntwo\n",
189  );
190  $tests['Literal block chomping clip with multiple trailing newlines'] = array($expected, $yaml);
191 
192  $yaml = <<<'EOF'
193 foo: |
194  one
195  two
196 bar: |
197  one
198  two
199 EOF;
200  $expected = array(
201  'foo' => "one\ntwo\n",
202  'bar' => "one\ntwo",
203  );
204  $tests['Literal block chomping clip without trailing newline'] = array($expected, $yaml);
205 
206  $yaml = <<<'EOF'
207 foo: |+
208  one
209  two
210 bar: |+
211  one
212  two
213 
214 EOF;
215  $expected = array(
216  'foo' => "one\ntwo\n",
217  'bar' => "one\ntwo\n",
218  );
219  $tests['Literal block chomping keep with single trailing newline'] = array($expected, $yaml);
220 
221  $yaml = <<<'EOF'
222 foo: |+
223  one
224  two
225 
226 bar: |+
227  one
228  two
229 
230 
231 EOF;
232  $expected = array(
233  'foo' => "one\ntwo\n\n",
234  'bar' => "one\ntwo\n\n",
235  );
236  $tests['Literal block chomping keep with multiple trailing newlines'] = array($expected, $yaml);
237 
238  $yaml = <<<'EOF'
239 foo: |+
240  one
241  two
242 bar: |+
243  one
244  two
245 EOF;
246  $expected = array(
247  'foo' => "one\ntwo\n",
248  'bar' => "one\ntwo",
249  );
250  $tests['Literal block chomping keep without trailing newline'] = array($expected, $yaml);
251 
252  $yaml = <<<'EOF'
253 foo: >-
254  one
255  two
256 bar: >-
257  one
258  two
259 
260 EOF;
261  $expected = array(
262  'foo' => 'one two',
263  'bar' => 'one two',
264  );
265  $tests['Folded block chomping strip with single trailing newline'] = array($expected, $yaml);
266 
267  $yaml = <<<'EOF'
268 foo: >-
269  one
270  two
271 
272 bar: >-
273  one
274  two
275 
276 
277 EOF;
278  $expected = array(
279  'foo' => 'one two',
280  'bar' => 'one two',
281  );
282  $tests['Folded block chomping strip with multiple trailing newlines'] = array($expected, $yaml);
283 
284  $yaml = <<<'EOF'
285 foo: >-
286  one
287  two
288 bar: >-
289  one
290  two
291 EOF;
292  $expected = array(
293  'foo' => 'one two',
294  'bar' => 'one two',
295  );
296  $tests['Folded block chomping strip without trailing newline'] = array($expected, $yaml);
297 
298  $yaml = <<<'EOF'
299 foo: >
300  one
301  two
302 bar: >
303  one
304  two
305 
306 EOF;
307  $expected = array(
308  'foo' => "one two\n",
309  'bar' => "one two\n",
310  );
311  $tests['Folded block chomping clip with single trailing newline'] = array($expected, $yaml);
312 
313  $yaml = <<<'EOF'
314 foo: >
315  one
316  two
317 
318 bar: >
319  one
320  two
321 
322 
323 EOF;
324  $expected = array(
325  'foo' => "one two\n",
326  'bar' => "one two\n",
327  );
328  $tests['Folded block chomping clip with multiple trailing newlines'] = array($expected, $yaml);
329 
330  $yaml = <<<'EOF'
331 foo: >
332  one
333  two
334 bar: >
335  one
336  two
337 EOF;
338  $expected = array(
339  'foo' => "one two\n",
340  'bar' => 'one two',
341  );
342  $tests['Folded block chomping clip without trailing newline'] = array($expected, $yaml);
343 
344  $yaml = <<<'EOF'
345 foo: >+
346  one
347  two
348 bar: >+
349  one
350  two
351 
352 EOF;
353  $expected = array(
354  'foo' => "one two\n",
355  'bar' => "one two\n",
356  );
357  $tests['Folded block chomping keep with single trailing newline'] = array($expected, $yaml);
358 
359  $yaml = <<<'EOF'
360 foo: >+
361  one
362  two
363 
364 bar: >+
365  one
366  two
367 
368 
369 EOF;
370  $expected = array(
371  'foo' => "one two\n\n",
372  'bar' => "one two\n\n",
373  );
374  $tests['Folded block chomping keep with multiple trailing newlines'] = array($expected, $yaml);
375 
376  $yaml = <<<'EOF'
377 foo: >+
378  one
379  two
380 bar: >+
381  one
382  two
383 EOF;
384  $expected = array(
385  'foo' => "one two\n",
386  'bar' => 'one two',
387  );
388  $tests['Folded block chomping keep without trailing newline'] = array($expected, $yaml);
389 
390  return $tests;
391  }
392 
396  public function testBlockChomping($expected, $yaml)
397  {
398  $this->assertSame($expected, $this->parser->parse($yaml));
399  }
400 
407  {
408  $yaml = <<<'EOF'
409 foo: |-
410 
411 
412  bar
413 
414 EOF;
415  $expected = array(
416  'foo' => "\n\nbar",
417  );
418 
419  $this->assertSame($expected, $this->parser->parse($yaml));
420  }
421 
422  public function testObjectSupportEnabled()
423  {
424  $input = <<<EOF
425 foo: !php/object:O:30:"Symfony\Component\Yaml\Tests\B":1:{s:1:"b";s:3:"foo";}
426 bar: 1
427 EOF;
428  $this->assertEquals(array('foo' => new B(), 'bar' => 1), $this->parser->parse($input, Yaml::PARSE_OBJECT), '->parse() is able to parse objects');
429  }
430 
435  {
436  $input = <<<EOF
437 foo: !php/object:O:30:"Symfony\Component\Yaml\Tests\B":1:{s:1:"b";s:3:"foo";}
438 bar: 1
439 EOF;
440  $this->assertEquals(array('foo' => new B(), 'bar' => 1), $this->parser->parse($input, false, true), '->parse() is able to parse objects');
441  }
442 
447  {
448  $input = <<<EOF
449 foo: !!php/object:O:30:"Symfony\Component\Yaml\Tests\B":1:{s:1:"b";s:3:"foo";}
450 bar: 1
451 EOF;
452  $this->assertEquals(array('foo' => new B(), 'bar' => 1), $this->parser->parse($input, Yaml::PARSE_OBJECT), '->parse() is able to parse objects');
453  }
454 
459  {
460  $this->assertEquals(array('foo' => null, 'bar' => 1), $this->parser->parse($input), '->parse() does not parse objects');
461  }
462 
466  public function testObjectForMap($yaml, $expected)
467  {
468  $this->assertEquals($expected, $this->parser->parse($yaml, Yaml::PARSE_OBJECT_FOR_MAP));
469  }
470 
476  {
477  $this->assertEquals($expected, $this->parser->parse($yaml, false, false, true));
478  }
479 
480  public function getObjectForMapTests()
481  {
482  $tests = array();
483 
484  $yaml = <<<EOF
485 foo:
486  fiz: [cat]
487 EOF;
488  $expected = new \stdClass();
489  $expected->foo = new \stdClass();
490  $expected->foo->fiz = array('cat');
491  $tests['mapping'] = array($yaml, $expected);
492 
493  $yaml = '{ "foo": "bar", "fiz": "cat" }';
494  $expected = new \stdClass();
495  $expected->foo = 'bar';
496  $expected->fiz = 'cat';
497  $tests['inline-mapping'] = array($yaml, $expected);
498 
499  $yaml = "foo: bar\nbaz: foobar";
500  $expected = new \stdClass();
501  $expected->foo = 'bar';
502  $expected->baz = 'foobar';
503  $tests['object-for-map-is-applied-after-parsing'] = array($yaml, $expected);
504 
505  $yaml = <<<EOT
506 array:
507  - key: one
508  - key: two
509 EOT;
510  $expected = new \stdClass();
511  $expected->array = array();
512  $expected->array[0] = new \stdClass();
513  $expected->array[0]->key = 'one';
514  $expected->array[1] = new \stdClass();
515  $expected->array[1]->key = 'two';
516  $tests['nest-map-and-sequence'] = array($yaml, $expected);
517 
518  $yaml = <<<YAML
519 map:
520  1: one
521  2: two
522 YAML;
523  $expected = new \stdClass();
524  $expected->map = new \stdClass();
525  $expected->map->{1} = 'one';
526  $expected->map->{2} = 'two';
527  $tests['numeric-keys'] = array($yaml, $expected);
528 
529  $yaml = <<<YAML
530 map:
531  0: one
532  1: two
533 YAML;
534  $expected = new \stdClass();
535  $expected->map = new \stdClass();
536  $expected->map->{0} = 'one';
537  $expected->map->{1} = 'two';
538  $tests['zero-indexed-numeric-keys'] = array($yaml, $expected);
539 
540  return $tests;
541  }
542 
548  {
549  $this->parser->parse($yaml, Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE);
550  }
551 
558  {
559  $this->parser->parse($yaml, true);
560  }
561 
562  public function invalidDumpedObjectProvider()
563  {
564  $yamlTag = <<<EOF
565 foo: !!php/object:O:30:"Symfony\Tests\Component\Yaml\B":1:{s:1:"b";s:3:"foo";}
566 bar: 1
567 EOF;
568  $localTag = <<<EOF
569 foo: !php/object:O:30:"Symfony\Tests\Component\Yaml\B":1:{s:1:"b";s:3:"foo";}
570 bar: 1
571 EOF;
572 
573  return array(
574  'yaml-tag' => array($yamlTag),
575  'local-tag' => array($localTag),
576  );
577  }
578 
582  public function testNonUtf8Exception()
583  {
584  $yamls = array(
585  iconv('UTF-8', 'ISO-8859-1', "foo: 'äöüß'"),
586  iconv('UTF-8', 'ISO-8859-15', "euro: '€'"),
587  iconv('UTF-8', 'CP1252', "cp1252: '©ÉÇáñ'"),
588  );
589 
590  foreach ($yamls as $yaml) {
591  try {
592  $this->parser->parse($yaml);
593 
594  $this->fail('charsets other than UTF-8 are rejected.');
595  } catch (\Exception $e) {
596  $this->assertInstanceOf('Symfony\Component\Yaml\Exception\ParseException', $e, 'charsets other than UTF-8 are rejected.');
597  }
598  }
599  }
600 
605  {
606  $yaml = <<<'EOF'
607 
608 collection:
609 -item1
610 -item2
611 -item3
612 
613 EOF;
614 
615  $this->parser->parse($yaml);
616  }
617 
622  {
623  $yaml = <<<'EOF'
624 
625 collection:
626 - key: foo
627  foo: bar
628 
629 EOF;
630 
631  $this->parser->parse($yaml);
632  }
633 
639  {
640  Yaml::parse(<<<'EOL'
641 # Ranking of 1998 home runs
642 ---
643 - Mark McGwire
644 - Sammy Sosa
645 - Ken Griffey
646 
647 # Team ranking
648 ---
649 - Chicago Cubs
650 - St Louis Cardinals
651 EOL
652  );
653  }
654 
658  public function testSequenceInAMapping()
659  {
660  Yaml::parse(<<<'EOF'
661 yaml:
662  hash: me
663  - array stuff
664 EOF
665  );
666  }
667 
669  {
670  $yaml = <<<EOT
671 a:
672 -
673  b:
674  -
675  bar: baz
676 - foo
677 d: e
678 EOT;
679  $expected = array(
680  'a' => array(
681  array(
682  'b' => array(
683  array(
684  'bar' => 'baz',
685  ),
686  ),
687  ),
688  'foo',
689  ),
690  'd' => 'e',
691  );
692 
693  $this->assertSame($expected, $this->parser->parse($yaml));
694  }
695 
697  {
698  $yaml = <<<EOT
699 a:
700  b:
701  - c
702 # comment
703  d: e
704 EOT;
705  $expected = array(
706  'a' => array(
707  'b' => array('c'),
708  'd' => 'e',
709  ),
710  );
711 
712  $this->assertSame($expected, $this->parser->parse($yaml));
713  }
714 
718  public function testMappingInASequence()
719  {
720  Yaml::parse(<<<'EOF'
721 yaml:
722  - array stuff
723  hash: me
724 EOF
725  );
726  }
727 
732  public function testScalarInSequence()
733  {
734  Yaml::parse(<<<EOF
735 foo:
736  - bar
737 "missing colon"
738  foo: bar
739 EOF
740  );
741  }
742 
754  {
755  $input = <<<EOD
756 parent:
757  child: first
758  child: duplicate
759 parent:
760  child: duplicate
761  child: duplicate
762 EOD;
763  $expected = array(
764  'parent' => array(
765  'child' => 'first',
766  ),
767  );
768  $this->assertSame($expected, Yaml::parse($input));
769  }
770 
771  public function testMappingDuplicateKeyFlow()
772  {
773  $input = <<<EOD
774 parent: { child: first, child: duplicate }
775 parent: { child: duplicate, child: duplicate }
776 EOD;
777  $expected = array(
778  'parent' => array(
779  'child' => 'first',
780  ),
781  );
782  $this->assertSame($expected, Yaml::parse($input));
783  }
784 
785  public function testEmptyValue()
786  {
787  $input = <<<'EOF'
788 hash:
789 EOF;
790 
791  $this->assertEquals(array('hash' => null), Yaml::parse($input));
792  }
793 
794  public function testCommentAtTheRootIndent()
795  {
796  $this->assertEquals(array(
797  'services' => array(
798  'app.foo_service' => array(
799  'class' => 'Foo',
800  ),
801  'app/bar_service' => array(
802  'class' => 'Bar',
803  ),
804  ),
805  ), Yaml::parse(<<<'EOF'
806 # comment 1
807 services:
808 # comment 2
809  # comment 3
810  app.foo_service:
811  class: Foo
812 # comment 4
813  # comment 5
814  app/bar_service:
815  class: Bar
816 EOF
817  ));
818  }
819 
820  public function testStringBlockWithComments()
821  {
822  $this->assertEquals(array('content' => <<<'EOT'
823 # comment 1
824 header
825 
826  # comment 2
827  <body>
828  <h1>title</h1>
829  </body>
830 
831 footer # comment3
832 EOT
833  ), Yaml::parse(<<<'EOF'
834 content: |
836  header
837 
838  # comment 2
839  <body>
840  <h1>title</h1>
841  </body>
842 
843  footer # comment3
844 EOF
845  ));
846  }
847 
849  {
850  $this->assertEquals(array(array('content' => <<<'EOT'
851 # comment 1
852 header
853 
854  # comment 2
855  <body>
856  <h1>title</h1>
857  </body>
858 
859 footer # comment3
860 EOT
861  )), Yaml::parse(<<<'EOF'
862 -
863  content: |
864  # comment 1
865  header
866 
867  # comment 2
868  <body>
869  <h1>title</h1>
870  </body>
871 
872  footer # comment3
873 EOF
874  ));
875  }
876 
878  {
879  $this->assertEquals(array(array(
880  'title' => 'some title',
881  'content' => <<<'EOT'
882 # comment 1
883 header
884 
885  # comment 2
886  <body>
887  <h1>title</h1>
888  </body>
889 
890 footer # comment3
891 EOT
892  )), Yaml::parse(<<<'EOF'
893 -
894  title: some title
895  content: |
896  # comment 1
897  header
898 
899  # comment 2
900  <body>
901  <h1>title</h1>
902  </body>
903 
904  footer # comment3
905 EOF
906  ));
907  }
908 
910  {
911  $this->assertEquals(array(
912  'var' => 'var-value',
913  'scalar' => 'var-value',
914  'list' => array('var-value'),
915  'list_in_list' => array(array('var-value')),
916  'map_in_list' => array(array('key' => 'var-value')),
917  'embedded_mapping' => array(array('key' => 'var-value')),
918  'map' => array('key' => 'var-value'),
919  'list_in_map' => array('key' => array('var-value')),
920  'map_in_map' => array('foo' => array('bar' => 'var-value')),
921  ), Yaml::parse(<<<'EOF'
922 var: &var var-value
923 scalar: *var
924 list: [ *var ]
925 list_in_list: [[ *var ]]
926 map_in_list: [ { key: *var } ]
927 embedded_mapping: [ key: *var ]
928 map: { key: *var }
929 list_in_map: { key: [*var] }
930 map_in_map: { foo: { bar: *var } }
931 EOF
932  ));
933  }
934 
935  public function testYamlDirective()
936  {
937  $yaml = <<<'EOF'
938 %YAML 1.2
939 ---
940 foo: 1
941 bar: 2
942 EOF;
943  $this->assertEquals(array('foo' => 1, 'bar' => 2), $this->parser->parse($yaml));
944  }
945 
946  public function testFloatKeys()
947  {
948  $yaml = <<<'EOF'
949 foo:
950  1.2: "bar"
951  1.3: "baz"
952 EOF;
953 
954  $expected = array(
955  'foo' => array(
956  '1.2' => 'bar',
957  '1.3' => 'baz',
958  ),
959  );
960 
961  $this->assertEquals($expected, $this->parser->parse($yaml));
962  }
963 
969  {
970  $yaml = <<<EOF
971 foo: bar: baz
972 EOF;
973 
974  $this->parser->parse($yaml);
975  }
976 
978  {
979  $yaml = <<<EOT
980 foo:
981  bar: foobar # Note: a comment after a colon
982 EOT;
983 
984  $this->assertSame(array('foo' => array('bar' => 'foobar')), $this->parser->parse($yaml));
985  }
986 
990  public function testCommentLikeStringsAreNotStrippedInBlockScalars($yaml, $expectedParserResult)
991  {
992  $this->assertSame($expectedParserResult, $this->parser->parse($yaml));
993  }
994 
996  {
997  $tests = array();
998 
999  $yaml = <<<'EOT'
1000 pages:
1001  -
1002  title: some title
1003  content: |
1004  # comment 1
1005  header
1006 
1007  # comment 2
1008  <body>
1009  <h1>title</h1>
1010  </body>
1011 
1012  footer # comment3
1013 EOT;
1014  $expected = array(
1015  'pages' => array(
1016  array(
1017  'title' => 'some title',
1018  'content' => <<<'EOT'
1019 # comment 1
1020 header
1021 
1022  # comment 2
1023  <body>
1024  <h1>title</h1>
1025  </body>
1026 
1027 footer # comment3
1028 EOT
1029  ,
1030  ),
1031  ),
1032  );
1033  $tests[] = array($yaml, $expected);
1034 
1035  $yaml = <<<'EOT'
1036 test: |
1037  foo
1038  # bar
1039  baz
1040 collection:
1041  - one: |
1042  foo
1043  # bar
1044  baz
1045  - two: |
1046  foo
1047  # bar
1048  baz
1049 EOT;
1050  $expected = array(
1051  'test' => <<<'EOT'
1052 foo
1053 # bar
1054 baz
1055 
1056 EOT
1057  ,
1058  'collection' => array(
1059  array(
1060  'one' => <<<'EOT'
1061 foo
1062 # bar
1063 baz
1064 
1065 EOT
1066  ,
1067  ),
1068  array(
1069  'two' => <<<'EOT'
1070 foo
1071 # bar
1072 baz
1073 EOT
1074  ,
1075  ),
1076  ),
1077  );
1078  $tests[] = array($yaml, $expected);
1079 
1080  $yaml = <<<EOT
1081 foo:
1082  bar:
1083  scalar-block: >
1084  line1
1085  line2>
1086  baz:
1087 # comment
1088  foobar: ~
1089 EOT;
1090  $expected = array(
1091  'foo' => array(
1092  'bar' => array(
1093  'scalar-block' => "line1 line2>\n",
1094  ),
1095  'baz' => array(
1096  'foobar' => null,
1097  ),
1098  ),
1099  );
1100  $tests[] = array($yaml, $expected);
1101 
1102  $yaml = <<<'EOT'
1103 a:
1104  b: hello
1105 # c: |
1106 # first row
1107 # second row
1108  d: hello
1109 EOT;
1110  $expected = array(
1111  'a' => array(
1112  'b' => 'hello',
1113  'd' => 'hello',
1114  ),
1115  );
1116  $tests[] = array($yaml, $expected);
1117 
1118  return $tests;
1119  }
1120 
1122  {
1123  $yaml = <<<EOT
1124 test: >
1125  <h2>A heading</h2>
1126 
1127  <ul>
1128  <li>a list</li>
1129  <li>may be a good example</li>
1130  </ul>
1131 EOT;
1132 
1133  $this->assertSame(
1134  array(
1135  'test' => <<<EOT
1136 <h2>A heading</h2>
1137 <ul> <li>a list</li> <li>may be a good example</li> </ul>
1138 EOT
1139  ,
1140  ),
1141  $this->parser->parse($yaml)
1142  );
1143  }
1144 
1146  {
1147  $yaml = <<<EOT
1148 test: >
1149  <h2>A heading</h2>
1150 
1151  <ul>
1152  <li>a list</li>
1153  <li>may be a good example</li>
1154  </ul>
1155 EOT;
1156 
1157  $this->assertSame(
1158  array(
1159  'test' => <<<EOT
1160 <h2>A heading</h2>
1161 <ul>
1162  <li>a list</li>
1163  <li>may be a good example</li>
1164 </ul>
1165 EOT
1166  ,
1167  ),
1168  $this->parser->parse($yaml)
1169  );
1170  }
1171 
1175  public function testParseBinaryData($data)
1176  {
1177  $this->assertSame(array('data' => 'Hello world'), $this->parser->parse($data));
1178  }
1179 
1180  public function getBinaryData()
1181  {
1182  return array(
1183  'enclosed with double quotes' => array('data: !!binary "SGVsbG8gd29ybGQ="'),
1184  'enclosed with single quotes' => array("data: !!binary 'SGVsbG8gd29ybGQ='"),
1185  'containing spaces' => array('data: !!binary "SGVs bG8gd 29ybGQ="'),
1186  'in block scalar' => array(
1187  <<<EOT
1188 data: !!binary |
1189  SGVsbG8gd29ybGQ=
1190 EOT
1191  ),
1192  'containing spaces in block scalar' => array(
1193  <<<EOT
1194 data: !!binary |
1195  SGVs bG8gd 29ybGQ=
1196 EOT
1197  ),
1198  );
1199  }
1200 
1204  public function testParseInvalidBinaryData($data, $expectedMessage)
1205  {
1206  $this->setExpectedExceptionRegExp('\Symfony\Component\Yaml\Exception\ParseException', $expectedMessage);
1207 
1208  $this->parser->parse($data);
1209  }
1210 
1211  public function getInvalidBinaryData()
1212  {
1213  return array(
1214  'length not a multiple of four' => array('data: !!binary "SGVsbG8d29ybGQ="', '/The normalized base64 encoded data \(data without whitespace characters\) length must be a multiple of four \(\d+ bytes given\)/'),
1215  'invalid characters' => array('!!binary "SGVsbG8#d29ybGQ="', '/The base64 encoded data \(.*\) contains invalid characters/'),
1216  'too many equals characters' => array('data: !!binary "SGVsbG8gd29yb==="', '/The base64 encoded data \(.*\) contains invalid characters/'),
1217  'misplaced equals character' => array('data: !!binary "SGVsbG8gd29ybG=Q"', '/The base64 encoded data \(.*\) contains invalid characters/'),
1218  'length not a multiple of four in block scalar' => array(
1219  <<<EOT
1220 data: !!binary |
1221  SGVsbG8d29ybGQ=
1222 EOT
1223  ,
1224  '/The normalized base64 encoded data \(data without whitespace characters\) length must be a multiple of four \(\d+ bytes given\)/',
1225  ),
1226  'invalid characters in block scalar' => array(
1227  <<<EOT
1228 data: !!binary |
1229  SGVsbG8#d29ybGQ=
1230 EOT
1231  ,
1232  '/The base64 encoded data \(.*\) contains invalid characters/',
1233  ),
1234  'too many equals characters in block scalar' => array(
1235  <<<EOT
1236 data: !!binary |
1237  SGVsbG8gd29yb===
1238 EOT
1239  ,
1240  '/The base64 encoded data \(.*\) contains invalid characters/',
1241  ),
1242  'misplaced equals character in block scalar' => array(
1243  <<<EOT
1244 data: !!binary |
1245  SGVsbG8gd29ybG=Q
1246 EOT
1247  ,
1248  '/The base64 encoded data \(.*\) contains invalid characters/',
1249  ),
1250  );
1251  }
1252 
1254  {
1255  $yaml = <<<EOT
1256 date: 2002-12-14
1257 EOT;
1258  $expectedDate = new \DateTime();
1259  $expectedDate->setTimeZone(new \DateTimeZone('UTC'));
1260  $expectedDate->setDate(2002, 12, 14);
1261  $expectedDate->setTime(0, 0, 0);
1262 
1263  $this->assertEquals(array('date' => $expectedDate), $this->parser->parse($yaml, Yaml::PARSE_DATETIME));
1264  }
1265 
1271  public function testParserThrowsExceptionWithCorrectLineNumber($lineNumber, $yaml)
1272  {
1273  $this->setExpectedException(
1274  '\Symfony\Component\Yaml\Exception\ParseException',
1275  sprintf('Unexpected characters near "," at line %d (near "bar: "123",").', $lineNumber)
1276  );
1277 
1278  $this->parser->parse($yaml);
1279  }
1280 
1282  {
1283  return array(
1284  array(
1285  4,
1286  <<<YAML
1287 foo:
1288  -
1289  # bar
1290  bar: "123",
1291 YAML
1292  ),
1293  array(
1294  5,
1295  <<<YAML
1296 foo:
1297  -
1298  # bar
1299  # bar
1300  bar: "123",
1301 YAML
1302  ),
1303  array(
1304  8,
1305  <<<YAML
1306 foo:
1307  -
1308  # foobar
1309  baz: 123
1310 bar:
1311  -
1312  # bar
1313  bar: "123",
1314 YAML
1315  ),
1316  array(
1317  10,
1318  <<<YAML
1319 foo:
1320  -
1321  # foobar
1322  # foobar
1323  baz: 123
1324 bar:
1325  -
1326  # bar
1327  # bar
1328  bar: "123",
1329 YAML
1330  ),
1331  );
1332  }
1333 }
1334 
1335 class B
1336 {
1337  public $b = 'foo';
1338 }
$files
Definition: add-vimline.php:18
testBlockLiteralWithLeadingNewlines()
Regression test for issue #7989.
Definition: ParserTest.php:406
Add some data
testParseBinaryData($data)
getBinaryData
$path
Definition: aliased.php:25
const PARSE_EXCEPTION_ON_INVALID_TYPE
Definition: Yaml.php:24
testObjectForMapEnabledWithMappingUsingBooleanToggles($yaml, $expected)
legacy getObjectForMapTests
Definition: ParserTest.php:475
testColonInMappingValueException()
A colon cannot be used in an unquoted mapping value
Definition: ParserTest.php:968
testObjectsSupportDisabledWithExceptions($yaml)
invalidDumpedObjectProvider
Definition: ParserTest.php:547
testMultipleDocumentsNotSupportedException()
/^Multiple documents are not supported.
Definition: ParserTest.php:638
testObjectSupportDisabledButNoExceptions($input)
invalidDumpedObjectProvider
Definition: ParserTest.php:458
testParseInvalidBinaryData($data, $expectedMessage)
getInvalidBinaryData
testMappingDuplicateKeyBlock()
It is an error for two equal keys to appear in the same mapping node.
Definition: ParserTest.php:753
Set header footer
Definition: 04printing.php:58
const EOL
static parse($input, $flags=0)
Parses YAML into a PHP value.
Definition: Yaml.php:48
comment()
Definition: comment.php:2
date( 'd-M-Y', $objPHPExcel->getProperties() ->getCreated())
e($cmd)
Definition: flush.php:14
testObjectsSupportDisabledWithExceptionsUsingBooleanToggles($yaml)
legacy invalidDumpedObjectProvider
Definition: ParserTest.php:557
testParserThrowsExceptionWithCorrectLineNumber($lineNumber, $yaml)
Add a drawing to the header
Definition: 04printing.php:69
EOD
Definition: example_053.php:93
Parser parses YAML strings to convert them to PHP arrays.
Definition: Parser.php:21
$comment
Definition: buildRTE.php:83
Create styles array
The data for the language used.
Add data(end) s
Create new PHPExcel object
obj_idprivate
testSpecifications($file, $expected, $yaml, $comment)
getDataFormSpecifications
Definition: ParserTest.php:34
if(!file_exists("$old.txt")) if($old===$new) if(file_exists("$new.txt")) $file
testBlockChomping($expected, $yaml)
getBlockChompingTests
Definition: ParserTest.php:396
testObjectForMap($yaml, $expected)
getObjectForMapTests
Definition: ParserTest.php:466
$test
Definition: Utf8Test.php:84
testCommentLikeStringsAreNotStrippedInBlockScalars($yaml, $expectedParserResult)
getCommentLikeStringInScalarBlockData
Definition: ParserTest.php:990
const EOF
How fgetc() reports an End Of File.
Definition: JSMin_lib.php:92