mirror of
https://github.com/pestphp/pest.git
synced 2026-03-12 10:47:25 +01:00
Merge pull request #339 from pestphp/exception-improvements
Allows you to just specify an exception message when calling `throws`.
This commit is contained in:
@ -59,11 +59,15 @@ final class TestCall
|
|||||||
/**
|
/**
|
||||||
* Asserts that the test throws the given `$exceptionClass` when called.
|
* Asserts that the test throws the given `$exceptionClass` when called.
|
||||||
*/
|
*/
|
||||||
public function throws(string $exceptionClass, string $exceptionMessage = null): TestCall
|
public function throws(string $exception, string $exceptionMessage = null): TestCall
|
||||||
{
|
{
|
||||||
|
if (class_exists($exception)) {
|
||||||
$this->testCaseFactory
|
$this->testCaseFactory
|
||||||
->proxies
|
->proxies
|
||||||
->add(Backtrace::file(), Backtrace::line(), 'expectException', [$exceptionClass]);
|
->add(Backtrace::file(), Backtrace::line(), 'expectException', [$exception]);
|
||||||
|
} else {
|
||||||
|
$exceptionMessage = $exception;
|
||||||
|
}
|
||||||
|
|
||||||
if (is_string($exceptionMessage)) {
|
if (is_string($exceptionMessage)) {
|
||||||
$this->testCaseFactory
|
$this->testCaseFactory
|
||||||
|
|||||||
@ -13,3 +13,7 @@ it('catch exceptions', function () {
|
|||||||
it('catch exceptions and messages', function () {
|
it('catch exceptions and messages', function () {
|
||||||
throw new Exception('Something bad happened');
|
throw new Exception('Something bad happened');
|
||||||
})->throws(Exception::class, 'Something bad happened');
|
})->throws(Exception::class, 'Something bad happened');
|
||||||
|
|
||||||
|
it('can just define the message', function () {
|
||||||
|
throw new Exception('Something bad happened');
|
||||||
|
})->throws('Something bad happened');
|
||||||
|
|||||||
Reference in New Issue
Block a user