Skip to content

Commit

Permalink
Fixes major bugs in Client class
Browse files Browse the repository at this point in the history
  • Loading branch information
AvantaR committed Dec 16, 2019
1 parent 8f08bcb commit 5192fe7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
18 changes: 12 additions & 6 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use GuzzleHttp\Exception\GuzzleException;
use Luft\ApiClient\ApiClient;
use Luft\HttpClient\HttpClient;
use Luft\HttpClient\HttpClientInterface;
use Luft\Models\Installation\Installation;
use Luft\Models\Measurement\Measurement;
use Symfony\Component\Serializer\Exception\ExceptionInterface;
Expand Down Expand Up @@ -36,9 +37,14 @@ class Client
*/
private $headers;

public function __construct(string $apiKey)
/**
* Client constructor.
* @param string $apiKey
* @param HttpClientInterface|null $httpClient
*/
public function __construct(string $apiKey, HttpClientInterface $httpClient = null)
{
$httpClient = new HttpClient();
$httpClient = $httpClient ?? new HttpClient();
$serializer = Serializer::getInstance();
$this->apiKey = $apiKey;
$this->client = new ApiClient($httpClient, $serializer);
Expand Down Expand Up @@ -90,7 +96,7 @@ public function getInstallationsNearest(
*/
public function getInstallation(int $installationId): Installation
{
$this->client->getInstallation($installationId);
return $this->client->getInstallation($installationId);
}

/**
Expand All @@ -107,7 +113,7 @@ public function getMeasurementsForInstallation(
?string $indexType = null
): Measurement
{
$this->client->getMeasurementsForInstallation($installationId, $includeWind, $indexType);
return $this->client->getMeasurementsForInstallation($installationId, $includeWind, $indexType);
}

/**
Expand All @@ -126,7 +132,7 @@ public function getMeasurementsNearest(
?string $indexType = null
): Measurement
{
$this->client->getMeasurementsNearest($lat, $lng, $maxDistanceKM, $indexType);
return $this->client->getMeasurementsNearest($lat, $lng, $maxDistanceKM, $indexType);
}

/**
Expand All @@ -142,7 +148,7 @@ public function getMeasurementsPoint(
?string $indexType = null
): Measurement
{
$this->client->getMeasurementsPoint($lat, $lng, $indexType);
return $this->client->getMeasurementsPoint($lat, $lng, $indexType);
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/HttpClient/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ class HttpClient implements HttpClientInterface
*/
private $client;

public function __construct($handler = null)
/**
* @param callable $handler
*/
public function __construct(callable $handler = null)
{
$config = [
'base_uri' => self::BASE_URI
Expand Down
25 changes: 10 additions & 15 deletions tests/unit/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\Psr7\Response;
use Luft\ApiClient\ApiClient;
use Luft\Client;
use Luft\HttpClient\HttpClient;
use Luft\Models\Installation\Address;
use Luft\Models\Installation\Coordinates;
Expand All @@ -12,7 +13,6 @@
use Luft\Models\Measurement\Measurement;
use Luft\Models\Meta\Level;
use Luft\Models\Meta\Type;
use Luft\Serializer;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Serializer\Exception\ExceptionInterface;

Expand All @@ -32,15 +32,14 @@ class ClientTest extends TestCase
protected function setUp(): void
{
$this->mockHandler = new MockHandler();
$this->client = new ApiClient(new HttpClient($this->mockHandler), Serializer::getInstance());
$this->client = new Client('randomKey', new HttpClient($this->mockHandler));
}

/**
* @test
* @throws GuzzleException
* @throws ExceptionInterface
*/
public function getMetaIndexes(): void
public function testGetMetaIndexes(): void
{
$this->mockHandler->append(new Response(200, [], file_get_contents(__DIR__ . '/data/meta.indexes.json')));

Expand All @@ -60,11 +59,10 @@ public function getMetaIndexes(): void
}

/**
* @test
* @throws GuzzleException
* @throws ExceptionInterface
*/
public function getInstallationsNearest(): void
public function testGetInstallationsNearest(): void
{
$this->mockHandler->append(new Response(200, [],
file_get_contents(__DIR__ . '/data/installations.nearest.json')));
Expand All @@ -89,11 +87,10 @@ public function getInstallationsNearest(): void
}

/**
* @test
* @throws GuzzleException
* @throws ExceptionInterface
*/
public function getInstallationId(): void
public function testGetInstallationId(): void
{
$this->mockHandler->append(new Response(200, [],
file_get_contents(__DIR__ . '/data/installations.id.json')));
Expand All @@ -104,11 +101,10 @@ public function getInstallationId(): void
}

/**
* @test
* @throws ExceptionInterface
* @throws GuzzleException
*/
public function getMeasurementsForInstallation(): void
public function testGetMeasurementsForInstallation(): void
{
$this->mockHandler->append(new Response(200, [],
file_get_contents(__DIR__ . '/data/measurements.installation.json')));
Expand All @@ -127,11 +123,10 @@ public function getMeasurementsForInstallation(): void
}

/**
* @test
* @throws ExceptionInterface
* @throws GuzzleException
*/
public function getMeasurementsNearest(): void
public function testGetMeasurementsNearest(): void
{
$this->mockHandler->append(new Response(200, [],
file_get_contents(__DIR__ . '/data/measurements.nearest.json')));
Expand All @@ -144,12 +139,12 @@ public function getMeasurementsNearest(): void
}

/**
* @test
* @throws ExceptionInterface
*/
public function getMeasurementsPoint(): void
public function testGetMeasurementsPoint(): void
{
$this->mockHandler->append(new Response(200, [],
file_get_contents(__DIR__ . '/data/measurements.nearest.json')));
file_get_contents(__DIR__ . '/data/measurements.point.json')));

$result = $this->client->getMeasurementsPoint(50.062006, 19.940984, 'AIRLY_CAQI');
$this->assertInstanceOf(Measurement::class, $result);
Expand Down

0 comments on commit 5192fe7

Please sign in to comment.