From f72783790a3c7f89b9a738adf1a2f5d43a1ca90b Mon Sep 17 00:00:00 2001 From: Raphael Stolt Date: Wed, 24 Jul 2024 16:02:37 +0200 Subject: [PATCH] feat: add `assertFaulty` assertion (#21) --- README.md | 2 +- src/CommandResult.php | 7 +++++++ tests/FunctionalTest.php | 1 + 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 582efc8..c36269f 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,7 @@ class CreateUserCommandTest extends KernelTestCase // test command throws exception $this->consoleCommand(CreateUserCommand::class) ->expectException(\RuntimeException::class, 'Username required!') - ->assertStatusCode(1) + ->assertStatusCode(1) // equivalent to ->assertFaulty() ->assertOutputContains('Could not create user!') // can still make assertions on output before exception was thrown ; diff --git a/src/CommandResult.php b/src/CommandResult.php index ea467ad..00153de 100644 --- a/src/CommandResult.php +++ b/src/CommandResult.php @@ -80,6 +80,13 @@ public function assertSuccessful(): self return $this->assertStatusCode(0); } + public function assertFaulty(): self + { + Assert::that($this->statusCode() > 0); + + return $this; + } + public function assertStatusCode(int $expected): self { Assert::that($this->statusCode())->is($expected); diff --git a/tests/FunctionalTest.php b/tests/FunctionalTest.php index 30acf64..630c2bf 100644 --- a/tests/FunctionalTest.php +++ b/tests/FunctionalTest.php @@ -165,6 +165,7 @@ public function can_expect_exception(): void ->expectException(\RuntimeException::class, 'Exception thrown!') ->execute() ->assertStatusCode(1) + ->assertFaulty() ->assertOutputContains('Executing command...') ->assertOutputContains('Error output.') ;