我们将ZF2用于web应用程序,并希望使用phpunit (v4.8.9)对其进行测试。在这个应用程序中,我们有一个方案-路由,可以在http/https-上下文之间切换(不重要为什么.)。路线如下:
'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');
}
}FYI:"SynHttpControllerTestCase“是来自Zend的原始AbstractHttpControllerTestCase的扩展。它被修改以在我们的测试中获得正确的引导文件。
如果我们正在运行测试,则会出现以下错误:
致命错误:在第563行的C:\xampp\htdocs\git\xxx\vendor\zendframework\zend-test\src\PHPUnit\Controller\AbstractControllerTestCase.php中调用null上的成员函数getParam()
我们查看了AbstractControllerTestCase,这一行抛出了错误:
$routeMatch = $this->getApplication()->getMvcEvent()->getRouteMatch();因为$routeMatch-对象是空的。我们在应用程序中有一些其他控制器和测试,它们都很好,不受这个问题的影响,导致到这些控制器的路由没有计划-路由。
你有什么办法解决这个问题吗?预先:在这种情况下,我们不能使用静态https路由。
发布于 2015-10-21 12:45:21
有关于如何编写适当的控制器测试的大量的官方文件。在您的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。
更新
您不能将路由匹配对象设置为:
$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);https://stackoverflow.com/questions/33258387
复制相似问题