简体   繁体   English

如何在Zend Framework中使用openid测试登录?

[英]How do I test login using openid in Zend Framework?

I made loging to my webiste in ZF using openid (eg using google, myopenid, yahoo). 我使用openid(例如使用google,myopenid,yahoo)在ZF中登录我的网站。 It works good. 它运作良好。 But I don't know how to write a unit test for it. 但我不知道如何为它编写单元测试。

As example, I would like to write unit tests: 例如,我想编写单元测试:

public function testUserLogsSuccessfullyUsingGoogle() {
        // don't know how to dispach/mock that my action
        // will take a user to google, and google will
        // return authentication data (e.g. email)
        // Once user is authenticated by google, 
        // I make Zend_Auth for the user. 
        // 


        $this->asertTrue(Zend_Auth::getInstance()->getIdentity());
}


public function testUserLogsUnSuccessfullyUsingGoogle() {
        // don't know how to dispach/mock that my action
        // will take a user to google, and USER WILL NOT ALLOW
        // for authentication. Then off course I don't make
        // Zend_Auth for the user. 
        // 


        $this->asertFalse(Zend_Auth::getInstance()->getIdentity());
}

Do anyone know how to mock this scenerio? 有谁知道如何嘲笑这个场景? Maybe someone have some example? 也许某人有一些例子?

You don't need to test that Google works and responds (even if it doesn't, you can't fix this), also you don't need to test Zend_OpenId (it's covered already). 你不需要测试Google的工作和响应(即使它没有,你也无法解决这个问题),你也不需要测试Zend_OpenId(它已经被覆盖了)。 You need to test only your own code. 您只需要测试自己的代码。 So it might be a good idea to stub OpenId responses. 因此,存储OpenId响应可能是个好主意。 I don't know how your code looks, let's say you have an example from Zend reference guide using your Mygoogle_OpenId_Consumer class 我不知道您的代码看起来如何,假设您使用Mygoogle_OpenId_Consumer类的Zend参考指南中有一个示例

if (isset($_POST['openid_action']) &&
    $_POST['openid_action'] == "login" &&
    !empty($_POST['openid_identifier'])) {
    $consumer = new Mygoogle_OpenId_Consumer();
    if (!$consumer->login($_POST['openid_identifier'])) {
        $status = "OpenID login failed.";
    }

} else if (isset($_GET['openid_mode'])) {
    if ($_GET['openid_mode'] == "id_res") {
        $consumer = new Mygoogle_OpenId_Consumer();
        if ($consumer->verify($_GET, $id)) {
            $status = "VALID " . htmlspecialchars($id);
       } else {
           $status = "INVALID " . htmlspecialchars($id);
       }
    } else if ($_GET['openid_mode'] == "cancel") {
        $status = "CANCELLED";
    }
} 

Here you don't want to make real calls to Google as well as testing Zend_OpenId_Consumer so we would need to stub Zend_OpenId_Consumer. 在这里,您不希望真正调用Google以及测试Zend_OpenId_Consumer,因此我们需要存根Zend_OpenId_Consumer。 To be able to stub it in your class we would use an adapter which is either Zend_OpenId_Consumer or your mock object. 为了能够在类中存根,我们将使用Zend_OpenId_Consumer或您的模拟对象的适配器。

class Mygoogle_OpenId_Consumer extends Zend_OpenId_Consumer
{
    private static $_adapter = null;

    public static function setAdapter($adapter)
    {
        self::$_adapter = $adapter;
    }

    public static function getAdapter()
    {
        if ( empty(self::$_adapter) ) {
            self::$_adapter = new Zend_OpenId_Consumer();
        }

        return self::$_adapter;
    }
.....

And finally in your tests we need to create a Mock object and use it for stubbing 最后在测试中我们需要创建一个Mock对象并将其用于存根

private function _stubGoogle($successful = false)
    {
        $adapter = $this->getMock('Mygoogle_OpenId_Consumer', array('login','verify'));

        $httpResponse = $successful?:null;
        $adapter->expects( $this->any() )
               ->method('login')
               ->will( $this->returnValue($httpResponse) );

        $adapter->expects( $this->any() )
               ->method('verify')
               ->will( $this->returnValue($successful) );

        Mygoogle_OpenId_Consumer::setAdapter($adapter);
    }

    public function testUserLogsSuccessfullyUsingGoogle()
    {
        $this->_stubGoogle(true);
        //do whatever you need to test your login action:
        //set and send request, dispatch your action
    }

    public function testUserLogsUnSuccessfullyUsingGoogle()
    {
        $this->_stubGoogle(false);
        ...
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM