2

我想测试在子域 www.username.domain.com 上工作的控制器

问题是当我在 ControllerTestCase 调度它抛出 Zend_Controller_Dispatcher_Exception


路线.php:

$userRouter = new Zend_Controller_Router_Route_Hostname(':user.domain.com'));

$router->addRoute('user', $userRouter->chain(new Zend_Controller_Router_Route('',
                            array('controller' => 'user'))));

用户控制器测试:

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');
    }
}

抽象控制器测试用例:

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。

有任何想法吗?谢谢你们。

4

2 回答 2

2

$_SERVER['HTTP_HOST']在调用之前定义dispatch()

在http://framework.zend.com/issues/browse/ZF-11680下已经有一张同样关注的票

于 2013-05-22T15:07:43.887 回答
2

您是否尝试在设置中设置 $_SERVER 变量?

例如

$_SERVER['SERVER_NAME'] = 'www.username.domain.com';

然后像往常一样调用调度。

见 - http://php.net/manual/en/reserved.variables.server.php

于 2010-10-14T00:44:26.900 回答