Skip to content

Commit 5dc8ad5

Browse files
committed
[Tests] Move expectException closer to the place of the expectation to avoid false positives
1 parent 117ae3d commit 5dc8ad5

16 files changed

+244
-162
lines changed

Tests/AliasTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,14 @@ public function testReturnsCorrectDeprecation()
7474
*/
7575
public function testCannotDeprecateWithAnInvalidTemplate($message)
7676
{
77-
$this->expectException(InvalidArgumentException::class);
7877
$def = new Alias('foo');
78+
79+
$this->expectException(InvalidArgumentException::class);
80+
7981
$def->setDeprecated('package', '1.1', $message);
8082
}
8183

82-
public static function invalidDeprecationMessageProvider()
84+
public static function invalidDeprecationMessageProvider(): array
8385
{
8486
return [
8587
"With \rs" => ["invalid \r message %alias_id%"],

Tests/ChildDefinitionTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,10 @@ public function testSetArgument()
9191

9292
public function testReplaceArgumentShouldRequireIntegerIndex()
9393
{
94-
$this->expectException(\InvalidArgumentException::class);
9594
$def = new ChildDefinition('foo');
9695

96+
$this->expectException(\InvalidArgumentException::class);
97+
9798
$def->replaceArgument('0', 'foo');
9899
}
99100

@@ -118,12 +119,13 @@ public function testReplaceArgument()
118119

119120
public function testGetArgumentShouldCheckBounds()
120121
{
121-
$this->expectException(\OutOfBoundsException::class);
122122
$def = new ChildDefinition('foo');
123123

124124
$def->setArguments([0 => 'foo']);
125125
$def->replaceArgument(0, 'foo');
126126

127+
$this->expectException(\OutOfBoundsException::class);
128+
127129
$def->getArgument(1);
128130
}
129131

Tests/Compiler/AbstractRecursivePassTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,12 @@ protected function processValue($value, $isRoot = false): mixed
107107

108108
public function testGetConstructorDefinitionNoClass()
109109
{
110-
$this->expectException(RuntimeException::class);
111-
$this->expectExceptionMessage('Invalid service "foo": the class is not set.');
112-
113110
$container = new ContainerBuilder();
114111
$container->register('foo');
115112

113+
$this->expectException(RuntimeException::class);
114+
$this->expectExceptionMessage('Invalid service "foo": the class is not set.');
115+
116116
(new class() extends AbstractRecursivePass {
117117
protected function processValue($value, $isRoot = false): mixed
118118
{

Tests/Compiler/AliasDeprecatedPublicServicesPassTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ public function testProcess()
4747
*/
4848
public function testProcessWithMissingAttribute(string $attribute, array $attributes)
4949
{
50-
$this->expectException(InvalidArgumentException::class);
51-
$this->expectExceptionMessage(sprintf('The "%s" attribute is mandatory for the "container.private" tag on the "foo" service.', $attribute));
52-
5350
$container = new ContainerBuilder();
5451
$container
5552
->register('foo')
5653
->addTag('container.private', $attributes);
5754

55+
$this->expectException(InvalidArgumentException::class);
56+
$this->expectExceptionMessage(sprintf('The "%s" attribute is mandatory for the "container.private" tag on the "foo" service.', $attribute));
57+
5858
(new AliasDeprecatedPublicServicesPass())->process($container);
5959
}
6060

Tests/Compiler/AutoAliasServicePassTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,30 @@ class AutoAliasServicePassTest extends TestCase
2121
{
2222
public function testProcessWithMissingParameter()
2323
{
24-
$this->expectException(ParameterNotFoundException::class);
2524
$container = new ContainerBuilder();
2625

2726
$container->register('example')
2827
->addTag('auto_alias', ['format' => '%non_existing%.example']);
2928

3029
$pass = new AutoAliasServicePass();
30+
31+
$this->expectException(ParameterNotFoundException::class);
32+
3133
$pass->process($container);
3234
}
3335

3436
public function testProcessWithMissingFormat()
3537
{
36-
$this->expectException(InvalidArgumentException::class);
3738
$container = new ContainerBuilder();
3839

3940
$container->register('example')
4041
->addTag('auto_alias', []);
4142
$container->setParameter('existing', 'mysql');
4243

4344
$pass = new AutoAliasServicePass();
45+
46+
$this->expectException(InvalidArgumentException::class);
47+
4448
$pass->process($container);
4549
}
4650

Tests/Compiler/AutowirePassTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -609,13 +609,14 @@ public function testScalarArgsCannotBeAutowired()
609609

610610
public function testUnionScalarArgsCannotBeAutowired()
611611
{
612-
$this->expectException(AutowiringFailedException::class);
613-
$this->expectExceptionMessage('Cannot autowire service "union_scalars": argument "$timeout" of method "Symfony\Component\DependencyInjection\Tests\Compiler\UnionScalars::__construct()" is type-hinted "float|int", you should configure its value explicitly.');
614612
$container = new ContainerBuilder();
615613

616614
$container->register('union_scalars', UnionScalars::class)
617615
->setAutowired(true);
618616

617+
$this->expectException(AutowiringFailedException::class);
618+
$this->expectExceptionMessage('Cannot autowire service "union_scalars": argument "$timeout" of method "Symfony\Component\DependencyInjection\Tests\Compiler\UnionScalars::__construct()" is type-hinted "float|int", you should configure its value explicitly.');
619+
619620
(new AutowirePass())->process($container);
620621
}
621622

Tests/Compiler/CheckCircularReferencesPassTest.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,29 @@ class CheckCircularReferencesPassTest extends TestCase
2424
{
2525
public function testProcess()
2626
{
27-
$this->expectException(ServiceCircularReferenceException::class);
2827
$container = new ContainerBuilder();
2928
$container->register('a')->addArgument(new Reference('b'));
3029
$container->register('b')->addArgument(new Reference('a'));
3130

31+
$this->expectException(ServiceCircularReferenceException::class);
32+
3233
$this->process($container);
3334
}
3435

3536
public function testProcessWithAliases()
3637
{
37-
$this->expectException(ServiceCircularReferenceException::class);
3838
$container = new ContainerBuilder();
3939
$container->register('a')->addArgument(new Reference('b'));
4040
$container->setAlias('b', 'c');
4141
$container->setAlias('c', 'a');
4242

43+
$this->expectException(ServiceCircularReferenceException::class);
44+
4345
$this->process($container);
4446
}
4547

4648
public function testProcessWithFactory()
4749
{
48-
$this->expectException(ServiceCircularReferenceException::class);
4950
$container = new ContainerBuilder();
5051

5152
$container
@@ -56,23 +57,25 @@ public function testProcessWithFactory()
5657
->register('b', 'stdClass')
5758
->setFactory([new Reference('a'), 'getInstance']);
5859

60+
$this->expectException(ServiceCircularReferenceException::class);
61+
5962
$this->process($container);
6063
}
6164

6265
public function testProcessDetectsIndirectCircularReference()
6366
{
64-
$this->expectException(ServiceCircularReferenceException::class);
6567
$container = new ContainerBuilder();
6668
$container->register('a')->addArgument(new Reference('b'));
6769
$container->register('b')->addArgument(new Reference('c'));
6870
$container->register('c')->addArgument(new Reference('a'));
6971

72+
$this->expectException(ServiceCircularReferenceException::class);
73+
7074
$this->process($container);
7175
}
7276

7377
public function testProcessDetectsIndirectCircularReferenceWithFactory()
7478
{
75-
$this->expectException(ServiceCircularReferenceException::class);
7679
$container = new ContainerBuilder();
7780

7881
$container->register('a')->addArgument(new Reference('b'));
@@ -83,17 +86,20 @@ public function testProcessDetectsIndirectCircularReferenceWithFactory()
8386

8487
$container->register('c')->addArgument(new Reference('a'));
8588

89+
$this->expectException(ServiceCircularReferenceException::class);
90+
8691
$this->process($container);
8792
}
8893

8994
public function testDeepCircularReference()
9095
{
91-
$this->expectException(ServiceCircularReferenceException::class);
9296
$container = new ContainerBuilder();
9397
$container->register('a')->addArgument(new Reference('b'));
9498
$container->register('b')->addArgument(new Reference('c'));
9599
$container->register('c')->addArgument(new Reference('b'));
96100

101+
$this->expectException(ServiceCircularReferenceException::class);
102+
97103
$this->process($container);
98104
}
99105

Tests/Compiler/CheckDefinitionValidityPassTest.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,21 @@ class CheckDefinitionValidityPassTest extends TestCase
2121
{
2222
public function testProcessDetectsSyntheticNonPublicDefinitions()
2323
{
24-
$this->expectException(RuntimeException::class);
2524
$container = new ContainerBuilder();
2625
$container->register('a')->setSynthetic(true)->setPublic(false);
2726

27+
$this->expectException(RuntimeException::class);
28+
2829
$this->process($container);
2930
}
3031

3132
public function testProcessDetectsNonSyntheticNonAbstractDefinitionWithoutClass()
3233
{
33-
$this->expectException(RuntimeException::class);
3434
$container = new ContainerBuilder();
3535
$container->register('a')->setSynthetic(false)->setAbstract(false);
3636

37+
$this->expectException(RuntimeException::class);
38+
3739
$this->process($container);
3840
}
3941

@@ -92,10 +94,12 @@ public function testValidTags()
9294
*/
9395
public function testInvalidTags(string $name, array $attributes, string $message)
9496
{
95-
$this->expectException(RuntimeException::class);
9697
$this->expectExceptionMessage($message);
9798
$container = new ContainerBuilder();
9899
$container->register('a', 'class')->addTag($name, $attributes);
100+
101+
$this->expectException(RuntimeException::class);
102+
99103
$this->process($container);
100104
}
101105

@@ -121,21 +125,23 @@ public static function provideInvalidTags(): iterable
121125

122126
public function testDynamicPublicServiceName()
123127
{
124-
$this->expectException(EnvParameterException::class);
125128
$container = new ContainerBuilder();
126129
$env = $container->getParameterBag()->get('env(BAR)');
127130
$container->register("foo.$env", 'class')->setPublic(true);
128131

132+
$this->expectException(EnvParameterException::class);
133+
129134
$this->process($container);
130135
}
131136

132137
public function testDynamicPublicAliasName()
133138
{
134-
$this->expectException(EnvParameterException::class);
135139
$container = new ContainerBuilder();
136140
$env = $container->getParameterBag()->get('env(BAR)');
137141
$container->setAlias("foo.$env", 'class')->setPublic(true);
138142

143+
$this->expectException(EnvParameterException::class);
144+
139145
$this->process($container);
140146
}
141147

Tests/Compiler/CheckExceptionOnInvalidReferenceBehaviorPassTest.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,20 @@ public function testProcess()
4141

4242
public function testProcessThrowsExceptionOnInvalidReference()
4343
{
44-
$this->expectException(ServiceNotFoundException::class);
4544
$container = new ContainerBuilder();
4645

4746
$container
4847
->register('a', '\stdClass')
4948
->addArgument(new Reference('b'))
5049
;
5150

51+
$this->expectException(ServiceNotFoundException::class);
52+
5253
$this->process($container);
5354
}
5455

5556
public function testProcessThrowsExceptionOnInvalidReferenceFromInlinedDefinition()
5657
{
57-
$this->expectException(ServiceNotFoundException::class);
5858
$container = new ContainerBuilder();
5959

6060
$def = new Definition();
@@ -65,6 +65,8 @@ public function testProcessThrowsExceptionOnInvalidReferenceFromInlinedDefinitio
6565
->addArgument($def)
6666
;
6767

68+
$this->expectException(ServiceNotFoundException::class);
69+
6870
$this->process($container);
6971
}
7072

@@ -84,34 +86,36 @@ public function testProcessDefinitionWithBindings()
8486

8587
public function testWithErroredServiceLocator()
8688
{
87-
$this->expectException(ServiceNotFoundException::class);
88-
$this->expectExceptionMessage('The service "foo" in the container provided to "bar" has a dependency on a non-existent service "baz".');
8989
$container = new ContainerBuilder();
9090

9191
ServiceLocatorTagPass::register($container, ['foo' => new Reference('baz')], 'bar');
9292

9393
(new AnalyzeServiceReferencesPass())->process($container);
9494
(new InlineServiceDefinitionsPass())->process($container);
95+
96+
$this->expectException(ServiceNotFoundException::class);
97+
$this->expectExceptionMessage('The service "foo" in the container provided to "bar" has a dependency on a non-existent service "baz".');
98+
9599
$this->process($container);
96100
}
97101

98102
public function testWithErroredHiddenService()
99103
{
100-
$this->expectException(ServiceNotFoundException::class);
101-
$this->expectExceptionMessage('The service "bar" has a dependency on a non-existent service "foo".');
102104
$container = new ContainerBuilder();
103105

104106
ServiceLocatorTagPass::register($container, ['foo' => new Reference('foo')], 'bar');
105107

106108
(new AnalyzeServiceReferencesPass())->process($container);
107109
(new InlineServiceDefinitionsPass())->process($container);
110+
111+
$this->expectException(ServiceNotFoundException::class);
112+
$this->expectExceptionMessage('The service "bar" has a dependency on a non-existent service "foo".');
113+
108114
$this->process($container);
109115
}
110116

111117
public function testProcessThrowsExceptionOnInvalidReferenceWithAlternatives()
112118
{
113-
$this->expectException(ServiceNotFoundException::class);
114-
$this->expectExceptionMessage('The service "a" has a dependency on a non-existent service "@ccc". Did you mean this: "ccc"?');
115119
$container = new ContainerBuilder();
116120

117121
$container
@@ -121,19 +125,22 @@ public function testProcessThrowsExceptionOnInvalidReferenceWithAlternatives()
121125
$container
122126
->register('ccc', '\stdClass');
123127

128+
$this->expectException(ServiceNotFoundException::class);
129+
$this->expectExceptionMessage('The service "a" has a dependency on a non-existent service "@ccc". Did you mean this: "ccc"?');
130+
124131
$this->process($container);
125132
}
126133

127134
public function testCurrentIdIsExcludedFromAlternatives()
128135
{
129-
$this->expectException(ServiceNotFoundException::class);
130-
$this->expectExceptionMessage('The service "app.my_service" has a dependency on a non-existent service "app.my_service2".');
131-
132136
$container = new ContainerBuilder();
133137
$container
134138
->register('app.my_service', \stdClass::class)
135139
->addArgument(new Reference('app.my_service2'));
136140

141+
$this->expectException(ServiceNotFoundException::class);
142+
$this->expectExceptionMessage('The service "app.my_service" has a dependency on a non-existent service "app.my_service2".');
143+
137144
$this->process($container);
138145
}
139146

Tests/Compiler/CheckReferenceValidityPassTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ class CheckReferenceValidityPassTest extends TestCase
2020
{
2121
public function testProcessDetectsReferenceToAbstractDefinition()
2222
{
23-
$this->expectException(\RuntimeException::class);
2423
$container = new ContainerBuilder();
2524

2625
$container->register('a')->setAbstract(true);
2726
$container->register('b')->addArgument(new Reference('a'));
2827

28+
$this->expectException(\RuntimeException::class);
29+
2930
$this->process($container);
3031
}
3132

0 commit comments

Comments
 (0)