2011-03-10 8 views
9

Logé acceder a mi sitio web en ZF usando openid (por ejemplo, usando google, myopenid, yahoo). Funciona bien Pero no sé cómo escribir una prueba unitaria para ello.¿Cómo pruebo el inicio de sesión usando openid en Zend Framework?

Como ejemplo, me gustaría escribir pruebas unitarias:

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()); 
} 

No Alguien sabe cómo se burlan de esta scenerio? Tal vez alguien tiene algún ejemplo?

+0

no tengo una respuesta completa para usted pero puede que desee echar un vistazo a la pruebas unitarias para el componente Zend_OpenID dentro de ZF. Puede darte algunas ideas. –

Respuesta

1

No necesita probar que Google funciona y responde (incluso si no lo hace, no puede arreglarlo), tampoco necesita probar Zend_OpenId (ya está cubierto). Necesita probar solo su propio código. Por lo tanto, podría ser una buena idea resguardar las respuestas de OpenId. No sé cómo se ve su código, digamos que tienes un ejemplo de guía de referencia Zend la utilización de su clase Mygoogle_OpenId_Consumer

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"; 
    } 
} 

Aquí no desea realizar llamadas reales a Google, así como las pruebas de lo que Zend_OpenId_Consumer necesitaría colgar Zend_OpenId_Consumer. Para poder insertarlo en su clase, usaríamos un adaptador que sea Zend_OpenId_Consumer o su objeto simulado.

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; 
    } 
..... 

Y, finalmente, en las pruebas que necesita para crear un objeto de burla y utilizarla para 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); 
     ... 
    } 
+0

Gracias por su tiempo. Mi código es muy similar al de http://stackoverflow.com/questions/5175846/zend-framework-user-authentication-integration-with-twitter-and-facebook/5176765#5176765. – user594791

Cuestiones relacionadas