Skip to content

Instantly share code, notes, and snippets.

@zircote
Created June 4, 2011 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zircote/1007931 to your computer and use it in GitHub Desktop.
Save zircote/1007931 to your computer and use it in GitHub Desktop.
Unit Testing Web-Services With Zend Framework
<?xml version='1.0' encoding='UTF-8'?>
<eveapi version="2">
<currentTime>2010-10-05 20:28:55</currentTime>
<result>
<userID>3000000</userID>
<paidUntil>2011-01-01 00:00:00</paidUntil>
<createDate>2004-01-01 00:00:00</createDate>
<logonCount>9999</logonCount>
<logonMinutes>9999</logonMinutes>
<rowset name="Offers" key="offerID" columns="offerID,offeredDate,from,to,ISK" />
</result>
<cachedUntil>2010-10-05 20:33:55</cachedUntil>
</eveapi>
<?php
class Api extends Zend_Http_Client
{
/**
*
* @group Account
*/
public function accountStatus()
{
...
/* Makes a call to the endpoint with parameters etc to an endpoint and returns a result. */
...
$result = $this->request(self::GET);
return new ApiParser($result);
}
}
<?php
require_once 'EveLib/Ccp/Api.php';
require_once 'PHPUnit/Framework/TestCase.php';
/**
* Api test case.
*/
class ApiTest extends PHPUnit_Framework_TestCase
{
private $responseHeader;
private $apiClient;
protected function setUp ()
{
$this->responseHeader = "HTTP/1.1 200 OK\r\n" .
"Content-type: text/xml\r\n\r\n";
$this->apiClient = new Api();
/* Set the Test Adapter in the fixture */
$this->apiClient->setAdapter(new Zend_Http_Client_Adapter_Test());
}
/**
* Prepares the environment before running a test.
*/
protected function setUp ()
{
$this->responseHeader = "HTTP/1.1 200 OK\r\n" .
"Content-type: text/xml\r\n\r\n";
$this->apiClient = new Api();
/* Set the Test Adapter in the fixture */
$this->apiClient->setAdapter(new Zend_Http_Client_Adapter_Test());
}
/**
* Cleans up the environment after running a test.
*/
protected function tearDown ()
{
$this->Api = null;
parent::tearDown();
}
/**
* Test the Api#accountStatus method using a mock adapter
* insure the desired return is received
*/
public function testApiSuccess()
{
/* contact the HTTP headers and the desired body into a single string */
$fixture = $this->responseHeader . file_get_contents(
FIXURES_LOCATION . '/accountStatus.200.xml' // The directory and file with the body
);
/* set the expected result in the adapter */
$this->Api->getAdapter()->setResponse($fixture);
/* make the call using the Test adapter */
$data = $this->Api->accountStatus();
/* assert our response handling is returning the proper object */
/* it would be here we could test all of the proper and expected data is in the object */
$this->assertInstanceOf('ApiParser', $data);
/* is this the correct response code? */
$this->assertEquals(200,$this->Api->getLastResponse()->getStatus());
}
}
$headers = array(
200 => "HTTP/1.1 200 OK\r\nContent-type: text/xml\r\n\r\n",
201 => "HTTP/1.1 201 Created\r\nContent-type: text/xml\r\n\r\n",
202 => "HTTP/1.1 202 Accepted\r\nContent-type: text/xml\r\n\r\n",
400 => "HTTP/1.1 400 Bad Request\r\nContent-type: text/xml\r\n\r\n",
500 => "HTTP/1.1 500 Internal Server Error\r\nContent-type: text/xml\r\n\r\n",
...
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment