Skip to content

Commit 5d2b2a5

Browse files
OskarStarknicolas-grekas
authored andcommitted
[Tests] Streamline
1 parent 2c98dad commit 5d2b2a5

File tree

3 files changed

+60
-49
lines changed

3 files changed

+60
-49
lines changed

Tests/Command/LintCommandTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,12 @@ public function testLintWithExclude()
143143

144144
public function testLintFileNotReadable()
145145
{
146-
$this->expectException(\RuntimeException::class);
147146
$tester = $this->createCommandTester();
148147
$filename = $this->createFile('');
149148
unlink($filename);
150149

150+
$this->expectException(\RuntimeException::class);
151+
151152
$tester->execute(['filename' => $filename], ['decorated' => false]);
152153
}
153154

Tests/InlineTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -827,20 +827,20 @@ public function testTheEmptyStringIsAValidMappingKey()
827827
/**
828828
* @dataProvider getNotPhpCompatibleMappingKeyData
829829
*/
830-
public function testImplicitStringCastingOfMappingKeysThrows($yaml, $expected)
830+
public function testImplicitStringCastingOfMappingKeysThrowsException(string $yaml)
831831
{
832832
$this->expectException(ParseException::class);
833833
$this->expectExceptionMessage('Implicit casting of incompatible mapping keys to strings is not supported. Quote your evaluable mapping keys instead');
834-
$this->assertSame($expected, Inline::parse($yaml));
834+
Inline::parse($yaml);
835835
}
836836

837837
public static function getNotPhpCompatibleMappingKeyData()
838838
{
839839
return [
840-
'boolean-true' => ['{true: "foo"}', ['true' => 'foo']],
841-
'boolean-false' => ['{false: "foo"}', ['false' => 'foo']],
842-
'null' => ['{null: "foo"}', ['null' => 'foo']],
843-
'float' => ['{0.25: "foo"}', ['0.25' => 'foo']],
840+
'boolean-true' => ['{true: "foo"}'],
841+
'boolean-false' => ['{false: "foo"}'],
842+
'null' => ['{null: "foo"}'],
843+
'float' => ['{0.25: "foo"}'],
844844
];
845845
}
846846

Tests/ParserTest.php

Lines changed: 52 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -650,25 +650,27 @@ public static function getObjectForMapTests()
650650

651651
public function testObjectsSupportDisabledWithExceptions()
652652
{
653-
$this->expectException(ParseException::class);
654653
$yaml = <<<'EOF'
655654
foo: !php/object:O:30:"Symfony\Tests\Component\Yaml\B":1:{s:1:"b";s:3:"foo";}
656655
bar: 1
657656
EOF;
658657

658+
$this->expectException(ParseException::class);
659+
659660
$this->parser->parse($yaml, Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE);
660661
}
661662

662663
public function testMappingKeyInMultiLineStringThrowsException()
663664
{
664-
$this->expectException(ParseException::class);
665-
$this->expectExceptionMessage('Mapping values are not allowed in multi-line blocks at line 2 (near "dbal:wrong").');
666-
667665
$yaml = <<<'EOF'
668666
data:
669667
dbal:wrong
670668
default_connection: monolith
671669
EOF;
670+
671+
$this->expectException(ParseException::class);
672+
$this->expectExceptionMessage('Mapping values are not allowed in multi-line blocks at line 2 (near "dbal:wrong").');
673+
672674
$this->parser->parse($yaml);
673675
}
674676

@@ -707,7 +709,6 @@ public function testNonUtf8Exception()
707709

708710
public function testUnindentedCollectionException()
709711
{
710-
$this->expectException(ParseException::class);
711712
$yaml = <<<'EOF'
712713
713714
collection:
@@ -717,12 +718,13 @@ public function testUnindentedCollectionException()
717718

718719
EOF;
719720

721+
$this->expectException(ParseException::class);
722+
720723
$this->parser->parse($yaml);
721724
}
722725

723726
public function testShortcutKeyUnindentedCollectionException()
724727
{
725-
$this->expectException(ParseException::class);
726728
$yaml = <<<'EOF'
727729
728730
collection:
@@ -731,6 +733,8 @@ public function testShortcutKeyUnindentedCollectionException()
731733

732734
EOF;
733735

736+
$this->expectException(ParseException::class);
737+
734738
$this->parser->parse($yaml);
735739
}
736740

@@ -929,8 +933,6 @@ public function testScalarInSequence()
929933
*/
930934
public function testMappingDuplicateKeyBlock()
931935
{
932-
$this->expectException(ParseException::class);
933-
$this->expectExceptionMessage('Duplicate key "child" detected');
934936
$input = <<<'EOD'
935937
parent:
936938
child: first
@@ -939,28 +941,24 @@ public function testMappingDuplicateKeyBlock()
939941
child: duplicate
940942
child: duplicate
941943
EOD;
942-
$expected = [
943-
'parent' => [
944-
'child' => 'first',
945-
],
946-
];
947-
$this->assertSame($expected, Yaml::parse($input));
944+
945+
$this->expectException(ParseException::class);
946+
$this->expectExceptionMessage('Duplicate key "child" detected');
947+
948+
Yaml::parse($input);
948949
}
949950

950951
public function testMappingDuplicateKeyFlow()
951952
{
952-
$this->expectException(ParseException::class);
953-
$this->expectExceptionMessage('Duplicate key "child" detected');
954953
$input = <<<'EOD'
955954
parent: { child: first, child: duplicate }
956955
parent: { child: duplicate, child: duplicate }
957956
EOD;
958-
$expected = [
959-
'parent' => [
960-
'child' => 'first',
961-
],
962-
];
963-
$this->assertSame($expected, Yaml::parse($input));
957+
958+
$this->expectException(ParseException::class);
959+
$this->expectExceptionMessage('Duplicate key "child" detected');
960+
961+
Yaml::parse($input);
964962
}
965963

966964
/**
@@ -1202,26 +1200,28 @@ public function testYamlDirective()
12021200

12031201
public function testFloatKeys()
12041202
{
1205-
$this->expectException(ParseException::class);
1206-
$this->expectExceptionMessage('Numeric keys are not supported. Quote your evaluable mapping keys instead');
12071203
$yaml = <<<'EOF'
12081204
foo:
12091205
1.2: "bar"
12101206
1.3: "baz"
12111207
EOF;
12121208

1209+
$this->expectException(ParseException::class);
1210+
$this->expectExceptionMessage('Numeric keys are not supported. Quote your evaluable mapping keys instead');
1211+
12131212
$this->parser->parse($yaml);
12141213
}
12151214

12161215
public function testBooleanKeys()
12171216
{
1218-
$this->expectException(ParseException::class);
1219-
$this->expectExceptionMessage('Non-string keys are not supported. Quote your evaluable mapping keys instead');
12201217
$yaml = <<<'EOF'
12211218
true: foo
12221219
false: bar
12231220
EOF;
12241221

1222+
$this->expectException(ParseException::class);
1223+
$this->expectExceptionMessage('Non-string keys are not supported. Quote your evaluable mapping keys instead');
1224+
12251225
$this->parser->parse($yaml);
12261226
}
12271227

@@ -1252,12 +1252,13 @@ public function testExplicitStringCasting()
12521252

12531253
public function testColonInMappingValueException()
12541254
{
1255-
$this->expectException(ParseException::class);
1256-
$this->expectExceptionMessage('A colon cannot be used in an unquoted mapping value');
12571255
$yaml = <<<'EOF'
12581256
foo: bar: baz
12591257
EOF;
12601258

1259+
$this->expectException(ParseException::class);
1260+
$this->expectExceptionMessage('A colon cannot be used in an unquoted mapping value');
1261+
12611262
$this->parser->parse($yaml);
12621263
}
12631264

@@ -2347,54 +2348,58 @@ public function testExceptionWhenUsingUnsupportedBuiltInTags()
23472348

23482349
public function testComplexMappingThrowsParseException()
23492350
{
2350-
$this->expectException(ParseException::class);
2351-
$this->expectExceptionMessage('Complex mappings are not supported at line 1 (near "? "1"").');
23522351
$yaml = <<<YAML
23532352
? "1"
23542353
:
23552354
name: végétalien
23562355
YAML;
23572356

2357+
$this->expectException(ParseException::class);
2358+
$this->expectExceptionMessage('Complex mappings are not supported at line 1 (near "? "1"").');
2359+
23582360
$this->parser->parse($yaml);
23592361
}
23602362

23612363
public function testComplexMappingNestedInMappingThrowsParseException()
23622364
{
2363-
$this->expectException(ParseException::class);
2364-
$this->expectExceptionMessage('Complex mappings are not supported at line 2 (near "? "1"").');
23652365
$yaml = <<<YAML
23662366
diet:
23672367
? "1"
23682368
:
23692369
name: végétalien
23702370
YAML;
23712371

2372+
$this->expectException(ParseException::class);
2373+
$this->expectExceptionMessage('Complex mappings are not supported at line 2 (near "? "1"").');
2374+
23722375
$this->parser->parse($yaml);
23732376
}
23742377

23752378
public function testComplexMappingNestedInSequenceThrowsParseException()
23762379
{
2377-
$this->expectException(ParseException::class);
2378-
$this->expectExceptionMessage('Complex mappings are not supported at line 1 (near "- ? "1"").');
23792380
$yaml = <<<YAML
23802381
- ? "1"
23812382
:
23822383
name: végétalien
23832384
YAML;
23842385

2386+
$this->expectException(ParseException::class);
2387+
$this->expectExceptionMessage('Complex mappings are not supported at line 1 (near "- ? "1"").');
2388+
23852389
$this->parser->parse($yaml);
23862390
}
23872391

23882392
public function testParsingIniThrowsException()
23892393
{
2390-
$this->expectException(ParseException::class);
2391-
$this->expectExceptionMessage('Unable to parse at line 2 (near " foo = bar").');
23922394
$ini = <<<INI
23932395
[parameters]
23942396
foo = bar
23952397
bar = %foo%
23962398
INI;
23972399

2400+
$this->expectException(ParseException::class);
2401+
$this->expectExceptionMessage('Unable to parse at line 2 (near " foo = bar").');
2402+
23982403
$this->parser->parse($ini);
23992404
}
24002405

@@ -2440,8 +2445,6 @@ public function testCanParseVeryLongValue()
24402445

24412446
public function testParserCleansUpReferencesBetweenRuns()
24422447
{
2443-
$this->expectException(ParseException::class);
2444-
$this->expectExceptionMessage('Reference "foo" does not exist at line 2');
24452448
$yaml = <<<YAML
24462449
foo: &foo
24472450
baz: foobar
@@ -2454,6 +2457,10 @@ public function testParserCleansUpReferencesBetweenRuns()
24542457
bar:
24552458
<<: *foo
24562459
YAML;
2460+
2461+
$this->expectException(ParseException::class);
2462+
$this->expectExceptionMessage('Reference "foo" does not exist at line 2');
2463+
24572464
$this->parser->parse($yaml);
24582465
}
24592466

@@ -2574,8 +2581,6 @@ public function testParsingNonExistentFilesThrowsException()
25742581

25752582
public function testParsingNotReadableFilesThrowsException()
25762583
{
2577-
$this->expectException(ParseException::class);
2578-
$this->expectExceptionMessageMatches('#^File ".+/Fixtures/not_readable.yml" cannot be read\.$#');
25792584
if ('\\' === \DIRECTORY_SEPARATOR) {
25802585
$this->markTestSkipped('chmod is not supported on Windows');
25812586
}
@@ -2587,6 +2592,9 @@ public function testParsingNotReadableFilesThrowsException()
25872592
$file = __DIR__.'/Fixtures/not_readable.yml';
25882593
chmod($file, 0200);
25892594

2595+
$this->expectException(ParseException::class);
2596+
$this->expectExceptionMessageMatches('#^File ".+/Fixtures/not_readable.yml" cannot be read\.$#');
2597+
25902598
$this->parser->parseFile($file);
25912599
}
25922600

@@ -2648,11 +2656,13 @@ public function testParseReferencesOnMergeKeysWithMappingsParsedAsObjects()
26482656

26492657
public function testEvalRefException()
26502658
{
2651-
$this->expectException(ParseException::class);
2652-
$this->expectExceptionMessage('Reference "foo" does not exist');
26532659
$yaml = <<<EOE
26542660
foo: { &foo { a: Steve, <<: *foo} }
26552661
EOE;
2662+
2663+
$this->expectException(ParseException::class);
2664+
$this->expectExceptionMessage('Reference "foo" does not exist');
2665+
26562666
$this->parser->parse($yaml);
26572667
}
26582668

0 commit comments

Comments
 (0)