Skip to content

Commit

Permalink
Merge pull request #22 from GetDKAN/mock-guzzle
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-m authored Jun 12, 2024
2 parents 51e317b + c0d012b commit 7c0ea98
Show file tree
Hide file tree
Showing 5 changed files with 447 additions and 15 deletions.
36 changes: 30 additions & 6 deletions src/ETL/Extract/DataJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,36 @@
namespace Harvest\ETL\Extract;

use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use Harvest\Util;

class DataJson extends Extract
{

/**
* Harvest Plan, decoded JSON object.
*
* @var object
*/
protected $harvest_plan;

public function __construct($harvest_plan)
/**
* Inject the guzzle client.
*
* @var \GuzzleHttp\ClientInterface
*/
protected $client;

public function __construct(object $harvest_plan, ClientInterface $client = null)
{
$this->client = $client ?? new Client();
$this->harvest_plan = $harvest_plan;
}

/**
* @return array<string, mixed>
* Get the items to be harvested.
*
* @return array
* The items to be harvested.
*/
public function getItems(): array
{
Expand Down Expand Up @@ -44,11 +60,19 @@ public function getItems(): array
return $datasets;
}

private function httpRequest($uri)
/**
* Make the HTTP request to get harvest data.
*
* @param string $uri
* URI for request.
*
* @return string
* The response body.
*/
private function httpRequest(string $uri): string
{
try {
$client = new Client();
$res = $client->get($uri);
$res = $this->client->request('GET', $uri);
return (string) $res->getBody();
} catch (\Exception $exception) {
throw new \Exception("Error reading {$uri}");
Expand Down
2 changes: 1 addition & 1 deletion src/ETL/Extract/Extract.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
abstract class Extract implements IExtract
{

/**
/**
* {@inheritDoc}
*/
public function run(): array
Expand Down
10 changes: 5 additions & 5 deletions src/ETL/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

namespace Harvest\ETL;

use Harvest\Storage\StorageInterface;
use Opis\JsonSchema\Validator;
use Opis\JsonSchema\ValidationResult;
use Opis\JsonSchema\ValidationError;
use Opis\JsonSchema\Schema;

class Factory
Expand All @@ -14,17 +11,20 @@ class Factory
public $harvestPlan;
public $itemStorage;
public $hashStorage;
protected $client;

public function __construct(
$harvest_plan,
$item_storage,
$hash_storage
$hash_storage,
$client = null
) {
if (self::validateHarvestPlan($harvest_plan)) {
$this->harvestPlan = $harvest_plan;
}
$this->itemStorage = $item_storage;
$this->hashStorage = $hash_storage;
$this->client = $client;
}

public function get($type)
Expand All @@ -37,7 +37,7 @@ public function get($type)
throw new \Exception("Class {$class} does not exist");
}

return new $class($this->harvestPlan);
return new $class($this->harvestPlan, $this->client);
} elseif ($type == "load") {
$class = $this->harvestPlan->load->type;

Expand Down
15 changes: 12 additions & 3 deletions test/HarvesterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,16 @@ public function testBasic(string $uri): void
$item_store = new MemStore();
$hash_store = new MemStore();

$harvester = $this->getHarvester($plan, $item_store, $hash_store);
$mock_client = $this->createMock(\GuzzleHttp\Client::class);
$mock_client->method('request')->willReturn(
new \GuzzleHttp\Psr7\Response(
200,
[],
file_get_contents(__DIR__ . "/json/data3.json")
)
);

$harvester = $this->getHarvester($plan, $item_store, $hash_store, $mock_client);

$result = $harvester->harvest();

Expand Down Expand Up @@ -101,7 +110,7 @@ private function getPlan(string $name)
return json_decode($content);
}

private function getHarvester($plan, $item_store = null, $hash_store = null): Harvester
private function getHarvester($plan, $item_store = null, $hash_store = null, $client = null): Harvester
{

if (!isset($item_store)) {
Expand All @@ -112,7 +121,7 @@ private function getHarvester($plan, $item_store = null, $hash_store = null): Ha
$hash_store = new MemStore();
}

$factory = new Factory($plan, $item_store, $hash_store);
$factory = new Factory($plan, $item_store, $hash_store, $client);
return new Harvester($factory);
}
}
Loading

0 comments on commit 7c0ea98

Please sign in to comment.