diff --git a/src/Symfony/Component/Console/CHANGELOG.md b/src/Symfony/Component/Console/CHANGELOG.md index 0ff71d2faf48..92b81819d5ea 100644 --- a/src/Symfony/Component/Console/CHANGELOG.md +++ b/src/Symfony/Component/Console/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +7.1 +--- + + * Add `assertCommandIsFaulty` assertion + 7.0 --- diff --git a/src/Symfony/Component/Console/Tester/Constraint/CommandIsFaulty.php b/src/Symfony/Component/Console/Tester/Constraint/CommandIsFaulty.php new file mode 100644 index 000000000000..8f8e09728a32 --- /dev/null +++ b/src/Symfony/Component/Console/Tester/Constraint/CommandIsFaulty.php @@ -0,0 +1,40 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Console\Tester\Constraint; + +use PHPUnit\Framework\Constraint\Constraint; +use Symfony\Component\Console\Command\Command; + +final class CommandIsFaulty extends Constraint +{ + public function toString(): string + { + return 'is faulty'; + } + + protected function matches($other): bool + { + return Command::FAILURE === $other || Command::INVALID === $other; + } + + protected function failureDescription($other): string + { + return 'the command '.$this->toString(); + } + + protected function additionalFailureDescription($other): string + { + return Command::SUCCESS === $other + ? 'Command was successful.' + : sprintf('Command returned exit status %d.', $other); + } +} diff --git a/src/Symfony/Component/Console/Tester/TesterTrait.php b/src/Symfony/Component/Console/Tester/TesterTrait.php index 1ab7a70aa22d..079848756ffd 100644 --- a/src/Symfony/Component/Console/Tester/TesterTrait.php +++ b/src/Symfony/Component/Console/Tester/TesterTrait.php @@ -16,6 +16,7 @@ use Symfony\Component\Console\Output\ConsoleOutput; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\StreamOutput; +use Symfony\Component\Console\Tester\Constraint\CommandIsFaulty; use Symfony\Component\Console\Tester\Constraint\CommandIsSuccessful; /** @@ -104,6 +105,11 @@ public function assertCommandIsSuccessful(string $message = ''): void Assert::assertThat($this->statusCode, new CommandIsSuccessful(), $message); } + public function assertCommandIsFaulty(string $message = ''): void + { + Assert::assertThat($this->statusCode, new CommandIsFaulty(), $message); + } + /** * Sets the user inputs. * diff --git a/src/Symfony/Component/Console/Tests/Tester/Constraint/CommandIsFaultyTest.php b/src/Symfony/Component/Console/Tests/Tester/Constraint/CommandIsFaultyTest.php new file mode 100644 index 000000000000..0f11458e523e --- /dev/null +++ b/src/Symfony/Component/Console/Tests/Tester/Constraint/CommandIsFaultyTest.php @@ -0,0 +1,44 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Console\Tests\Tester\Constraint; + +use PHPUnit\Framework\ExpectationFailedException; +use PHPUnit\Framework\TestCase; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Tester\Constraint\CommandIsFaulty; + +final class CommandIsFaultyTest extends TestCase +{ + public function testConstraint() + { + $constraint = new CommandIsFaulty(); + + $this->assertFalse($constraint->evaluate(Command::SUCCESS, returnResult: true)); + $this->assertTrue($constraint->evaluate(Command::FAILURE, returnResult: true)); + $this->assertTrue($constraint->evaluate(Command::INVALID, returnResult: true)); + } + + public function testSuccessfulCommand() + { + $constraint = new CommandIsFaulty(); + + try { + $constraint->evaluate(Command::SUCCESS); + } catch (ExpectationFailedException $e) { + $this->assertStringContainsString('Failed asserting that the command is faulty.', $e->getMessage()); + + return; + } + + $this->fail(); + } +}