Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add assertion to check that a given command was not executed #24

Merged
merged 1 commit into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 11 additions & 0 deletions src/Fakes/BuilderFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
41 changes: 41 additions & 0 deletions tests/FakeTerminalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand All @@ -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([
Expand Down