我想测试在子域www.username.domain.com上运行的控制器
问题是当我在ControllerTestCase中调度时抛出Zend_Controller_Dispatcher_Exception
routes.php文件:
$userRouter = new Zend_Controller_Router_Route_Hostname(':user.domain.com'));
$router->addRoute('user', $userRouter->chain(new Zend_Controller_Router_Route('',
array('controller' => 'user'))));
UserControllerTest:
require_once 'AbstarctControllerTestCase.php';
class UserControllerTest extends AbstarctControllerTestCase
{
public function setUp()
{
$this->cleardb();
parent::setUp();
}
public function testRoute()
{
$this->dispatch('www.username.domain.com');
$this->assertController('user');
}
}
AbstarctControllerTestCase:
abstract class AbstarctControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase
{
public function setUp()
{
$this->bootstrap = array($this, 'appBootstrap');
parent::setUp();
}
public function appBootstrap()
{
chdir(dirname(dirname(dirname(dirname(__FILE__)))));
require 'application/test/controllerunit/routes.php';
Zend_Session::start();
}
(...)
}
结果:
PHPUnit 3.3.17 by Sebastian Bergmann.
F
Time: 1 second
There was 1 failure:
1) testRoute(UserControllerTest)
Failed asserting last controller used was "user"
当我发送像/ login这样的普通URI时,它运行良好,但问题是使用主机名调度URL。
有什么想法吗? 谢谢大家。
答案 0 :(得分:2)
您是否尝试在设置中设置$ _SERVER变量?
e.g。
$_SERVER['SERVER_NAME'] = 'www.username.domain.com';
然后按照惯例调用调度。
请参阅 - http://php.net/manual/en/reserved.variables.server.php
答案 1 :(得分:2)
在致电$_SERVER['HTTP_HOST']
之前定义dispatch()
。