Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like ParserTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ParserTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class ParserTest extends \PHPUnit_Framework_TestCase |
||
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 | |||
31 | /** |
||
32 | * @dataProvider getDataFormSpecifications |
||
33 | */ |
||
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 | |||
393 | /** |
||
394 | * @dataProvider getBlockChompingTests |
||
395 | */ |
||
396 | public function testBlockChomping($expected, $yaml) |
||
397 | { |
||
398 | $this->assertSame($expected, $this->parser->parse($yaml)); |
||
399 | } |
||
400 | |||
401 | /** |
||
402 | * Regression test for issue #7989. |
||
403 | * |
||
404 | * @see https://github.com/symfony/symfony/issues/7989 |
||
405 | */ |
||
406 | public function testBlockLiteralWithLeadingNewlines() |
||
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, false, true), '->parse() is able to parse objects'); |
||
429 | |||
430 | $input = <<<EOF |
||
431 | foo: !php/object:O:30:"Symfony\Component\Yaml\Tests\B":1:{s:1:"b";s:3:"foo";} |
||
432 | bar: 1 |
||
433 | EOF; |
||
434 | $this->assertEquals(array('foo' => new B(), 'bar' => 1), $this->parser->parse($input, false, true), '->parse() is able to parse objects'); |
||
435 | } |
||
436 | |||
437 | /** |
||
438 | * @dataProvider invalidDumpedObjectProvider |
||
439 | */ |
||
440 | public function testObjectSupportDisabledButNoExceptions($input) |
||
441 | { |
||
442 | $this->assertEquals(array('foo' => null, 'bar' => 1), $this->parser->parse($input), '->parse() does not parse objects'); |
||
443 | } |
||
444 | |||
445 | public function testObjectForMapEnabledWithMapping() |
||
446 | { |
||
447 | $yaml = <<<EOF |
||
448 | foo: |
||
449 | fiz: [cat] |
||
450 | EOF; |
||
451 | $result = $this->parser->parse($yaml, false, false, true); |
||
452 | |||
453 | $this->assertInstanceOf('stdClass', $result); |
||
454 | $this->assertInstanceOf('stdClass', $result->foo); |
||
455 | $this->assertEquals(array('cat'), $result->foo->fiz); |
||
456 | } |
||
457 | |||
458 | public function testObjectForMapEnabledWithInlineMapping() |
||
459 | { |
||
460 | $result = $this->parser->parse('{ "foo": "bar", "fiz": "cat" }', false, false, true); |
||
461 | |||
462 | $this->assertInstanceOf('stdClass', $result); |
||
463 | $this->assertEquals('bar', $result->foo); |
||
464 | $this->assertEquals('cat', $result->fiz); |
||
465 | } |
||
466 | |||
467 | public function testObjectForMapIsAppliedAfterParsing() |
||
468 | { |
||
469 | $expected = new \stdClass(); |
||
470 | $expected->foo = 'bar'; |
||
471 | $expected->baz = 'foobar'; |
||
472 | |||
473 | $this->assertEquals($expected, $this->parser->parse("foo: bar\nbaz: foobar", false, false, true)); |
||
474 | } |
||
475 | |||
476 | /** |
||
477 | * @dataProvider invalidDumpedObjectProvider |
||
478 | * @expectedException \Symfony\Component\Yaml\Exception\ParseException |
||
479 | */ |
||
480 | public function testObjectsSupportDisabledWithExceptions($yaml) |
||
481 | { |
||
482 | $this->parser->parse($yaml, true, false); |
||
483 | } |
||
484 | |||
485 | public function invalidDumpedObjectProvider() |
||
486 | { |
||
487 | $yamlTag = <<<EOF |
||
488 | foo: !!php/object:O:30:"Symfony\Tests\Component\Yaml\B":1:{s:1:"b";s:3:"foo";} |
||
489 | bar: 1 |
||
490 | EOF; |
||
491 | $localTag = <<<EOF |
||
492 | foo: !php/object:O:30:"Symfony\Tests\Component\Yaml\B":1:{s:1:"b";s:3:"foo";} |
||
493 | bar: 1 |
||
494 | EOF; |
||
495 | |||
496 | return array( |
||
497 | 'yaml-tag' => array($yamlTag), |
||
498 | 'local-tag' => array($localTag), |
||
499 | ); |
||
500 | } |
||
501 | |||
502 | /** |
||
503 | * @requires extension iconv |
||
504 | */ |
||
505 | public function testNonUtf8Exception() |
||
506 | { |
||
507 | $yamls = array( |
||
508 | iconv('UTF-8', 'ISO-8859-1', "foo: 'äöüß'"), |
||
509 | iconv('UTF-8', 'ISO-8859-15', "euro: '€'"), |
||
510 | iconv('UTF-8', 'CP1252', "cp1252: '©ÉÇáñ'"), |
||
511 | ); |
||
512 | |||
513 | foreach ($yamls as $yaml) { |
||
514 | try { |
||
515 | $this->parser->parse($yaml); |
||
516 | |||
517 | $this->fail('charsets other than UTF-8 are rejected.'); |
||
518 | } catch (\Exception $e) { |
||
519 | $this->assertInstanceOf('Symfony\Component\Yaml\Exception\ParseException', $e, 'charsets other than UTF-8 are rejected.'); |
||
520 | } |
||
521 | } |
||
522 | } |
||
523 | |||
524 | /** |
||
525 | * @expectedException \Symfony\Component\Yaml\Exception\ParseException |
||
526 | */ |
||
527 | public function testUnindentedCollectionException() |
||
528 | { |
||
529 | $yaml = <<<'EOF' |
||
530 | |||
531 | collection: |
||
532 | -item1 |
||
533 | -item2 |
||
534 | -item3 |
||
535 | |||
536 | EOF; |
||
537 | |||
538 | $this->parser->parse($yaml); |
||
539 | } |
||
540 | |||
541 | /** |
||
542 | * @expectedException \Symfony\Component\Yaml\Exception\ParseException |
||
543 | */ |
||
544 | public function testShortcutKeyUnindentedCollectionException() |
||
545 | { |
||
546 | $yaml = <<<'EOF' |
||
547 | |||
548 | collection: |
||
549 | - key: foo |
||
550 | foo: bar |
||
551 | |||
552 | EOF; |
||
553 | |||
554 | $this->parser->parse($yaml); |
||
555 | } |
||
556 | |||
557 | /** |
||
558 | * @expectedException \Symfony\Component\Yaml\Exception\ParseException |
||
559 | * @expectedExceptionMessage Multiple documents are not supported. |
||
560 | */ |
||
561 | public function testMultipleDocumentsNotSupportedException() |
||
562 | { |
||
563 | Yaml::parse(<<<'EOL' |
||
564 | # Ranking of 1998 home runs |
||
565 | --- |
||
566 | - Mark McGwire |
||
567 | - Sammy Sosa |
||
568 | - Ken Griffey |
||
569 | |||
570 | # Team ranking |
||
571 | --- |
||
572 | - Chicago Cubs |
||
573 | - St Louis Cardinals |
||
574 | EOL |
||
575 | ); |
||
576 | } |
||
577 | |||
578 | /** |
||
579 | * @expectedException \Symfony\Component\Yaml\Exception\ParseException |
||
580 | */ |
||
581 | public function testSequenceInAMapping() |
||
582 | { |
||
583 | Yaml::parse(<<<'EOF' |
||
584 | yaml: |
||
585 | hash: me |
||
586 | - array stuff |
||
587 | EOF |
||
588 | ); |
||
589 | } |
||
590 | |||
591 | /** |
||
592 | * @expectedException \Symfony\Component\Yaml\Exception\ParseException |
||
593 | */ |
||
594 | public function testMappingInASequence() |
||
595 | { |
||
596 | Yaml::parse(<<<'EOF' |
||
597 | yaml: |
||
598 | - array stuff |
||
599 | hash: me |
||
600 | EOF |
||
601 | ); |
||
602 | } |
||
603 | |||
604 | /** |
||
605 | * @expectedException \Symfony\Component\Yaml\Exception\ParseException |
||
606 | * @expectedExceptionMessage missing colon |
||
607 | */ |
||
608 | public function testScalarInSequence() |
||
609 | { |
||
610 | Yaml::parse(<<<EOF |
||
611 | foo: |
||
612 | - bar |
||
613 | "missing colon" |
||
614 | foo: bar |
||
615 | EOF |
||
616 | ); |
||
617 | } |
||
618 | |||
619 | /** |
||
620 | * > It is an error for two equal keys to appear in the same mapping node. |
||
621 | * > In such a case the YAML processor may continue, ignoring the second |
||
622 | * > `key: value` pair and issuing an appropriate warning. This strategy |
||
623 | * > preserves a consistent information model for one-pass and random access |
||
624 | * > applications. |
||
625 | * |
||
626 | * @see http://yaml.org/spec/1.2/spec.html#id2759572 |
||
627 | * @see http://yaml.org/spec/1.1/#id932806 |
||
628 | */ |
||
629 | public function testMappingDuplicateKeyBlock() |
||
630 | { |
||
631 | $input = <<<EOD |
||
632 | parent: |
||
633 | child: first |
||
634 | child: duplicate |
||
635 | parent: |
||
636 | child: duplicate |
||
637 | child: duplicate |
||
638 | EOD; |
||
639 | $expected = array( |
||
640 | 'parent' => array( |
||
641 | 'child' => 'first', |
||
642 | ), |
||
643 | ); |
||
644 | $this->assertSame($expected, Yaml::parse($input)); |
||
645 | } |
||
646 | |||
647 | public function testMappingDuplicateKeyFlow() |
||
648 | { |
||
649 | $input = <<<EOD |
||
650 | parent: { child: first, child: duplicate } |
||
651 | parent: { child: duplicate, child: duplicate } |
||
652 | EOD; |
||
653 | $expected = array( |
||
654 | 'parent' => array( |
||
655 | 'child' => 'first', |
||
656 | ), |
||
657 | ); |
||
658 | $this->assertSame($expected, Yaml::parse($input)); |
||
659 | } |
||
660 | |||
661 | public function testEmptyValue() |
||
662 | { |
||
663 | $input = <<<'EOF' |
||
664 | hash: |
||
665 | EOF; |
||
666 | |||
667 | $this->assertEquals(array('hash' => null), Yaml::parse($input)); |
||
668 | } |
||
669 | |||
670 | public function testCommentAtTheRootIndent() |
||
671 | { |
||
672 | $this->assertEquals(array( |
||
673 | 'services' => array( |
||
674 | 'app.foo_service' => array( |
||
675 | 'class' => 'Foo', |
||
676 | ), |
||
677 | 'app/bar_service' => array( |
||
678 | 'class' => 'Bar', |
||
679 | ), |
||
680 | ), |
||
681 | ), Yaml::parse(<<<'EOF' |
||
682 | # comment 1 |
||
683 | services: |
||
684 | # comment 2 |
||
685 | # comment 3 |
||
686 | app.foo_service: |
||
687 | class: Foo |
||
688 | # comment 4 |
||
689 | # comment 5 |
||
690 | app/bar_service: |
||
691 | class: Bar |
||
692 | EOF |
||
693 | )); |
||
694 | } |
||
695 | |||
696 | public function testStringBlockWithComments() |
||
697 | { |
||
698 | $this->assertEquals(array('content' => <<<'EOT' |
||
699 | # comment 1 |
||
700 | header |
||
701 | |||
702 | # comment 2 |
||
703 | <body> |
||
704 | <h1>title</h1> |
||
705 | </body> |
||
706 | |||
707 | footer # comment3 |
||
708 | EOT |
||
709 | ), Yaml::parse(<<<'EOF' |
||
710 | content: | |
||
711 | # comment 1 |
||
712 | header |
||
713 | |||
714 | # comment 2 |
||
715 | <body> |
||
716 | <h1>title</h1> |
||
717 | </body> |
||
718 | |||
719 | footer # comment3 |
||
720 | EOF |
||
721 | )); |
||
722 | } |
||
723 | |||
724 | public function testFoldedStringBlockWithComments() |
||
725 | { |
||
726 | $this->assertEquals(array(array('content' => <<<'EOT' |
||
727 | # comment 1 |
||
728 | header |
||
729 | |||
730 | # comment 2 |
||
731 | <body> |
||
732 | <h1>title</h1> |
||
733 | </body> |
||
734 | |||
735 | footer # comment3 |
||
736 | EOT |
||
737 | )), Yaml::parse(<<<'EOF' |
||
738 | - |
||
739 | content: | |
||
740 | # comment 1 |
||
741 | header |
||
742 | |||
743 | # comment 2 |
||
744 | <body> |
||
745 | <h1>title</h1> |
||
746 | </body> |
||
747 | |||
748 | footer # comment3 |
||
749 | EOF |
||
750 | )); |
||
751 | } |
||
752 | |||
753 | public function testNestedFoldedStringBlockWithComments() |
||
754 | { |
||
755 | $this->assertEquals(array(array( |
||
756 | 'title' => 'some title', |
||
757 | 'content' => <<<'EOT' |
||
758 | # comment 1 |
||
759 | header |
||
760 | |||
761 | # comment 2 |
||
762 | <body> |
||
763 | <h1>title</h1> |
||
764 | </body> |
||
765 | |||
766 | footer # comment3 |
||
767 | EOT |
||
768 | )), Yaml::parse(<<<'EOF' |
||
769 | - |
||
770 | title: some title |
||
771 | content: | |
||
772 | # comment 1 |
||
773 | header |
||
774 | |||
775 | # comment 2 |
||
776 | <body> |
||
777 | <h1>title</h1> |
||
778 | </body> |
||
779 | |||
780 | footer # comment3 |
||
781 | EOF |
||
782 | )); |
||
783 | } |
||
784 | |||
785 | public function testReferenceResolvingInInlineStrings() |
||
786 | { |
||
787 | $this->assertEquals(array( |
||
788 | 'var' => 'var-value', |
||
789 | 'scalar' => 'var-value', |
||
790 | 'list' => array('var-value'), |
||
791 | 'list_in_list' => array(array('var-value')), |
||
792 | 'map_in_list' => array(array('key' => 'var-value')), |
||
793 | 'embedded_mapping' => array(array('key' => 'var-value')), |
||
794 | 'map' => array('key' => 'var-value'), |
||
795 | 'list_in_map' => array('key' => array('var-value')), |
||
796 | 'map_in_map' => array('foo' => array('bar' => 'var-value')), |
||
797 | ), Yaml::parse(<<<'EOF' |
||
798 | var: &var var-value |
||
799 | scalar: *var |
||
800 | list: [ *var ] |
||
801 | list_in_list: [[ *var ]] |
||
802 | map_in_list: [ { key: *var } ] |
||
803 | embedded_mapping: [ key: *var ] |
||
804 | map: { key: *var } |
||
805 | list_in_map: { key: [*var] } |
||
806 | map_in_map: { foo: { bar: *var } } |
||
807 | EOF |
||
808 | )); |
||
809 | } |
||
810 | |||
811 | public function testYamlDirective() |
||
812 | { |
||
813 | $yaml = <<<'EOF' |
||
814 | %YAML 1.2 |
||
815 | --- |
||
816 | foo: 1 |
||
817 | bar: 2 |
||
818 | EOF; |
||
819 | $this->assertEquals(array('foo' => 1, 'bar' => 2), $this->parser->parse($yaml)); |
||
820 | } |
||
821 | |||
822 | public function testFloatKeys() |
||
823 | { |
||
824 | $yaml = <<<'EOF' |
||
825 | foo: |
||
826 | 1.2: "bar" |
||
827 | 1.3: "baz" |
||
828 | EOF; |
||
829 | |||
830 | $expected = array( |
||
831 | 'foo' => array( |
||
832 | '1.2' => 'bar', |
||
833 | '1.3' => 'baz', |
||
834 | ), |
||
835 | ); |
||
836 | |||
837 | $this->assertEquals($expected, $this->parser->parse($yaml)); |
||
838 | } |
||
839 | |||
840 | /** |
||
841 | * @expectedException \Symfony\Component\Yaml\Exception\ParseException |
||
842 | * @expectedExceptionMessage A colon cannot be used in an unquoted mapping value |
||
843 | */ |
||
844 | public function testColonInMappingValueException() |
||
845 | { |
||
846 | $yaml = <<<EOF |
||
847 | foo: bar: baz |
||
848 | EOF; |
||
849 | |||
850 | $this->parser->parse($yaml); |
||
851 | } |
||
852 | |||
853 | public function testColonInMappingValueExceptionNotTriggeredByColonInComment() |
||
854 | { |
||
855 | $yaml = <<<EOT |
||
856 | foo: |
||
857 | bar: foobar # Note: a comment after a colon |
||
858 | EOT; |
||
859 | |||
860 | $this->assertSame(array('foo' => array('bar' => 'foobar')), $this->parser->parse($yaml)); |
||
861 | } |
||
862 | |||
863 | /** |
||
864 | * @dataProvider getCommentLikeStringInScalarBlockData |
||
865 | */ |
||
866 | public function testCommentLikeStringsAreNotStrippedInBlockScalars($yaml, $expectedParserResult) |
||
867 | { |
||
868 | $this->assertSame($expectedParserResult, $this->parser->parse($yaml)); |
||
869 | } |
||
870 | |||
871 | public function getCommentLikeStringInScalarBlockData() |
||
872 | { |
||
873 | $tests = array(); |
||
874 | |||
875 | $yaml = <<<'EOT' |
||
876 | pages: |
||
877 | - |
||
878 | title: some title |
||
879 | content: | |
||
880 | # comment 1 |
||
881 | header |
||
882 | |||
883 | # comment 2 |
||
884 | <body> |
||
885 | <h1>title</h1> |
||
886 | </body> |
||
887 | |||
888 | footer # comment3 |
||
889 | EOT; |
||
890 | $expected = array( |
||
891 | 'pages' => array( |
||
892 | array( |
||
893 | 'title' => 'some title', |
||
894 | 'content' => <<<'EOT' |
||
895 | # comment 1 |
||
896 | header |
||
897 | |||
898 | # comment 2 |
||
899 | <body> |
||
900 | <h1>title</h1> |
||
901 | </body> |
||
902 | |||
903 | footer # comment3 |
||
904 | EOT |
||
905 | , |
||
906 | ), |
||
907 | ), |
||
908 | ); |
||
909 | $tests[] = array($yaml, $expected); |
||
910 | |||
911 | $yaml = <<<'EOT' |
||
912 | test: | |
||
913 | foo |
||
914 | # bar |
||
915 | baz |
||
916 | collection: |
||
917 | - one: | |
||
918 | foo |
||
919 | # bar |
||
920 | baz |
||
921 | - two: | |
||
922 | foo |
||
923 | # bar |
||
924 | baz |
||
925 | EOT; |
||
926 | $expected = array( |
||
927 | 'test' => <<<'EOT' |
||
928 | foo |
||
929 | # bar |
||
930 | baz |
||
931 | |||
932 | EOT |
||
933 | , |
||
934 | 'collection' => array( |
||
935 | array( |
||
936 | 'one' => <<<'EOT' |
||
937 | foo |
||
938 | # bar |
||
939 | baz |
||
940 | EOT |
||
941 | , |
||
942 | ), |
||
943 | array( |
||
944 | 'two' => <<<'EOT' |
||
945 | foo |
||
946 | # bar |
||
947 | baz |
||
948 | EOT |
||
949 | , |
||
950 | ), |
||
951 | ), |
||
952 | ); |
||
953 | $tests[] = array($yaml, $expected); |
||
954 | |||
955 | $yaml = <<<EOT |
||
956 | foo: |
||
957 | bar: |
||
958 | scalar-block: > |
||
959 | line1 |
||
960 | line2> |
||
961 | baz: |
||
962 | # comment |
||
963 | foobar: ~ |
||
964 | EOT; |
||
965 | $expected = array( |
||
966 | 'foo' => array( |
||
967 | 'bar' => array( |
||
968 | 'scalar-block' => 'line1 line2>', |
||
969 | ), |
||
970 | 'baz' => array( |
||
971 | 'foobar' => null, |
||
972 | ), |
||
973 | ), |
||
974 | ); |
||
975 | $tests[] = array($yaml, $expected); |
||
976 | |||
977 | $yaml = <<<'EOT' |
||
978 | a: |
||
979 | b: hello |
||
980 | # c: | |
||
981 | # first row |
||
982 | # second row |
||
983 | d: hello |
||
984 | EOT; |
||
985 | $expected = array( |
||
986 | 'a' => array( |
||
987 | 'b' => 'hello', |
||
988 | 'd' => 'hello', |
||
989 | ), |
||
990 | ); |
||
991 | $tests[] = array($yaml, $expected); |
||
992 | |||
993 | return $tests; |
||
994 | } |
||
995 | |||
996 | public function testBlankLinesAreParsedAsNewLinesInFoldedBlocks() |
||
997 | { |
||
998 | $yaml = <<<EOT |
||
999 | test: > |
||
1000 | <h2>A heading</h2> |
||
1001 | |||
1002 | <ul> |
||
1003 | <li>a list</li> |
||
1004 | <li>may be a good example</li> |
||
1005 | </ul> |
||
1006 | EOT; |
||
1007 | |||
1008 | $this->assertSame( |
||
1009 | array( |
||
1010 | 'test' => <<<EOT |
||
1011 | <h2>A heading</h2> |
||
1012 | <ul> <li>a list</li> <li>may be a good example</li> </ul> |
||
1013 | EOT |
||
1014 | , |
||
1015 | ), |
||
1016 | $this->parser->parse($yaml) |
||
1017 | ); |
||
1018 | } |
||
1019 | |||
1020 | public function testAdditionallyIndentedLinesAreParsedAsNewLinesInFoldedBlocks() |
||
1021 | { |
||
1022 | $yaml = <<<EOT |
||
1023 | test: > |
||
1024 | <h2>A heading</h2> |
||
1025 | |||
1026 | <ul> |
||
1027 | <li>a list</li> |
||
1028 | <li>may be a good example</li> |
||
1029 | </ul> |
||
1030 | EOT; |
||
1031 | |||
1032 | $this->assertSame( |
||
1033 | array( |
||
1034 | 'test' => <<<EOT |
||
1035 | <h2>A heading</h2> |
||
1036 | <ul> |
||
1037 | <li>a list</li> |
||
1038 | <li>may be a good example</li> |
||
1039 | </ul> |
||
1040 | EOT |
||
1041 | , |
||
1042 | ), |
||
1043 | $this->parser->parse($yaml) |
||
1044 | ); |
||
1045 | } |
||
1046 | } |
||
1047 | |||
1052 |