zf2 phpunit scheme routes

时间:2015-10-21 11:40:46

标签: php zend-framework2 phpunit

我们将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的扩展。它被修改为在我们的测试中获得正确的bootstrap文件。

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

  

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

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

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

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

您有什么想法如何解决这个问题?提前:在这种情况下我们无法使用静态https路由。

1 个答案:

答案 0 :(得分:0)

如何编写适当的控制器测试有a lot of official documentation。在setUp方法中,您需要将Router实例附加到附加到控制器中RouteMatch的{​​{1}}实例。

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