Skip to content

Commit

Permalink
[Tests] Move expectException closer to the place of the expectation t…
Browse files Browse the repository at this point in the history
…o avoid false positives
  • Loading branch information
OskarStark committed Oct 31, 2023
1 parent 858c7b5 commit c5cff26
Show file tree
Hide file tree
Showing 16 changed files with 144 additions and 97 deletions.
85 changes: 52 additions & 33 deletions Tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ public function testAddCommandWithEmptyConstructor()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Command class "Foo5Command" is not correctly initialized. You probably forgot to call the parent constructor.');
$application = new Application();
$application->add(new \Foo5Command());

(new Application())->add(new \Foo5Command());
}

public function testHasGet()
Expand Down Expand Up @@ -294,8 +294,8 @@ public function testGetInvalidCommand()
{
$this->expectException(CommandNotFoundException::class);
$this->expectExceptionMessage('The command "foofoo" does not exist.');
$application = new Application();
$application->get('foofoo');

(new Application())->get('foofoo');
}

public function testGetNamespaces()
Expand Down Expand Up @@ -351,20 +351,21 @@ public function testFindInvalidNamespace()
{
$this->expectException(NamespaceNotFoundException::class);
$this->expectExceptionMessage('There are no commands defined in the "bar" namespace.');
$application = new Application();
$application->findNamespace('bar');

(new Application)->findNamespace('bar');
}

public function testFindUniqueNameButNamespaceName()
{
$this->expectException(CommandNotFoundException::class);
$this->expectExceptionMessage('Command "foo1" is not defined');
$application = new Application();
$application->add(new \FooCommand());
$application->add(new \Foo1Command());
$application->add(new \Foo2Command());

$application->find($commandName = 'foo1');
$this->expectException(CommandNotFoundException::class);
$this->expectExceptionMessage('Command "foo1" is not defined');

$application->find('foo1');
}

public function testFind()
Expand Down Expand Up @@ -403,13 +404,14 @@ public function testFindCaseInsensitiveAsFallback()

public function testFindCaseInsensitiveSuggestions()
{
$this->expectException(CommandNotFoundException::class);
$this->expectExceptionMessage('Command "FoO:BaR" is ambiguous');
$application = new Application();
$application->add(new \FooSameCaseLowercaseCommand());
$application->add(new \FooSameCaseUppercaseCommand());

$this->assertInstanceOf(\FooSameCaseLowercaseCommand::class, $application->find('FoO:BaR'), '->find() will find two suggestions with case insensitivity');
$this->expectException(CommandNotFoundException::class);
$this->expectExceptionMessage('Command "FoO:BaR" is ambiguous');

$application->find('FoO:BaR');
}

public function testFindWithCommandLoader()
Expand Down Expand Up @@ -506,10 +508,12 @@ public function testFindCommandWithMissingNamespace()
*/
public function testFindAlternativeExceptionMessageSingle($name)
{
$this->expectException(CommandNotFoundException::class);
$this->expectExceptionMessage('Did you mean this');
$application = new Application();
$application->add(new \Foo3Command());

$this->expectException(CommandNotFoundException::class);
$this->expectExceptionMessage('Did you mean this');

$application->find($name);
}

Expand Down Expand Up @@ -744,11 +748,13 @@ public function testFindNamespaceDoesNotFailOnDeepSimilarNamespaces()

public function testFindWithDoubleColonInNameThrowsException()
{
$this->expectException(CommandNotFoundException::class);
$this->expectExceptionMessage('Command "foo::bar" is not defined.');
$application = new Application();
$application->add(new \FooCommand());
$application->add(new \Foo4Command());

$this->expectException(CommandNotFoundException::class);
$this->expectExceptionMessage('Command "foo::bar" is not defined.');

$application->find('foo::bar');
}

Expand Down Expand Up @@ -1248,8 +1254,6 @@ public function testRunReturnsExitCodeOneForNegativeExceptionCode($exceptionCode

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

$this->expectException(\LogicException::class);
$this->expectExceptionMessage('An option with shortcut "e" already exists.');

$application->run($input, $output);
}

Expand All @@ -1276,7 +1283,6 @@ public function testAddingOptionWithDuplicateShortcut()
*/
public function testAddingAlreadySetDefinitionElementData($def)
{
$this->expectException(\LogicException::class);
$application = new Application();
$application->setAutoExit(false);
$application->setCatchExceptions(false);
Expand All @@ -1288,10 +1294,13 @@ public function testAddingAlreadySetDefinitionElementData($def)

$input = new ArrayInput(['command' => 'foo']);
$output = new NullOutput();

$this->expectException(\LogicException::class);

$application->run($input, $output);
}

public static function getAddingAlreadySetDefinitionElementData()
public static function getAddingAlreadySetDefinitionElementData(): array
{
return [
[new InputArgument('command', InputArgument::REQUIRED)],
Expand Down Expand Up @@ -1428,8 +1437,6 @@ public function testRunWithDispatcher()

public function testRunWithExceptionAndDispatcher()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('error');
$application = new Application();
$application->setDispatcher($this->getDispatcher());
$application->setAutoExit(false);
Expand All @@ -1440,6 +1447,10 @@ public function testRunWithExceptionAndDispatcher()
});

$tester = new ApplicationTester($application);

$this->expectException(\LogicException::class);
$this->expectExceptionMessage('error');

$tester->run(['command' => 'foo']);
}

Expand Down Expand Up @@ -1504,9 +1515,6 @@ public function testRunWithError()

public function testRunWithFindError()
{
$this->expectException(\Error::class);
$this->expectExceptionMessage('Find exception');

$application = new Application();
$application->setAutoExit(false);
$application->setCatchExceptions(false);
Expand All @@ -1518,6 +1526,10 @@ public function testRunWithFindError()

// The exception should not be ignored
$tester = new ApplicationTester($application);

$this->expectException(\Error::class);
$this->expectExceptionMessage('Find exception');

$tester->run(['command' => 'foo']);
}

Expand Down Expand Up @@ -1590,8 +1602,6 @@ public function testErrorIsRethrownIfNotHandledByConsoleErrorEvent()

public function testRunWithErrorAndDispatcher()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('error');
$application = new Application();
$application->setDispatcher($this->getDispatcher());
$application->setAutoExit(false);
Expand All @@ -1604,6 +1614,10 @@ public function testRunWithErrorAndDispatcher()
});

$tester = new ApplicationTester($application);

$this->expectException(\LogicException::class);
$this->expectExceptionMessage('error');

$tester->run(['command' => 'dym']);
$this->assertStringContainsString('before.dym.error.after.', $tester->getDisplay(), 'The PHP error did not dispatch events');
}
Expand Down Expand Up @@ -1802,9 +1816,11 @@ public function testRunLazyCommandService()

public function testGetDisabledLazyCommand()
{
$this->expectException(CommandNotFoundException::class);
$application = new Application();
$application->setCommandLoader(new FactoryCommandLoader(['disabled' => fn () => new DisabledCommand()]));

$this->expectException(CommandNotFoundException::class);

$application->get('disabled');
}

Expand Down Expand Up @@ -1895,8 +1911,6 @@ public function testErrorIsRethrownIfNotHandledByConsoleErrorEventWithCatchingEn

public function testThrowingErrorListener()
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('foo');
$dispatcher = $this->getDispatcher();
$dispatcher->addListener('console.error', function (ConsoleErrorEvent $event) {
throw new \RuntimeException('foo');
Expand All @@ -1916,20 +1930,25 @@ public function testThrowingErrorListener()
});

$tester = new ApplicationTester($application);

$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('foo');

$tester->run(['command' => 'foo']);
}

public function testCommandNameMismatchWithCommandLoaderKeyThrows()
{
$this->expectException(CommandNotFoundException::class);
$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()".');

$app = new Application();
$loader = new FactoryCommandLoader([
'test' => static fn () => new Command('test-command'),
]);

$app->setCommandLoader($loader);

$this->expectException(CommandNotFoundException::class);
$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()".');

$app->get('test');
}

Expand Down
17 changes: 8 additions & 9 deletions Tests/Command/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,10 @@ public function testInvalidCommandNames($name)
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage(sprintf('Command name "%s" is invalid.', $name));

$command = new \TestCommand();
$command->setName($name);
(new \TestCommand())->setName($name);
}

public static function provideInvalidCommandNames()
public static function provideInvalidCommandNames(): array
{
return [
[''],
Expand Down Expand Up @@ -236,8 +235,7 @@ public function testGetHelperWithoutHelperSet()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Cannot retrieve helper "formatter" because there is no HelperSet defined.');
$command = new \TestCommand();
$command->getHelper('formatter');
(new \TestCommand())->getHelper('formatter');
}

public function testMergeApplicationDefinition()
Expand Down Expand Up @@ -305,16 +303,17 @@ public function testExecuteMethodNeedsToBeOverridden()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('You must override the execute() method in the concrete command class.');
$command = new Command('foo');
$command->run(new StringInput(''), new NullOutput());
(new Command('foo'))->run(new StringInput(''), new NullOutput());
}

public function testRunWithInvalidOption()
{
$this->expectException(InvalidOptionException::class);
$this->expectExceptionMessage('The "--bar" option does not exist.');
$command = new \TestCommand();
$tester = new CommandTester($command);

$this->expectException(InvalidOptionException::class);
$this->expectExceptionMessage('The "--bar" option does not exist.');

$tester->execute(['--bar' => true]);
}

Expand Down
15 changes: 9 additions & 6 deletions Tests/DependencyInjection/AddConsoleCommandPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,6 @@ public function testEscapesDefaultFromPhp()

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

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('The service "my-command" tagged "console.command" must not be abstract.');

$container->compile();
}

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

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('The service "my-command" tagged "console.command" must be a subclass of "Symfony\Component\Console\Command\Command".');

$container->compile();
}

Expand Down Expand Up @@ -281,8 +283,6 @@ public function testProcessOnChildDefinitionWithParentClass()

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

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

$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('The definition for "my-child-command" has no class.');

$container->compile();
}
}
Expand Down
4 changes: 3 additions & 1 deletion Tests/Formatter/OutputFormatterStyleStackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ public function testPopNotLast()

public function testInvalidPop()
{
$this->expectException(\InvalidArgumentException::class);
$stack = new OutputFormatterStyleStack();
$stack->push(new OutputFormatterStyle('white', 'black'));

$this->expectException(\InvalidArgumentException::class);

$stack->pop(new OutputFormatterStyle('yellow', 'blue'));
}
}
6 changes: 4 additions & 2 deletions Tests/Helper/ProgressIndicatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,12 @@ public function testCannotSetInvalidIndicatorCharacters()

public function testCannotStartAlreadyStartedIndicator()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Progress indicator already started.');
$bar = new ProgressIndicator($this->getOutputStream());
$bar->start('Starting...');

$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Progress indicator already started.');

$bar->start('Starting Again.');
}

Expand Down
Loading

0 comments on commit c5cff26

Please sign in to comment.