From c5cff26e1aab4126118f3e5f96029e0678adcaaf Mon Sep 17 00:00:00 2001 From: Oskar Stark Date: Fri, 27 Oct 2023 13:50:26 +0200 Subject: [PATCH] [Tests] Move expectException closer to the place of the expectation to avoid false positives --- Tests/ApplicationTest.php | 85 ++++++++++++------- Tests/Command/CommandTest.php | 17 ++-- .../AddConsoleCommandPassTest.php | 15 ++-- .../OutputFormatterStyleStackTest.php | 4 +- Tests/Helper/ProgressIndicatorTest.php | 6 +- Tests/Helper/QuestionHelperTest.php | 18 ++-- Tests/Helper/SymfonyQuestionHelperTest.php | 3 +- Tests/Helper/TableStyleTest.php | 3 +- Tests/Helper/TableTest.php | 7 +- Tests/Input/ArgvInputTest.php | 10 +-- Tests/Input/ArrayInputTest.php | 4 +- Tests/Input/InputArgumentTest.php | 15 +++- Tests/Input/InputOptionTest.php | 11 ++- Tests/Input/InputTest.php | 28 ++++-- Tests/Logger/ConsoleLoggerTest.php | 3 +- Tests/Tester/CommandTesterTest.php | 12 ++- 16 files changed, 144 insertions(+), 97 deletions(-) diff --git a/Tests/ApplicationTest.php b/Tests/ApplicationTest.php index 68bb459e8..f575f5e52 100644 --- a/Tests/ApplicationTest.php +++ b/Tests/ApplicationTest.php @@ -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() @@ -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() @@ -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() @@ -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() @@ -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); } @@ -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'); } @@ -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); @@ -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); } @@ -1276,7 +1283,6 @@ public function testAddingOptionWithDuplicateShortcut() */ public function testAddingAlreadySetDefinitionElementData($def) { - $this->expectException(\LogicException::class); $application = new Application(); $application->setAutoExit(false); $application->setCatchExceptions(false); @@ -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)], @@ -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); @@ -1440,6 +1447,10 @@ public function testRunWithExceptionAndDispatcher() }); $tester = new ApplicationTester($application); + + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('error'); + $tester->run(['command' => 'foo']); } @@ -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); @@ -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']); } @@ -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); @@ -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'); } @@ -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'); } @@ -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'); @@ -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'); } diff --git a/Tests/Command/CommandTest.php b/Tests/Command/CommandTest.php index 99fc554b5..76dacfadb 100644 --- a/Tests/Command/CommandTest.php +++ b/Tests/Command/CommandTest.php @@ -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 [ [''], @@ -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() @@ -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]); } diff --git a/Tests/DependencyInjection/AddConsoleCommandPassTest.php b/Tests/DependencyInjection/AddConsoleCommandPassTest.php index 6819282a3..639e5091e 100644 --- a/Tests/DependencyInjection/AddConsoleCommandPassTest.php +++ b/Tests/DependencyInjection/AddConsoleCommandPassTest.php @@ -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); @@ -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); @@ -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(); } @@ -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); @@ -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(); } } diff --git a/Tests/Formatter/OutputFormatterStyleStackTest.php b/Tests/Formatter/OutputFormatterStyleStackTest.php index 7fbe4f415..0ceab34ea 100644 --- a/Tests/Formatter/OutputFormatterStyleStackTest.php +++ b/Tests/Formatter/OutputFormatterStyleStackTest.php @@ -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')); } } diff --git a/Tests/Helper/ProgressIndicatorTest.php b/Tests/Helper/ProgressIndicatorTest.php index ffb3472ec..7f7dbc0a0 100644 --- a/Tests/Helper/ProgressIndicatorTest.php +++ b/Tests/Helper/ProgressIndicatorTest.php @@ -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.'); } diff --git a/Tests/Helper/QuestionHelperTest.php b/Tests/Helper/QuestionHelperTest.php index 9a37558ec..8c5fe8a20 100644 --- a/Tests/Helper/QuestionHelperTest.php +++ b/Tests/Helper/QuestionHelperTest.php @@ -677,8 +677,6 @@ public function testSelectChoiceFromChoiceList($providedAnswer, $expectedValue) public function testAmbiguousChoiceFromChoicelist() { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('The provided answer is ambiguous. Value should be one of "env_2" or "env_3".'); $possibleChoices = [ 'env_1' => 'My first environment', 'env_2' => 'My environment', @@ -692,10 +690,13 @@ public function testAmbiguousChoiceFromChoicelist() $question = new ChoiceQuestion('Please select the environment to load', $possibleChoices); $question->setMaxAttempts(1); + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('The provided answer is ambiguous. Value should be one of "env_2" or "env_3".'); + $dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream("My environment\n")), $this->createOutputInterface(), $question); } - public static function answerProvider() + public static function answerProvider(): array { return [ ['env_1', 'env_1'], @@ -743,22 +744,18 @@ public function testAskThrowsExceptionOnMissingInput() { $this->expectException(MissingInputException::class); $this->expectExceptionMessage('Aborted.'); - $dialog = new QuestionHelper(); - $dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream('')), $this->createOutputInterface(), new Question('What\'s your name?')); + (new QuestionHelper())->ask($this->createStreamableInputInterfaceMock($this->getInputStream('')), $this->createOutputInterface(), new Question('What\'s your name?')); } public function testAskThrowsExceptionOnMissingInputForChoiceQuestion() { $this->expectException(MissingInputException::class); $this->expectExceptionMessage('Aborted.'); - $dialog = new QuestionHelper(); - $dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream('')), $this->createOutputInterface(), new ChoiceQuestion('Choice', ['a', 'b'])); + (new QuestionHelper())->ask($this->createStreamableInputInterfaceMock($this->getInputStream('')), $this->createOutputInterface(), new ChoiceQuestion('Choice', ['a', 'b'])); } public function testAskThrowsExceptionOnMissingInputWithValidator() { - $this->expectException(MissingInputException::class); - $this->expectExceptionMessage('Aborted.'); $dialog = new QuestionHelper(); $question = new Question('What\'s your name?'); @@ -768,6 +765,9 @@ public function testAskThrowsExceptionOnMissingInputWithValidator() } }); + $this->expectException(MissingInputException::class); + $this->expectExceptionMessage('Aborted.'); + $dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream('')), $this->createOutputInterface(), $question); } diff --git a/Tests/Helper/SymfonyQuestionHelperTest.php b/Tests/Helper/SymfonyQuestionHelperTest.php index 2af0b199c..6cf79965b 100644 --- a/Tests/Helper/SymfonyQuestionHelperTest.php +++ b/Tests/Helper/SymfonyQuestionHelperTest.php @@ -137,8 +137,7 @@ public function testAskThrowsExceptionOnMissingInput() { $this->expectException(RuntimeException::class); $this->expectExceptionMessage('Aborted.'); - $dialog = new SymfonyQuestionHelper(); - $dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream('')), $this->createOutputInterface(), new Question('What\'s your name?')); + (new SymfonyQuestionHelper())->ask($this->createStreamableInputInterfaceMock($this->getInputStream('')), $this->createOutputInterface(), new Question('What\'s your name?')); } public function testChoiceQuestionPadding() diff --git a/Tests/Helper/TableStyleTest.php b/Tests/Helper/TableStyleTest.php index 5ff28f19f..dd740421f 100644 --- a/Tests/Helper/TableStyleTest.php +++ b/Tests/Helper/TableStyleTest.php @@ -20,7 +20,6 @@ public function testSetPadTypeWithInvalidType() { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Invalid padding type. Expected one of (STR_PAD_LEFT, STR_PAD_RIGHT, STR_PAD_BOTH).'); - $style = new TableStyle(); - $style->setPadType(31); + (new TableStyle())->setPadType(31); } } diff --git a/Tests/Helper/TableTest.php b/Tests/Helper/TableTest.php index 2411a6fb4..728ea847f 100644 --- a/Tests/Helper/TableTest.php +++ b/Tests/Helper/TableTest.php @@ -1017,16 +1017,15 @@ public function testColumnStyle() public function testThrowsWhenTheCellInAnArray() { - $table = new Table($this->getOutputStream()); + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('A cell must be a TableCell, a scalar or an object implementing "__toString()", "array" given.'); + $table = new Table($output = $this->getOutputStream()); $table ->setHeaders(['ISBN', 'Title', 'Author', 'Price']) ->setRows([ ['99921-58-10-7', [], 'Dante Alighieri', '9.95'], ]); - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('A cell must be a TableCell, a scalar or an object implementing "__toString()", "array" given.'); - $table->render(); } diff --git a/Tests/Input/ArgvInputTest.php b/Tests/Input/ArgvInputTest.php index 920dc492c..a47d557b7 100644 --- a/Tests/Input/ArgvInputTest.php +++ b/Tests/Input/ArgvInputTest.php @@ -242,8 +242,7 @@ public function testInvalidInput($argv, $definition, $expectedExceptionMessage) $this->expectException(\RuntimeException::class); $this->expectExceptionMessage($expectedExceptionMessage); - $input = new ArgvInput($argv); - $input->bind($definition); + (new ArgvInput($argv))->bind($definition); } /** @@ -254,11 +253,10 @@ public function testInvalidInputNegatable($argv, $definition, $expectedException $this->expectException(\RuntimeException::class); $this->expectExceptionMessage($expectedExceptionMessage); - $input = new ArgvInput($argv); - $input->bind($definition); + (new ArgvInput($argv))->bind($definition); } - public static function provideInvalidInput() + public static function provideInvalidInput(): array { return [ [ @@ -329,7 +327,7 @@ public static function provideInvalidInput() ]; } - public static function provideInvalidNegatableInput() + public static function provideInvalidNegatableInput(): array { return [ [ diff --git a/Tests/Input/ArrayInputTest.php b/Tests/Input/ArrayInputTest.php index 733322490..d6fe32bb3 100644 --- a/Tests/Input/ArrayInputTest.php +++ b/Tests/Input/ArrayInputTest.php @@ -74,7 +74,7 @@ public function testParseOptions($input, $options, $expectedOptions, $message) $this->assertEquals($expectedOptions, $input->getOptions(), $message); } - public static function provideOptions() + public static function provideOptions(): array { return [ [ @@ -133,7 +133,7 @@ public function testParseInvalidInput($parameters, $definition, $expectedExcepti new ArrayInput($parameters, $definition); } - public static function provideInvalidInput() + public static function provideInvalidInput(): array { return [ [ diff --git a/Tests/Input/InputArgumentTest.php b/Tests/Input/InputArgumentTest.php index 398048cbc..05447426c 100644 --- a/Tests/Input/InputArgumentTest.php +++ b/Tests/Input/InputArgumentTest.php @@ -86,25 +86,31 @@ public function testSetDefault() public function testSetDefaultWithRequiredArgument() { + $argument = new InputArgument('foo', InputArgument::REQUIRED); + $this->expectException(\LogicException::class); $this->expectExceptionMessage('Cannot set a default value except for InputArgument::OPTIONAL mode.'); - $argument = new InputArgument('foo', InputArgument::REQUIRED); + $argument->setDefault('default'); } public function testSetDefaultWithRequiredArrayArgument() { + $argument = new InputArgument('foo', InputArgument::REQUIRED | InputArgument::IS_ARRAY); + $this->expectException(\LogicException::class); $this->expectExceptionMessage('Cannot set a default value except for InputArgument::OPTIONAL mode.'); - $argument = new InputArgument('foo', InputArgument::REQUIRED | InputArgument::IS_ARRAY); + $argument->setDefault([]); } public function testSetDefaultWithArrayArgument() { + $argument = new InputArgument('foo', InputArgument::IS_ARRAY); + $this->expectException(\LogicException::class); $this->expectExceptionMessage('A default value for an array argument must be an array.'); - $argument = new InputArgument('foo', InputArgument::IS_ARRAY); + $argument->setDefault('default'); } @@ -130,10 +136,11 @@ public function testCompleteClosure() public function testCompleteClosureReturnIncorrectType() { + $argument = new InputArgument('foo', InputArgument::OPTIONAL, '', null, fn (CompletionInput $input) => 'invalid'); + $this->expectException(LogicException::class); $this->expectExceptionMessage('Closure for argument "foo" must return an array. Got "string".'); - $argument = new InputArgument('foo', InputArgument::OPTIONAL, '', null, fn (CompletionInput $input) => 'invalid'); $argument->complete(new CompletionInput(), new CompletionSuggestions()); } } diff --git a/Tests/Input/InputOptionTest.php b/Tests/Input/InputOptionTest.php index 0b5271b32..74bf69586 100644 --- a/Tests/Input/InputOptionTest.php +++ b/Tests/Input/InputOptionTest.php @@ -162,17 +162,21 @@ public function testSetDefault() public function testDefaultValueWithValueNoneMode() { + $option = new InputOption('foo', 'f', InputOption::VALUE_NONE); + $this->expectException(\LogicException::class); $this->expectExceptionMessage('Cannot set a default value when using InputOption::VALUE_NONE mode.'); - $option = new InputOption('foo', 'f', InputOption::VALUE_NONE); + $option->setDefault('default'); } public function testDefaultValueWithIsArrayMode() { + $option = new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY); + $this->expectException(\LogicException::class); $this->expectExceptionMessage('A default value for an array option must be an array.'); - $option = new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY); + $option->setDefault('default'); } @@ -229,10 +233,11 @@ public function testCompleteClosure() public function testCompleteClosureReturnIncorrectType() { + $option = new InputOption('foo', null, InputOption::VALUE_OPTIONAL, '', null, fn (CompletionInput $input) => 'invalid'); + $this->expectException(LogicException::class); $this->expectExceptionMessage('Closure for option "foo" must return an array. Got "string".'); - $option = new InputOption('foo', null, InputOption::VALUE_OPTIONAL, '', null, fn (CompletionInput $input) => 'invalid'); $option->complete(new CompletionInput(), new CompletionSuggestions()); } } diff --git a/Tests/Input/InputTest.php b/Tests/Input/InputTest.php index 6547822fb..34fb4833b 100644 --- a/Tests/Input/InputTest.php +++ b/Tests/Input/InputTest.php @@ -63,17 +63,21 @@ public function testOptions() public function testSetInvalidOption() { + $input = new ArrayInput(['--name' => 'foo'], new InputDefinition([new InputOption('name'), new InputOption('bar', '', InputOption::VALUE_OPTIONAL, '', 'default')])); + $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('The "foo" option does not exist.'); - $input = new ArrayInput(['--name' => 'foo'], new InputDefinition([new InputOption('name'), new InputOption('bar', '', InputOption::VALUE_OPTIONAL, '', 'default')])); + $input->setOption('foo', 'bar'); } public function testGetInvalidOption() { + $input = new ArrayInput(['--name' => 'foo'], new InputDefinition([new InputOption('name'), new InputOption('bar', '', InputOption::VALUE_OPTIONAL, '', 'default')])); + $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('The "foo" option does not exist.'); - $input = new ArrayInput(['--name' => 'foo'], new InputDefinition([new InputOption('name'), new InputOption('bar', '', InputOption::VALUE_OPTIONAL, '', 'default')])); + $input->getOption('foo'); } @@ -93,35 +97,43 @@ public function testArguments() public function testSetInvalidArgument() { + $input = new ArrayInput(['name' => 'foo'], new InputDefinition([new InputArgument('name'), new InputArgument('bar', InputArgument::OPTIONAL, '', 'default')])); + $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('The "foo" argument does not exist.'); - $input = new ArrayInput(['name' => 'foo'], new InputDefinition([new InputArgument('name'), new InputArgument('bar', InputArgument::OPTIONAL, '', 'default')])); + $input->setArgument('foo', 'bar'); } public function testGetInvalidArgument() { + $input = new ArrayInput(['name' => 'foo'], new InputDefinition([new InputArgument('name'), new InputArgument('bar', InputArgument::OPTIONAL, '', 'default')])); + $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('The "foo" argument does not exist.'); - $input = new ArrayInput(['name' => 'foo'], new InputDefinition([new InputArgument('name'), new InputArgument('bar', InputArgument::OPTIONAL, '', 'default')])); + $input->getArgument('foo'); } public function testValidateWithMissingArguments() { - $this->expectException(\RuntimeException::class); - $this->expectExceptionMessage('Not enough arguments (missing: "name").'); $input = new ArrayInput([]); $input->bind(new InputDefinition([new InputArgument('name', InputArgument::REQUIRED)])); + + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('Not enough arguments (missing: "name").'); + $input->validate(); } public function testValidateWithMissingRequiredArguments() { - $this->expectException(\RuntimeException::class); - $this->expectExceptionMessage('Not enough arguments (missing: "name").'); $input = new ArrayInput(['bar' => 'baz']); $input->bind(new InputDefinition([new InputArgument('name', InputArgument::REQUIRED), new InputArgument('bar', InputArgument::OPTIONAL)])); + + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('Not enough arguments (missing: "name").'); + $input->validate(); } diff --git a/Tests/Logger/ConsoleLoggerTest.php b/Tests/Logger/ConsoleLoggerTest.php index 41205d793..43d779631 100644 --- a/Tests/Logger/ConsoleLoggerTest.php +++ b/Tests/Logger/ConsoleLoggerTest.php @@ -137,8 +137,7 @@ public static function provideLevelsAndMessages() public function testThrowsOnInvalidLevel() { $this->expectException(InvalidArgumentException::class); - $logger = $this->getLogger(); - $logger->log('invalid level', 'Foo'); + $this->getLogger()->log('invalid level', 'Foo'); } public function testContextReplacement() diff --git a/Tests/Tester/CommandTesterTest.php b/Tests/Tester/CommandTesterTest.php index 3fec7df2d..ce0a24b99 100644 --- a/Tests/Tester/CommandTesterTest.php +++ b/Tests/Tester/CommandTesterTest.php @@ -154,8 +154,6 @@ public function testCommandWithDefaultInputs() public function testCommandWithWrongInputsNumber() { - $this->expectException(\RuntimeException::class); - $this->expectExceptionMessage('Aborted.'); $questions = [ 'What\'s your name?', 'How are you?', @@ -174,13 +172,15 @@ public function testCommandWithWrongInputsNumber() $tester = new CommandTester($command); $tester->setInputs(['a', 'Bobby', 'Fine']); + + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('Aborted.'); + $tester->execute([]); } public function testCommandWithQuestionsButNoInputs() { - $this->expectException(\RuntimeException::class); - $this->expectExceptionMessage('Aborted.'); $questions = [ 'What\'s your name?', 'How are you?', @@ -198,6 +198,10 @@ public function testCommandWithQuestionsButNoInputs() }); $tester = new CommandTester($command); + + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('Aborted.'); + $tester->execute([]); }