Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 单元测试Zend控制器刚刚退出_Php_Unit Testing_Zend Framework_Phpunit - Fatal编程技术网

Php 单元测试Zend控制器刚刚退出

Php 单元测试Zend控制器刚刚退出,php,unit-testing,zend-framework,phpunit,Php,Unit Testing,Zend Framework,Phpunit,所以我有一个单元测试类: class HomeControllerTest extends ControllerTestCase { public function testLoginAction() { $this->dispatch('/home/login'); $this->assertController('home'); $this->assertAction('login'); $th

所以我有一个单元测试类:

class HomeControllerTest extends ControllerTestCase
{
    public function testLoginAction()
    {
        $this->dispatch('/home/login');
        $this->assertController('home');
        $this->assertAction('login');
        $this->assertQueryCount('div.login', 1);
    }

    public function testProcessloginAction()
    {
        $this->getRequest()
        ->setMethod('POST')
        ->setPost(array("username" => "example@example.com",
                                       "password" => "password"));
        $this->dispatch('/home/processlogin');

        $session = new Zend_Session_Namespace('session');
        $this->assertEquals($session->isLoggedIn, true);
        $this->assertRedirectTo('/home');
    }
}
这是测试的输出:

root@ubuntu:/mnt/hgfs/app# phpunit --stderr
PHPUnit 3.5.15 by Sebastian Bergmann.

.root@ubuntu:/mnt/hgfs/app#
第一个测试运行良好,但是第二个测试正好在$this->dispatch('/home/processlogin')之前退出;线路。我不知道为什么,但我能看到唯一不同的是home/processlogin执行重定向


是否有人在使用ZF 1.11.x时遇到过这种情况(针对1.11.7和1.11.11进行测试)?

如果您能向我们展示processlogin操作代码,那将非常有用


从我在单元测试中看到的情况来看,它可能会失败,因为在控制器中禁用重定向以查看是否是问题。

在Zend Framework中测试重定向时遇到的一个问题是,如果重定向没有正确退出操作,它们通常会失败。这似乎不会影响浏览器中的重定向,但会在CLI上通过PHPUnit运行代码时导致重定向失败

而不是使用:

$this->_redirect('home');
尝试返回重定向,使其正确退出操作:

return $this->_redirect('home');

在内部,Zend框架调用
exit

但是,当通过Zend控制器测试用例运行时,允许调用代码(例如测试用例)继续执行

您需要处理两种情况: 1.没有实际的重定向。 由于页面实际上没有重定向,因此现在必须使用测试方法:

断言已发生重定向,并且 Location header是提供的$url

2.重定向时操作不退出。 要停止操作中代码的执行,必须确保重定向时从操作返回:

public function userAction()
{
    // ... Some code
    return $this->getHelper('Redirector')->goToRoute(array ('id' => $id), 'userView');

    // ... Will no longer execute the following:
    var_dump("Never gets here");
}

您使用的是自定义的基本测试类吗?可能就是问题所在。我使用使用ZF工具创建的默认ZF项目测试了您的代码,它可以工作(当然,有失败的断言),但它会分派请求。我提交了执行重定向的行,现在它显示了一个错误。这是否意味着我无法测试任何执行重定向的方法,或者我已经修改了所有重定向以使用其他方法?错误是什么?您使用什么方法进行重定向?您可以测试重定向,这就是assertRedirectTo()的用途:)通常我们使用$redirect->gotoRoute($redirectAction,'default',true);用于重定向。
public function userAction()
{
    // ... Some code
    return $this->getHelper('Redirector')->goToRoute(array ('id' => $id), 'userView');

    // ... Will no longer execute the following:
    var_dump("Never gets here");
}