phpunit assert exception thrown code example
Example 1: phpunit expect exception
<?php
require_once 'PHPUnit/Framework.php';
class ExceptionTest extends PHPUnit_Framework_TestCase
{
public function testException()
{
$this->expectException(InvalidArgumentException::class);
// or for PHPUnit < 5.2
// $this->setExpectedException(InvalidArgumentException::class);
//...and then add your test code that generates the exception
exampleMethod($anInvalidArgument);
}
}
Example 2: phpunit check exception not thrown
$this->addToAssertionCount(1); // does not throw an exception