PHPUnit provides helper functions and annotations to verify that a given block of code throws an exception and to assert different properties of that exception. The provided helper functions are:

* ``++expectException()++``
* ``++expectExceptionCode()++``
* ``++expectExceptionMessage()++``
* ``++expectExceptionMessageRegExp()++``

This check raises an issue when the throw of an exception is verified using a _try-catch_ approach instead of relying on the provided helper functions.


== Noncompliant Code Example

[source,php]
----
public function testA()
{
    try {
        doSomething();
        $this->fail("Assertion should have been thrown");
    } catch (MyException $e) {
        assertEquals("Exception message", $e->getMessage());
    }
}
----


== Compliant Solution

[source,php]
----
public function testB()
{
    $this->expectException(MyException::class);
    $this->expectExceptionMessage("Exception message");

    doSomething();
}
----


== See

thePHP.cc: https://thephp.cc/articles/questioning-phpunit-best-practices[Questioning PHPUnit Best Practice]

