From 695cd9de116c7b7c2f686883d757f7e9e9533011 Mon Sep 17 00:00:00 2001 From: Yoann Renard Date: Fri, 3 Sep 2021 20:50:32 +0200 Subject: [PATCH] [Console] Add more context when CommandIsSuccessful fails --- .../Tester/Constraint/CommandIsSuccessful.php | 13 +++++++++++++ .../Constraint/CommandIsSuccessfulTest.php | 18 +++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Console/Tester/Constraint/CommandIsSuccessful.php b/src/Symfony/Component/Console/Tester/Constraint/CommandIsSuccessful.php index ee0d98a2d4c01..a473242376d0f 100644 --- a/src/Symfony/Component/Console/Tester/Constraint/CommandIsSuccessful.php +++ b/src/Symfony/Component/Console/Tester/Constraint/CommandIsSuccessful.php @@ -39,4 +39,17 @@ protected function failureDescription($other): string { return 'the command '.$this->toString(); } + + /** + * {@inheritdoc} + */ + protected function additionalFailureDescription($other): string + { + $mapping = [ + Command::FAILURE => 'Command failed.', + Command::INVALID => 'Command was invalid.', + ]; + + return $mapping[$other] ?? sprintf('Command returned exit status %d.', $other); + } } diff --git a/src/Symfony/Component/Console/Tests/Tester/Constraint/CommandIsSuccessfulTest.php b/src/Symfony/Component/Console/Tests/Tester/Constraint/CommandIsSuccessfulTest.php index a5aeda719bbbd..2de8ac3458160 100644 --- a/src/Symfony/Component/Console/Tests/Tester/Constraint/CommandIsSuccessfulTest.php +++ b/src/Symfony/Component/Console/Tests/Tester/Constraint/CommandIsSuccessfulTest.php @@ -26,15 +26,31 @@ public function testConstraint() $this->assertTrue($constraint->evaluate(Command::SUCCESS, '', true)); $this->assertFalse($constraint->evaluate(Command::FAILURE, '', true)); $this->assertFalse($constraint->evaluate(Command::INVALID, '', true)); + } + + /** + * @dataProvider providesUnsuccessful + */ + public function testUnsuccessfulCommand(string $expectedException, int $exitCode) + { + $constraint = new CommandIsSuccessful(); try { - $constraint->evaluate(Command::FAILURE); + $constraint->evaluate($exitCode); } catch (ExpectationFailedException $e) { $this->assertStringContainsString('Failed asserting that the command is successful.', TestFailure::exceptionToString($e)); + $this->assertStringContainsString($expectedException, TestFailure::exceptionToString($e)); return; } $this->fail(); } + + public function providesUnsuccessful(): iterable + { + yield 'Failed' => ['Command failed.', Command::FAILURE]; + yield 'Invalid' => ['Command was invalid.', Command::INVALID]; + yield 'Exit code 3' => ['Command returned exit status 3.', 3]; + } }