UnitTest Error Controller in Zend Framework

I'm a fan of 100% code coverage, but I don't know how to test ErrorController in the Zend Framework.

No problem checking 404Action and errorAction:

    public function testDispatchErrorAction()
    {
        $this->dispatch('/error/error');
        $this->assertResponseCode(200);
        $this->assertController('error');
        $this->assertAction('error');
    }

    public function testDispatch404()
    {
        $this->dispatch('/error/errorxxxxx');
        $this->assertResponseCode(404);
        $this->assertController('error');
        $this->assertAction('error');
    }

But how to test application error (500)? maybe i need something like that?

public function testDispatch500()
{
    throw new Exception('test');

    $this->dispatch('/error/error');
    $this->assertResponseCode(500);
    $this->assertController('error');
    $this->assertAction('error');

}
+5
source share
2 answers

This is an old question, but I struggled with it today and could not find a good answer anywhere, so I will continue and publish what I did to solve this problem. The answer is actually very simple.

Direct the dispatch to an action that will throw exceptions.

My application throws an error when receiving a request to the JSON endpoint, so I used one of them to verify this.

   /**
   * @covers  ErrorController::errorAction
   */
    public function testErrorAction500() {
        /**
         * Requesting a page that doesn't exist returns the proper error message 
         */
        $this->dispatch('/my-json-controller/json-end-point');
        $body = $this->getResponse()->getBody();
        $this->assertResponseCode('500');
        $this->assertContains('Application error',$body);
    }

, , , unit test.

public function errorAction() {
    throw new Exception('You should not be here');
}

:

   /**
   * @covers  ErrorController::errorAction
   */
    public function testErrorAction500() {
        /**
         * Requesting a page that doesn't exist returns the proper error message 
         */
        $this->dispatch('/my-error-controller/error');
        $body = $this->getResponse()->getBody();
        $this->assertResponseCode('500');
        $this->assertContains('Application error',$body);
    }
+1

, , ErrorHandler ( , ). , .

0

All Articles