4

We are currently putting unit tests around zend framework controllers.

(I've abstracted this code example a little bit, but the idea is the same....)

We've happily managed to make the test fail, with the error message

Failed asserting last controller used <"error"> ....

with the test:

$this->dispatch('/controller/action/param');
$this->assertController('controller');
$this->assertAction('action');

So in this instance, how do I get the real error message to bubble up to PHPUnit, i.e. if there is an error in the controller, I want to know about it rather than invoking the error controller.

If I set resources.frontController.params.noErrorHandler = 1 in the application.ini, the test passes even though there is an error, because that controller and action still occurs, but just outputs nothing (I know I could look for assertions in the output, but that's not the point - I want the original error).

I've tried turned on

convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true" 

in phpunit.xml as well, no joy.

Any pointers would be much appreciated.

I hope all of that made sense!?

Many thanks.

2 Answers 2

5

Unfortunately Zend_Test_PHPUnit_ControllerTestCase explicitly overrides some frontcontroller options during dispatch. The relevant bit of code is here:

$controller = $this->getFrontController();
$this->frontController
     ->setRequest($request)
     ->setResponse($this->getResponse())
     ->throwExceptions(false)
     ->returnResponse(false);

Because this is done inside dispatch() you have absolutely no chance of altering the options.

One solution I've used is to create my own base ControllerTestCase that extends the Zend one and copy the entire dispatch() function from the Zend object, then change that one line.

It's messy but it works. You could do a more elaborate version that read something from a config etc.

Sign up to request clarification or add additional context in comments.

2 Comments

Dumb question: which line of code needs to be altered, and to what?
@blainarmstrong pass true to throwExceptions
1

This is an issue I've run into as well, and it can be pretty difficult sometimes to track down the error. One solution I stumbled on by chance -- I modified the error controller to send an e-mail in the event of an error in our production environment, but I accidentally had it set to send in the testing environment as well -- and then when one of these errors happened on a unit test, I got an e-mail detailing the error.

You don't have to use e-mail notification, but you might want to look into error logging at http://framework.zend.com/manual/en/zend.log.writers.html. You could have the errors written to a log file and then check the log file.

1 Comment

Thanks - that will certainly tell me the problem (what is causing it), and a bit neater than putting debug lines into the code when trying to track it down. I'm glad I'm not the only one with this problem though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.