Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/TestTools.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Prophecy\Argument;
use Prophecy\Prophecy\MethodProphecy;
use Prophecy\Prophecy\ObjectProphecy;
use PHPUnit\Framework\Assert;

/**
* Class TestTools
Expand Down Expand Up @@ -131,4 +132,33 @@ public static function assertIsProphesizable($className)
throw new NotProphesizableException();
}
}

/**
* Allow multiple exception assertions within one test using a callable
*
* @param callable $callable
* @param string $exceptionClass
* @param int $exceptionCode
* @param string $exceptionMessage
*/
public static function assertCallableThrowsException(callable $callable, $exceptionClass, $exceptionCode = null, $exceptionMessage = null)
{
try {
$callable();
} catch (\Exception $exception) {
Assert::assertInstanceOf($exceptionClass, $exception);

if ($exceptionCode !== null) {
Assert::assertEquals($exceptionCode, $exception->getCode());
}

if ($exceptionMessage !== null) {
Assert::assertContains($exceptionMessage, $exception->getMessage());
}

return;
}

Assert::fail('Expected ' . $exceptionClass . ' but no exception was thrown');
}
}