2011-03-10 6 views
9

openid를 사용하여 ZF에서 내 webiste에 로그인했습니다 (예 : google, myopenid, yahoo 사용). 그것은 잘 작동합니다. 하지만 그것을위한 단위 테스트를 작성하는 방법을 모르겠습니다. 예를 들어Zend Framework에서 openid를 사용하여 로그인을 테스트하려면 어떻게해야합니까?

, I는 단위 테스트를 작성하고 싶습니다 :

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

사람이 시나리오에는 조롱하는 방법을 알고 계십니까? 어쩌면 누군가가 어떤 모범을 보였을까요?

+0

스텁을 위해 그것을 사용할 필요가 ZF 내의 Zend_OpenID 컴포넌트에 대한 유닛 테스트. 그것은 당신에게 몇 가지 아이디어를 줄 수 있습니다. –

답변

1

Google이 작동하고 반응하는지 테스트 할 필요가 없으며 (수정하지 못하더라도 해결할 수 없음) Zend_OpenId (아직 다루지 않음)를 테스트 할 필요가 없습니다. 자신의 코드 만 테스트해야합니다. OpenId 응답을 스텁하는 것이 좋습니다. 나는 여기에

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

당신이 진짜 구글에 대한 호출뿐만 아니라 테스트 Zend_OpenId_Consumer을하지 않으의이 Mygoogle_OpenId_Consumer 클래스를 사용하여 당신이 젠드 참조 가이드에서 예를 가정 해 봅시다, 코드가 어떻게 보이는지 모르겠어요 그래서 우리는 Zend_OpenId_Consumer를 스텁해야합니다. 클래스에서 스텁을 만들려면 Zend_OpenId_Consumer 또는 mock 객체 인 어댑터를 사용합니다. 당신의 검사 결과에

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

그리고 마지막으로 우리는 당신에 대한 전체 답변을하지 않습니다하지만 당신이보고 싶을 수도 모의 객체를 생성하고

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

시간 내 주셔서 감사합니다. 내 코드는 http://stackoverflow.com/questions/5175846/zend-framework-user-authentication-integration-with-twitter-and-facebook/5176765#5176765의 코드와 매우 유사합니다. – user594791

관련 문제