Question

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

No correct solution

OTHER TIPS

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');
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top