From e087a5c117a7373b4396a610e6f130008802b1d5 Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Mon, 18 Jun 2018 01:29:16 -0700 Subject: [PATCH 1/5] Clean up --- tests/PHPCurlClass/PHPCurlClassTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/PHPCurlClass/PHPCurlClassTest.php b/tests/PHPCurlClass/PHPCurlClassTest.php index 2bbb672..3b5fae3 100644 --- a/tests/PHPCurlClass/PHPCurlClassTest.php +++ b/tests/PHPCurlClass/PHPCurlClassTest.php @@ -2595,7 +2595,7 @@ class CurlTest extends \PHPUnit\Framework\TestCase } } - public function testXMLResponse() + public function testXmlResponse() { foreach (array( 'Content-Type', @@ -3069,7 +3069,7 @@ class CurlTest extends \PHPUnit\Framework\TestCase $this->assertNotEmpty($stderr); } - public function testXMLDecoder() + public function testXmlDecoder() { $test = new Test(); $test->server('xml_with_cdata_response', 'POST'); From 2bb151e03d51b53cb6d19a4ee3302003735ff071 Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Mon, 18 Jun 2018 23:08:26 -0700 Subject: [PATCH 2/5] Allow specifying xml decoder options when calling Curl::setDefaultXmlDecoder --- src/Curl/Curl.php | 13 ++++++++++++- src/Curl/Decoder.php | 11 ++++++++--- tests/PHPCurlClass/PHPCurlClassTest.php | 25 +++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index b8c17dd..55c9a01 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -55,6 +55,7 @@ class Curl private $jsonDecoderArgs = array(); private $jsonPattern = '/^(?:application|text)\/(?:[a-z]+(?:[\.-][0-9a-z]+){0,}[\+\.]|x-)?json(?:-[a-z]+)?/i'; + private $xmlDecoderArgs = array(); private $xmlPattern = '~^(?:text/|application/(?:atom\+|rss\+|soap\+)?)xml~i'; private $defaultDecoder = null; @@ -206,6 +207,7 @@ class Curl $this->jsonDecoder = null; $this->jsonDecoderArgs = null; $this->xmlDecoder = null; + $this->xmlDecoderArgs = null; $this->defaultDecoder = null; } @@ -855,10 +857,15 @@ class Curl * Set Default XML Decoder * * @access public + * @param $class_name + * @param $options + * @param $ns + * @param $is_prefix */ public function setDefaultXmlDecoder() { $this->xmlDecoder = '\Curl\Decoder::decodeXml'; + $this->xmlDecoderArgs = func_get_args(); } /** @@ -973,8 +980,10 @@ class Curl { if ($mixed === false) { $this->xmlDecoder = false; + $this->xmlDecoderArgs = array(); } elseif (is_callable($mixed)) { $this->xmlDecoder = $mixed; + $this->xmlDecoderArgs = array(); } } @@ -1418,7 +1427,9 @@ class Curl } } elseif (preg_match($this->xmlPattern, $response_headers['Content-Type'])) { if ($this->xmlDecoder) { - $response = call_user_func($this->xmlDecoder, $response); + $args = $this->xmlDecoderArgs; + array_unshift($args, $response); + $response = call_user_func_array($this->xmlDecoder, $args); } } else { if ($this->defaultDecoder) { diff --git a/src/Curl/Decoder.php b/src/Curl/Decoder.php index f12fcce..236cf54 100644 --- a/src/Curl/Decoder.php +++ b/src/Curl/Decoder.php @@ -35,11 +35,16 @@ class Decoder * Decode XML * * @access public - * @param $response + * @param $data + * @param $class_name + * @param $options + * @param $ns + * @param $is_prefix */ - public static function decodeXml($response) + public static function decodeXml() { - $xml_obj = @simplexml_load_string($response); + $args = func_get_args(); + $xml_obj = @call_user_func_array('simplexml_load_string', $args); if (!($xml_obj === false)) { $response = $xml_obj; } diff --git a/tests/PHPCurlClass/PHPCurlClassTest.php b/tests/PHPCurlClass/PHPCurlClassTest.php index 3b5fae3..199e0a1 100644 --- a/tests/PHPCurlClass/PHPCurlClassTest.php +++ b/tests/PHPCurlClass/PHPCurlClassTest.php @@ -2574,6 +2574,31 @@ class CurlTest extends \PHPUnit\Framework\TestCase } } + public function testXmlDecoderOptions() + { + // Implicit default xml decoder should return object. + $test = new Test(); + $test->server('xml_with_cdata_response', 'GET'); + $this->assertTrue(is_object($test->curl->response)); + $this->assertFalse(strpos($test->curl->response->saveXML(), 'curl->setDefaultXmlDecoder(); + $test->server('xml_with_cdata_response', 'GET'); + $this->assertTrue(is_object($test->curl->response)); + $this->assertFalse(strpos($test->curl->response->saveXML(), 'curl->setDefaultXmlDecoder($class_name, $options); + $test->server('xml_with_cdata_response', 'GET'); + $this->assertTrue(is_object($test->curl->response)); + $this->assertTrue(strpos($test->curl->response->saveXML(), ' Date: Mon, 18 Jun 2018 23:12:57 -0700 Subject: [PATCH 3/5] Reorder tests --- tests/PHPCurlClass/PHPCurlClassTest.php | 135 ++++++++++++------------ 1 file changed, 68 insertions(+), 67 deletions(-) diff --git a/tests/PHPCurlClass/PHPCurlClassTest.php b/tests/PHPCurlClass/PHPCurlClassTest.php index 199e0a1..41d319e 100644 --- a/tests/PHPCurlClass/PHPCurlClassTest.php +++ b/tests/PHPCurlClass/PHPCurlClassTest.php @@ -2599,6 +2599,29 @@ class CurlTest extends \PHPUnit\Framework\TestCase $this->assertTrue(strpos($test->curl->response->saveXML(), 'server('xml_with_cdata_response', 'POST'); + $this->assertTrue(is_object($test->curl->response)); + $this->assertInstanceOf('SimpleXMLElement', $test->curl->response); + $this->assertFalse(strpos($test->curl->response->saveXML(), 'curl->setXmlDecoder(function ($response) { + return simplexml_load_string($response, 'SimpleXMLElement', LIBXML_NOCDATA); + }); + $test->server('xml_with_cdata_response', 'POST'); + $this->assertTrue(is_object($test->curl->response)); + $this->assertInstanceOf('SimpleXMLElement', $test->curl->response); + $this->assertTrue(strpos($test->curl->response->saveXML(), 'curl->setXmlDecoder(false); + $test->server('xml_with_cdata_response', 'POST'); + $this->assertTrue(is_string($test->curl->response)); + } + public function testXmlContentTypeDetection() { $xml_content_types = array( @@ -2671,6 +2694,51 @@ class CurlTest extends \PHPUnit\Framework\TestCase } } + public function testDefaultDecoder() + { + // Default. + $test = new Test(); + $test->server('download_file_size', 'GET'); + $this->assertTrue(is_string($test->curl->response)); + + // Callable. + $test = new Test(); + $test->curl->setDefaultDecoder(function ($response) { + return '123'; + }); + $test->server('download_file_size', 'GET'); + $this->assertEquals('123', $test->curl->response); + + // "json". + $test = new Test(); + $test->curl->setDefaultDecoder('json'); + $test->server('json_response', 'POST', array( + 'key' => 'Content-Type', + 'value' => 'application/but-not-json', + )); + $this->assertInstanceOf('stdClass', $test->curl->response); + + // "xml". + $test = new Test(); + $test->curl->setDefaultDecoder('xml'); + $test->server('xml_response', 'POST', array( + 'key' => 'Content-Type', + 'value' => 'text/but-not-xml', + )); + $this->assertInstanceOf('SimpleXMLElement', $test->curl->response); + + // False. + $test = new Test(); + $test->curl->setDefaultDecoder('json'); + $test->curl->setDefaultDecoder(false); + $test->server('json_response', 'POST', array( + 'key' => 'Content-Type', + 'value' => 'application/but-not-json', + )); + $this->assertTrue(is_string($test->curl->response)); + } + + public function testEmptyResponse() { $response = "\r\n\r\n"; @@ -3094,73 +3162,6 @@ class CurlTest extends \PHPUnit\Framework\TestCase $this->assertNotEmpty($stderr); } - public function testXmlDecoder() - { - $test = new Test(); - $test->server('xml_with_cdata_response', 'POST'); - $this->assertTrue(is_object($test->curl->response)); - $this->assertInstanceOf('SimpleXMLElement', $test->curl->response); - $this->assertFalse(strpos($test->curl->response->saveXML(), 'curl->setXmlDecoder(function ($response) { - return simplexml_load_string($response, 'SimpleXMLElement', LIBXML_NOCDATA); - }); - $test->server('xml_with_cdata_response', 'POST'); - $this->assertTrue(is_object($test->curl->response)); - $this->assertInstanceOf('SimpleXMLElement', $test->curl->response); - $this->assertTrue(strpos($test->curl->response->saveXML(), 'curl->setXmlDecoder(false); - $test->server('xml_with_cdata_response', 'POST'); - $this->assertTrue(is_string($test->curl->response)); - } - - public function testDefaultDecoder() - { - // Default. - $test = new Test(); - $test->server('download_file_size', 'GET'); - $this->assertTrue(is_string($test->curl->response)); - - // Callable. - $test = new Test(); - $test->curl->setDefaultDecoder(function ($response) { - return '123'; - }); - $test->server('download_file_size', 'GET'); - $this->assertEquals('123', $test->curl->response); - - // "json". - $test = new Test(); - $test->curl->setDefaultDecoder('json'); - $test->server('json_response', 'POST', array( - 'key' => 'Content-Type', - 'value' => 'application/but-not-json', - )); - $this->assertInstanceOf('stdClass', $test->curl->response); - - // "xml". - $test = new Test(); - $test->curl->setDefaultDecoder('xml'); - $test->server('xml_response', 'POST', array( - 'key' => 'Content-Type', - 'value' => 'text/but-not-xml', - )); - $this->assertInstanceOf('SimpleXMLElement', $test->curl->response); - - // False. - $test = new Test(); - $test->curl->setDefaultDecoder('json'); - $test->curl->setDefaultDecoder(false); - $test->server('json_response', 'POST', array( - 'key' => 'Content-Type', - 'value' => 'application/but-not-json', - )); - $this->assertTrue(is_string($test->curl->response)); - } - public function testTotalTime() { $test = new Test(); From bc1dd9791762ae59edd77aebf36af4e349b737b2 Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Mon, 18 Jun 2018 23:50:28 -0700 Subject: [PATCH 4/5] Simplify test --- tests/PHPCurlClass/PHPCurlClassTest.php | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/tests/PHPCurlClass/PHPCurlClassTest.php b/tests/PHPCurlClass/PHPCurlClassTest.php index 41d319e..3d3cc90 100644 --- a/tests/PHPCurlClass/PHPCurlClassTest.php +++ b/tests/PHPCurlClass/PHPCurlClassTest.php @@ -1406,13 +1406,8 @@ class CurlTest extends \PHPUnit\Framework\TestCase public function testJsonDecoder() { - $data = array( - 'key' => 'Content-Type', - 'value' => 'application/json', - ); - $test = new Test(); - $test->server('json_response', 'POST', $data); + $test->server('json_response', 'GET'); $this->assertTrue(is_object($test->curl->response)); $this->assertFalse(is_array($test->curl->response)); @@ -1420,13 +1415,13 @@ class CurlTest extends \PHPUnit\Framework\TestCase $test->curl->setJsonDecoder(function ($response) { return json_decode($response, true); }); - $test->server('json_response', 'POST', $data); + $test->server('json_response', 'GET'); $this->assertFalse(is_object($test->curl->response)); $this->assertTrue(is_array($test->curl->response)); $test = new Test(); $test->curl->setJsonDecoder(false); - $test->server('json_response', 'POST', $data); + $test->server('json_response', 'GET'); $this->assertTrue(is_string($test->curl->response)); } From 0e1a0a16311d9070ac092c18f95bab3bfa803d7b Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Tue, 19 Jun 2018 00:09:47 -0700 Subject: [PATCH 5/5] Clean up --- src/Curl/Curl.php | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index 55c9a01..67435f3 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -961,10 +961,7 @@ class Curl */ public function setJsonDecoder($mixed) { - if ($mixed === false) { - $this->jsonDecoder = false; - $this->jsonDecoderArgs = array(); - } elseif (is_callable($mixed)) { + if ($mixed === false || is_callable($mixed)) { $this->jsonDecoder = $mixed; $this->jsonDecoderArgs = array(); } @@ -978,10 +975,7 @@ class Curl */ public function setXmlDecoder($mixed) { - if ($mixed === false) { - $this->xmlDecoder = false; - $this->xmlDecoderArgs = array(); - } elseif (is_callable($mixed)) { + if ($mixed === false || is_callable($mixed)) { $this->xmlDecoder = $mixed; $this->xmlDecoderArgs = array(); }