【问题标题】:Using the set redirectUrl methods in Zend Redirector action helper在 Zend Redirector 动作助手中使用 set redirectUrl 方法
【发布时间】:2010-07-23 18:03:54
【问题描述】:

我正在 SimpleTest 中对 Zend_Controller_Action 的一些扩展进行单元测试。我希望能够使用 Redirector 操作助手的 set 方法设置重定向 url,然后使用 Redirector 的 redirectAndExit() 方法在该过程的后期实际重定向。通过阅读文档并查看操作控制器、重定向器和响应类中的代码,这个过程似乎没有像我预期的那样工作。

这是我写的一个 UnitTestCase 方法:

  public function testSetGoToUrl() {

    $request    = new Zend_Controller_Request_Http();
    $response   = new Zend_Controller_Response_Http();
    $controller = new App_Zend_Controller_Action($request, $response, array());

    $controller->getHelper('redirector')->setGoToUrl('/');

  }

App_Zend_Controller_Action 类仅仅是抽象类 Zend_Controller_Action 的具体扩展。我在这里所做的只是实例化控制器并设置重定向 url。正如预期的那样,SimpleTest 报告器首先发送标头。但是这个测试方法会产生一个“headers sent”异常,我不明白为什么。我不知道在这种情况下调用了任何 run() 或 dispatch() 方法。

发送第二组标头的是什么?

【问题讨论】:

    标签: php zend-framework simpletest


    【解决方案1】:

    在测试时,您应该使用Zend_Controller_Response_HttpTestCase 和Zend_Controller_Request_HttpTestCase。
    Zend_Controller_Response_HttpTestCase::sendHeaders() 返回一个包含所有要发送的标头的数组,而不是执行真正的header() 语句。

    这些也用于Zend_Test_PHPUnit_ControllerTestCase。

    【讨论】:

    • 这是个好主意,我刚刚弄清楚发生了什么。没有尝试发送第二组标头;异常是由响应对象的 canSendHeaders() 方法生成的,该方法在响应对象设置重定向时调用。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-31
    • 2023-03-07
    • 2011-03-01
    相关资源
    最近更新 更多