Thursday, 15 August 2013

PHP Unit Testing: How to test low level functions of an external api

PHP Unit Testing: How to test low level functions of an external api

Hello folks, I wrote a low level implementation for a XmlRPC-Api and I've
trouble to test the communication.
Here is my code.
abstract class BaseClient
{
protected function call($method, array $params = array())
{
$request = xmlrpc_encode_request($method, $parameters);
$file = file_get_contents($this->getDSN(), false, $context);
$response = xmlrpc_decode($file);
if ($response && xmlrpc_is_fault(array($response))) {
trigger_error("xmlrpc: {$response[faultString]}
({$response[faultCode]})");
}
return $response;
}
}
Client extends BaseClient
{
public function testCall($msg)
{
return $this->call('testMethid', array($msg));
}
}
And here is my Test.
ClientTest extends PHPUnit_FrameWork_TestCase
{
public function testTestCall()
{
$c = new Client();
$resp = $c->testCall('Hello World');
$this->assertEquals('Hello World', $resp);
}
}
This test will crash every time, because its not possible to access the
api inside a testing environment. I can't see a solution to mock and
inject the call function. What can I do? Maybe my object structure is bad
and not able to test and how can I improve this structure (if this
happen)?
Cheers.

No comments:

Post a Comment