Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I test login using openid in Zend Framework?

I made loging to my webiste in ZF using openid (e.g. using google, myopenid, yahoo). 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?

like image 301
user594791 Avatar asked Mar 10 '11 11:03

user594791


1 Answers

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). You need to test only your own code. So it might be a good idea to stub OpenId responses. 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

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. To be able to stub it in your class we would use an adapter which is either Zend_OpenId_Consumer or your mock object.

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

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);
        ...
    }
like image 99
criticus Avatar answered Sep 30 '22 14:09

criticus