Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem testing exceptions with PHPUnit and Zend Framework

When a user accesses /user/validate without the correct post parameters, my Zend application throws a zend exception. (I get the standard "An Error Occurred" message, framed within my layout). This is intentional.

I am now trying to test that behaviour with PHPUnit. Here's my test:

/**
 * @expectedException Zend_Exception
 */
public function testEmptyUserValidationParametersCauseException() {
    $this->dispatch('/user/validate');
}

When I run the test I get a message saying it failed, "Expected exception Zend_Exception". Any ideas?

I have other tests in the file which are working just fine...

Thanks!

like image 447
DatsunBing Avatar asked Jul 01 '11 08:07

DatsunBing


2 Answers

The Zend_Controller_Plugin_ErrorHandler plugin handles exceptions for you and the default Error_Controller forces a 500 redirect which may mean the exception you are testing for no longer exists. Try the following unit test and see if it passes:

public function testUnknownUserRedirectsToErrorPage()
{
    $this->dispatch('/user/validate');
    $this->assertController('error');
    $this->assertAction('error');
    $this->assertResponseCode('500');
}

If this works then it shows that by the time you are rendering the error view the exception will no longer exist as it is contained in the code before the redirect.

like image 98
Tom Jowitt Avatar answered Nov 07 '22 23:11

Tom Jowitt


Well, maybe it simply fails because no exception is thrown?

Did you try running the test without "@expectedException Zend_Exception"? Is there an exception at all?

like image 2
wonk0 Avatar answered Nov 07 '22 23:11

wonk0