ZF-11589: PHP Fatal Error in Zend_Controller_Broker_PluginTest

Description

When running the Zend_Controller_Plugin test suite ({{./runtests.sh Controller_Plugin}}) I get the following error:


Starting test 'Zend_Controller_Plugin_BrokerTest::testBrokerCatchesExceptions'.
PHP Fatal error:  Call to a member function setDispatched() on a non-object in /usr/local/apache2/htdocs/lib/zfdev/trunk/library/Zend/Controller/Plugin/Broker.php on line 316

Comments

In unit test {{Zend_Controller_Plugin_BrokerTest::testBrokerCatchesExceptions}}, the broker instance is only given the request object as a parameter to the hook methods (in this case, {{preDispatch}}), and not injected via {{setRequest}}. This causes {{Zend_Controller_Plugin_Broker::preDispatch}} to throw a PHP fatal error because it calls {{$this->getRequest()->setDispatched(false)}}:


public function preDispatch(Zend_Controller_Request_Abstract $request)
{
    foreach ($this->_plugins as $plugin) {
        try {
            $plugin->preDispatch($request);
        } catch (Exception $e) {
            if (Zend_Controller_Front::getInstance()->throwExceptions()) {
                throw new Zend_Controller_Exception($e->getMessage() . $e->getTraceAsString(), $e->getCode(), $e);
            } else {
                $this->getResponse()->setException($e);
                // skip rendering of normal dispatch give the error handler a try
                $this->getRequest()->setDispatched(false);     // <---- ERROR IS TRIGGERED HERE
            }
        }
    }
}

and {{getRequest()}} returns null as {{setRequest}} wasn't given a request instance.

As I see it, there are two ways to fix this: 1. Fix the unit test: Inject {{$request}} via {{setRequest}}


Index: tests/Zend/Controller/Plugin/BrokerTest.php
===================================================================
--- tests/Zend/Controller/Plugin/BrokerTest.php (revision 24251)
+++ tests/Zend/Controller/Plugin/BrokerTest.php (working copy)
@@ -186,6 +186,7 @@
         $response = new Zend_Controller_Response_Cli();
         $broker   = new Zend_Controller_Plugin_Broker();
         $broker->setResponse($response);
+        $broker->setRequest($request);
         $broker->registerPlugin(new Zend_Controller_Plugin_BrokerTest_ExceptionTestPlugin());
         try {
             $broker->routeStartup($request);
  1. Change {{Zend_Controller_Plugin_Broker::preDispatch}} to call {{setDispatched}} directly it's argument {{$request}}:

Index: library/Zend/Controller/Plugin/Broker.php
===================================================================
--- library/Zend/Controller/Plugin/Broker.php   (revision 24251)
+++ library/Zend/Controller/Plugin/Broker.php   (working copy)
@@ -313,7 +313,7 @@
                 } else {
                     $this->getResponse()->setException($e);
                                        // skip rendering of normal dispatch give the error handler a try
-                                       $this->getRequest()->setDispatched(false);
+                                       $request->setDispatched(false);
                 }
             }
         }

(Yes, that line and comment above it are tabbed and not space-indented. That should be fixed)

Thoughts? Both options fix this specific issue, but which fix is best overall (ie: is this an implementation issue with the broker or a just a test construction issue?)

Fixed by r24295