From 6bc03e367f16e72c7dc36a803020bd7b5a1625e3 Mon Sep 17 00:00:00 2001 From: Jordan Kniest Date: Tue, 24 Nov 2020 16:29:13 +0100 Subject: [PATCH] Add assertion to check that a given command was not executed (#24) --- README.md | 8 ++++++++ src/Fakes/BuilderFake.php | 11 ++++++++++ tests/FakeTerminalTest.php | 41 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) diff --git a/README.md b/README.md index b12d7fd..caefa4f 100644 --- a/README.md +++ b/README.md @@ -371,6 +371,14 @@ Terminal::run('php artisan migrate'); Terminal::assertExecuted('php artisan migrate'); ``` +Alternatively you can also check that a given command was not executed. You may accomplish this by calling the `Terminal::assertNotExecuted` method after calling `Terminal::fake`. + +```php +Terminal::fake(); + +Terminal::assertNotExecuted('php artisan migrate'); +``` + ### Mocking Symfony Process If you need to mock the underlying Symfony's Process, you may use the Terminal's `response` method. diff --git a/src/Fakes/BuilderFake.php b/src/Fakes/BuilderFake.php index 6901948..8959427 100644 --- a/src/Fakes/BuilderFake.php +++ b/src/Fakes/BuilderFake.php @@ -123,4 +123,15 @@ public static function assertExecuted($command, int $times = 1) 'The command was executed %s times instead of expected %s times.', $count, $times )); } + + /** + * Assert that a given command was not executed. + * + * @param mixed $command + * @return void + */ + public static function assertNotExecuted($command) + { + self::assertExecuted($command, 0); + } } diff --git a/tests/FakeTerminalTest.php b/tests/FakeTerminalTest.php index b22c1ff..9255c56 100644 --- a/tests/FakeTerminalTest.php +++ b/tests/FakeTerminalTest.php @@ -5,6 +5,7 @@ use Mockery; use DateTime; use DateInterval; +use PHPUnit\Framework\ExpectationFailedException; use TitasGailius\Terminal\Builder; use TitasGailius\Terminal\Terminal; use Symfony\Component\Process\Process; @@ -40,6 +41,23 @@ public function testCaptureAndAssertExecuted() Terminal::assertExecuted($expected); } + /** + * Test that Terminal can assert that a given command was not executed. + * + * @return void + */ + public function testAssertNotExecuted() + { + Terminal::fake(); + + Terminal::assertNotExecuted($expected = 'echo "Hello, World"'); + + Terminal::execute($expected); + + $this->expectException(ExpectationFailedException::class); + Terminal::assertNotExecuted($expected = 'echo "Hello, World"'); + } + /** * Test that Terminal can capture and assert executions using a custom filter. * @@ -56,6 +74,29 @@ public function testCaptureAndAssertExecutedUsingCustomFilter() }); } + /** + * Test that Terminal can assert that a given command was not executed using a custom filter. + * + * @return void + */ + public function testAssertNotExecutedUsingCustomFilter() + { + Terminal::fake(); + + $expected = 'echo "Hello, World"'; + + Terminal::assertNotExecuted(function ($captured) use ($expected) { + return $captured->toString() == $expected; + }); + + Terminal::execute($expected); + + $this->expectException(ExpectationFailedException::class); + Terminal::assertNotExecuted(function ($captured) use ($expected) { + return $captured->toString() == $expected; + }); + } + public function testResponseLinesAsStrings() { Terminal::fake([