Skip to content

Commit c5cff26

Browse files
committed
[Tests] Move expectException closer to the place of the expectation to avoid false positives
1 parent 858c7b5 commit c5cff26

16 files changed

+144
-97
lines changed

Tests/ApplicationTest.php

Lines changed: 52 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,8 @@ public function testAddCommandWithEmptyConstructor()
229229
{
230230
$this->expectException(\LogicException::class);
231231
$this->expectExceptionMessage('Command class "Foo5Command" is not correctly initialized. You probably forgot to call the parent constructor.');
232-
$application = new Application();
233-
$application->add(new \Foo5Command());
232+
233+
(new Application())->add(new \Foo5Command());
234234
}
235235

236236
public function testHasGet()
@@ -294,8 +294,8 @@ public function testGetInvalidCommand()
294294
{
295295
$this->expectException(CommandNotFoundException::class);
296296
$this->expectExceptionMessage('The command "foofoo" does not exist.');
297-
$application = new Application();
298-
$application->get('foofoo');
297+
298+
(new Application())->get('foofoo');
299299
}
300300

301301
public function testGetNamespaces()
@@ -351,20 +351,21 @@ public function testFindInvalidNamespace()
351351
{
352352
$this->expectException(NamespaceNotFoundException::class);
353353
$this->expectExceptionMessage('There are no commands defined in the "bar" namespace.');
354-
$application = new Application();
355-
$application->findNamespace('bar');
354+
355+
(new Application)->findNamespace('bar');
356356
}
357357

358358
public function testFindUniqueNameButNamespaceName()
359359
{
360-
$this->expectException(CommandNotFoundException::class);
361-
$this->expectExceptionMessage('Command "foo1" is not defined');
362360
$application = new Application();
363361
$application->add(new \FooCommand());
364362
$application->add(new \Foo1Command());
365363
$application->add(new \Foo2Command());
366364

367-
$application->find($commandName = 'foo1');
365+
$this->expectException(CommandNotFoundException::class);
366+
$this->expectExceptionMessage('Command "foo1" is not defined');
367+
368+
$application->find('foo1');
368369
}
369370

370371
public function testFind()
@@ -403,13 +404,14 @@ public function testFindCaseInsensitiveAsFallback()
403404

404405
public function testFindCaseInsensitiveSuggestions()
405406
{
406-
$this->expectException(CommandNotFoundException::class);
407-
$this->expectExceptionMessage('Command "FoO:BaR" is ambiguous');
408407
$application = new Application();
409408
$application->add(new \FooSameCaseLowercaseCommand());
410409
$application->add(new \FooSameCaseUppercaseCommand());
411410

412-
$this->assertInstanceOf(\FooSameCaseLowercaseCommand::class, $application->find('FoO:BaR'), '->find() will find two suggestions with case insensitivity');
411+
$this->expectException(CommandNotFoundException::class);
412+
$this->expectExceptionMessage('Command "FoO:BaR" is ambiguous');
413+
414+
$application->find('FoO:BaR');
413415
}
414416

415417
public function testFindWithCommandLoader()
@@ -506,10 +508,12 @@ public function testFindCommandWithMissingNamespace()
506508
*/
507509
public function testFindAlternativeExceptionMessageSingle($name)
508510
{
509-
$this->expectException(CommandNotFoundException::class);
510-
$this->expectExceptionMessage('Did you mean this');
511511
$application = new Application();
512512
$application->add(new \Foo3Command());
513+
514+
$this->expectException(CommandNotFoundException::class);
515+
$this->expectExceptionMessage('Did you mean this');
516+
513517
$application->find($name);
514518
}
515519

@@ -744,11 +748,13 @@ public function testFindNamespaceDoesNotFailOnDeepSimilarNamespaces()
744748

745749
public function testFindWithDoubleColonInNameThrowsException()
746750
{
747-
$this->expectException(CommandNotFoundException::class);
748-
$this->expectExceptionMessage('Command "foo::bar" is not defined.');
749751
$application = new Application();
750752
$application->add(new \FooCommand());
751753
$application->add(new \Foo4Command());
754+
755+
$this->expectException(CommandNotFoundException::class);
756+
$this->expectExceptionMessage('Command "foo::bar" is not defined.');
757+
752758
$application->find('foo::bar');
753759
}
754760

@@ -1248,8 +1254,6 @@ public function testRunReturnsExitCodeOneForNegativeExceptionCode($exceptionCode
12481254

12491255
public function testAddingOptionWithDuplicateShortcut()
12501256
{
1251-
$this->expectException(\LogicException::class);
1252-
$this->expectExceptionMessage('An option with shortcut "e" already exists.');
12531257
$dispatcher = new EventDispatcher();
12541258
$application = new Application();
12551259
$application->setAutoExit(false);
@@ -1268,6 +1272,9 @@ public function testAddingOptionWithDuplicateShortcut()
12681272
$input = new ArrayInput(['command' => 'foo']);
12691273
$output = new NullOutput();
12701274

1275+
$this->expectException(\LogicException::class);
1276+
$this->expectExceptionMessage('An option with shortcut "e" already exists.');
1277+
12711278
$application->run($input, $output);
12721279
}
12731280

@@ -1276,7 +1283,6 @@ public function testAddingOptionWithDuplicateShortcut()
12761283
*/
12771284
public function testAddingAlreadySetDefinitionElementData($def)
12781285
{
1279-
$this->expectException(\LogicException::class);
12801286
$application = new Application();
12811287
$application->setAutoExit(false);
12821288
$application->setCatchExceptions(false);
@@ -1288,10 +1294,13 @@ public function testAddingAlreadySetDefinitionElementData($def)
12881294

12891295
$input = new ArrayInput(['command' => 'foo']);
12901296
$output = new NullOutput();
1297+
1298+
$this->expectException(\LogicException::class);
1299+
12911300
$application->run($input, $output);
12921301
}
12931302

1294-
public static function getAddingAlreadySetDefinitionElementData()
1303+
public static function getAddingAlreadySetDefinitionElementData(): array
12951304
{
12961305
return [
12971306
[new InputArgument('command', InputArgument::REQUIRED)],
@@ -1428,8 +1437,6 @@ public function testRunWithDispatcher()
14281437

14291438
public function testRunWithExceptionAndDispatcher()
14301439
{
1431-
$this->expectException(\LogicException::class);
1432-
$this->expectExceptionMessage('error');
14331440
$application = new Application();
14341441
$application->setDispatcher($this->getDispatcher());
14351442
$application->setAutoExit(false);
@@ -1440,6 +1447,10 @@ public function testRunWithExceptionAndDispatcher()
14401447
});
14411448

14421449
$tester = new ApplicationTester($application);
1450+
1451+
$this->expectException(\LogicException::class);
1452+
$this->expectExceptionMessage('error');
1453+
14431454
$tester->run(['command' => 'foo']);
14441455
}
14451456

@@ -1504,9 +1515,6 @@ public function testRunWithError()
15041515

15051516
public function testRunWithFindError()
15061517
{
1507-
$this->expectException(\Error::class);
1508-
$this->expectExceptionMessage('Find exception');
1509-
15101518
$application = new Application();
15111519
$application->setAutoExit(false);
15121520
$application->setCatchExceptions(false);
@@ -1518,6 +1526,10 @@ public function testRunWithFindError()
15181526

15191527
// The exception should not be ignored
15201528
$tester = new ApplicationTester($application);
1529+
1530+
$this->expectException(\Error::class);
1531+
$this->expectExceptionMessage('Find exception');
1532+
15211533
$tester->run(['command' => 'foo']);
15221534
}
15231535

@@ -1590,8 +1602,6 @@ public function testErrorIsRethrownIfNotHandledByConsoleErrorEvent()
15901602

15911603
public function testRunWithErrorAndDispatcher()
15921604
{
1593-
$this->expectException(\LogicException::class);
1594-
$this->expectExceptionMessage('error');
15951605
$application = new Application();
15961606
$application->setDispatcher($this->getDispatcher());
15971607
$application->setAutoExit(false);
@@ -1604,6 +1614,10 @@ public function testRunWithErrorAndDispatcher()
16041614
});
16051615

16061616
$tester = new ApplicationTester($application);
1617+
1618+
$this->expectException(\LogicException::class);
1619+
$this->expectExceptionMessage('error');
1620+
16071621
$tester->run(['command' => 'dym']);
16081622
$this->assertStringContainsString('before.dym.error.after.', $tester->getDisplay(), 'The PHP error did not dispatch events');
16091623
}
@@ -1802,9 +1816,11 @@ public function testRunLazyCommandService()
18021816

18031817
public function testGetDisabledLazyCommand()
18041818
{
1805-
$this->expectException(CommandNotFoundException::class);
18061819
$application = new Application();
18071820
$application->setCommandLoader(new FactoryCommandLoader(['disabled' => fn () => new DisabledCommand()]));
1821+
1822+
$this->expectException(CommandNotFoundException::class);
1823+
18081824
$application->get('disabled');
18091825
}
18101826

@@ -1895,8 +1911,6 @@ public function testErrorIsRethrownIfNotHandledByConsoleErrorEventWithCatchingEn
18951911

18961912
public function testThrowingErrorListener()
18971913
{
1898-
$this->expectException(\RuntimeException::class);
1899-
$this->expectExceptionMessage('foo');
19001914
$dispatcher = $this->getDispatcher();
19011915
$dispatcher->addListener('console.error', function (ConsoleErrorEvent $event) {
19021916
throw new \RuntimeException('foo');
@@ -1916,20 +1930,25 @@ public function testThrowingErrorListener()
19161930
});
19171931

19181932
$tester = new ApplicationTester($application);
1933+
1934+
$this->expectException(\RuntimeException::class);
1935+
$this->expectExceptionMessage('foo');
1936+
19191937
$tester->run(['command' => 'foo']);
19201938
}
19211939

19221940
public function testCommandNameMismatchWithCommandLoaderKeyThrows()
19231941
{
1924-
$this->expectException(CommandNotFoundException::class);
1925-
$this->expectExceptionMessage('The "test" command cannot be found because it is registered under multiple names. Make sure you don\'t set a different name via constructor or "setName()".');
1926-
19271942
$app = new Application();
19281943
$loader = new FactoryCommandLoader([
19291944
'test' => static fn () => new Command('test-command'),
19301945
]);
19311946

19321947
$app->setCommandLoader($loader);
1948+
1949+
$this->expectException(CommandNotFoundException::class);
1950+
$this->expectExceptionMessage('The "test" command cannot be found because it is registered under multiple names. Make sure you don\'t set a different name via constructor or "setName()".');
1951+
19331952
$app->get('test');
19341953
}
19351954

Tests/Command/CommandTest.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,10 @@ public function testInvalidCommandNames($name)
144144
$this->expectException(\InvalidArgumentException::class);
145145
$this->expectExceptionMessage(sprintf('Command name "%s" is invalid.', $name));
146146

147-
$command = new \TestCommand();
148-
$command->setName($name);
147+
(new \TestCommand())->setName($name);
149148
}
150149

151-
public static function provideInvalidCommandNames()
150+
public static function provideInvalidCommandNames(): array
152151
{
153152
return [
154153
[''],
@@ -236,8 +235,7 @@ public function testGetHelperWithoutHelperSet()
236235
{
237236
$this->expectException(\LogicException::class);
238237
$this->expectExceptionMessage('Cannot retrieve helper "formatter" because there is no HelperSet defined.');
239-
$command = new \TestCommand();
240-
$command->getHelper('formatter');
238+
(new \TestCommand())->getHelper('formatter');
241239
}
242240

243241
public function testMergeApplicationDefinition()
@@ -305,16 +303,17 @@ public function testExecuteMethodNeedsToBeOverridden()
305303
{
306304
$this->expectException(\LogicException::class);
307305
$this->expectExceptionMessage('You must override the execute() method in the concrete command class.');
308-
$command = new Command('foo');
309-
$command->run(new StringInput(''), new NullOutput());
306+
(new Command('foo'))->run(new StringInput(''), new NullOutput());
310307
}
311308

312309
public function testRunWithInvalidOption()
313310
{
314-
$this->expectException(InvalidOptionException::class);
315-
$this->expectExceptionMessage('The "--bar" option does not exist.');
316311
$command = new \TestCommand();
317312
$tester = new CommandTester($command);
313+
314+
$this->expectException(InvalidOptionException::class);
315+
$this->expectExceptionMessage('The "--bar" option does not exist.');
316+
318317
$tester->execute(['--bar' => true]);
319318
}
320319

Tests/DependencyInjection/AddConsoleCommandPassTest.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,6 @@ public function testEscapesDefaultFromPhp()
180180

181181
public function testProcessThrowAnExceptionIfTheServiceIsAbstract()
182182
{
183-
$this->expectException(\InvalidArgumentException::class);
184-
$this->expectExceptionMessage('The service "my-command" tagged "console.command" must not be abstract.');
185183
$container = new ContainerBuilder();
186184
$container->setResourceTracking(false);
187185
$container->addCompilerPass(new AddConsoleCommandPass(), PassConfig::TYPE_BEFORE_REMOVING);
@@ -191,13 +189,14 @@ public function testProcessThrowAnExceptionIfTheServiceIsAbstract()
191189
$definition->setAbstract(true);
192190
$container->setDefinition('my-command', $definition);
193191

192+
$this->expectException(\InvalidArgumentException::class);
193+
$this->expectExceptionMessage('The service "my-command" tagged "console.command" must not be abstract.');
194+
194195
$container->compile();
195196
}
196197

197198
public function testProcessThrowAnExceptionIfTheServiceIsNotASubclassOfCommand()
198199
{
199-
$this->expectException(\InvalidArgumentException::class);
200-
$this->expectExceptionMessage('The service "my-command" tagged "console.command" must be a subclass of "Symfony\Component\Console\Command\Command".');
201200
$container = new ContainerBuilder();
202201
$container->setResourceTracking(false);
203202
$container->addCompilerPass(new AddConsoleCommandPass(), PassConfig::TYPE_BEFORE_REMOVING);
@@ -206,6 +205,9 @@ public function testProcessThrowAnExceptionIfTheServiceIsNotASubclassOfCommand()
206205
$definition->addTag('console.command');
207206
$container->setDefinition('my-command', $definition);
208207

208+
$this->expectException(\InvalidArgumentException::class);
209+
$this->expectExceptionMessage('The service "my-command" tagged "console.command" must be a subclass of "Symfony\Component\Console\Command\Command".');
210+
209211
$container->compile();
210212
}
211213

@@ -281,8 +283,6 @@ public function testProcessOnChildDefinitionWithParentClass()
281283

282284
public function testProcessOnChildDefinitionWithoutClass()
283285
{
284-
$this->expectException(\RuntimeException::class);
285-
$this->expectExceptionMessage('The definition for "my-child-command" has no class.');
286286
$container = new ContainerBuilder();
287287
$container->addCompilerPass(new AddConsoleCommandPass(), PassConfig::TYPE_BEFORE_REMOVING);
288288

@@ -298,6 +298,9 @@ public function testProcessOnChildDefinitionWithoutClass()
298298
$container->setDefinition($parentId, $parentDefinition);
299299
$container->setDefinition($childId, $childDefinition);
300300

301+
$this->expectException(\RuntimeException::class);
302+
$this->expectExceptionMessage('The definition for "my-child-command" has no class.');
303+
301304
$container->compile();
302305
}
303306
}

Tests/Formatter/OutputFormatterStyleStackTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,11 @@ public function testPopNotLast()
6161

6262
public function testInvalidPop()
6363
{
64-
$this->expectException(\InvalidArgumentException::class);
6564
$stack = new OutputFormatterStyleStack();
6665
$stack->push(new OutputFormatterStyle('white', 'black'));
66+
67+
$this->expectException(\InvalidArgumentException::class);
68+
6769
$stack->pop(new OutputFormatterStyle('yellow', 'blue'));
6870
}
6971
}

Tests/Helper/ProgressIndicatorTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,12 @@ public function testCannotSetInvalidIndicatorCharacters()
118118

119119
public function testCannotStartAlreadyStartedIndicator()
120120
{
121-
$this->expectException(\LogicException::class);
122-
$this->expectExceptionMessage('Progress indicator already started.');
123121
$bar = new ProgressIndicator($this->getOutputStream());
124122
$bar->start('Starting...');
123+
124+
$this->expectException(\LogicException::class);
125+
$this->expectExceptionMessage('Progress indicator already started.');
126+
125127
$bar->start('Starting Again.');
126128
}
127129

0 commit comments

Comments
 (0)