-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from kbond/expect-exception
Add `TestCommand::expectException()`
- Loading branch information
Showing
4 changed files
with
106 additions
and
12 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
use Symfony\Component\Console\Application; | ||
use Symfony\Component\Console\Command\Command; | ||
use Zenstruck\Assert; | ||
|
||
/** | ||
* @author Kevin Bond <[email protected]> | ||
|
@@ -15,6 +16,10 @@ final class TestCommand | |
|
||
/** @var string[] */ | ||
private array $inputs = []; | ||
|
||
/** @var callable|class-string|null */ | ||
private $expectedException; | ||
private ?string $expectedExceptionMessage = null; | ||
private bool $splitOutputStreams = false; | ||
|
||
private function __construct(Command $command, string $cli) | ||
|
@@ -96,6 +101,25 @@ public function withInput(array $inputs): self | |
return $this; | ||
} | ||
|
||
/** | ||
* Expect executing the command will throw this exception. Fails if not thrown. | ||
* | ||
* @param class-string|callable $expectedException string: class name of the expected exception | ||
* callable: uses the first argument's type-hint | ||
* to determine the expected exception class. When | ||
* exception is caught, callable is invoked with | ||
* the caught exception | ||
* @param string|null $expectedMessage Assert the caught exception message "contains" | ||
* this string | ||
*/ | ||
public function expectException($expectedException, ?string $expectedMessage = null): self | ||
{ | ||
$this->expectedException = $expectedException; | ||
$this->expectedExceptionMessage = $expectedMessage; | ||
|
||
return $this; | ||
} | ||
|
||
public function execute(?string $cli = null): CommandResult | ||
{ | ||
$autoExit = $this->application->isAutoExitEnabled(); | ||
|
@@ -105,7 +129,7 @@ public function execute(?string $cli = null): CommandResult | |
$this->application->setAutoExit(false); | ||
$this->application->setCatchExceptions(false); | ||
|
||
$status = $this->application->run( | ||
$status = $this->doRun( | ||
$input = new TestInput($cli, $this->inputs), | ||
$output = new TestOutput($this->splitOutputStreams, $input) | ||
); | ||
|
@@ -115,4 +139,17 @@ public function execute(?string $cli = null): CommandResult | |
|
||
return new CommandResult($cli, $status, $output); | ||
} | ||
|
||
private function doRun(TestInput $input, TestOutput $output): int | ||
{ | ||
$fn = fn() => $this->application->run($input, $output); | ||
|
||
if (!$this->expectedException) { | ||
return $fn(); | ||
} | ||
|
||
Assert::that($fn)->throws($this->expectedException, $this->expectedExceptionMessage); | ||
|
||
return 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters