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

use*_*791 9 php openid phpunit zend-framework

我使用openid(例如使用google,myopenid,yahoo)在ZF中登录我的网站.它运作良好.但我不知道如何为它编写单元测试.

例如,我想编写单元测试:

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());
}
Run Code Online (Sandbox Code Playgroud)

有谁知道如何嘲笑这个场景?也许某人有一些例子?

小智 1

您不需要测试 Google 是否工作并响应(即使它不工作,您也无法解决此问题),也不需要测试 Zend_OpenId(它已经涵盖了)。您只需要测试您自己的代码。因此,存根 OpenId 响应可能是个好主意。我不知道你的代码看起来如何,假设你有一个使用 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";
    }
} 
Run Code Online (Sandbox Code Playgroud)

在这里,您不想对 Google 进行真正的调用以及测试 Zend_OpenId_Consumer,因此我们需要存根 Zend_OpenId_Consumer。为了能够在您的类中存根它,我们将使用一个适配器,它可以是 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;
    }
.....
Run Code Online (Sandbox Code Playgroud)

最后,在您的测试中,我们需要创建一个 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);
        ...
    }
Run Code Online (Sandbox Code Playgroud)