测试使用Twitter API的Zend_Controller

时间:2010-12-30 06:52:38

标签: php zend-framework phpunit

我正在尝试为我的Controller编写一个单元测试,它使用Zend_Service调用Twitter API。

/**
 * Authenticate Step 1 for Twitter
 */
public function authenticateAction()
{
    $this->service->authenticate();
}

服务确实:

/**
 * Authenticate with twitter
 *
 * @return void
 */
public function authenticate()
{
    $consumer = new Zend_Oauth_Consumer($this->config);
    $token = $consumer->getRequestToken();
    $this->session->twitterRequestToken = serialize($token);
    $consumer->redirect();
    exit;
}

我的问题是我不知道如何替换单元测试服务中的身份验证操作。我不想在测试运行时调用Twitter API。

有没有可以做这些事情的模拟框架?

1 个答案:

答案 0 :(得分:2)

Zend_Service_Twitter使用Zend_Http_Client,因此您可以在测试用例中将其存根

$httpClient = $this->getMock('Zend_Http_Client');

$httpResponse = new Zend_Http_Response(200, array(), $data);  // $data - response you expected to get

$httpClient->expects($this->any())
                   ->method('request')
                   ->will($this->returnValue($httpResponse));

Zend_Service_Twitter::setLocalHttpClient($httpClient);