feat: Add code to the throws arg

This commit is contained in:
Kohei Matsumoto
2022-03-04 13:05:55 +09:00
parent e08d6f2803
commit 0e85921964
2 changed files with 32 additions and 4 deletions

View File

@ -45,9 +45,11 @@ final class TestCall
/**
* Asserts that the test throws the given `$exceptionClass` when called.
*/
public function throws(string $exception, string $exceptionMessage = null): TestCall
public function throws(string|int $exception, string $exceptionMessage = null, int $exceptionCode = null): TestCall
{
if (class_exists($exception)) {
if (is_int($exception)) {
$exceptionCode = $exception;
} else if (class_exists($exception)) {
$this->testCaseMethod
->proxies
->add(Backtrace::file(), Backtrace::line(), 'expectException', [$exception]);
@ -61,6 +63,12 @@ final class TestCall
->add(Backtrace::file(), Backtrace::line(), 'expectExceptionMessage', [$exceptionMessage]);
}
if (is_int($exceptionCode)) {
$this->testCaseMethod
->proxies
->add(Backtrace::file(), Backtrace::line(), 'expectExceptionCode', [$exceptionCode]);
}
return $this;
}
@ -69,7 +77,7 @@ final class TestCall
*
* @param (callable(): bool)|bool $condition
*/
public function throwsIf(callable|bool $condition, string $exception, string $exceptionMessage = null): TestCall
public function throwsIf(callable|bool $condition, string|int $exception, string $exceptionMessage = null, int $exceptionCode = null): TestCall
{
$condition = is_callable($condition)
? $condition
@ -78,7 +86,7 @@ final class TestCall
};
if ($condition()) {
return $this->throws($exception, $exceptionMessage);
return $this->throws($exception, $exceptionMessage, $exceptionCode);
}
return $this;