I am working on setting up a test kit for testing a PHP Propel project using Phactory and PHPUnit . I am currently trying to execute a unit test a function that makes an external request, and I want to drown out the layout of the response to this request.
Here is a snippet of the class I'm trying to test:
class Endpoint {
...
public function parseThirdPartyResponse() {
$response = $this->fetchUrl("www.example.com/api.xml");
...
}
public function fetchUrl($url) {
return file_get_contents($url);
}
...
And here is the test function I'm trying to write.
Phactory::define('endpoint', array('identifier' => 'endpoint_$n');
public function testParseThirdPartyResponse() {
$phEndpoint = Phactory::create('endpoint', $options);
$endpoint = new EndpointQuery()::create()->findPK($phEndpoint->id);
$stub = $this->getMock('Endpoint');
$xml = "...<target>test_target</target>...";
$stub->expects($this->any())
->method('fetchUrl')
->will($this->returnValue($xml));
$result = $endpoint->parseThirdPartyResponse();
$this->assertEquals('test_target', $result);
}
, ,
getMock, . , fetchUrl
, .
Phactory endpoint,
factory.
?
fetch_url $endpoint Endpoint, ?
; unit test
, -?
PHPUnit, "Stubbing and Mocking Web Services", 40 , wsdl. , , SO .
, . !!