繁体   English   中英

zf2 phpunit方案路由

[英]zf2 phpunit scheme routes

我们正在将ZF2用于Web应用程序,并希望使用phpunit(v4.8.9)对其进行测试。 在此应用程序中,我们有一个计划路由,能够在http / https-context之间切换(无论为什么...)。 路线如下所示:

'http' => array(
    'type' => 'Scheme',
    'options' => array(
        'scheme' => 'http',
        'defaults' => array(
            'http' => true
        )
    ),
    'child_routes' => array(
        'search' => array(
            'type'    => 'segment',
            'options' => array(
                'route' => '/search[/:keyword[/:page]]',
                'constraints' => array(
                    'page' => '[1-9]+[0-9]*'
                ),
                'defaults' => array(
                    'controller'    => SearchController::class,
                    'action'        => 'search',
                ),
            ),
        ),
    ),
),
'https' => array(
    'type' => 'Scheme',
    'options' => array(
        'scheme' => 'https',
        'defaults' => array(
            'https' => true
        )
    ),
    'child_routes' => array(
        'search' => array(
            'type'    => 'segment',
            'options' => array(
                'route' => '/search[/:keyword[/:page]]',
                'constraints' => array(
                    'page' => '[1-9]+[0-9]*'
                ),
                'defaults' => array(
                    'controller'    => SearchController::class,
                    'action'        => 'search',
                ),
            ),
        ),
    ),
),

测试的类如下所示:

class SearchControllerTest extends SynHttpControllerTestCase
{
    public function setUp()
    {
        $this->setApplicationConfig($this->getCurrentBootstrap()->getApplicationConfig());
        parent::setUp();
        $this->getApplicationServiceLocator()->setAllowOverride(true);
    }

    public function testSearchActionCanBeAccessed()
    {
        $this->dispatch('/search');
        $this->assertResponseStatusCode(200);
        $this->assertControllerName(SearchController::class);
        $this->assertControllerClass('SearchController');
        $this->assertActionName('search');
        $this->assertMatchedRouteName('search');
    }
}

仅供参考: “ SynHttpControllerTestCase”是Zend-Test随附的原始AbstractHttpControllerTestCase的扩展。 对其进行了修改,以在我们的测试中获得正确的引导文件。

如果我们正在运行测试,则会出现此错误:

致命错误:在第563行的C:\\ xampp \\ htdocs \\ git \\ xxx \\ vendor \\ zendframework \\ zend-test \\ src \\ PHPUnit \\ Controller \\ AbstractControllerTestCase.php中调用成员函数getParam()为null

我们调查了AbstractControllerTestCase,此行引发了错误:

$routeMatch = $this->getApplication()->getMvcEvent()->getRouteMatch();

因为$ routeMatch-Object为空。 我们的应用程序中还有其他一些控制器和测试,它们都很好,并且不受此问题的影响,从而导致到这些控制器的路由以及方案路由。

您有任何解决方法的想法吗? 提前:在这种情况下,我们将无法使用静态https-routes。

关于如何编写适当的控制器测试, 有很多官方文档 setUp方法中,您需要将Router实例附加到RouteMatch实例,然后将其附加到控制器中的MvcEvent

protected function setUp()
{
    $serviceManager = Bootstrap::getServiceManager();
    $this->controller = new IndexController();
    $this->request    = new Request();
    $this->routeMatch = new RouteMatch(array('controller' => 'index'));
    $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);
}

然后,您可以调用dispatch

UPDATE

您是否可以像这样设置路线匹配对象:

$routeParams  = array('controller' => 'search', 'action' => 'search');
$routeMatch   = new RouteMatch($routeParams);
$routerConfig = isset($config['router']) ? $config['router'] : array();
$router = HttpRouter::factory($routerConfig);

$event        = new MvcEvent();
$event->setRouter($router);
$event->setRouteMatch($routeMatch);

$this->getApplication()->setMvcEvent($event);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM