From dd490da57bf16131b51c5da7054ff951d4139b59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Pimpa=CC=83o?= Date: Wed, 23 Aug 2023 16:47:38 +0100 Subject: [PATCH 1/8] chore: added PHP8 keyword --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 42c9637..aec0545 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "programmatordev/openweathermap-php-api", "description": "OpenWeatherMap PHP library that provides convenient access to the OpenWeatherMap API", "type": "library", - "keywords": ["OpenWeatherMap", "API", "PHP", "SDK", "PSR-18", "PSR-17", "PSR-6", "PSR-3"], + "keywords": ["OpenWeatherMap", "API", "PHP", "PHP8", "SDK", "PSR-18", "PSR-17", "PSR-6", "PSR-3"], "license": "MIT", "authors": [ { From 86c88c5257834b90a1ea529076d6ad6eb1d95ca0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Pimpa=CC=83o?= Date: Wed, 23 Aug 2023 18:08:10 +0100 Subject: [PATCH 2/8] chore: dot now allow cache ttl to be null --- docs/03-supported-apis.md | 10 ++++++++-- src/Endpoint/AbstractEndpoint.php | 2 +- src/Endpoint/GeocodingEndpoint.php | 2 +- src/Endpoint/Util/WithCacheTtlTrait.php | 6 +++--- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/docs/03-supported-apis.md b/docs/03-supported-apis.md index 580f29a..0f62849 100644 --- a/docs/03-supported-apis.md +++ b/docs/03-supported-apis.md @@ -278,10 +278,16 @@ $openWeatherMap->getWeather() #### `withCacheTtl` ```php -withCacheTtl(?int $time): self +withCacheTtl(int $seconds): self ``` -Makes a request and saves into cache with the provided time duration value (in seconds). +Makes a request and saves into cache with the provided value in seconds. + +If `0` seconds is provided, the request will not be cached. + +> **Note** +> Setting cache to `0` seconds will **not** invalidate any existing cache. + Check the [Cache TTL](02-configuration.md#cache-ttl) section for more information regarding default values. Available for all APIs if `cache` is enabled in the [configuration](02-configuration.md#cache). diff --git a/src/Endpoint/AbstractEndpoint.php b/src/Endpoint/AbstractEndpoint.php index ca570dd..ef48054 100644 --- a/src/Endpoint/AbstractEndpoint.php +++ b/src/Endpoint/AbstractEndpoint.php @@ -39,7 +39,7 @@ class AbstractEndpoint protected string $language; - protected ?int $cacheTtl = 60 * 10; // 10 minutes + protected int $cacheTtl = 60 * 10; // 10 minutes public function __construct(protected OpenWeatherMap $api) { diff --git a/src/Endpoint/GeocodingEndpoint.php b/src/Endpoint/GeocodingEndpoint.php index b254a32..df8b5ee 100644 --- a/src/Endpoint/GeocodingEndpoint.php +++ b/src/Endpoint/GeocodingEndpoint.php @@ -26,7 +26,7 @@ class GeocodingEndpoint extends AbstractEndpoint private string $urlGeocodingReverse = 'https://api.openweathermap.org/geo/1.0/reverse'; - protected ?int $cacheTtl = 60 * 60 * 24 * 30; // 30 days + protected int $cacheTtl = 60 * 60 * 24 * 30; // 30 days /** * @return Location[] diff --git a/src/Endpoint/Util/WithCacheTtlTrait.php b/src/Endpoint/Util/WithCacheTtlTrait.php index 7695f77..5919b01 100644 --- a/src/Endpoint/Util/WithCacheTtlTrait.php +++ b/src/Endpoint/Util/WithCacheTtlTrait.php @@ -4,15 +4,15 @@ trait WithCacheTtlTrait { - public function withCacheTtl(?int $time): static + public function withCacheTtl(int $seconds): static { $clone = clone $this; - $clone->cacheTtl = $time; + $clone->cacheTtl = $seconds; return $clone; } - public function getCacheTtl(): ?int + public function getCacheTtl(): int { return $this->cacheTtl; } From 2a04001fa0fbdd88bb4bfc29936fc173d577e183 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Pimpa=CC=83o?= Date: Wed, 23 Aug 2023 18:10:12 +0100 Subject: [PATCH 3/8] fix: documentation wrong object return value --- docs/05-objects.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/05-objects.md b/docs/05-objects.md index 05f96a2..ce8faa0 100644 --- a/docs/05-objects.md +++ b/docs/05-objects.md @@ -299,7 +299,7 @@ ### AirPollutionLocationList -`getCoordinate()`: `float` +`getCoordinate()`: [`Coordinate`](#coordinate) `getList()`: [`AirPollution[]`](#airpollution) From 71320be83374fe38446cbafae2b27e9e68d2baa7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Pimpa=CC=83o?= Date: Wed, 23 Aug 2023 19:22:50 +0100 Subject: [PATCH 4/8] chore: test improvements --- src/Test/AbstractTest.php | 2 +- tests/AirPollutionEndpointTest.php | 30 +++++++++++++++--------------- tests/ApiErrorTest.php | 2 +- tests/ConfigTest.php | 10 ++++------ tests/GeocodingEndpointTest.php | 14 +++++++------- tests/OneCallEndpointTest.php | 26 +++++++++++++------------- tests/OpenWeatherMapTest.php | 10 +++++----- tests/WeatherEndpointTest.php | 12 ++++++------ tests/WithCacheTtl.php | 4 ++-- tests/WithLanguageTest.php | 6 +++--- tests/WithUnitSystemTest.php | 6 +++--- 11 files changed, 60 insertions(+), 62 deletions(-) diff --git a/src/Test/AbstractTest.php b/src/Test/AbstractTest.php index 4a8c75d..f778fc7 100644 --- a/src/Test/AbstractTest.php +++ b/src/Test/AbstractTest.php @@ -21,7 +21,7 @@ protected function setUp(): void $this->mockHttpClient = new Client(); } - protected function getApi(): OpenWeatherMap + protected function givenApi(): OpenWeatherMap { return new OpenWeatherMap( new Config([ diff --git a/tests/AirPollutionEndpointTest.php b/tests/AirPollutionEndpointTest.php index 52e5efa..8b1f13d 100644 --- a/tests/AirPollutionEndpointTest.php +++ b/tests/AirPollutionEndpointTest.php @@ -23,7 +23,7 @@ public function testAirPollutionGetCurrent() ) ); - $response = $this->getApi()->getAirPollution()->getCurrent(38.7077507, -9.1365919); + $response = $this->givenApi()->getAirPollution()->getCurrent(50, 50); $this->assertCurrentResponse($response); } @@ -31,7 +31,7 @@ public function testAirPollutionGetCurrent() public function testAirPollutionGetCurrentWithInvalidCoordinate(float $latitude, float $longitude, string $expectedException) { $this->expectException($expectedException); - $this->getApi()->getAirPollution()->getCurrent($latitude, $longitude); + $this->givenApi()->getAirPollution()->getCurrent($latitude, $longitude); } public function testAirPollutionGetForecast() @@ -43,7 +43,7 @@ public function testAirPollutionGetForecast() ) ); - $response = $this->getApi()->getAirPollution()->getForecast(38.7077507, -9.1365919); + $response = $this->givenApi()->getAirPollution()->getForecast(50, 50); $this->assertForecastResponse($response); } @@ -51,7 +51,7 @@ public function testAirPollutionGetForecast() public function testAirPollutionGetForecastWithInvalidCoordinate(float $latitude, float $longitude, string $expectedException) { $this->expectException($expectedException); - $this->getApi()->getAirPollution()->getForecast($latitude, $longitude); + $this->givenApi()->getAirPollution()->getForecast($latitude, $longitude); } public function testAirPollutionGetHistory() @@ -65,9 +65,9 @@ public function testAirPollutionGetHistory() $utcTimezone = new \DateTimeZone('UTC'); - $response = $this->getApi()->getAirPollution()->getHistory( - 38.7077507, - -9.1365919, + $response = $this->givenApi()->getAirPollution()->getHistory( + 50, + 50, new \DateTimeImmutable('-5 days', $utcTimezone), new \DateTimeImmutable('-4 days', $utcTimezone) ); @@ -82,7 +82,7 @@ public function testAirPollutionGetHistoryWithInvalidCoordinate(float $latitude, $startDate = new \DateTimeImmutable('-5 days'); $endDate = new \DateTimeImmutable('-4 days'); - $this->getApi()->getAirPollution()->getHistory($latitude, $longitude, $startDate, $endDate); + $this->givenApi()->getAirPollution()->getHistory($latitude, $longitude, $startDate, $endDate); } #[DataProviderExternal(InvalidParamDataProvider::class, 'provideInvalidPastDateData')] @@ -92,9 +92,9 @@ public function testAirPollutionGetHistoryWithInvalidPastStartDate( ) { $this->expectException($expectedException); - $this->getApi()->getAirPollution()->getHistory( - 38.7077507, - -9.1365919, + $this->givenApi()->getAirPollution()->getHistory( + 50, + 50, $startDate, new \DateTimeImmutable('-5 days', new \DateTimeZone('UTC')) ); @@ -107,9 +107,9 @@ public function testAirPollutionGetHistoryWithInvalidPastEndDate( ) { $this->expectException($expectedException); - $this->getApi()->getAirPollution()->getHistory( - 38.7077507, - -9.1365919, + $this->givenApi()->getAirPollution()->getHistory( + 50, + 50, new \DateTimeImmutable('-5 days', new \DateTimeZone('UTC')), $endDate ); @@ -123,7 +123,7 @@ public function testAirPollutionGetHistoryWithInvalidDateRange( ) { $this->expectException($expectedException); - $this->getApi()->getAirPollution()->getHistory(38.7077507, -9.1365919, $startDate, $endDate); + $this->givenApi()->getAirPollution()->getHistory(50, 50, $startDate, $endDate); } private function assertCurrentResponse(AirPollutionLocation $response): void diff --git a/tests/ApiErrorTest.php b/tests/ApiErrorTest.php index 82ce2c2..950ef98 100644 --- a/tests/ApiErrorTest.php +++ b/tests/ApiErrorTest.php @@ -24,7 +24,7 @@ public function testApiError(int $statusCode, string $expectedException) ); $this->expectException($expectedException); - $this->getApi()->getWeather()->getCurrent(38.7077507, -9.1365919); + $this->givenApi()->getWeather()->getCurrent(38.7077507, -9.1365919); } public static function provideApiErrorData(): \Generator diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index 4763ca6..b8a95e1 100644 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php @@ -2,7 +2,6 @@ namespace ProgrammatorDev\OpenWeatherMap\Test; -use Monolog\Logger; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\DataProviderExternal; use ProgrammatorDev\OpenWeatherMap\Config; @@ -11,7 +10,6 @@ use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException; use Psr\Cache\CacheItemPoolInterface; use Psr\Log\LoggerInterface; -use Symfony\Component\Cache\Adapter\FilesystemAdapter; use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException; use Symfony\Component\OptionsResolver\Exception\MissingOptionsException; @@ -45,8 +43,8 @@ public function testConfigWithOptions() 'unitSystem' => 'imperial', 'language' => 'pt', 'httpClientBuilder' => new HttpClientBuilder(), - 'cache' => new FilesystemAdapter(), - 'logger' => new Logger('test') + 'cache' => $this->createMock(CacheItemPoolInterface::class), + 'logger' => $this->createMock(LoggerInterface::class) ]); $this->assertSame('newtestappkey', $config->getApplicationKey()); @@ -139,13 +137,13 @@ public function testConfigSetHttpClientBuilder() public function testConfigSetCache() { - $this->config->setCache(new FilesystemAdapter()); + $this->config->setCache($this->createMock(CacheItemPoolInterface::class)); $this->assertInstanceOf(CacheItemPoolInterface::class, $this->config->getCache()); } public function testConfigSetLogger() { - $this->config->setLogger(new Logger('test')); + $this->config->setLogger($this->createMock(LoggerInterface::class)); $this->assertInstanceOf(LoggerInterface::class, $this->config->getLogger()); } } \ No newline at end of file diff --git a/tests/GeocodingEndpointTest.php b/tests/GeocodingEndpointTest.php index 68d564e..eee959d 100644 --- a/tests/GeocodingEndpointTest.php +++ b/tests/GeocodingEndpointTest.php @@ -22,14 +22,14 @@ public function testGeocodingGetByLocationName() ) ); - $response = $this->getApi()->getGeocoding()->getByLocationName('lisbon, pt'); + $response = $this->givenApi()->getGeocoding()->getByLocationName('lisbon, pt'); $this->assertLocationListResponse($response); } public function testGeocodingGetByLocationNameWithBlankValue() { $this->expectException(ValidationException::class); - $this->getApi()->getGeocoding()->getByLocationName(''); + $this->givenApi()->getGeocoding()->getByLocationName(''); } public function testGeocodingGetByZipCode() @@ -41,7 +41,7 @@ public function testGeocodingGetByZipCode() ) ); - $response = $this->getApi()->getGeocoding()->getByZipCode('1000-001', 'pt'); + $response = $this->givenApi()->getGeocoding()->getByZipCode('1000-001', 'pt'); $this->assertInstanceOf(ZipCodeLocation::class, $response); $this->assertSame('1000-001', $response->getZipCode()); @@ -58,7 +58,7 @@ public function testGeocodingGetByZipCode() public function testGeocodingGetByZipCodeWithInvalidValue(string $zipCode, string $countryCode) { $this->expectException(ValidationException::class); - $this->getApi()->getGeocoding()->getByZipCode($zipCode, $countryCode); + $this->givenApi()->getGeocoding()->getByZipCode($zipCode, $countryCode); } public static function provideGeocodingGetByZipCodeWithInvalidValueData(): \Generator @@ -77,7 +77,7 @@ public function testGeocodingGetByCoordinate() ) ); - $response = $this->getApi()->getGeocoding()->getByCoordinate(38.7077507, -9.1365919); + $response = $this->givenApi()->getGeocoding()->getByCoordinate(50, 50); $this->assertLocationListResponse($response); } @@ -89,7 +89,7 @@ public function testGeocodingGetByCoordinateWithInvalidCoordinate( ) { $this->expectException($expectedException); - $this->getApi()->getGeocoding()->getByCoordinate($latitude, $longitude); + $this->givenApi()->getGeocoding()->getByCoordinate($latitude, $longitude); } #[DataProviderExternal(InvalidParamDataProvider::class, 'provideInvalidNumResultsData')] @@ -99,7 +99,7 @@ public function testGeocodingGetByCoordinateWithInvalidNumResults( ) { $this->expectException($expectedException); - $this->getApi()->getGeocoding()->getByCoordinate(38.7077507, -9.1365919, $numResults); + $this->givenApi()->getGeocoding()->getByCoordinate(50, 50, $numResults); } /** diff --git a/tests/OneCallEndpointTest.php b/tests/OneCallEndpointTest.php index 229163e..742c79c 100644 --- a/tests/OneCallEndpointTest.php +++ b/tests/OneCallEndpointTest.php @@ -30,7 +30,7 @@ public function testOneCallGetWeather() ) ); - $response = $this->getApi()->getOneCall()->getWeather(38.7077507, -9.1365919); + $response = $this->givenApi()->getOneCall()->getWeather(50, 50); $this->assertWeatherResponse($response); } @@ -38,7 +38,7 @@ public function testOneCallGetWeather() public function testOneCallGetWeatherWithInvalidCoordinate(float $latitude, float $longitude, string $expectedException) { $this->expectException($expectedException); - $this->getApi()->getOneCall()->getWeather($latitude, $longitude); + $this->givenApi()->getOneCall()->getWeather($latitude, $longitude); } public function testOneCallGetHistoryMoment() @@ -50,9 +50,9 @@ public function testOneCallGetHistoryMoment() ) ); - $response = $this->getApi()->getOneCall()->getHistoryMoment( - 38.7077507, - -9.1365919, + $response = $this->givenApi()->getOneCall()->getHistoryMoment( + 50, + 50, new \DateTimeImmutable('2023-01-01 00:00:00') ); $this->assertHistoryMomentResponse($response); @@ -62,7 +62,7 @@ public function testOneCallGetHistoryMoment() public function testOneCallGetHistoryMomentWithInvalidCoordinate(float $latitude, float $longitude, string $expectedException) { $this->expectException($expectedException); - $this->getApi()->getOneCall()->getHistoryMoment( + $this->givenApi()->getOneCall()->getHistoryMoment( $latitude, $longitude, new \DateTimeImmutable('2023-01-01 00:00:00') @@ -73,7 +73,7 @@ public function testOneCallGetHistoryMomentWithInvalidCoordinate(float $latitude public function testOneCallGetHistoryMomentWithInvalidPastDate(\DateTimeImmutable $date, string $expectedException) { $this->expectException($expectedException); - $this->getApi()->getOneCall()->getHistoryMoment(38.7077507, -9.1365919, $date); + $this->givenApi()->getOneCall()->getHistoryMoment(50, 50, $date); } public function testOneCallGetHistoryAggregate() @@ -85,9 +85,9 @@ public function testOneCallGetHistoryAggregate() ) ); - $response = $this->getApi()->getOneCall()->getHistoryAggregate( - 38.7077507, - -9.1365919, + $response = $this->givenApi()->getOneCall()->getHistoryAggregate( + 50, + 50, new \DateTimeImmutable('2023-01-01') ); $this->assertHistoryAggregateResponse($response); @@ -97,7 +97,7 @@ public function testOneCallGetHistoryAggregate() public function testOneCallGetHistoryAggregateWithInvalidCoordinate(float $latitude, float $longitude, string $expectedException) { $this->expectException($expectedException); - $this->getApi()->getOneCall()->getHistoryAggregate( + $this->givenApi()->getOneCall()->getHistoryAggregate( $latitude, $longitude, new \DateTimeImmutable('2023-01-01') @@ -108,12 +108,12 @@ public function testOneCallGetHistoryAggregateWithInvalidCoordinate(float $latit public function testOneCallGetHistoryAggregateWithInvalidPastDate(\DateTimeImmutable $date, string $expectedException) { $this->expectException($expectedException); - $this->getApi()->getOneCall()->getHistoryAggregate(38.7077507, -9.1365919, $date); + $this->givenApi()->getOneCall()->getHistoryAggregate(50, 50, $date); } public function testOneCallMethodsWithExist() { - $weatherEndpoint = $this->getApi()->getWeather(); + $weatherEndpoint = $this->givenApi()->getWeather(); $this->assertSame(true, method_exists($weatherEndpoint, 'withLanguage')); $this->assertSame(true, method_exists($weatherEndpoint, 'withUnitSystem')); diff --git a/tests/OpenWeatherMapTest.php b/tests/OpenWeatherMapTest.php index 667d637..288fe59 100644 --- a/tests/OpenWeatherMapTest.php +++ b/tests/OpenWeatherMapTest.php @@ -12,26 +12,26 @@ class OpenWeatherMapTest extends AbstractTest { public function testOpenWeatherMapGetConfig() { - $this->assertInstanceOf(Config::class, $this->getApi()->getConfig()); + $this->assertInstanceOf(Config::class, $this->givenApi()->getConfig()); } public function testOpenWeatherMapGetOneCall() { - $this->assertInstanceOf(OneCallEndpoint::class, $this->getApi()->getOneCall()); + $this->assertInstanceOf(OneCallEndpoint::class, $this->givenApi()->getOneCall()); } public function testOpenWeatherMapGetWeather() { - $this->assertInstanceOf(WeatherEndpoint::class, $this->getApi()->getWeather()); + $this->assertInstanceOf(WeatherEndpoint::class, $this->givenApi()->getWeather()); } public function testOpenWeatherMapGetAirPollution() { - $this->assertInstanceOf(AirPollutionEndpoint::class, $this->getApi()->getAirPollution()); + $this->assertInstanceOf(AirPollutionEndpoint::class, $this->givenApi()->getAirPollution()); } public function testOpenWeatherMapGetGeocoding() { - $this->assertInstanceOf(GeocodingEndpoint::class, $this->getApi()->getGeocoding()); + $this->assertInstanceOf(GeocodingEndpoint::class, $this->givenApi()->getGeocoding()); } } \ No newline at end of file diff --git a/tests/WeatherEndpointTest.php b/tests/WeatherEndpointTest.php index 4eb7c23..f0439a8 100644 --- a/tests/WeatherEndpointTest.php +++ b/tests/WeatherEndpointTest.php @@ -29,7 +29,7 @@ public function testWeatherGetCurrent() ) ); - $response = $this->getApi()->getWeather()->getCurrent(38.7077507, -9.1365919); + $response = $this->givenApi()->getWeather()->getCurrent(50, 50); $this->assertCurrentResponse($response); } @@ -37,7 +37,7 @@ public function testWeatherGetCurrent() public function testWeatherGetCurrentWithInvalidCoordinate(float $latitude, float $longitude, string $expectedException) { $this->expectException($expectedException); - $this->getApi()->getWeather()->getCurrent($latitude, $longitude); + $this->givenApi()->getWeather()->getCurrent($latitude, $longitude); } public function testWeatherGetForecast() @@ -49,7 +49,7 @@ public function testWeatherGetForecast() ) ); - $response = $this->getApi()->getWeather()->getForecast(38.7077507, -9.1365919, 1); + $response = $this->givenApi()->getWeather()->getForecast(50, 50, 1); $this->assertForecastResponse($response); } @@ -57,19 +57,19 @@ public function testWeatherGetForecast() public function testWeatherGetForecastWithInvalidCoordinate(float $latitude, float $longitude, string $expectedException) { $this->expectException($expectedException); - $this->getApi()->getWeather()->getForecast($latitude, $longitude, 10); + $this->givenApi()->getWeather()->getForecast($latitude, $longitude, 10); } #[DataProviderExternal(InvalidParamDataProvider::class, 'provideInvalidNumResultsData')] public function testWeatherGetForecastWithInvalidNumResults(int $numResults, string $expectedException) { $this->expectException($expectedException); - $this->getApi()->getWeather()->getForecast(38.7077507, -9.1365919, $numResults); + $this->givenApi()->getWeather()->getForecast(50, 50, $numResults); } public function testWeatherMethodsWithExist() { - $weatherEndpoint = $this->getApi()->getWeather(); + $weatherEndpoint = $this->givenApi()->getWeather(); $this->assertSame(true, method_exists($weatherEndpoint, 'withLanguage')); $this->assertSame(true, method_exists($weatherEndpoint, 'withUnitSystem')); diff --git a/tests/WithCacheTtl.php b/tests/WithCacheTtl.php index efdbbda..24b2bcd 100644 --- a/tests/WithCacheTtl.php +++ b/tests/WithCacheTtl.php @@ -8,7 +8,7 @@ public function testWithCacheTtl() { $this->assertSame( 60 * 60, - $this->getApi()->getWeather() + $this->givenApi()->getWeather() ->withCacheTtl(60 * 60) ->getCacheTtl() ); @@ -16,6 +16,6 @@ public function testWithCacheTtl() public function testWithCacheTtlGetCacheTtl() { - $this->assertSame(60 * 10, $this->getApi()->getWeather()->getCacheTtl()); + $this->assertSame(60 * 10, $this->givenApi()->getWeather()->getCacheTtl()); } } \ No newline at end of file diff --git a/tests/WithLanguageTest.php b/tests/WithLanguageTest.php index 369c144..dedcdac 100644 --- a/tests/WithLanguageTest.php +++ b/tests/WithLanguageTest.php @@ -11,7 +11,7 @@ public function testWithLanguage() { $this->assertSame( 'pt', - $this->getApi()->getWeather() + $this->givenApi()->getWeather() ->withLanguage('pt') ->getLanguage() ); @@ -21,11 +21,11 @@ public function testWithLanguage() public function testWithLanguageWithInvalidValue(string $language, string $expectedException) { $this->expectException($expectedException); - $this->getApi()->getWeather()->withLanguage($language); + $this->givenApi()->getWeather()->withLanguage($language); } public function testWithLanguageGetLanguage() { - $this->assertSame('en', $this->getApi()->getWeather()->getLanguage()); + $this->assertSame('en', $this->givenApi()->getWeather()->getLanguage()); } } \ No newline at end of file diff --git a/tests/WithUnitSystemTest.php b/tests/WithUnitSystemTest.php index 239123d..cbe87aa 100644 --- a/tests/WithUnitSystemTest.php +++ b/tests/WithUnitSystemTest.php @@ -11,7 +11,7 @@ public function testWithUnitSystem() { $this->assertSame( 'imperial', - $this->getApi()->getWeather() + $this->givenApi()->getWeather() ->withUnitSystem('imperial') ->getUnitSystem() ); @@ -21,11 +21,11 @@ public function testWithUnitSystem() public function testWithUnitSystemWithInvalidValue(string $unitSystem, string $expectedException) { $this->expectException($expectedException); - $this->getApi()->getWeather()->withUnitSystem($unitSystem); + $this->givenApi()->getWeather()->withUnitSystem($unitSystem); } public function testWithUnitSystemGetUnitSystem() { - $this->assertSame('metric', $this->getApi()->getWeather()->getUnitSystem()); + $this->assertSame('metric', $this->givenApi()->getWeather()->getUnitSystem()); } } \ No newline at end of file From ccd0a7348a9ef037dae5525cb9dc0279d9232e45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Pimpa=CC=83o?= Date: Thu, 24 Aug 2023 12:11:56 +0100 Subject: [PATCH 5/8] chore: added tests to assert that with methods exist --- tests/AirPollutionEndpointTest.php | 19 ++++++++++++++++++- tests/GeocodingEndpointTest.php | 17 +++++++++++++++++ tests/OneCallEndpointTest.php | 20 +++++++++++++++----- tests/WeatherEndpointTest.php | 18 +++++++++++++----- 4 files changed, 63 insertions(+), 11 deletions(-) diff --git a/tests/AirPollutionEndpointTest.php b/tests/AirPollutionEndpointTest.php index 8b1f13d..aeaf245 100644 --- a/tests/AirPollutionEndpointTest.php +++ b/tests/AirPollutionEndpointTest.php @@ -4,16 +4,18 @@ use Nyholm\Psr7\Response; use PHPUnit\Framework\Attributes\DataProviderExternal; +use ProgrammatorDev\OpenWeatherMap\Endpoint\AirPollutionEndpoint; use ProgrammatorDev\OpenWeatherMap\Entity\AirPollution\AirPollution; use ProgrammatorDev\OpenWeatherMap\Entity\AirPollution\AirPollutionLocationList; use ProgrammatorDev\OpenWeatherMap\Entity\AirPollution\AirQuality; use ProgrammatorDev\OpenWeatherMap\Entity\AirPollution\AirPollutionLocation; use ProgrammatorDev\OpenWeatherMap\Entity\Coordinate; -use ProgrammatorDev\OpenWeatherMap\Entity\Location; use ProgrammatorDev\OpenWeatherMap\Test\DataProvider\InvalidParamDataProvider; class AirPollutionEndpointTest extends AbstractTest { + // --- CURRENT --- + public function testAirPollutionGetCurrent() { $this->mockHttpClient->addResponse( @@ -34,6 +36,8 @@ public function testAirPollutionGetCurrentWithInvalidCoordinate(float $latitude, $this->givenApi()->getAirPollution()->getCurrent($latitude, $longitude); } + // --- FORECAST --- + public function testAirPollutionGetForecast() { $this->mockHttpClient->addResponse( @@ -54,6 +58,8 @@ public function testAirPollutionGetForecastWithInvalidCoordinate(float $latitude $this->givenApi()->getAirPollution()->getForecast($latitude, $longitude); } + // --- HISTORY --- + public function testAirPollutionGetHistory() { $this->mockHttpClient->addResponse( @@ -126,6 +132,17 @@ public function testAirPollutionGetHistoryWithInvalidDateRange( $this->givenApi()->getAirPollution()->getHistory(50, 50, $startDate, $endDate); } + // --- ASSERT METHODS EXIST --- + + public function testAirPollutionMethodsExist() + { + $this->assertSame(false, method_exists(AirPollutionEndpoint::class, 'withUnitSystem')); + $this->assertSame(false, method_exists(AirPollutionEndpoint::class, 'withLanguage')); + $this->assertSame(true, method_exists(AirPollutionEndpoint::class, 'withCacheTtl')); + } + + // --- ASSERT RESPONSES --- + private function assertCurrentResponse(AirPollutionLocation $response): void { $this->assertInstanceOf(AirPollutionLocation::class, $response); diff --git a/tests/GeocodingEndpointTest.php b/tests/GeocodingEndpointTest.php index eee959d..4e8fb8e 100644 --- a/tests/GeocodingEndpointTest.php +++ b/tests/GeocodingEndpointTest.php @@ -5,6 +5,7 @@ use Nyholm\Psr7\Response; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\DataProviderExternal; +use ProgrammatorDev\OpenWeatherMap\Endpoint\GeocodingEndpoint; use ProgrammatorDev\OpenWeatherMap\Entity\Coordinate; use ProgrammatorDev\OpenWeatherMap\Entity\Geocoding\ZipCodeLocation; use ProgrammatorDev\OpenWeatherMap\Entity\Location; @@ -13,6 +14,7 @@ class GeocodingEndpointTest extends AbstractTest { + // --- BY LOCATION NAME --- public function testGeocodingGetByLocationName() { $this->mockHttpClient->addResponse( @@ -32,6 +34,8 @@ public function testGeocodingGetByLocationNameWithBlankValue() $this->givenApi()->getGeocoding()->getByLocationName(''); } + // --- BY ZIP CODE --- + public function testGeocodingGetByZipCode() { $this->mockHttpClient->addResponse( @@ -68,6 +72,8 @@ public static function provideGeocodingGetByZipCodeWithInvalidValueData(): \Gene yield 'invalid country code' => ['1000-100', 'invalid']; } + // --- BY COORDINATE --- + public function testGeocodingGetByCoordinate() { $this->mockHttpClient->addResponse( @@ -102,6 +108,17 @@ public function testGeocodingGetByCoordinateWithInvalidNumResults( $this->givenApi()->getGeocoding()->getByCoordinate(50, 50, $numResults); } + // --- ASSERT METHODS EXIST --- + + public function testGeocodingMethodsExist() + { + $this->assertSame(false, method_exists(GeocodingEndpoint::class, 'withUnitSystem')); + $this->assertSame(false, method_exists(GeocodingEndpoint::class, 'withLanguage')); + $this->assertSame(true, method_exists(GeocodingEndpoint::class, 'withCacheTtl')); + } + + // --- ASSERT RESPONSES --- + /** * @param Location[] $response */ diff --git a/tests/OneCallEndpointTest.php b/tests/OneCallEndpointTest.php index 742c79c..e499b91 100644 --- a/tests/OneCallEndpointTest.php +++ b/tests/OneCallEndpointTest.php @@ -4,6 +4,7 @@ use Nyholm\Psr7\Response; use PHPUnit\Framework\Attributes\DataProviderExternal; +use ProgrammatorDev\OpenWeatherMap\Endpoint\OneCallEndpoint; use ProgrammatorDev\OpenWeatherMap\Entity\Coordinate; use ProgrammatorDev\OpenWeatherMap\Entity\Icon; use ProgrammatorDev\OpenWeatherMap\Entity\MoonPhase; @@ -21,6 +22,8 @@ class OneCallEndpointTest extends AbstractTest { + // --- WEATHER --- + public function testOneCallGetWeather() { $this->mockHttpClient->addResponse( @@ -41,6 +44,8 @@ public function testOneCallGetWeatherWithInvalidCoordinate(float $latitude, floa $this->givenApi()->getOneCall()->getWeather($latitude, $longitude); } + // --- HISTORY MOMENT --- + public function testOneCallGetHistoryMoment() { $this->mockHttpClient->addResponse( @@ -76,6 +81,8 @@ public function testOneCallGetHistoryMomentWithInvalidPastDate(\DateTimeImmutabl $this->givenApi()->getOneCall()->getHistoryMoment(50, 50, $date); } + // --- HISTORY AGGREGATE --- + public function testOneCallGetHistoryAggregate() { $this->mockHttpClient->addResponse( @@ -111,14 +118,17 @@ public function testOneCallGetHistoryAggregateWithInvalidPastDate(\DateTimeImmut $this->givenApi()->getOneCall()->getHistoryAggregate(50, 50, $date); } - public function testOneCallMethodsWithExist() - { - $weatherEndpoint = $this->givenApi()->getWeather(); + // --- ASSERT METHODS EXIST --- - $this->assertSame(true, method_exists($weatherEndpoint, 'withLanguage')); - $this->assertSame(true, method_exists($weatherEndpoint, 'withUnitSystem')); + public function testOneCallMethodsExist() + { + $this->assertSame(true, method_exists(OneCallEndpoint::class, 'withUnitSystem')); + $this->assertSame(true, method_exists(OneCallEndpoint::class, 'withLanguage')); + $this->assertSame(true, method_exists(OneCallEndpoint::class, 'withCacheTtl')); } + // --- ASSERT RESPONSES --- + private function assertWeatherResponse(OneCall $response): void { $this->assertInstanceOf(OneCall::class, $response); diff --git a/tests/WeatherEndpointTest.php b/tests/WeatherEndpointTest.php index f0439a8..ba725f7 100644 --- a/tests/WeatherEndpointTest.php +++ b/tests/WeatherEndpointTest.php @@ -4,6 +4,7 @@ use Nyholm\Psr7\Response; use PHPUnit\Framework\Attributes\DataProviderExternal; +use ProgrammatorDev\OpenWeatherMap\Endpoint\WeatherEndpoint; use ProgrammatorDev\OpenWeatherMap\Entity\AtmosphericPressure; use ProgrammatorDev\OpenWeatherMap\Entity\Coordinate; use ProgrammatorDev\OpenWeatherMap\Entity\Icon; @@ -20,6 +21,8 @@ class WeatherEndpointTest extends AbstractTest { + // --- CURRENT --- + public function testWeatherGetCurrent() { $this->mockHttpClient->addResponse( @@ -40,6 +43,8 @@ public function testWeatherGetCurrentWithInvalidCoordinate(float $latitude, floa $this->givenApi()->getWeather()->getCurrent($latitude, $longitude); } + // --- FORECAST --- + public function testWeatherGetForecast() { $this->mockHttpClient->addResponse( @@ -67,14 +72,17 @@ public function testWeatherGetForecastWithInvalidNumResults(int $numResults, str $this->givenApi()->getWeather()->getForecast(50, 50, $numResults); } - public function testWeatherMethodsWithExist() - { - $weatherEndpoint = $this->givenApi()->getWeather(); + // --- ASSERT METHODS EXIST --- - $this->assertSame(true, method_exists($weatherEndpoint, 'withLanguage')); - $this->assertSame(true, method_exists($weatherEndpoint, 'withUnitSystem')); + public function testWeatherMethodsExist() + { + $this->assertSame(true, method_exists(WeatherEndpoint::class, 'withUnitSystem')); + $this->assertSame(true, method_exists(WeatherEndpoint::class, 'withLanguage')); + $this->assertSame(true, method_exists(WeatherEndpoint::class, 'withCacheTtl')); } + // --- ASSERT RESPONSES --- + private function assertCurrentResponse(WeatherLocation $response): void { $this->assertInstanceOf(WeatherLocation::class, $response); From edc19713b2835d7819302103ff79e2ae73208c1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Pimpa=CC=83o?= Date: Thu, 24 Aug 2023 12:20:50 +0100 Subject: [PATCH 6/8] chore: some abstract endpoint refactoring --- src/Endpoint/AbstractEndpoint.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Endpoint/AbstractEndpoint.php b/src/Endpoint/AbstractEndpoint.php index ef48054..f650143 100644 --- a/src/Endpoint/AbstractEndpoint.php +++ b/src/Endpoint/AbstractEndpoint.php @@ -70,8 +70,12 @@ protected function sendRequest( { $this->configurePlugins(); - $uri = $this->buildUrl($baseUrl, $query); - $response = $this->httpClientBuilder->getHttpClient()->send($method, $uri, $headers, $body); + $response = $this->httpClientBuilder->getHttpClient()->send( + $method, + $this->buildUrl($baseUrl, $query), + $headers, + $body + ); if (($statusCode = $response->getStatusCode()) >= 400) { $this->handleResponseErrors($response, $statusCode); @@ -84,14 +88,13 @@ private function configurePlugins(): void { // Plugin order is important // CachePlugin should come first, otherwise the LoggerPlugin will log requests even if they are cached + if ($this->cache !== null) { $this->httpClientBuilder->addPlugin( new CachePlugin($this->cache, $this->httpClientBuilder->getStreamFactory(), [ 'default_ttl' => $this->cacheTtl, 'cache_lifetime' => 0, - 'cache_listeners' => ($this->logger !== null) - ? [new LoggerCacheListener($this->logger)] - : [] + 'cache_listeners' => ($this->logger !== null) ? [new LoggerCacheListener($this->logger)] : [] ]) ); } From 6510fc8a2b5186fd3626b2be474f5c2f6c26d4fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Pimpa=CC=83o?= Date: Thu, 24 Aug 2023 16:30:50 +0100 Subject: [PATCH 7/8] feat: added tests for cache and logger usage assertion --- tests/AbstractEndpointTest.php | 52 +++++++++++++++++++++++++++++++++ tests/GeocodingEndpointTest.php | 1 + 2 files changed, 53 insertions(+) create mode 100644 tests/AbstractEndpointTest.php diff --git a/tests/AbstractEndpointTest.php b/tests/AbstractEndpointTest.php new file mode 100644 index 0000000..41a9e13 --- /dev/null +++ b/tests/AbstractEndpointTest.php @@ -0,0 +1,52 @@ +mockHttpClient->addResponse( + new Response(body: '[]') + ); + + $cache = $this->createMock(CacheItemPoolInterface::class); + $cache->expects($this->once())->method('save'); + + $api = $this->givenApi(); + $api->getConfig()->setCache($cache); + + $this->mockSendRequest($api); + } + + public function testAbstractEndpointWithLogger() + { + $this->mockHttpClient->addResponse( + new Response(body: '[]') + ); + + $logger = $this->createMock(LoggerInterface::class); + $logger->expects($this->atLeastOnce())->method('info'); + + $api = $this->givenApi(); + $api->getConfig()->setLogger($logger); + + $this->mockSendRequest($api); + } + + private function mockSendRequest(OpenWeatherMap $api): void + { + // Using ReflectionClass to be able to call the *protected* sendRequest method + // (otherwise it would not be possible) + $endpoint = new AbstractEndpoint($api); + $reflectionClass = new \ReflectionClass($endpoint); + $sendRequest = $reflectionClass->getMethod('sendRequest'); + $sendRequest->invokeArgs($endpoint, ['GET', 'https://mock.test']); + } +} \ No newline at end of file diff --git a/tests/GeocodingEndpointTest.php b/tests/GeocodingEndpointTest.php index 4e8fb8e..d060f22 100644 --- a/tests/GeocodingEndpointTest.php +++ b/tests/GeocodingEndpointTest.php @@ -15,6 +15,7 @@ class GeocodingEndpointTest extends AbstractTest { // --- BY LOCATION NAME --- + public function testGeocodingGetByLocationName() { $this->mockHttpClient->addResponse( From 545aff4f5a9a9ef662e86c60f8f3f745b358db94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Pimpa=CC=83o?= Date: Thu, 24 Aug 2023 17:08:17 +0100 Subject: [PATCH 8/8] chore: improved withCacheTtl docs --- docs/03-supported-apis.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/03-supported-apis.md b/docs/03-supported-apis.md index 0f62849..de96ebe 100644 --- a/docs/03-supported-apis.md +++ b/docs/03-supported-apis.md @@ -281,7 +281,7 @@ $openWeatherMap->getWeather() withCacheTtl(int $seconds): self ``` -Makes a request and saves into cache with the provided value in seconds. +Makes a request and saves into cache for the provided duration in seconds. If `0` seconds is provided, the request will not be cached.