Skip to content

Commit fccfb9f

Browse files
committed
[Tests] Move expectException closer to the place of the expectation to avoid false positives
1 parent 4942e74 commit fccfb9f

10 files changed

+59
-34
lines changed

Tests/Command/TranslationDebugCommandTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,16 @@ public function testDebugCustomDirectory()
118118

119119
public function testDebugInvalidDirectory()
120120
{
121-
$this->expectException(\InvalidArgumentException::class);
122121
$kernel = $this->createMock(KernelInterface::class);
123122
$kernel->expects($this->once())
124123
->method('getBundle')
125124
->with($this->equalTo('dir'))
126125
->willThrowException(new \InvalidArgumentException());
127126

128127
$tester = $this->createCommandTester([], [], $kernel);
128+
129+
$this->expectException(\InvalidArgumentException::class);
130+
129131
$tester->execute(['locale' => 'en', 'bundle' => 'dir']);
130132
}
131133

Tests/Command/YamlLintCommandTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,12 @@ public function testLintIncorrectFile()
6060

6161
public function testLintFileNotReadable()
6262
{
63-
$this->expectException(\RuntimeException::class);
6463
$tester = $this->createCommandTester();
6564
$filename = $this->createFile('');
6665
unlink($filename);
6766

67+
$this->expectException(\RuntimeException::class);
68+
6869
$tester->execute(['filename' => $filename], ['decorated' => false]);
6970
}
7071

Tests/Controller/AbstractControllerTest.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,14 @@ public function testGetParameter()
9292

9393
public function testMissingParameterBag()
9494
{
95-
$this->expectException(ServiceNotFoundException::class);
96-
$this->expectExceptionMessage('TestAbstractController::getParameter()" method is missing a parameter bag');
9795
$container = new Container();
9896

9997
$controller = $this->createController();
10098
$controller->setContainer($container);
10199

100+
$this->expectException(ServiceNotFoundException::class);
101+
$this->expectExceptionMessage('TestAbstractController::getParameter()" method is missing a parameter bag');
102+
102103
$controller->getParameter('foo');
103104
}
104105

@@ -146,12 +147,12 @@ public function testGetUserWithEmptyTokenStorage()
146147

147148
public function testGetUserWithEmptyContainer()
148149
{
149-
$this->expectException(\LogicException::class);
150-
$this->expectExceptionMessage('The SecurityBundle is not registered in your application.');
151-
152150
$controller = $this->createController();
153151
$controller->setContainer(new Container());
154152

153+
$this->expectException(\LogicException::class);
154+
$this->expectExceptionMessage('The SecurityBundle is not registered in your application.');
155+
155156
$controller->getUser();
156157
}
157158

@@ -327,10 +328,10 @@ public function testFileFromPathWithCustomizedFileName()
327328

328329
public function testFileWhichDoesNotExist()
329330
{
330-
$this->expectException(FileNotFoundException::class);
331-
332331
$controller = $this->createController();
333332

333+
$this->expectException(FileNotFoundException::class);
334+
334335
$controller->file('some-file.txt', 'test.php');
335336
}
336337

@@ -350,8 +351,6 @@ public function testIsGranted()
350351

351352
public function testdenyAccessUnlessGranted()
352353
{
353-
$this->expectException(AccessDeniedException::class);
354-
355354
$authorizationChecker = $this->createMock(AuthorizationCheckerInterface::class);
356355
$authorizationChecker->expects($this->once())->method('isGranted')->willReturn(false);
357356

@@ -361,6 +360,8 @@ public function testdenyAccessUnlessGranted()
361360
$controller = $this->createController();
362361
$controller->setContainer($container);
363362

363+
$this->expectException(AccessDeniedException::class);
364+
364365
$controller->denyAccessUnlessGranted('foo');
365366
}
366367

Tests/Controller/TemplateControllerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function testNoTwigInvokeMethod()
4747
$controller = new TemplateController();
4848

4949
$this->expectException(\LogicException::class);
50-
$this->expectExceptionMessage('You cannot use the TemplateController if the Twig Bundle is not available.');
50+
$this->expectExceptionMessage('You cannot use the TemplateController if the Twig Bundle is not available. Try running "composer require symfony/twig-bundle".');
5151

5252
$controller('mytemplate')->getContent();
5353
}

Tests/DependencyInjection/Compiler/ProfilerPassTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@ class ProfilerPassTest extends TestCase
3434
*/
3535
public function testTemplateNoIdThrowsException()
3636
{
37-
$this->expectException(\InvalidArgumentException::class);
3837
$builder = new ContainerBuilder();
3938
$builder->register('profiler', 'ProfilerClass');
4039
$builder->register('my_collector_service')
4140
->addTag('data_collector', ['template' => 'foo']);
4241

4342
$profilerPass = new ProfilerPass();
43+
44+
$this->expectException(\InvalidArgumentException::class);
45+
4446
$profilerPass->process($builder);
4547
}
4648

Tests/DependencyInjection/Compiler/WorkflowGuardListenerPassTest.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,49 +58,53 @@ public function testNoExeptionIfAllDependenciesArePresent()
5858

5959
public function testExceptionIfTheTokenStorageServiceIsNotPresent()
6060
{
61-
$this->expectException(LogicException::class);
62-
$this->expectExceptionMessage('The "security.token_storage" service is needed to be able to use the workflow guard listener.');
6361
$this->container->setParameter('workflow.has_guard_listeners', true);
6462
$this->container->register('security.authorization_checker', AuthorizationCheckerInterface::class);
6563
$this->container->register('security.authentication.trust_resolver', AuthenticationTrustResolverInterface::class);
6664
$this->container->register('security.role_hierarchy', RoleHierarchy::class);
6765

66+
$this->expectException(LogicException::class);
67+
$this->expectExceptionMessage('The "security.token_storage" service is needed to be able to use the workflow guard listener.');
68+
6869
$this->compilerPass->process($this->container);
6970
}
7071

7172
public function testExceptionIfTheAuthorizationCheckerServiceIsNotPresent()
7273
{
73-
$this->expectException(LogicException::class);
74-
$this->expectExceptionMessage('The "security.authorization_checker" service is needed to be able to use the workflow guard listener.');
7574
$this->container->setParameter('workflow.has_guard_listeners', true);
7675
$this->container->register('security.token_storage', TokenStorageInterface::class);
7776
$this->container->register('security.authentication.trust_resolver', AuthenticationTrustResolverInterface::class);
7877
$this->container->register('security.role_hierarchy', RoleHierarchy::class);
7978

79+
$this->expectException(LogicException::class);
80+
$this->expectExceptionMessage('The "security.authorization_checker" service is needed to be able to use the workflow guard listener.');
81+
8082
$this->compilerPass->process($this->container);
8183
}
8284

8385
public function testExceptionIfTheAuthenticationTrustResolverServiceIsNotPresent()
8486
{
85-
$this->expectException(LogicException::class);
86-
$this->expectExceptionMessage('The "security.authentication.trust_resolver" service is needed to be able to use the workflow guard listener.');
8787
$this->container->setParameter('workflow.has_guard_listeners', true);
8888
$this->container->register('security.token_storage', TokenStorageInterface::class);
8989
$this->container->register('security.authorization_checker', AuthorizationCheckerInterface::class);
9090
$this->container->register('security.role_hierarchy', RoleHierarchy::class);
9191

92+
$this->expectException(LogicException::class);
93+
$this->expectExceptionMessage('The "security.authentication.trust_resolver" service is needed to be able to use the workflow guard listener.');
94+
9295
$this->compilerPass->process($this->container);
9396
}
9497

9598
public function testExceptionIfTheRoleHierarchyServiceIsNotPresent()
9699
{
97-
$this->expectException(LogicException::class);
98-
$this->expectExceptionMessage('The "security.role_hierarchy" service is needed to be able to use the workflow guard listener.');
99100
$this->container->setParameter('workflow.has_guard_listeners', true);
100101
$this->container->register('security.token_storage', TokenStorageInterface::class);
101102
$this->container->register('security.authorization_checker', AuthorizationCheckerInterface::class);
102103
$this->container->register('security.authentication.trust_resolver', AuthenticationTrustResolverInterface::class);
103104

105+
$this->expectException(LogicException::class);
106+
$this->expectExceptionMessage('The "security.role_hierarchy" service is needed to be able to use the workflow guard listener.');
107+
104108
$this->compilerPass->process($this->container);
105109
}
106110
}

Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@ public function getTestValidSessionName()
6161
*/
6262
public function testInvalidSessionName($sessionName)
6363
{
64-
$this->expectException(InvalidConfigurationException::class);
6564
$processor = new Processor();
65+
66+
$this->expectException(InvalidConfigurationException::class);
67+
6668
$processor->processConfiguration(
6769
new Configuration(true),
6870
[[
@@ -177,11 +179,12 @@ public static function provideValidAssetsPackageNameConfigurationTests(): array
177179
*/
178180
public function testInvalidAssetsConfiguration(array $assetConfig, $expectedMessage)
179181
{
182+
$processor = new Processor();
183+
$configuration = new Configuration(true);
184+
180185
$this->expectException(InvalidConfigurationException::class);
181186
$this->expectExceptionMessage($expectedMessage);
182187

183-
$processor = new Processor();
184-
$configuration = new Configuration(true);
185188
$processor->processConfiguration($configuration, [
186189
[
187190
'http_method_override' => false,

Tests/DependencyInjection/FrameworkExtensionTestCase.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,9 +634,11 @@ public function testRouter()
634634

635635
public function testRouterRequiresResourceOption()
636636
{
637-
$this->expectException(InvalidConfigurationException::class);
638637
$container = $this->createContainer();
639638
$loader = new FrameworkExtension();
639+
640+
$this->expectException(InvalidConfigurationException::class);
641+
640642
$loader->load([['http_method_override' => false, 'handle_all_throwables' => true, 'php_errors' => ['log' => true], 'router' => true]], $container);
641643
}
642644

Tests/Functional/RouterDebugCommandTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,11 @@ public function testSearchMultipleRoutesWithoutInteraction()
8181

8282
public function testSearchWithThrow()
8383
{
84+
$tester = $this->createCommandTester();
85+
8486
$this->expectException(\InvalidArgumentException::class);
8587
$this->expectExceptionMessage('The route "gerard" does not exist.');
86-
$tester = $this->createCommandTester();
88+
8789
$tester->execute(['name' => 'gerard'], ['interactive' => true]);
8890
}
8991

Tests/Routing/RouterTest.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -299,25 +299,29 @@ public function testPatternPlaceholdersWithSfContainer()
299299

300300
public function testEnvPlaceholders()
301301
{
302-
$this->expectException(RuntimeException::class);
303-
$this->expectExceptionMessage('Using "%env(FOO)%" is not allowed in routing configuration.');
304302
$routes = new RouteCollection();
305303

306304
$routes->add('foo', new Route('/%env(FOO)%'));
307305

308306
$router = new Router($this->getPsr11ServiceContainer($routes), 'foo', [], null, $this->getParameterBag());
307+
308+
$this->expectException(RuntimeException::class);
309+
$this->expectExceptionMessage('Using "%env(FOO)%" is not allowed in routing configuration.');
310+
309311
$router->getRouteCollection();
310312
}
311313

312314
public function testEnvPlaceholdersWithSfContainer()
313315
{
314-
$this->expectException(RuntimeException::class);
315-
$this->expectExceptionMessage('Using "%env(FOO)%" is not allowed in routing configuration.');
316316
$routes = new RouteCollection();
317317

318318
$routes->add('foo', new Route('/%env(FOO)%'));
319319

320320
$router = new Router($this->getServiceContainer($routes), 'foo');
321+
322+
$this->expectException(RuntimeException::class);
323+
$this->expectExceptionMessage('Using "%env(FOO)%" is not allowed in routing configuration.');
324+
321325
$router->getRouteCollection();
322326
}
323327

@@ -381,22 +385,22 @@ public function testHostPlaceholdersWithSfContainer()
381385

382386
public function testExceptionOnNonExistentParameterWithSfContainer()
383387
{
384-
$this->expectException(ParameterNotFoundException::class);
385-
$this->expectExceptionMessage('You have requested a non-existent parameter "nope".');
386388
$routes = new RouteCollection();
387389

388390
$routes->add('foo', new Route('/%nope%'));
389391

390392
$sc = $this->getServiceContainer($routes);
391393

392394
$router = new Router($sc, 'foo');
395+
396+
$this->expectException(ParameterNotFoundException::class);
397+
$this->expectExceptionMessage('You have requested a non-existent parameter "nope".');
398+
393399
$router->getRouteCollection()->get('foo');
394400
}
395401

396402
public function testExceptionOnNonStringParameter()
397403
{
398-
$this->expectException(RuntimeException::class);
399-
$this->expectExceptionMessage('The container parameter "object", used in the route configuration value "/%object%", must be a string or numeric, but it is of type "stdClass".');
400404
$routes = new RouteCollection();
401405

402406
$routes->add('foo', new Route('/%object%'));
@@ -405,6 +409,10 @@ public function testExceptionOnNonStringParameter()
405409
$parameters = $this->getParameterBag(['object' => new \stdClass()]);
406410

407411
$router = new Router($sc, 'foo', [], null, $parameters);
412+
413+
$this->expectException(RuntimeException::class);
414+
$this->expectExceptionMessage('The container parameter "object", used in the route configuration value "/%object%", must be a string or numeric, but it is of type "stdClass".');
415+
408416
$router->getRouteCollection()->get('foo');
409417
}
410418

0 commit comments

Comments
 (0)