Simplified predictions if a piece of codes throws an Exception
$ composer require try/predict-exception
<?php
require_once '/path/to/autoload.php';
use TryPhp\PredictExeptionTrait;
$assertions = new class() {
use PredictExeptionTrait();
}
$assertions->predictException(function () {
throw new \RuntimeException('Oooops. Something broke.')
}, \RuntimeException::class); // won't throw an exception
$assertions->predictException(function () {
}, \Exception::class); // will throw an exception
$assertions->predictException(function () {
throw new \RuntimeException('something else happened.');
}, \Exception::class); // will throw an exception
Method to check if a given piece of code throws an Throwable of the expected type.
Arguments | Type | Description |
---|---|---|
$capture | callable |
Closure in which the Throwable shall be thrown. |
$exceptionClass | string |
Class of the Throwable that is expected to be thrown. |
GPL-2.0 © Willi Eßer