mirror of
https://github.com/pestphp/pest.git
synced 2026-03-06 15:57:21 +01:00
Merge pull request #933 from hungthai1401/throws_unless
[2.x] Add `throwsUnless`
This commit is contained in:
@ -105,6 +105,24 @@ final class TestCall
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Asserts that the test throws the given `$exceptionClass` when called if the given condition is false.
|
||||||
|
*
|
||||||
|
* @param (callable(): bool)|bool $condition
|
||||||
|
*/
|
||||||
|
public function throwsUnless(callable|bool $condition, string|int $exception, string $exceptionMessage = null, int $exceptionCode = null): self
|
||||||
|
{
|
||||||
|
$condition = is_callable($condition)
|
||||||
|
? $condition
|
||||||
|
: static fn (): bool => $condition;
|
||||||
|
|
||||||
|
if (! $condition()) {
|
||||||
|
return $this->throws($exception, $exceptionMessage, $exceptionCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Runs the current test multiple times with
|
* Runs the current test multiple times with
|
||||||
* each item of the given `iterable`.
|
* each item of the given `iterable`.
|
||||||
|
|||||||
@ -59,3 +59,37 @@ it('can just define the message if given condition is 1', function () {
|
|||||||
it('can just define the code if given condition is 1', function () {
|
it('can just define the code if given condition is 1', function () {
|
||||||
throw new Exception('Something bad happened', 1);
|
throw new Exception('Something bad happened', 1);
|
||||||
})->throwsIf(1, 1);
|
})->throwsIf(1, 1);
|
||||||
|
|
||||||
|
it('not catch exceptions if given condition is true', function () {
|
||||||
|
$this->assertTrue(true);
|
||||||
|
})->throwsUnless(true, Exception::class);
|
||||||
|
|
||||||
|
it('catch exceptions if given condition is false', function () {
|
||||||
|
throw new Exception('Something bad happened');
|
||||||
|
})->throwsUnless(function () {
|
||||||
|
return false;
|
||||||
|
}, Exception::class);
|
||||||
|
|
||||||
|
it('catch exceptions and messages if given condition is false', function () {
|
||||||
|
throw new Exception('Something bad happened');
|
||||||
|
})->throwsUnless(false, Exception::class, 'Something bad happened');
|
||||||
|
|
||||||
|
it('catch exceptions, messages and code if given condition is false', function () {
|
||||||
|
throw new Exception('Something bad happened', 1);
|
||||||
|
})->throwsUnless(false, Exception::class, 'Something bad happened', 1);
|
||||||
|
|
||||||
|
it('can just define the message if given condition is false', function () {
|
||||||
|
throw new Exception('Something bad happened');
|
||||||
|
})->throwsUnless(false, 'Something bad happened');
|
||||||
|
|
||||||
|
it('can just define the code if given condition is false', function () {
|
||||||
|
throw new Exception('Something bad happened', 1);
|
||||||
|
})->throwsUnless(false, 1);
|
||||||
|
|
||||||
|
it('can just define the message if given condition is 0', function () {
|
||||||
|
throw new Exception('Something bad happened');
|
||||||
|
})->throwsUnless(0, 'Something bad happened');
|
||||||
|
|
||||||
|
it('can just define the code if given condition is 0', function () {
|
||||||
|
throw new Exception('Something bad happened', 1);
|
||||||
|
})->throwsUnless(0, 1);
|
||||||
|
|||||||
Reference in New Issue
Block a user