PHP code example of codeception / assert-throws

1. Go to this page and download the library: Download codeception/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/ */

    

codeception / assert-throws example snippets




class MyTest extends PHPUnit\Framework\TestCase
{
    use Codeception\AssertThrows;

    //...
} 



$this->assertThrows(NotFoundException::class, function() {
	$this->userController->show(99);
});

// alternatively
$this->assertThrows(new NotFoundException(), function() {
	$this->userController->show(99);
});

// you can also assert that an exception is not throw
$this->assertDoesNotThrow(NotFoundException::class, function() {
    $this->userController->show(99);
});



$this->assertThrowsWithMessage(
    NotFoundException::class, 'my error message', function() {
	throw new NotFoundException('my error message');
    }
);