While working on updating my unit tests from PHPUnit v3.2 to v3.6, I came across the error message; “You must not expect the generic exception class”, upon first glance it sort of made sense, but after some thought and investigation, it really doesn’t make much sense. My preference is to throw exceptions, and codes, versus throwing specific exceptions, and the code which threw the error was doing just that, throwing a generic exception with an exception code. I spoke with the PHPUnit developer via github, and he stated that a fix is in 3.7 but will not added in 3.6 (current stable). Well I can’t wait for 3.7 to be released so I came up with the following solution:


/**
 * @expectedException Test_Exception
 */
 public function testDivByZero()
 {
     try {
         // Fyi you don't need to do an assert test here, as we are only testing the exception, so just make the call
         $result = $this->object->div(1,0);
     } catch (Exception $e) {
         if ('Exception' === get_class($e)) {
             throw new Test_Exception($e->getMessage(), $e->getCode());
         }
     }
 }

// Test_Exception.php
class Test_Exception extends Exception
{
    public function __construct($message = null, $code = 0, Exception $previous = null)
    {
        parent::__construct($message, $code, $previous);
    }
}

Basically all I did was create my own exception class, then when testing if an api call throws an exception, I would check the class of the exception, and if it was a ‘generic’ (Exception) class, then I would wrap it within the custom, expected Test_Exception.