Skip to content

Commit

Permalink
Curl retrieve path fallback to FileGetContents
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Nov 17, 2020
1 parent 235e949 commit 3fa45eb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/JsonSchema/Uri/Retrievers/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace JsonSchema\Uri\Retrievers;

use JsonSchema\Exception\InvalidSourceUriException;
use JsonSchema\Exception\RuntimeException;
use JsonSchema\Validator;

Expand All @@ -19,14 +20,17 @@
*/
class Curl extends AbstractRetriever
{
private $decorated;
protected $messageBody;

public function __construct()
public function __construct(UriRetrieverInterface $decorated = null)
{
if (!function_exists('curl_init')) {
// Cannot test this, because curl_init is present on all test platforms plus mock
throw new RuntimeException('cURL not installed'); // @codeCoverageIgnore
}

$this->decorated = $decorated;
}

/**
Expand All @@ -36,6 +40,15 @@ public function __construct()
*/
public function retrieve($uri)
{
$scheme = parse_url($uri, PHP_URL_SCHEME);
if (!$scheme) {
if ($decorated) {
return $this->decorated->retrieve($uri);
}

throw new InvalidSourceUriExceptiog('No scheme provided in URI: '. $uri);
}

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $uri);
Expand Down
16 changes: 16 additions & 0 deletions tests/Uri/Retrievers/CurlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ public function testNoContentType()
$c = new Curl();
$c->retrieve(realpath(__DIR__ . '/../../fixtures') . '/foobar-noheader.json');
}

public function testCurlFallbackFileGetContents()
{
$retriever = new Curl(new FileGetContents());
$result = $retriever->retrieve(__DIR__.'/../Fixture/child.json');
$this->assertNotEmpty($result);
}

/**
* @expectedException JsonSchema\Exception\ResourceNotFoundException
*/
public function testFetchMissingFile()
{
$retriever = new Curl(new FileGetContents());
$retriever->retrieve(__DIR__.'/Fixture/missing.json');
}
}
}

Expand Down

0 comments on commit 3fa45eb

Please sign in to comment.