Php 如何在Zend框架中使用openid测试登录?
我使用openid(例如使用google、myopenid、yahoo)在ZF登录我的webiste。它工作得很好。但我不知道如何为它编写单元测试 例如,我想编写单元测试:Php 如何在Zend框架中使用openid测试登录?,php,zend-framework,openid,phpunit,Php,Zend Framework,Openid,Phpunit,我使用openid(例如使用google、myopenid、yahoo)在ZF登录我的webiste。它工作得很好。但我不知道如何为它编写单元测试 例如,我想编写单元测试: public function testUserLogsSuccessfullyUsingGoogle() { // don't know how to dispach/mock that my action // will take a user to google, and google
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());
}
有人知道怎么嘲笑这个场景吗?也许有人举了一些例子?您不需要测试Google是否工作和响应(即使它不工作,您也无法修复此问题),也不需要测试Zend_OpenId(已经介绍过了)。您只需要测试自己的代码。因此,存根OpenId响应可能是一个好主意。我不知道你的代码看起来如何,假设你有一个来自Zend参考指南的例子,使用你的Mygoogle\u OpenId\u 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";
}
}
在这里,你不想给谷歌打真正的电话,也不想测试Zend_OpenId_消费者,所以我们需要存根Zend_OpenId_消费者。为了能够在您的类中存根它,我们将使用一个适配器,它可以是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;
}
.....
最后,在测试中,我们需要创建一个模拟对象,并将其用于存根
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);
...
}
我没有完整的答案,但您可能想看看ZF中Zend_OpenID组件的单元测试。它可能会给你一些想法。谢谢你的时间。我的代码与中的代码非常相似。