-- Tuan Ngo <[EMAIL PROTECTED]> wrote
(on Wednesday, 09 July 2008, 11:05 AM +0700):
> > Please update from svn, and try again!
> The update code from SVN works well now on example project and on one action 
> of
> my real project.
> It's great ! We will try it further.
> 
> > Running your tests now, I get errors -- looks like you may need to create an
> ErrorController.
> When I setup ErrorController (controller, action, view codes), I never got
> exception originated from wrong action name testcase any more but it seems 
> that
> I can't catch my own throwed exception such as:
> class SearchController extends Zend_Controller_Action {
>     public function mayThrowExceptionAction(){
>         throw new MyAppException();
>     }
> 
> I force frontcontroller to throw exception by 
> $frontController->throwExceptions
> (true);
> but it seems whatever setting to true or false, I never catch my own 
> exception.
> 
> So I try to remove ErrorController, I see my own throwed exception again.
> 
> So how do I still have ErrorController defined but still can catch my own
> throwed exception.

I actually turn off throwExceptions() in the test case dispatch()
method, so that approach won't work anyways. What I recommend is to have
your ErrorController have logic to determine the type of exception, and
have it then set the HTTP response code, something like this:

    class ErrorController extends Zend_Controller_Action
    {
        public function errorAction()
        {
            $errors = $this->_getParam('error_handler');

            switch ($errors->type) {
                case 
Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
                case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
                    // 404 error -- controller or action not found
                    $this->getResponse()->setHttpResponseCode(404);
                    $this->view->message = 'Page not found';
                    break;
                default:
                    // application error
                    $this->getResponse()->setHttpResponseCode(500);
                    $this->view->message   = 'Application error';
                    $this->view->exception = $errors->exception;
                    break;
            }
        }
    }

Then, in your test case, to determine if you had an application
exception, you can test for the response code:

    $this->assertResponseCode(500);

The error handler above, however, is a great way to report 404 and 500
errors, and allows you to make use of response codes in your assertions
to simplify determining if expected error conditions occurred.

If you really, really want to determine that the exception is of a
specific type, well, with the class definition above, you've set the
exception in the view, so you can now retrieve it from there:

    // using Zend_Layout object:
    $e = Zend_Layout::getMvcInstance()->getView()->exception;

    // via ViewRenderer:
    $e = 
Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->view->exception;

and from there, check to see the type:

    $this->assertTrue($e instanceof SomeFooException);

or simply re-throw it and use @expectedException in your docblock:

    throw $e;

-- 
Matthew Weier O'Phinney
Software Architect       | [EMAIL PROTECTED]
Zend Framework           | http://framework.zend.com/

Reply via email to