Add MultiCurl::setXmlDecoder

This commit is contained in:
Zach Borboa
2016-01-31 10:53:32 -08:00
parent dce9acb7a2
commit ebca4c2c19
3 changed files with 46 additions and 0 deletions
+1
View File
@@ -248,6 +248,7 @@ MultiCurl::setReferrer($referrer)
MultiCurl::setTimeout($seconds)
MultiCurl::setURL($url)
MultiCurl::setUserAgent($user_agent)
MultiCurl::setXmlDecoder($function)
MultiCurl::start()
MultiCurl::success($callback)
MultiCurl::unsetHeader($key)
+15
View File
@@ -21,6 +21,7 @@ class MultiCurl
private $options = array();
private $jsonDecoder = null;
private $xmlDecoder = null;
/**
* Construct
@@ -387,6 +388,19 @@ class MultiCurl
}
}
/**
* Set XML Decoder
*
* @access public
* @param $function
*/
public function setXmlDecoder($function)
{
if (is_callable($function)) {
$this->xmlDecoder = $function;
}
}
/**
* Set Opt
*
@@ -593,6 +607,7 @@ class MultiCurl
$curl->setHeader($key, $value);
}
$curl->setJsonDecoder($this->jsonDecoder);
$curl->setXmlDecoder($this->xmlDecoder);
$curl->call($curl->beforeSendFunction);
}
}
@@ -1961,6 +1961,36 @@ class MultiCurlTest extends PHPUnit_Framework_TestCase
$this->assertEquals('bar', $get_2->response);
}
public function testXMLDecoder()
{
$multi_curl = new MultiCurl();
$multi_curl->setHeader('X-DEBUG-TEST', 'xml_with_cdata_response');
$multi_curl->setXmlDecoder(function($response) {
return 'foo';
});
$get_1 = $multi_curl->addGet(Test::TEST_URL);
$get_1->complete(function ($instance) {
PHPUnit_Framework_Assert::assertInstanceOf('Curl\Curl', $instance);
PHPUnit_Framework_Assert::assertEquals('foo', $instance->response);
});
$get_2 = $multi_curl->addGet(Test::TEST_URL);
$get_2->beforeSend(function ($instance) {
$instance->setXmlDecoder(function($response) {
return 'bar';
});
});
$get_2->complete(function ($instance) {
PHPUnit_Framework_Assert::assertInstanceOf('Curl\Curl', $instance);
PHPUnit_Framework_Assert::assertEquals('bar', $instance->response);
});
$multi_curl->start();
$this->assertEquals('foo', $get_1->response);
$this->assertEquals('bar', $get_2->response);
}
public function testDownloadCallback()
{
// Upload a file.