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": [ { diff --git a/docs/03-supported-apis.md b/docs/03-supported-apis.md index 580f29a..de96ebe 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 for the provided duration 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/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) diff --git a/src/Endpoint/AbstractEndpoint.php b/src/Endpoint/AbstractEndpoint.php index ca570dd..f650143 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) { @@ -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)] : [] ]) ); } 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; } 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/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/AirPollutionEndpointTest.php b/tests/AirPollutionEndpointTest.php index 52e5efa..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( @@ -23,7 +25,7 @@ public function testAirPollutionGetCurrent() ) ); - $response = $this->getApi()->getAirPollution()->getCurrent(38.7077507, -9.1365919); + $response = $this->givenApi()->getAirPollution()->getCurrent(50, 50); $this->assertCurrentResponse($response); } @@ -31,9 +33,11 @@ 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); } + // --- FORECAST --- + public function testAirPollutionGetForecast() { $this->mockHttpClient->addResponse( @@ -43,7 +47,7 @@ public function testAirPollutionGetForecast() ) ); - $response = $this->getApi()->getAirPollution()->getForecast(38.7077507, -9.1365919); + $response = $this->givenApi()->getAirPollution()->getForecast(50, 50); $this->assertForecastResponse($response); } @@ -51,9 +55,11 @@ 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); } + // --- HISTORY --- + public function testAirPollutionGetHistory() { $this->mockHttpClient->addResponse( @@ -65,9 +71,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 +88,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 +98,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 +113,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,9 +129,20 @@ public function testAirPollutionGetHistoryWithInvalidDateRange( ) { $this->expectException($expectedException); - $this->getApi()->getAirPollution()->getHistory(38.7077507, -9.1365919, $startDate, $endDate); + $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/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..d060f22 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,8 @@ class GeocodingEndpointTest extends AbstractTest { + // --- BY LOCATION NAME --- + public function testGeocodingGetByLocationName() { $this->mockHttpClient->addResponse( @@ -22,16 +25,18 @@ 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(''); } + // --- BY ZIP CODE --- + public function testGeocodingGetByZipCode() { $this->mockHttpClient->addResponse( @@ -41,7 +46,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 +63,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 @@ -68,6 +73,8 @@ public static function provideGeocodingGetByZipCodeWithInvalidValueData(): \Gene yield 'invalid country code' => ['1000-100', 'invalid']; } + // --- BY COORDINATE --- + public function testGeocodingGetByCoordinate() { $this->mockHttpClient->addResponse( @@ -77,7 +84,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 +96,7 @@ public function testGeocodingGetByCoordinateWithInvalidCoordinate( ) { $this->expectException($expectedException); - $this->getApi()->getGeocoding()->getByCoordinate($latitude, $longitude); + $this->givenApi()->getGeocoding()->getByCoordinate($latitude, $longitude); } #[DataProviderExternal(InvalidParamDataProvider::class, 'provideInvalidNumResultsData')] @@ -99,9 +106,20 @@ public function testGeocodingGetByCoordinateWithInvalidNumResults( ) { $this->expectException($expectedException); - $this->getApi()->getGeocoding()->getByCoordinate(38.7077507, -9.1365919, $numResults); + $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 229163e..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( @@ -30,7 +33,7 @@ public function testOneCallGetWeather() ) ); - $response = $this->getApi()->getOneCall()->getWeather(38.7077507, -9.1365919); + $response = $this->givenApi()->getOneCall()->getWeather(50, 50); $this->assertWeatherResponse($response); } @@ -38,9 +41,11 @@ 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); } + // --- HISTORY MOMENT --- + public function testOneCallGetHistoryMoment() { $this->mockHttpClient->addResponse( @@ -50,9 +55,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 +67,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,9 +78,11 @@ 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); } + // --- HISTORY AGGREGATE --- + public function testOneCallGetHistoryAggregate() { $this->mockHttpClient->addResponse( @@ -85,9 +92,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 +104,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,17 +115,20 @@ 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(); + // --- 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/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..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( @@ -29,7 +32,7 @@ public function testWeatherGetCurrent() ) ); - $response = $this->getApi()->getWeather()->getCurrent(38.7077507, -9.1365919); + $response = $this->givenApi()->getWeather()->getCurrent(50, 50); $this->assertCurrentResponse($response); } @@ -37,9 +40,11 @@ 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); } + // --- FORECAST --- + public function testWeatherGetForecast() { $this->mockHttpClient->addResponse( @@ -49,7 +54,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,24 +62,27 @@ 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(); + // --- 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); 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