PHPUnit cannot find exception class PHPUnit cannot find exception class symfony symfony

PHPUnit cannot find exception class


You need to fully qualify the exception class along with its namespace. For ex:

$this->setExpectedException('\AssetManagerBundle\Services\Exceptions\ImageResizerException');

or

use AssetManagerBundle\Services\Exceptions\ImageResizerException;$exceptionClass = get_class(new ImageResizerException(''));$this->setExpectedException($exceptionClass);


You need to use the FQCN of the ImageResizerException exception:

AssetManagerBundle\Services\Exceptions\ImageResizerException

The use clause on top of the file is for that file only - not for PHPUnit which has it's code in some other files.

Correction: It's not (only) that the use clause does not work for PHPUnit but because that ReflectionClass expects a FQCN. That is similar when use a variable (dynamic) class-name in PHP like new $var:

<?phpnamespace Sugar {    class Exception extends \Exception {}}namespace {    use Sugar\Exception;    $class = 'Sugar\Exception';    $e = new $class;    var_dump($e);}

Output:

object(Sugar\Exception)#1 (7) {  ["message":protected]=>  string(0) ""  ["string":"Exception":private]=>  string(0) ""  ["code":protected]=>  int(0)  ["file":protected]=>  string(45) "/tmp/execpad-7141d0116de7/source-7141d0116de7"  ["line":protected]=>  int(10)  ["trace":"Exception":private]=>  array(0) {  }  ["previous":"Exception":private]=>  NULL}

Demo: http://eval.in/7883


Instead of writing the class name, use the full namespace of the class instead. Then you will not get the 'class doesn't exist' exception.