2

I had a simple controller action

class CatalogController extends AbstractActionController {

    public function indexAction() {
        return new ViewModel();
    }
    // ...
}

and a unit test for it:

class CatalogControllerTest extends AbstractHttpControllerTestCase
{
    public function testIndexActionCanBeAccessed()
    {
        $this->routeMatch->setParam('action', 'index');
        $result   = $this->controller->dispatch($this->request);
        $response = $this->controller->getResponse();
        $this->assertEquals(200, $response->getStatusCode());
        $this->assertInstanceOf('Zend\View\Model\ViewModel', $result);
}

It worked fine.

Now I'm forwarding the request

public function indexAction() {
    return $this->forward()->dispatch('Catalog/Controller/Catalog', array('action' => 'list-cities'));
}

and getting an error by unit testing after $this->controller->dispatch($this->request);:

PHP Fatal error:  Call to a member function getEventManager() on a non-object in /var/www/path/to/project/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/Plugin/Forward.php on line 147

How do you / how should one test action methods with forwards?

Thx

automatix
  • 14,018
  • 26
  • 105
  • 230

1 Answers1

0

Have you tried dispatching like this? I have just tried forwarding inside one of my controller actions and unit tests work fine. This is my code:

use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase

class IndexControllerTest extends AbstractHttpControllerTestCase
{

    public function setUp()
    {
        require APPLICATION_PATH . '/init_autoloader.php';
        $testConfig = include APPLICATION_PATH . '/config/test.php';
        $this->setApplicationConfig($testConfig);
        parent::setUp();
    }

    public function testFoo()
    {
        $this->dispatch('/catalogue');
        $this->assertResponseStatusCode(200);
        $this->assertModuleName('Catalogue');
        $this->assertControllerName('Catalogue\Controller\Index');
        $this->assertControllerClass('IndexController');
        $this->assertActionName('index');
        $this->assertMatchedRouteName('logcataloguen');
    }

}
Richard Knop
  • 81,041
  • 149
  • 392
  • 552
  • Thank you for your answer, but it's ZF2, not ZF1. There is no `APPLICATION_PATH` and `_dispatch(...)` anymore and the first argument of the new `dispatch(...)` method needs to be a `Request` object (`must implement interface Zend\Stdlib\RequestInterface`). – automatix May 08 '13 at 15:48
  • EDIT: What I tried out, was `$this->controller->dispatch('/index');`. And it can actually not work, since the `AbstractController#dispatch(...)` needs a `Request` object. Now I've tried `$this->dispatch('/index');` and it works. Thank you! – automatix May 08 '13 at 17:26
  • Yes, I know it's ZF2, the code I posted is from one of my ZF2 apps. I just made a typo. It was meant to be ->dispath, not ->_dispatch. And no, first argument of the new dispatch doesn't need to be a request object. See here: http://framework.zend.com/manual/2.1/en/modules/zend.test.phpunit.html – Richard Knop May 08 '13 at 23:10