Question

I am testing an AbstractRestfulController within a Zend Framework 2 application.

I am failing even the basic assertion

I am thinking that my setup could be broken.. ie not route matching properly. How can I print this out in the controller, so as to assure my settings in ZF2RestServiceTest.setup() are correct?

Documentation is pretty sparse on what goes where here :

ControllerTest.php

protected function setUp()
{
    $serviceManager = Bootstrap::getServiceManager();
    $this->controller = new IndexController();
    $this->request    = new Request();
    $this->routeMatch = new RouteMatch(array('controller' => 'IndexController'));
    $this->event      = new MvcEvent();

    $config = $serviceManager->get('Config');
    $routerConfig = isset($config['router']) ? $config['router'] : array();

    $router = HttpRouter::factory($routerConfig);
    $this->event->setRouter($router);
    $this->event->setRouteMatch($this->routeMatch);
    $this->controller->setEvent($this->event);
    $this->controller->setServiceLocator($serviceManager);
}

RouteMatch takes in an array, looking for my controller name set in the module.config.php?

public function testIndexActionCanBeAccessed()
{
    $this->routeMatch->setParam('action', 'index');

    $result   = $this->controller->dispatch($this->request);
    $response = $this->controller->getResponse();

    $this->assertEquals(200, $response->getStatusCode());
}

module.config.php

<?php

return array(
    'router' => array(
        'routes' => array(
            'myservice' => array(
                'type' => 'Zend\Mvc\Router\Http\Segment',
                'options' => array(
                    'route' => '/api/myservice[/:optionalParameter]',
                    'constraints' => array(
                        'id' => '\w+'
                    ),
                    'defaults' => array(
                        'controller' => 'MyService\Controller\Index'
                    ),
                ),
            ),
        ),
    ),
    'controllers' => array(
        'invokables' => array(
            'MyService\Controller\Index' => 'MyService\Controller\IndexController',
        ),
    ),
);

Testing Result

PHPUnit_Framework_ExpectationFailedException : Failed asserting that 404 matches expected 200.
Expected :200
Actual   :404
Was it helpful?

Solution

It turns out I need the full controller name under this part, and modify the http parameters since I'm not using an Action Controller.

what was confusing me was a lot of Zend Framework code examples were using Aliases, or different Controller types..

here's the link that helped : https://github.com/RichardKnop/zend-v2-skeleton

protected function setUp()
{
    $serviceManager = Bootstrap::getServiceManager();
    $this->controller = new IndexController();
    $this->request    = new Request();
    $this->routeMatch = new RouteMatch(array('controller' => 'MyService\Controller\Index'));
    $this->event      = new MvcEvent();

...

public function testIndexActionCanBeAccessed()
{
    $this->routeMatch->setParam('optionalParameter', 'someParam');

    $result   = $this->controller->dispatch($this->request);
    $response = $this->controller->getResponse();

    $this->assertEquals(200, $response->getStatusCode());
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top