Skip to content

Commit

Permalink
Add assertion to check that a given command was not executed (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkniest authored Nov 24, 2020
1 parent 816ed67 commit 6bc03e3
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
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

0 comments on commit 6bc03e3

Please sign in to comment.