homelessDevOps
homelessDevOps

Reputation: 20726

Testing Zend_Controller which is using Twitter API

I'm trying to write a unit test for my Controller which calls the Twitter API using Zend_Service.

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

The Service does:

/**
 * 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;
}

My Problem is that I have no idea how to replace the authenticate action inside the service for the unit test. I don't want to call the Twitter API while the tests run.

Is there any Mocking Framework which can do such things?

Upvotes: 0

Views: 179

Answers (1)

criticus
criticus

Reputation: 1599

Zend_Service_Twitter uses Zend_Http_Client so you can stub it in your test case

$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);

Upvotes: 2

Related Questions