1. Go to this page and download the library: Download cspray/assert-throws library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
cspray / assert-throws example snippets
declare(strict_types=1);
use Cspray\AssertThrows\ThrowableAssert;
use PHPUnit\Framework\TestCase;
final class MyTest extends TestCase {
public function testExceptionPreviousInstanceOf() : void {
$throwable = ThrowableAssert::assertThrows(
static fn() => throw new RuntimeException(previous: new BadMethodCallException())
);
self::assertInstanceOf(BadMethodCallException::class, $throwable->getPrevious());
}
}
use \Cspray\AssertThrows\ThrowableAssert;
$throwable = ThrowableAssert::assertThrows(static fn() => throw new RuntimeException());
$throwable = ThrowableAssert::assertThrowsExceptionType(
static fn() => throw new BadMethodCallException(),
BadMethodCallException::class
);
$throwable = ThrowableAssert::assertThrowsExceptionTypeWithMessage(
static fn() => throw new RuntimeException('My exception message'),
RuntimeException::class,
'My exception message'
);
use function Cspray\AssertThrows\assertThrows;
use function Cspray\AssertThrows\assertThrowsExceptionType;
use function Cspray\AssertThrows\assertThrowsExceptionTypeWithMessage;
$throwable = assertThrows(static fn() => throw new RuntimeException());
$throwable = assertThrowsExceptionType(
static fn() => throw new BadMethodCallException(),
BadMethodCallException::class
);
$throwable = assertThrowsExceptionTypeWithMessage(
static fn() => throw new RuntimeException('My exception message'),
RuntimeException::class,
'My exception message'
);
use Cspray\AssertThrows\ThrowableAssertTestCaseMethods;
use PHPUnit\Framework\TestCase;
class MyTestCase extends TestCase {
use ThrowableAssertTestCaseMethods;
public function testAssertThrows() : void {
$throwable = self::assertThrows(static fn() => new RuntimeException());
}
public function testAssertThrowsExceptionType() : void {
$throwable = self::assertThrowsExceptionType(
static fn() => throw new BadMethodCallException(),
BadMethodCallException::class
);
}
public function testAssertThrowsExceptionTypeWithMessage() : void {
$throwable = self::assertThrowsExceptionTypeWithMessage(
static fn() => throw new RuntimeException('My exception message'),
RuntimeException::class,
'My exception message'
);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.