From 2886b9f339f39e066e757a5eaffb0b698b8028bc Mon Sep 17 00:00:00 2001 From: Barnaby Walters Date: Sun, 6 Nov 2022 00:26:15 +0100 Subject: [PATCH 1/2] Applied default options to fetch call as well as parser processing --- lib/XRay.php | 7 ++++++- tests/LibraryTest.php | 1 - 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/XRay.php b/lib/XRay.php index 273e6fa..da76868 100644 --- a/lib/XRay.php +++ b/lib/XRay.php @@ -30,7 +30,12 @@ class XRay { public function parse($url, $opts_or_body=false, $opts_for_body=[]) { if(!$opts_or_body || is_array($opts_or_body)) { $fetch = new XRay\Fetcher($this->http); - $response = $fetch->fetch($url, $opts_or_body); + if (is_array($opts_or_body)) { + $fetch_opts = array_merge($this->defaultOptions, $opts_or_body); + } else { + $fetch_opts = $this->defaultOptions; + } + $response = $fetch->fetch($url, $fetch_opts); if(!empty($response['error'])) return $response; $body = $response['body']; diff --git a/tests/LibraryTest.php b/tests/LibraryTest.php index 3218bbd..2a70683 100644 --- a/tests/LibraryTest.php +++ b/tests/LibraryTest.php @@ -108,5 +108,4 @@ class LibraryTest extends PHPUnit\Framework\TestCase $normalXRay->parse($url, $html) ); } - } From 3766187ce252773f4cd0ca491eec5e71b88914f6 Mon Sep 17 00:00:00 2001 From: Barnaby Walters Date: Sun, 6 Nov 2022 00:46:27 +0100 Subject: [PATCH 2/2] Added (network!) test for default options being used for fetching as well as parsing --- tests/FetchTestDisabled.php | 3 +-- tests/LibraryTest.php | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/tests/FetchTestDisabled.php b/tests/FetchTestDisabled.php index b939370..6c27c85 100644 --- a/tests/FetchTestDisabled.php +++ b/tests/FetchTestDisabled.php @@ -32,7 +32,6 @@ class FetchTest extends PHPUnit\Framework\TestCase { $url = 'https://nghttp2.org/httpbin/ip'; $response = $this->http->get($url); - $this->assertEquals('', $response['error']); + $this->assertEquals('', $response['error']); } - } \ No newline at end of file diff --git a/tests/LibraryTest.php b/tests/LibraryTest.php index 2a70683..3252faa 100644 --- a/tests/LibraryTest.php +++ b/tests/LibraryTest.php @@ -108,4 +108,30 @@ class LibraryTest extends PHPUnit\Framework\TestCase $normalXRay->parse($url, $html) ); } + + public function testDefaultOptionsAreUsedForFetching() + { + // LibraryTest::testDefaultOptionsAreUsed can only test that default options are merged and passed + // to the relevant format handler. To test that they’re additionally passed to the fetcher currently + // requires a network request to the twitter API for an auth error. + // A potential future improvement for this would be to make a new mock HTTP client object which + // accepts a callback, which gets passed the request it would send. We can then check that the + // request has the parameters we want without having to actually hit the network. + $url = 'https://twitter.com/BarnabyWalters/status/990659593561952256'; + + // Confirm the expected behaviour. + $xray = new p3k\XRay(); + $result = $xray->parse($url); + $this->assertEquals('missing_parameters', $result['error']); + + $xray = new p3k\XRay([ + 'twitter_api_key' => 'extremely real API credentials', + 'twitter_api_secret' => 'extremely real API credentials', + 'twitter_access_token' => 'extremely real API credentials', + 'twitter_access_token_secret' => 'extremely real API credentials' + ]); + $result = $xray->parse($url); + $this->assertEquals('twitter_error', $result['error']); + } + }