From e0ee23ec0bdbb0f8336952ce99e73a1e2353422c Mon Sep 17 00:00:00 2001 From: Vivek Kumar Date: Fri, 3 Jun 2016 23:47:59 +0530 Subject: [PATCH 01/12] Closes #167 - Add json() for json responses * Add json() which returns decoded json from the response body if the response body is a valid json else throws an exception --- src/Response.php | 26 ++++++++++++++++++++++++++ tests/Response.php | 29 +++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 tests/Response.php diff --git a/src/Response.php b/src/Response.php index 98304fe8b..494d7b336 100644 --- a/src/Response.php +++ b/src/Response.php @@ -127,4 +127,30 @@ public function throw_for_status($allow_redirects = true) { throw new $exception(null, $this); } } + + /** + * Returns json decoded response + * + * @throws Requests_Exception If `$this->body` is not a valid json + * @return array + */ + public function json($assoc = true, $depth = 512, $options = 0) { + static $json_errors = array( + JSON_ERROR_DEPTH => 'JSON_ERROR_DEPTH - Maximum stack depth exceeded', + JSON_ERROR_STATE_MISMATCH => 'JSON_ERROR_STATE_MISMATCH - Underflow or the modes mismatch', + JSON_ERROR_CTRL_CHAR => 'JSON_ERROR_CTRL_CHAR - Unexpected control character found', + JSON_ERROR_SYNTAX => 'JSON_ERROR_SYNTAX - Syntax error, malformed JSON', + JSON_ERROR_UTF8 => 'JSON_ERROR_UTF8 - Malformed UTF-8 characters, possibly incorrectly encoded' + ); + + $data = json_decode($this->body, $assoc, $depth, $options); + + if (JSON_ERROR_NONE !== json_last_error()) { + $last_error = json_last_error(); + $error = isset($json_errors[$last_error]) ? $json_errors[$last_error] : 'Unknown error'; + throw new Requests_Exception('Unable to parse JSON data: ' . $error, 'response.invalid', $this); + } + + return $data; + } } diff --git a/tests/Response.php b/tests/Response.php new file mode 100644 index 000000000..bba8582c8 --- /dev/null +++ b/tests/Response.php @@ -0,0 +1,29 @@ +body = 'Invalid JSON'; + $response->json(); + } + + public function testJsonResponse() { + $response = new Requests_Response(); + $response->body = '{"success": false, "error": [], "data": null}'; + $decodedBody = $response->json(); + + $expected = array( + 'success' => false, + 'error' => array(), + 'data' => null + ); + + foreach($expected as $key => $value) + { + $this->assertEquals($value, $decodedBody[$key]); + } + } +} From 67236ca17c6973e4fac81c6233b7710c6a035d8c Mon Sep 17 00:00:00 2001 From: Vivek Kumar Date: Sat, 4 Jun 2016 00:03:14 +0530 Subject: [PATCH 02/12] Fix json_decode() for php 5.2 and 5.3 --- src/Response.php | 18 ++++++++++++------ tests/Response.php | 44 ++++++++++++++++++++++---------------------- 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/src/Response.php b/src/Response.php index 494d7b336..ebd53da21 100644 --- a/src/Response.php +++ b/src/Response.php @@ -134,7 +134,7 @@ public function throw_for_status($allow_redirects = true) { * @throws Requests_Exception If `$this->body` is not a valid json * @return array */ - public function json($assoc = true, $depth = 512, $options = 0) { + public function json($assoc = true) { static $json_errors = array( JSON_ERROR_DEPTH => 'JSON_ERROR_DEPTH - Maximum stack depth exceeded', JSON_ERROR_STATE_MISMATCH => 'JSON_ERROR_STATE_MISMATCH - Underflow or the modes mismatch', @@ -143,14 +143,20 @@ public function json($assoc = true, $depth = 512, $options = 0) { JSON_ERROR_UTF8 => 'JSON_ERROR_UTF8 - Malformed UTF-8 characters, possibly incorrectly encoded' ); - $data = json_decode($this->body, $assoc, $depth, $options); + $data = json_decode($this->body, $assoc); - if (JSON_ERROR_NONE !== json_last_error()) { - $last_error = json_last_error(); - $error = isset($json_errors[$last_error]) ? $json_errors[$last_error] : 'Unknown error'; - throw new Requests_Exception('Unable to parse JSON data: ' . $error, 'response.invalid', $this); + if (function_exists('json_last_error')) { + if (JSON_ERROR_NONE !== json_last_error()) { + $last_error = json_last_error(); + $error = isset($json_errors[$last_error]) ? $json_errors[$last_error] : 'Unknown error'; + throw new Requests_Exception('Unable to parse JSON data: ' . $error, 'response.invalid', $this); + } + } + elseif ($data === null) { + throw new Requests_Exception('Unable to parse JSON data.', 'response.invalid', $this); } + return $data; } } diff --git a/tests/Response.php b/tests/Response.php index bba8582c8..d78837dba 100644 --- a/tests/Response.php +++ b/tests/Response.php @@ -1,29 +1,29 @@ body = 'Invalid JSON'; - $response->json(); - } + /** + * @expectedException Requests_Exception + */ + public function testInvalidJsonResponse() { + $response = new Requests_Response(); + $response->body = 'Invalid JSON'; + $response->json(); + } - public function testJsonResponse() { - $response = new Requests_Response(); - $response->body = '{"success": false, "error": [], "data": null}'; - $decodedBody = $response->json(); + public function testJsonResponse() { + $response = new Requests_Response(); + $response->body = '{"success": false, "error": [], "data": null}'; + $decodedBody = $response->json(); - $expected = array( - 'success' => false, - 'error' => array(), - 'data' => null - ); + $expected = array( + 'success' => false, + 'error' => array(), + 'data' => null + ); - foreach($expected as $key => $value) - { - $this->assertEquals($value, $decodedBody[$key]); - } - } + foreach($expected as $key => $value) + { + $this->assertEquals($value, $decodedBody[$key]); + } + } } From 2c8ce6ce226eae84631d3f7975a06911e31cd585 Mon Sep 17 00:00:00 2001 From: Vivek Kumar Date: Sun, 7 Aug 2016 11:48:20 +0530 Subject: [PATCH 03/12] Fix json() for php5.4 --- src/Response.php | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/Response.php b/src/Response.php index ebd53da21..1afe18372 100644 --- a/src/Response.php +++ b/src/Response.php @@ -135,7 +135,7 @@ public function throw_for_status($allow_redirects = true) { * @return array */ public function json($assoc = true) { - static $json_errors = array( + $json_errors = array( JSON_ERROR_DEPTH => 'JSON_ERROR_DEPTH - Maximum stack depth exceeded', JSON_ERROR_STATE_MISMATCH => 'JSON_ERROR_STATE_MISMATCH - Underflow or the modes mismatch', JSON_ERROR_CTRL_CHAR => 'JSON_ERROR_CTRL_CHAR - Unexpected control character found', @@ -145,18 +145,10 @@ public function json($assoc = true) { $data = json_decode($this->body, $assoc); - if (function_exists('json_last_error')) { - if (JSON_ERROR_NONE !== json_last_error()) { - $last_error = json_last_error(); - $error = isset($json_errors[$last_error]) ? $json_errors[$last_error] : 'Unknown error'; - throw new Requests_Exception('Unable to parse JSON data: ' . $error, 'response.invalid', $this); - } - } - elseif ($data === null) { + if (!$data) { throw new Requests_Exception('Unable to parse JSON data.', 'response.invalid', $this); } - return $data; } } From fad715b1863a1050fdcacda87da1af987fd665f9 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 18 Sep 2021 02:15:39 +0200 Subject: [PATCH 04/12] Response::json(): revert changes for PHP 5.2 -5.4 compatibility PHP 5.6 is the new minimum PHP version, so these accommodations for PHP 5.2 - 5.4 can be reverted again. This reverts commit 8ea43344207964e707fdf62bced731745f314290 and 2d2ecf330a0bae3220534f2620bb7679364404d9. --- src/Response.php | 12 +++++++----- tests/Response.php | 44 ++++++++++++++++++++++---------------------- 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/src/Response.php b/src/Response.php index 1afe18372..494d7b336 100644 --- a/src/Response.php +++ b/src/Response.php @@ -134,8 +134,8 @@ public function throw_for_status($allow_redirects = true) { * @throws Requests_Exception If `$this->body` is not a valid json * @return array */ - public function json($assoc = true) { - $json_errors = array( + public function json($assoc = true, $depth = 512, $options = 0) { + static $json_errors = array( JSON_ERROR_DEPTH => 'JSON_ERROR_DEPTH - Maximum stack depth exceeded', JSON_ERROR_STATE_MISMATCH => 'JSON_ERROR_STATE_MISMATCH - Underflow or the modes mismatch', JSON_ERROR_CTRL_CHAR => 'JSON_ERROR_CTRL_CHAR - Unexpected control character found', @@ -143,10 +143,12 @@ public function json($assoc = true) { JSON_ERROR_UTF8 => 'JSON_ERROR_UTF8 - Malformed UTF-8 characters, possibly incorrectly encoded' ); - $data = json_decode($this->body, $assoc); + $data = json_decode($this->body, $assoc, $depth, $options); - if (!$data) { - throw new Requests_Exception('Unable to parse JSON data.', 'response.invalid', $this); + if (JSON_ERROR_NONE !== json_last_error()) { + $last_error = json_last_error(); + $error = isset($json_errors[$last_error]) ? $json_errors[$last_error] : 'Unknown error'; + throw new Requests_Exception('Unable to parse JSON data: ' . $error, 'response.invalid', $this); } return $data; diff --git a/tests/Response.php b/tests/Response.php index d78837dba..bba8582c8 100644 --- a/tests/Response.php +++ b/tests/Response.php @@ -1,29 +1,29 @@ body = 'Invalid JSON'; - $response->json(); - } + /** + * @expectedException Requests_Exception + */ + public function testInvalidJsonResponse() { + $response = new Requests_Response(); + $response->body = 'Invalid JSON'; + $response->json(); + } - public function testJsonResponse() { - $response = new Requests_Response(); - $response->body = '{"success": false, "error": [], "data": null}'; - $decodedBody = $response->json(); + public function testJsonResponse() { + $response = new Requests_Response(); + $response->body = '{"success": false, "error": [], "data": null}'; + $decodedBody = $response->json(); - $expected = array( - 'success' => false, - 'error' => array(), - 'data' => null - ); + $expected = array( + 'success' => false, + 'error' => array(), + 'data' => null + ); - foreach($expected as $key => $value) - { - $this->assertEquals($value, $decodedBody[$key]); - } - } + foreach($expected as $key => $value) + { + $this->assertEquals($value, $decodedBody[$key]); + } + } } From 87b5aa1c4b61f3aed44e644165ff4f8c8b14107c Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 18 Sep 2021 02:07:51 +0200 Subject: [PATCH 05/12] Response::json(): update to make code runnable again * Use namespaced names. * Namespace test class and rename test file to match. * Use `expectException()` method instead of annotation. --- src/Response.php | 4 ++-- tests/{Response.php => ResponseTest.php} | 18 ++++++++++++------ 2 files changed, 14 insertions(+), 8 deletions(-) rename tests/{Response.php => ResponseTest.php} (65%) diff --git a/src/Response.php b/src/Response.php index 494d7b336..ef9f64644 100644 --- a/src/Response.php +++ b/src/Response.php @@ -131,7 +131,7 @@ public function throw_for_status($allow_redirects = true) { /** * Returns json decoded response * - * @throws Requests_Exception If `$this->body` is not a valid json + * @throws \WpOrg\Requests\Exception If `$this->body` is not a valid json * @return array */ public function json($assoc = true, $depth = 512, $options = 0) { @@ -148,7 +148,7 @@ public function json($assoc = true, $depth = 512, $options = 0) { if (JSON_ERROR_NONE !== json_last_error()) { $last_error = json_last_error(); $error = isset($json_errors[$last_error]) ? $json_errors[$last_error] : 'Unknown error'; - throw new Requests_Exception('Unable to parse JSON data: ' . $error, 'response.invalid', $this); + throw new Exception('Unable to parse JSON data: ' . $error, 'response.invalid', $this); } return $data; diff --git a/tests/Response.php b/tests/ResponseTest.php similarity index 65% rename from tests/Response.php rename to tests/ResponseTest.php index bba8582c8..0ae678d06 100644 --- a/tests/Response.php +++ b/tests/ResponseTest.php @@ -1,17 +1,23 @@ expectException(Exception::class); + + $response = new Response(); $response->body = 'Invalid JSON'; $response->json(); } public function testJsonResponse() { - $response = new Requests_Response(); + $response = new Response(); $response->body = '{"success": false, "error": [], "data": null}'; $decodedBody = $response->json(); From 60dda54111993ee0bf4296ea7bbb1ce58f32ce75 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 18 Sep 2021 02:11:31 +0200 Subject: [PATCH 06/12] Response::json(): update to comply with coding standards --- src/Response.php | 12 +++++----- tests/ResponseTest.php | 51 +++++++++++++++++++++--------------------- 2 files changed, 31 insertions(+), 32 deletions(-) diff --git a/src/Response.php b/src/Response.php index ef9f64644..083b6ad3f 100644 --- a/src/Response.php +++ b/src/Response.php @@ -136,18 +136,18 @@ public function throw_for_status($allow_redirects = true) { */ public function json($assoc = true, $depth = 512, $options = 0) { static $json_errors = array( - JSON_ERROR_DEPTH => 'JSON_ERROR_DEPTH - Maximum stack depth exceeded', + JSON_ERROR_DEPTH => 'JSON_ERROR_DEPTH - Maximum stack depth exceeded', JSON_ERROR_STATE_MISMATCH => 'JSON_ERROR_STATE_MISMATCH - Underflow or the modes mismatch', - JSON_ERROR_CTRL_CHAR => 'JSON_ERROR_CTRL_CHAR - Unexpected control character found', - JSON_ERROR_SYNTAX => 'JSON_ERROR_SYNTAX - Syntax error, malformed JSON', - JSON_ERROR_UTF8 => 'JSON_ERROR_UTF8 - Malformed UTF-8 characters, possibly incorrectly encoded' + JSON_ERROR_CTRL_CHAR => 'JSON_ERROR_CTRL_CHAR - Unexpected control character found', + JSON_ERROR_SYNTAX => 'JSON_ERROR_SYNTAX - Syntax error, malformed JSON', + JSON_ERROR_UTF8 => 'JSON_ERROR_UTF8 - Malformed UTF-8 characters, possibly incorrectly encoded', ); $data = json_decode($this->body, $assoc, $depth, $options); - if (JSON_ERROR_NONE !== json_last_error()) { + if (json_last_error() !== JSON_ERROR_NONE) { $last_error = json_last_error(); - $error = isset($json_errors[$last_error]) ? $json_errors[$last_error] : 'Unknown error'; + $error = isset($json_errors[$last_error]) ? $json_errors[$last_error] : 'Unknown error'; throw new Exception('Unable to parse JSON data: ' . $error, 'response.invalid', $this); } diff --git a/tests/ResponseTest.php b/tests/ResponseTest.php index 0ae678d06..bb3d95b7a 100644 --- a/tests/ResponseTest.php +++ b/tests/ResponseTest.php @@ -6,30 +6,29 @@ use WpOrg\Requests\Response; use WpOrg\Requests\Tests\TestCase; -class ResponseTest extends TestCase { - - public function testInvalidJsonResponse() { - $this->expectException(Exception::class); - - $response = new Response(); - $response->body = 'Invalid JSON'; - $response->json(); - } - - public function testJsonResponse() { - $response = new Response(); - $response->body = '{"success": false, "error": [], "data": null}'; - $decodedBody = $response->json(); - - $expected = array( - 'success' => false, - 'error' => array(), - 'data' => null - ); - - foreach($expected as $key => $value) - { - $this->assertEquals($value, $decodedBody[$key]); - } - } +final class ResponseTest extends TestCase { + + public function testInvalidJsonResponse() { + $this->expectException(Exception::class); + + $response = new Response(); + $response->body = 'Invalid JSON'; + $response->json(); + } + + public function testJsonResponse() { + $response = new Response(); + $response->body = '{"success": false, "error": [], "data": null}'; + $decoded_body = $response->json(); + + $expected = array( + 'success' => false, + 'error' => array(), + 'data' => null, + ); + + foreach ($expected as $key => $value) { + $this->assertEquals($value, $decoded_body[$key]); + } + } } From f22bedd96516d9d9ad76c93b0942708226fc4b7c Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 18 Sep 2021 02:27:05 +0200 Subject: [PATCH 07/12] Response::json(): rename the method ... to be more descriptive. --- src/Response.php | 2 +- tests/ResponseTest.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Response.php b/src/Response.php index 083b6ad3f..450495572 100644 --- a/src/Response.php +++ b/src/Response.php @@ -134,7 +134,7 @@ public function throw_for_status($allow_redirects = true) { * @throws \WpOrg\Requests\Exception If `$this->body` is not a valid json * @return array */ - public function json($assoc = true, $depth = 512, $options = 0) { + public function decode_body($assoc = true, $depth = 512, $options = 0) { static $json_errors = array( JSON_ERROR_DEPTH => 'JSON_ERROR_DEPTH - Maximum stack depth exceeded', JSON_ERROR_STATE_MISMATCH => 'JSON_ERROR_STATE_MISMATCH - Underflow or the modes mismatch', diff --git a/tests/ResponseTest.php b/tests/ResponseTest.php index bb3d95b7a..bab04ed91 100644 --- a/tests/ResponseTest.php +++ b/tests/ResponseTest.php @@ -13,13 +13,13 @@ public function testInvalidJsonResponse() { $response = new Response(); $response->body = 'Invalid JSON'; - $response->json(); + $response->decode_body(); } public function testJsonResponse() { $response = new Response(); $response->body = '{"success": false, "error": [], "data": null}'; - $decoded_body = $response->json(); + $decoded_body = $response->decode_body(); $expected = array( 'success' => false, From 9f1401a4b5f13465961749c572347f4de80ea0cb Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 18 Sep 2021 02:28:09 +0200 Subject: [PATCH 08/12] Response::decode_body(): rename parameter ... to be in-line with the parameter name used in PHP itself. Ref: https://www.php.net/json-decode --- src/Response.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Response.php b/src/Response.php index 450495572..46ce66681 100644 --- a/src/Response.php +++ b/src/Response.php @@ -134,7 +134,7 @@ public function throw_for_status($allow_redirects = true) { * @throws \WpOrg\Requests\Exception If `$this->body` is not a valid json * @return array */ - public function decode_body($assoc = true, $depth = 512, $options = 0) { + public function decode_body($associative = true, $depth = 512, $options = 0) { static $json_errors = array( JSON_ERROR_DEPTH => 'JSON_ERROR_DEPTH - Maximum stack depth exceeded', JSON_ERROR_STATE_MISMATCH => 'JSON_ERROR_STATE_MISMATCH - Underflow or the modes mismatch', @@ -143,7 +143,7 @@ public function decode_body($assoc = true, $depth = 512, $options = 0) { JSON_ERROR_UTF8 => 'JSON_ERROR_UTF8 - Malformed UTF-8 characters, possibly incorrectly encoded', ); - $data = json_decode($this->body, $assoc, $depth, $options); + $data = json_decode($this->body, $associative, $depth, $options); if (json_last_error() !== JSON_ERROR_NONE) { $last_error = json_last_error(); From c5e76756f6dafa9323a5091b8b4f77bb93c84adf Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 18 Sep 2021 02:36:46 +0200 Subject: [PATCH 09/12] Response::decode_body(): modernize the error handling As of PHP 5.5.0, PHP contains the `json_last_error_msg()` function which will return the last error message. This removes the need for the error decoding array, which was outdated by now anyway due to new error codes having been introduced in PHP since the PR was originally pulled. Ref: https://www.php.net/manual/en/function.json-last-error-msg.php --- src/Response.php | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/src/Response.php b/src/Response.php index 46ce66681..6207b790d 100644 --- a/src/Response.php +++ b/src/Response.php @@ -135,20 +135,11 @@ public function throw_for_status($allow_redirects = true) { * @return array */ public function decode_body($associative = true, $depth = 512, $options = 0) { - static $json_errors = array( - JSON_ERROR_DEPTH => 'JSON_ERROR_DEPTH - Maximum stack depth exceeded', - JSON_ERROR_STATE_MISMATCH => 'JSON_ERROR_STATE_MISMATCH - Underflow or the modes mismatch', - JSON_ERROR_CTRL_CHAR => 'JSON_ERROR_CTRL_CHAR - Unexpected control character found', - JSON_ERROR_SYNTAX => 'JSON_ERROR_SYNTAX - Syntax error, malformed JSON', - JSON_ERROR_UTF8 => 'JSON_ERROR_UTF8 - Malformed UTF-8 characters, possibly incorrectly encoded', - ); - $data = json_decode($this->body, $associative, $depth, $options); if (json_last_error() !== JSON_ERROR_NONE) { - $last_error = json_last_error(); - $error = isset($json_errors[$last_error]) ? $json_errors[$last_error] : 'Unknown error'; - throw new Exception('Unable to parse JSON data: ' . $error, 'response.invalid', $this); + $last_error = json_last_error_msg(); + throw new Exception('Unable to parse JSON data: ' . $last_error, 'response.invalid', $this); } return $data; From 5731672051c45725d9ae3335089ef48e545e2707 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 18 Sep 2021 02:38:26 +0200 Subject: [PATCH 10/12] Response::decode_body(): improve the method documentation ... to be in-line with the documentation for the same parameters in the PHP native function. Ref: https://www.php.net/json-decode --- src/Response.php | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/Response.php b/src/Response.php index 6207b790d..ca50de2ed 100644 --- a/src/Response.php +++ b/src/Response.php @@ -129,10 +129,26 @@ public function throw_for_status($allow_redirects = true) { } /** - * Returns json decoded response + * JSON decode the response body. + * + * The method parameters are the same as those for the PHP native `json_decode()` function. + * + * @link https://php.net/json-decode + * + * @param ?bool $associative Optional. When `true`, JSON objects will be returned as associative arrays; + * When `false`, JSON objects will be returned as objects. + * When `null`, JSON objects will be returned as associative arrays + * or objects depending on whether `JSON_OBJECT_AS_ARRAY` is set in the flags. + * Defaults to `true` (in contrast to the PHP native default of `null`). + * @param int $depth Optional. Maximum nesting depth of the structure being decoded. + * Defaults to `512`. + * @param int $options Optional. Bitmask of JSON_BIGINT_AS_STRING, JSON_INVALID_UTF8_IGNORE, + * JSON_INVALID_UTF8_SUBSTITUTE, JSON_OBJECT_AS_ARRAY, JSON_THROW_ON_ERROR. + * Defaults to `0` (no options set). * - * @throws \WpOrg\Requests\Exception If `$this->body` is not a valid json * @return array + * + * @throws \WpOrg\Requests\Exception If `$this->body` is not valid json. */ public function decode_body($associative = true, $depth = 512, $options = 0) { $data = json_decode($this->body, $associative, $depth, $options); From 5a8e738dee13e761645181a00b70b742271c27e2 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 18 Sep 2021 03:08:05 +0200 Subject: [PATCH 11/12] Response::decode_body(): improve the tests * Use type safe `assertSame()` instead of `assertEquals()` and remove the unnecessary `foreach`. * Rework the "invalid JSON" test to a data provider and add some adidtional test cases. * Add `@covers` tags. * Add `@requires` tags for the JSON extension as while that extension has been bundled with PHP for a long time, it could still be disabled until PHP 8.0. * Add documentation. --- tests/ResponseTest.php | 54 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 49 insertions(+), 5 deletions(-) diff --git a/tests/ResponseTest.php b/tests/ResponseTest.php index bab04ed91..827d66594 100644 --- a/tests/ResponseTest.php +++ b/tests/ResponseTest.php @@ -6,16 +6,62 @@ use WpOrg\Requests\Response; use WpOrg\Requests\Tests\TestCase; +/** + * @coversDefaultClass \WpOrg\Requests\Response + */ final class ResponseTest extends TestCase { - public function testInvalidJsonResponse() { + /** + * Verify that an exception is thrown when the body content is invalid as JSON. + * + * @requires extension json + * + * @covers ::decode_body + * + * @dataProvider dataInvalidJsonResponse + * + * @param mixed $body Data to use as the Response body. + * + * @return void + */ + public function testInvalidJsonResponse($body) { $this->expectException(Exception::class); + $this->expectExceptionMessage('Unable to parse JSON data: '); $response = new Response(); - $response->body = 'Invalid JSON'; + $response->body = $body; + $response->decode_body(); } + /** + * Data provider. + * + * @return array + */ + public function dataInvalidJsonResponse() { + $data = array( + 'text string, not JSON (syntax error)' => array('Invalid JSON'), + 'invalid JSON: single quotes (syntax error)' => array("{ 'bar': 'baz' }"), + ); + + // An empty string is only regarded as invalid JSON since PHP 7.0. + if (PHP_VERSION_ID >= 70000) { + $data['empty string (syntax error)'] = array(''); + } + + return $data; + } + + /** + * Verify correctly decoding a body in valid JSON. + * + * @requires extension json + * + * @covers ::decode_body + * + * @return void + */ public function testJsonResponse() { $response = new Response(); $response->body = '{"success": false, "error": [], "data": null}'; @@ -27,8 +73,6 @@ public function testJsonResponse() { 'data' => null, ); - foreach ($expected as $key => $value) { - $this->assertEquals($value, $decoded_body[$key]); - } + $this->assertSame($expected, $decoded_body); } } From f454910a2e4d790273d10efd8f28e0775dff4ab6 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 18 Sep 2021 03:49:02 +0200 Subject: [PATCH 12/12] Composer: make the dependency on the JSON extension explicit --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 48301f5b2..de6e7ed12 100644 --- a/composer.json +++ b/composer.json @@ -36,7 +36,8 @@ "docs": "https://requests.ryanmccue.info/" }, "require": { - "php": ">=5.6" + "php": ">=5.6", + "ext-json": "*" }, "require-dev": { "requests/test-server": "dev-master",