簡體   English   中英

如何將其他參數傳遞給routeMatch對象?

[英]How can I pass extra parameters to the routeMatch object?

我正在嘗試對控制器進行單元測試,但無法弄清楚如何將一些額外的參數傳遞給routeMatch對象。

我關注了tomoram的帖子,網址為http://devblog.x2k.co.uk/unit-testing-a-zend-framework-2-controller/http://devblog.x2k.co.uk/getting-the- servicemanager- to -test-environment-and-dependency-injection / ,但是例如,當我嘗試將請求分派到/ album / edit / 1時,它將引發以下異常:

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

這是我的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();

基本上,我們正在創建一個Zend\\Mvc\\Application環境。

我的PHPUnit_Framework_TestCase包含在一個自定義類中,如下所示:

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

然后,我們創建一個Controller實例和一個帶有RouteMatch的請求。

測試代碼:

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
}

我不確定我在這里缺少什么。 它可以是測試引導程序的任何配置嗎? 還是應該以其他方式傳遞參數? 還是我忘記實例化某些東西?

我要解決的問題是將Application::init()和配置從Bootstrap移到setUp()方法。 加載需要一段時間,但是可以正常工作。

我的Bootstrap僅具有配置自動加載器所需的代碼,而setUp()方法與舊的Bootstrap::go()代碼類似。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM