【发布时间】: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