diff --git a/src/Client.php b/src/Client.php index c4eaa10..2490d1e 100644 --- a/src/Client.php +++ b/src/Client.php @@ -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; @@ -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); @@ -90,7 +96,7 @@ public function getInstallationsNearest( */ public function getInstallation(int $installationId): Installation { - $this->client->getInstallation($installationId); + return $this->client->getInstallation($installationId); } /** @@ -107,7 +113,7 @@ public function getMeasurementsForInstallation( ?string $indexType = null ): Measurement { - $this->client->getMeasurementsForInstallation($installationId, $includeWind, $indexType); + return $this->client->getMeasurementsForInstallation($installationId, $includeWind, $indexType); } /** @@ -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); } /** @@ -142,7 +148,7 @@ public function getMeasurementsPoint( ?string $indexType = null ): Measurement { - $this->client->getMeasurementsPoint($lat, $lng, $indexType); + return $this->client->getMeasurementsPoint($lat, $lng, $indexType); } /** diff --git a/src/HttpClient/HttpClient.php b/src/HttpClient/HttpClient.php index 0060da5..929e9e1 100644 --- a/src/HttpClient/HttpClient.php +++ b/src/HttpClient/HttpClient.php @@ -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 diff --git a/tests/unit/ClientTest.php b/tests/unit/ClientTest.php index 50421c8..2bc5497 100644 --- a/tests/unit/ClientTest.php +++ b/tests/unit/ClientTest.php @@ -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; @@ -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; @@ -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'))); @@ -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'))); @@ -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'))); @@ -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'))); @@ -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'))); @@ -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);