【问题标题】:PHPUnit test settings for AbstractRestfulController in ZF2ZF2 中 AbstractRestfulController 的 PHPUnit 测试设置
【发布时间】:2014-04-22 15:55:22
【问题描述】:

我正在 Zend Framework 2 应用程序中测试 AbstractRestfulController。

我连基本的断言都失败了

我认为我的设置可能会被破坏.. 即没有正确匹配路由。如何在控制器中打印出来,以确保我在ZF2RestServiceTest.setup() 中的设置正确?

关于这里的内容的文档很少:

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 接收一个数组,寻找我在 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',
        ),
    ),
);

测试结果

PHPUnit_Framework_ExpectationFailedException : Failed asserting that 404 matches expected 200.
Expected :200
Actual   :404

【问题讨论】:

    标签: php zend-framework2 phpunit


    【解决方案1】:

    原来我需要这部分下的完整控制器名称,并修改 http 参数,因为我没有使用动作控制器。

    让我感到困惑的是,很多 Zend Framework 代码示例都使用别名或不同的控制器类型。

    这里有帮助的链接: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());
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-24
      • 2015-04-22
      • 2016-02-18
      • 1970-01-01
      • 1970-01-01
      • 2012-08-16
      • 2015-03-15
      • 2022-11-20
      相关资源
      最近更新 更多