php - 如何使用 PHPUnit 进行单元测试来登录我的 Zend 应用程序

标签 php zend-framework phpunit

我正在尝试测试我的登录 Controller ,我已经阅读了很多帖子,并尝试了 ZF 引用中显示的内容。指南也是如此,但仍然有问题。

我也是 Zend 和 PHPUnit 的新手。

我将 CSRF 设置为仅在“生产”中使用,并在“开发”(或“测试”)环境中进行测试。消除这种麻烦。

我的登录表单“代理机构”中有一个字段,我认为它可能会阻碍我的进步(或不会)......我真的不确定。它是一个多选下拉列表,通常填充有一个数组(来自数据库中的枚举列)。

这是我尝试使用的测试,我也将保留注释掉的行,它们显示了我已经尝试过和/或想要尝试的内容。方法“canLoginToApp”将通过,但我知道它没有登录(或测试太多),并且我没有在登录操作(或登录表单类)中获得代码覆盖率,并且不能断言我拥有身份或登录后应该存在的任何其他内容。此外,如果我使用上面的方法,测试登录表单,它会失败并出现无法查询 DOM 异常。

这是我的测试:

<?php
/**
 * Description of LoginControllerTest
 * @author rob
 */
class LoginControllerTest 
    extends ControllerTestCase
{
    protected $userModel;

    public function setUp()
    {
        parent::setUp();
        $this->userModel = new Application_Model_Users();
    }

//    public function testIndexActionShouldContainLoginForm() {
//        $this->dispatch('/login');
//        $this->assertAction('index');
//        $this->assertQueryCount('form#loginForm', 1);
//    }

    public function testGetAGoodPage()
    {
        $this->dispatch('/');
        $this->assertResponseCode(302);
    }


    public function testCanLoginToApp()
    {   
                $this->request->setMethod('POST')
                ->setPost(array(
                    'agency'    => array('3'),
                    'username'  => 'rob',
                    'password'  => 'nonenone',
                ));
       $this->dispatch('/');

        $this->assertController('index');
        $this->assertAction('index');
        $this->dispatch('/login/index');
        $this->assertResponseCode(302);
       // $this->assertTrue($this->userModel->currentUserIdentity());
       // var_dump($this->users->currentUserIdentity());
        //$this->assertQueryContentContains('<span>', 'User Name: rob');
                $data = $this->getResponse();
        //var_dump($data);
    }
}

如果您尚未登录(或者更具体地说,如果 'Zend_Auth::getInstance != hasIdentity() ),我的应用程序总是将您带到 '/login'。我想断言它在测试中确实是“hasIdentity”。我也只能在测试得到的任何页面上找到“302”代码,但在 phpfirebug 中我得到 200。我认为,这是因为(zend 的)路由(?)

昨晚搞乱了这个,我添加了 $data=getResponse() 并转储了它(var_dump 现在被注释掉了),var_dump 继续继续,继续,继续。看起来它正在滚动整个应用程序......奇怪:) 这是其中的一小段:

  ["_moduleControllerDirectoryName":protected]=>
                      string(11) "controllers"
                      ["_plugins":protected]=>
                      object(Zend_Controller_Plugin_Broker)#90 (3) {
                        ["_plugins":protected]=>
                        array(0) {
                        }
                        ["_request":protected]=>
                        object(Zend_Controller_Request_HttpTestCase)#76 (18) {
                          ["_headers":protected]=>
                          array(0) {
                          }
                          ["_method":protected]=>
                          string(4) "POST"
                          ["_rawBody":protected]=>
                          NULL
                          ["_validMethodTypes":protected]=>
                          array(6) {
                            [0]=>
                            string(6) "DELETE"
                            [1]=>
                            string(3) "GET"
                            [2]=>
                            string(4) "HEAD"
                            [3]=>
                            string(7) "OPTIONS"
                            [4]=>
                            string(4) "POST"
                            [5]=>
                            string(3) "PUT"
                          }
                          ["_paramSources":protected]=>
                          array(2) {
                            [0]=>
                            string(4) "_GET"
                            [1]=>
                            string(5) "_POST"
                          }
                          ["_requestUri":protected]=>
                          string(12) "/login/index"
                          ["_baseUrl":protected]=>
                          string(0) ""
                          ["_basePath":protected]=>
                          NULL
                          ["_pathInfo":protected]=>
                          string(12) "/login/index"
                          ["_params":protected]=>
                          array(3) {
                            ["controller"]=>
                            string(5) "login"
                            ["action"]=>
                            string(5) "index"
                            ["module"]=>
                            string(7) "default"
                          }
                          ["_aliases":protected]=>
                          array(0) {
                          }
                          ["_dispatched":protected]=>
                          bool(true)
                          ["_module":protected]=>
                          string(7) "default"
                          ["_moduleKey":protected]=>
                          string(6) "module"
                          ["_controller":protected]=>
                          string(5) "login"
                          ["_controllerKey":protected]=>
                          string(10) "controller"
                          ["_action":protected]=>
                          string(5) "index"
                          ["_actionKey":protected]=>
                          string(6) "action"
                        }
                        ["_response":protected]=>
                        *RECURSION*
                      }

这是登录页面的源 View (来自浏览器):

<h2>Login</h2>

<form id="login" enctype="application/x-www-form-urlencoded" method="post" action="/login"><dl class="zend_form">
<dt id="agency-label"><label for="agency" class="required">Agency:</label></dt>
<dd id="agency-element">
<select name="agency" id="agency">
    <option value="00003" label="Dev Agency">Dev Agency</option>
    <option value="00004" label="ISR">ISR</option>
</select></dd>
<dt id="username-label"><label for="username" class="required">Username:</label></dt>
<dd id="username-element">
<input type="text" name="username" id="username" value="" /></dd>
<dt id="password-label"><label for="password" class="required">Password:</label></dt>

<dd id="password-element">
<input type="password" name="password" id="password" value="" /></dd>
<dt id="login-label">&#160;</dt><dd id="login-element">
<input type="submit" name="login" id="login" value="Login" /></dd></dl></form></div>

    </body>
</html>

页面的顶部只是普通的头元标记(css、标题等)。

也在我的“Post”数组中,对于“agency”我已经尝试过

3
00003
'3'
'00003'
'Dev Agency'
array('00003' => 'Dev Agency')
array('3')
array(3)
array(00003)

我也尝试过 $this->dispatch('/'), ('/login'), ('index') 或许还有其他一些。 我不确定测试我的登录是否真的非常重要,但我希望能够做到这一点,并且我正在考虑测试我需要登录的一些代码,或者至少得到'来自 Zend_Auth 的 hasIdentity' = true ,因为我在应用程序中对此做了很多检查。我更喜欢从测试中给出“假”Auth 对象。

作为一个侧面或扩展的问题“在 ENV '测试' 时,zend 框架是否在内部执行任何操作,或者这完全是我自己的环境,不符合惯例(当然可供我使用)? ZF 的任何部分是否关注与此相关的环境(生产、测试​​、开发、升级)?我问是因为我还在“测试”环境中尝试了一些测试。认为调度或路由器在测试环境中的行为可能有点不同。 (重定向、响应、请求等)。

谢谢

最佳答案

如果您想测试,您应该能够像平常一样访问 Zend_Auth 并对其进行测试。例如

// after correct login there should be zend_auth identity setup 
$auth = Zend_Auth::getInstance();
$this->assertTrue($auth->hasIdentity());

关于php - 如何使用 PHPUnit 进行单元测试来登录我的 Zend 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5821288/

相关文章:

php - 使用 .htaccess 将 http 重写为 https,反之亦然

PHPUnit 找不到我的 PHP 文件,命名空间问题?

php - 数据未插入

php - zf3 zend 导航助手

php - Zend Framework - 安装旧版本

php - 如果断言失败,如何让 PHPUnit 执行某些操作

selenium - PHPUnit Selenium captureScreenshotOnFailure 不起作用?

php - PDO 更新多条记录

php - 在 Magento 观察器中获取 POST 数据

php - 显示图像并运行脚本十五秒