ZF-5721: Annoying to debug test cases, Zend_Test_PHPUnit::dispatch() never throws FC exceptions
Description
Zend_Test_PHPUnit::dispatch() will always set the FC throw exceptions flag to false, this makes debugging test cases slightly more annoying and lengthy. Allowing the front controller to throw exceptions when using this method would help when tracking down any bugs in the text cases and/or the bootstraping required for testing controllers.
Here's a diff with a trivial fix
diff --git a/library/Zend/Test/PHPUnit/ControllerTestCase.php b/library/Zend/Test/PHPUnit/ControllerTestCase.php index 3885e00..67ecf76 100644 --- a/library/Zend/Test/PHPUnit/ControllerTestCase.php +++ b/library/Zend/Test/PHPUnit/ControllerTestCase.php @@ -146,7 +146,7 @@ abstract class Zend_Test_PHPUnit_ControllerTestCase extends PHPUnit_Framework_Te * @param string|null $url * @return void */ - public function dispatch($url = null) + public function dispatch($url = null, $throwExceptions = false) { // redirector should not exit $redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector'); @@ -165,7 +165,7 @@ abstract class Zend_Test_PHPUnit_ControllerTestCase extends PHPUnit_Framework_Te $this->frontController ->setRequest($request) ->setResponse($this->getResponse()) - ->throwExceptions(false) + ->throwExceptions($throwExceptions) ->returnResponse(false); $this->frontController->dispatch(); }
Comments
Posted by Graham Anderson (gnanderson) on 2009-02-10T03:38:47.000+0000
oops, wikified version of the diff
diff --git a/library/Zend/Test/PHPUnit/ControllerTestCase.php b/library/Zend/Test/PHPUnit/ControllerTestCase.php index 3885e00..67ecf76 100644 --- a/library/Zend/Test/PHPUnit/ControllerTestCase.php +++ b/library/Zend/Test/PHPUnit/ControllerTestCase.php @@ -146,7 +146,7 @@ abstract class Zend_Test_PHPUnit_ControllerTestCase extends PHPUnit_Framework_Te * @param string|null $url * @return void */ - public function dispatch($url = null) + public function dispatch($url = null, $throwExceptions = false) { // redirector should not exit $redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector'); @@ -165,7 +165,7 @@ abstract class Zend_Test_PHPUnit_ControllerTestCase extends PHPUnit_Framework_Te $this->frontController ->setRequest($request) ->setResponse($this->getResponse()) - ->throwExceptions(false) + ->throwExceptions($throwExceptions) ->returnResponse(false); $this->frontController->dispatch(); }Posted by Giorgio Sironi (giorgiosironi) on 2009-02-17T08:22:19.000+0000
I also had the same problem. IMHO the throwExceptions argument might be true by default. Test are executed in a command line environment, so there's no point is wrapping an exception in a error controller or similar one.
Posted by Matthew Weier O'Phinney (matthew) on 2009-02-17T09:13:26.000+0000
You know that you can always check the request object for the error_handler parameter, and pull the exception from that object, right? ;)
Posted by Giorgio Sironi (giorgiosironi) on 2009-02-24T00:40:13.000+0000
...OK (127 tests, 331 assertions) I have many integration tests and I just want to know quickly is something has exploded and front controller is serving a beatiful page (with menu and header and a nice formatted 40+ rows stacktrace in the middle) instead of the correct page. :)
Posted by Mike Rheinheimer (rott) on 2009-04-18T20:59:21.000+0000
I threw in my vote for this issue too. I had a view partial that was trying to load a partialLoop file that was unable to be found:
The directory where viewPartialLoop.phtml is located was not in my include path. The exception I should have recieved was:
My testcase was only doing this:
The error I got was much more obscure, because ControllerTestCase sets frontController->throwExceptions(false):
This problem turned out to be very difficult to debug. I had to fire up a server, and point my config, etc, to the proper databases, and hit that controller/action to see the real problem. Alternatively, I could have just modified ControllerTestCase just long enough to see the real issue.
I think there are three possible solutions:
1) do as Matthew says and check the request object for the error_handler param and pull any exceptions (for EVERY controller dispatch test, ugh) 2) extend the ControllerTestCase class and override the dispatch function (again, ugh -- because ControllerTestCase->dispatch could change in the future) 3) ControllerTestCase should allow users to specify the 'throwExceptions" for the frontController object (PREFERRED -- and this is what I was trying to do in various ways until I finally looked at the ControllerTestCase->dispatch function)
That's my 2 cents. Thanks!
Posted by Mike Rheinheimer (rott) on 2009-04-18T21:46:24.000+0000
The ideal solution is to perhaps let me do this:
Posted by Benjamin Eberlei (beberlei) on 2009-09-18T01:17:11.000+0000
@Matthew:
how about adding a second parameter:
Posted by Michelangelo van Dam (dragonbe) on 2010-07-17T12:31:44.000+0000
For as long as I've been using ZF, I always have my testing environment report, log errors and throw exceptions.
The minimum I have set up in my "testing" section of my application.ini is the following:
One point of mentioning is that I use PHPUnit on CLI or within a CI system, but that shouldn't make a difference since tests are tests, no matter where they're run.
Posted by Aaron S. Hawley (ashawley) on 2011-02-11T08:22:08.000+0000
I agree that this should be fixed. Why not just let the user decide.
Index: library/Zend/Test/PHPUnit/ControllerTestCase.php =================================================================== --- library/Zend/Test/PHPUnit/ControllerTestCase.php (revision 23692) +++ library/Zend/Test/PHPUnit/ControllerTestCase.php (working copy) @@ -196,7 +196,6 @@ $this->frontController ->setRequest($request) ->setResponse($this->getResponse()) - ->throwExceptions(false) ->returnResponse(false); if ($this->bootstrap instanceof Zend_Application) {Posted by Andras Gyomrey (andras) on 2011-04-06T20:13:18.000+0000
Please, this isn't a huge change. Even the patch is provided. When is it going to be commited?
Posted by Matt Makins (makiemoo) on 2012-06-08T10:47:42.000+0000
Like Matthew Weier O'Phinney states use the error_handler param for debugging.
This works in my tests:
// get the errors $errors = $this->request->getParam('error_handler'); // throw the exception so it is apparent on cli when running tests throw new Zend_Exception($errors->exception);
Posted by Aaron S. Hawley (ashawley) on 2012-06-10T04:04:28.000+0000
Adding two-lines of code to unit tests is just an admission that this is indeed broken.
I want to use the config file as [~dragonbe] suggests, not add two-lines of code to every unit test or worse have to have maintain a base class that my unit tests inherit from.