How can I pass extra parameters to the routeMatch object?

Posted by Marcos Garcia on Stack Overflow See other posts from Stack Overflow or by Marcos Garcia
Published on 2012-09-24T18:02:45Z Indexed on 2012/09/26 21:37 UTC
Read the original article Hit count: 419

Filed under:
|
|

I'm trying to unit test a controller, but can't figure out how to pass some extra parameters to the routeMatch object.

I followed the posts from tomoram at http://devblog.x2k.co.uk/unit-testing-a-zend-framework-2-controller/ and http://devblog.x2k.co.uk/getting-the-servicemanager-into-the-test-environment-and-dependency-injection/, but when I try to dispatch a request to /album/edit/1, for instance, it throws the following exception:

Zend\Mvc\Exception\DomainException: Url plugin requires that controller event compose a router; none found

Here is my PHPUnit Bootstrap:

class Bootstrap
{
    static $serviceManager;
    static $di;

    static public function go()
    {
        include 'init_autoloader.php';

        $config = include 'config/application.config.php';
        // append some testing configuration
        $config['module_listener_options']['config_static_paths'] = array(getcwd() . '/config/test.config.php');

        // append some module-specific testing configuration
        if (file_exists(__DIR__ . '/config/test.config.php')) {
            $moduleConfig = include __DIR__ . '/config/test.config.php';
            array_unshift($config['module_listener_options']['config_static_paths'], $moduleConfig);
        }

        $serviceManager = Application::init($config)->getServiceManager();

        self::$serviceManager = $serviceManager;

        // Setup Di
        $di = new Di();

        $di->instanceManager()->addTypePreference('Zend\ServiceManager\ServiceLocatorInterface', 'Zend\ServiceManager\ServiceManager');
        $di->instanceManager()->addTypePreference('Zend\EventManager\EventManagerInterface', 'Zend\EventManager\EventManager');
        $di->instanceManager()->addTypePreference('Zend\EventManager\SharedEventManagerInterface', 'Zend\EventManager\SharedEventManager');

        self::$di = $di;
    }

    static public function getServiceManager()
    {
        return self::$serviceManager;
    }

    static public function getDi()
    {
        return self::$di;
    }

}

Bootstrap::go();

Basically, we are creating a Zend\Mvc\Application environment.

My PHPUnit_Framework_TestCase is enclosed in a custom class, which goes like this:

abstract class ControllerTestCase extends TestCase
{
    /**
     * The ActionController we are testing
     *
     * @var Zend\Mvc\Controller\AbstractActionController
     */
    protected $controller;

    /**
     * A request object
     *
     * @var Zend\Http\Request
     */
    protected $request;

    /**
     * A response object
     *
     * @var Zend\Http\Response
     */
    protected $response;

    /**
     * The matched route for the controller
     *
     * @var Zend\Mvc\Router\RouteMatch
     */
    protected $routeMatch;

    /**
     * An MVC event to be assigned to the controller
     *
     * @var Zend\Mvc\MvcEvent
     */
    protected $event;

    /**
     * The Controller fully qualified domain name, so each ControllerTestCase can create an instance
     * of the tested controller
     *
     * @var string
     */
    protected $controllerFQDN;

    /**
     * The route to the controller, as defined in the configuration files
     *
     * @var string
     */
    protected $controllerRoute;

    public function setup()
    {
        parent::setup();

        $di = \Bootstrap::getDi();

        // Create a Controller and set some properties
        $this->controller = $di->newInstance($this->controllerFQDN);

        $this->request    = new Request();
        $this->routeMatch = new RouteMatch(array('controller' => $this->controllerRoute));
        $this->event      = new MvcEvent();

        $this->event->setRouteMatch($this->routeMatch);

        $this->controller->setEvent($this->event);
        $this->controller->setServiceLocator(\Bootstrap::getServiceManager());
    }

    public function tearDown()
    {
        parent::tearDown();
        unset($this->controller);
        unset($this->request);
        unset($this->routeMatch);
        unset($this->event);
    }
}

And we create a Controller instance and a Request with a RouteMatch.

The code for the test:

public function testEditActionWithGetRequest()
{
    // Dispatch the edit action
    $this->routeMatch->setParam('action', 'edit');
    $this->routeMatch->setParam('id', $album->id);
    $result = $this->controller->dispatch($this->request, $this->response);

    // rest of the code isn't executed
}

I'm not sure what I'm missing here. Can it be any configuration for the testing bootstrap? Or should I pass the parameters in some other way? Or am I forgetting to instantiate something?

© Stack Overflow or respective owner

Related posts about php

Related posts about php5