Browse Source

Merge pull request #113 from barnabywalters/main

Used default options for fetching as well as parsing
pull/119/head
Aaron Parecki 1 year ago
committed by GitHub
parent
commit
2a4827a781
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 3 deletions
  1. +6
    -1
      lib/XRay.php
  2. +1
    -2
      tests/FetchTestDisabled.php
  3. +25
    -0
      tests/LibraryTest.php

+ 6
- 1
lib/XRay.php View File

@ -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'];

+ 1
- 2
tests/FetchTestDisabled.php View File

@ -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']);
}
}

+ 25
- 0
tests/LibraryTest.php View File

@ -109,4 +109,29 @@ class LibraryTest extends PHPUnit\Framework\TestCase
);
}
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']);
}
}

Loading…
Cancel
Save