From 9b101f57862318e4ba17898ca50ad227abd3e39b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Pimpa=CC=83o?= Date: Tue, 29 Aug 2023 18:08:30 +0100 Subject: [PATCH 1/5] fix: wrong withCacheTtl test file name --- tests/{WithCacheTtl.php => WithCacheTtlTest.php} | 7 +------ tests/WithLanguageTest.php | 5 ----- tests/WithUnitSystemTest.php | 5 ----- 3 files changed, 1 insertion(+), 16 deletions(-) rename tests/{WithCacheTtl.php => WithCacheTtlTest.php} (57%) diff --git a/tests/WithCacheTtl.php b/tests/WithCacheTtlTest.php similarity index 57% rename from tests/WithCacheTtl.php rename to tests/WithCacheTtlTest.php index 3900576..ffb764c 100644 --- a/tests/WithCacheTtl.php +++ b/tests/WithCacheTtlTest.php @@ -2,7 +2,7 @@ namespace ProgrammatorDev\OpenWeatherMap\Test; -class WithCacheTtl extends AbstractTest +class WithCacheTtlTest extends AbstractTest { public function testWithCacheTtl() { @@ -11,9 +11,4 @@ public function testWithCacheTtl() $this->givenApi()->weather->withCacheTtl(60 * 60)->getCacheTtl() ); } - - public function testWithCacheTtlGetCacheTtl() - { - $this->assertSame(60 * 10, $this->givenApi()->weather->getCacheTtl()); - } } \ No newline at end of file diff --git a/tests/WithLanguageTest.php b/tests/WithLanguageTest.php index 12584e8..7af67cb 100644 --- a/tests/WithLanguageTest.php +++ b/tests/WithLanguageTest.php @@ -21,9 +21,4 @@ public function testWithLanguageWithInvalidValue(string $language, string $expec $this->expectException($expectedException); $this->givenApi()->weather->withLanguage($language); } - - public function testWithLanguageGetLanguage() - { - $this->assertSame('en', $this->givenApi()->weather->getLanguage()); - } } \ No newline at end of file diff --git a/tests/WithUnitSystemTest.php b/tests/WithUnitSystemTest.php index 4160c76..9ccc44a 100644 --- a/tests/WithUnitSystemTest.php +++ b/tests/WithUnitSystemTest.php @@ -21,9 +21,4 @@ public function testWithUnitSystemWithInvalidValue(string $unitSystem, string $e $this->expectException($expectedException); $this->givenApi()->weather->withUnitSystem($unitSystem); } - - public function testWithUnitSystemGetUnitSystem() - { - $this->assertSame('metric', $this->givenApi()->weather->getUnitSystem()); - } } \ No newline at end of file From 0b13a313c3cd359cab04ba400f370aff1db62fe5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Pimpa=CC=83o?= Date: Tue, 29 Aug 2023 18:37:23 +0100 Subject: [PATCH 2/5] chore: added global api base url --- src/Config.php | 7 +++++++ src/Endpoint/AbstractEndpoint.php | 21 ++++++++------------- src/Endpoint/AirPollutionEndpoint.php | 12 +++--------- src/Endpoint/GeocodingEndpoint.php | 12 +++--------- src/Endpoint/OneCallEndpoint.php | 12 +++--------- src/Endpoint/WeatherEndpoint.php | 8 ++------ tests/AbstractEndpointTest.php | 2 +- 7 files changed, 27 insertions(+), 47 deletions(-) diff --git a/src/Config.php b/src/Config.php index 4e2b500..9ad517f 100644 --- a/src/Config.php +++ b/src/Config.php @@ -13,6 +13,8 @@ class Config { + private string $baseUrl = 'https://api.openweathermap.org'; + private array $options; public function __construct(array $options = []) @@ -50,6 +52,11 @@ private function resolveOptions(array $options): array return $resolver->resolve($options); } + public function getBaseUrl(): string + { + return $this->baseUrl; + } + public function getApplicationKey(): string { return $this->options['applicationKey']; diff --git a/src/Endpoint/AbstractEndpoint.php b/src/Endpoint/AbstractEndpoint.php index 2455c21..fa9a4e8 100644 --- a/src/Endpoint/AbstractEndpoint.php +++ b/src/Endpoint/AbstractEndpoint.php @@ -20,7 +20,6 @@ use Psr\Cache\CacheItemPoolInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\StreamInterface; -use Psr\Http\Message\UriInterface; use Psr\Log\LoggerInterface; class AbstractEndpoint @@ -62,7 +61,7 @@ public function __construct(protected OpenWeatherMap $api) */ protected function sendRequest( string $method, - UriInterface|string $baseUrl, + string $path, array $query = [], array $headers = [], StreamInterface|string $body = null @@ -72,13 +71,13 @@ protected function sendRequest( $response = $this->httpClientBuilder->getHttpClient()->send( $method, - $this->buildUrl($baseUrl, $query), + $this->buildUrl($path, $query), $headers, $body ); - if (($statusCode = $response->getStatusCode()) >= 400) { - $this->handleResponseErrors($response, $statusCode); + if ($response->getStatusCode() >= 400) { + $this->handleResponseErrors($response); } return ResponseMediator::toArray($response); @@ -113,13 +112,13 @@ private function configurePlugins(): void * @throws UnauthorizedException * @throws BadRequestException */ - private function handleResponseErrors(ResponseInterface $response, int $statusCode): void + private function handleResponseErrors(ResponseInterface $response): void { $error = new Error( ResponseMediator::toArray($response) ); - match ($statusCode) { + match ($response->getStatusCode()) { 400 => throw new BadRequestException($error), 401 => throw new UnauthorizedException($error), 404 => throw new NotFoundException($error), @@ -128,17 +127,13 @@ private function handleResponseErrors(ResponseInterface $response, int $statusCo }; } - private function buildUrl(UriInterface|string $baseUrl, array $query): string + private function buildUrl(string $path, array $query): string { - if ($baseUrl instanceof UriInterface) { - $baseUrl = (string) $baseUrl; - } - // Add application key to all requests $query = $query + [ 'appid' => $this->config->getApplicationKey() ]; - return \sprintf('%s?%s', $baseUrl, http_build_query($query)); + return \sprintf('%s%s?%s', $this->config->getBaseUrl(), $path, http_build_query($query)); } } \ No newline at end of file diff --git a/src/Endpoint/AirPollutionEndpoint.php b/src/Endpoint/AirPollutionEndpoint.php index d5a1066..1e323ed 100644 --- a/src/Endpoint/AirPollutionEndpoint.php +++ b/src/Endpoint/AirPollutionEndpoint.php @@ -15,12 +15,6 @@ class AirPollutionEndpoint extends AbstractEndpoint { - private string $urlAirPollution = 'https://api.openweathermap.org/data/2.5/air_pollution'; - - private string $urlAirPollutionForecast = 'https://api.openweathermap.org/data/2.5/air_pollution/forecast'; - - private string $urlAirPollutionHistory = 'https://api.openweathermap.org/data/2.5/air_pollution/history'; - /** * @throws Exception * @throws BadRequestException @@ -37,7 +31,7 @@ public function getCurrent(float $latitude, float $longitude): AirPollutionLocat $data = $this->sendRequest( method: 'GET', - baseUrl: $this->urlAirPollution, + path: '/data/2.5/air_pollution', query: [ 'lat' => $latitude, 'lon' => $longitude @@ -63,7 +57,7 @@ public function getForecast(float $latitude, float $longitude): AirPollutionLoca $data = $this->sendRequest( method: 'GET', - baseUrl: $this->urlAirPollutionForecast, + path: '/data/2.5/air_pollution/forecast', query: [ 'lat' => $latitude, 'lon' => $longitude @@ -98,7 +92,7 @@ public function getHistory( $data = $this->sendRequest( method: 'GET', - baseUrl: $this->urlAirPollutionHistory, + path: '/data/2.5/air_pollution/history', query: [ 'lat' => $latitude, 'lon' => $longitude, diff --git a/src/Endpoint/GeocodingEndpoint.php b/src/Endpoint/GeocodingEndpoint.php index df8b5ee..545b33f 100644 --- a/src/Endpoint/GeocodingEndpoint.php +++ b/src/Endpoint/GeocodingEndpoint.php @@ -20,12 +20,6 @@ class GeocodingEndpoint extends AbstractEndpoint private const NUM_RESULTS = 5; - private string $urlGeocodingDirect = 'https://api.openweathermap.org/geo/1.0/direct'; - - private string $urlGeocodingZipCode = 'https://api.openweathermap.org/geo/1.0/zip'; - - private string $urlGeocodingReverse = 'https://api.openweathermap.org/geo/1.0/reverse'; - protected int $cacheTtl = 60 * 60 * 24 * 30; // 30 days /** @@ -45,7 +39,7 @@ public function getByLocationName(string $locationName, int $numResults = self:: $data = $this->sendRequest( method: 'GET', - baseUrl: $this->urlGeocodingDirect, + path: '/geo/1.0/direct', query: [ 'q' => $locationName, 'limit' => $numResults @@ -71,7 +65,7 @@ public function getByZipCode(string $zipCode, string $countryCode): ZipCodeLocat $data = $this->sendRequest( method: 'GET', - baseUrl: $this->urlGeocodingZipCode, + path: '/geo/1.0/zip', query: [ 'zip' => \sprintf('%s,%s', $zipCode, $countryCode) ] @@ -98,7 +92,7 @@ public function getByCoordinate(float $latitude, float $longitude, int $numResul $data = $this->sendRequest( method: 'GET', - baseUrl: $this->urlGeocodingReverse, + path: '/geo/1.0/reverse', query: [ 'lat' => $latitude, 'lon' => $longitude, diff --git a/src/Endpoint/OneCallEndpoint.php b/src/Endpoint/OneCallEndpoint.php index 7dfaf8f..f8f468d 100644 --- a/src/Endpoint/OneCallEndpoint.php +++ b/src/Endpoint/OneCallEndpoint.php @@ -21,12 +21,6 @@ class OneCallEndpoint extends AbstractEndpoint use WithUnitSystemTrait; use WithLanguageTrait; - private string $urlOneCall = 'https://api.openweathermap.org/data/3.0/onecall'; - - private string $urlOneCallHistoryMoment = 'https://api.openweathermap.org/data/3.0/onecall/timemachine'; - - private string $urlOneCallHistoryAggregate = 'https://api.openweathermap.org/data/3.0/onecall/day_summary'; - /** * @throws Exception * @throws BadRequestException @@ -43,7 +37,7 @@ public function getWeather(float $latitude, float $longitude): OneCall $data = $this->sendRequest( method: 'GET', - baseUrl: $this->urlOneCall, + path: '/data/3.0/onecall', query: [ 'lat' => $latitude, 'lon' => $longitude, @@ -72,7 +66,7 @@ public function getHistoryMoment(float $latitude, float $longitude, \DateTimeInt $data = $this->sendRequest( method: 'GET', - baseUrl: $this->urlOneCallHistoryMoment, + path: '/data/3.0/onecall/timemachine', query: [ 'lat' => $latitude, 'lon' => $longitude, @@ -102,7 +96,7 @@ public function getHistoryAggregate(float $latitude, float $longitude, \DateTime $data = $this->sendRequest( method: 'GET', - baseUrl: $this->urlOneCallHistoryAggregate, + path: '/data/3.0/onecall/day_summary', query: [ 'lat' => $latitude, 'lon' => $longitude, diff --git a/src/Endpoint/WeatherEndpoint.php b/src/Endpoint/WeatherEndpoint.php index b38c552..66a3a78 100644 --- a/src/Endpoint/WeatherEndpoint.php +++ b/src/Endpoint/WeatherEndpoint.php @@ -22,10 +22,6 @@ class WeatherEndpoint extends AbstractEndpoint private const NUM_RESULTS = 40; - private string $urlWeather = 'https://api.openweathermap.org/data/2.5/weather'; - - private string $urlWeatherForecast = 'https://api.openweathermap.org/data/2.5/forecast'; - /** * @throws Exception * @throws BadRequestException @@ -42,7 +38,7 @@ public function getCurrent(float $latitude, float $longitude): WeatherLocation $data = $this->sendRequest( method: 'GET', - baseUrl: $this->urlWeather, + path: '/data/2.5/weather', query: [ 'lat' => $latitude, 'lon' => $longitude, @@ -71,7 +67,7 @@ public function getForecast(float $latitude, float $longitude, int $numResults = $data = $this->sendRequest( method: 'GET', - baseUrl: $this->urlWeatherForecast, + path: '/data/2.5/forecast', query: [ 'lat' => $latitude, 'lon' => $longitude, diff --git a/tests/AbstractEndpointTest.php b/tests/AbstractEndpointTest.php index 59061e4..71a599d 100644 --- a/tests/AbstractEndpointTest.php +++ b/tests/AbstractEndpointTest.php @@ -47,6 +47,6 @@ private function mockSendRequest(OpenWeatherMap $api): void $endpoint = new AbstractEndpoint($api); $reflectionClass = new \ReflectionClass($endpoint); $sendRequest = $reflectionClass->getMethod('sendRequest'); - $sendRequest->invokeArgs($endpoint, ['GET', 'https://mock.test']); + $sendRequest->invokeArgs($endpoint, ['GET', '/test']); } } \ No newline at end of file From 2d7e5996caeb1172d9248e16188fdd780adbfc30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Pimpa=CC=83o?= Date: Tue, 29 Aug 2023 18:45:58 +0100 Subject: [PATCH 3/5] chore: removed prefix "with" from endpoint file name traits --- src/Endpoint/AbstractEndpoint.php | 4 ++-- src/Endpoint/OneCallEndpoint.php | 8 ++++---- .../Util/{WithCacheTtlTrait.php => CacheTtlTrait.php} | 2 +- .../Util/{WithLanguageTrait.php => LanguageTrait.php} | 2 +- .../Util/{WithUnitSystemTrait.php => UnitSystemTrait.php} | 2 +- src/Endpoint/WeatherEndpoint.php | 8 ++++---- tests/{WithCacheTtlTest.php => CacheTtlTraitTest.php} | 2 +- tests/{WithLanguageTest.php => LanguageTraitTest.php} | 2 +- tests/{WithUnitSystemTest.php => UnitSystemTraitTest.php} | 2 +- 9 files changed, 16 insertions(+), 16 deletions(-) rename src/Endpoint/Util/{WithCacheTtlTrait.php => CacheTtlTrait.php} (92%) rename src/Endpoint/Util/{WithLanguageTrait.php => LanguageTrait.php} (96%) rename src/Endpoint/Util/{WithUnitSystemTrait.php => UnitSystemTrait.php} (96%) rename tests/{WithCacheTtlTest.php => CacheTtlTraitTest.php} (84%) rename tests/{WithLanguageTest.php => LanguageTraitTest.php} (93%) rename tests/{WithUnitSystemTest.php => UnitSystemTraitTest.php} (93%) diff --git a/src/Endpoint/AbstractEndpoint.php b/src/Endpoint/AbstractEndpoint.php index fa9a4e8..4301d61 100644 --- a/src/Endpoint/AbstractEndpoint.php +++ b/src/Endpoint/AbstractEndpoint.php @@ -6,7 +6,7 @@ use Http\Client\Common\Plugin\LoggerPlugin; use Http\Client\Exception; use ProgrammatorDev\OpenWeatherMap\Config; -use ProgrammatorDev\OpenWeatherMap\Endpoint\Util\WithCacheTtlTrait; +use ProgrammatorDev\OpenWeatherMap\Endpoint\Util\CacheTtlTrait; use ProgrammatorDev\OpenWeatherMap\Entity\Error; use ProgrammatorDev\OpenWeatherMap\Exception\BadRequestException; use ProgrammatorDev\OpenWeatherMap\Exception\NotFoundException; @@ -24,7 +24,7 @@ class AbstractEndpoint { - use WithCacheTtlTrait; + use CacheTtlTrait; private Config $config; diff --git a/src/Endpoint/OneCallEndpoint.php b/src/Endpoint/OneCallEndpoint.php index f8f468d..f48eb27 100644 --- a/src/Endpoint/OneCallEndpoint.php +++ b/src/Endpoint/OneCallEndpoint.php @@ -3,8 +3,8 @@ namespace ProgrammatorDev\OpenWeatherMap\Endpoint; use Http\Client\Exception; -use ProgrammatorDev\OpenWeatherMap\Endpoint\Util\WithLanguageTrait; -use ProgrammatorDev\OpenWeatherMap\Endpoint\Util\WithUnitSystemTrait; +use ProgrammatorDev\OpenWeatherMap\Endpoint\Util\LanguageTrait; +use ProgrammatorDev\OpenWeatherMap\Endpoint\Util\UnitSystemTrait; use ProgrammatorDev\OpenWeatherMap\Entity\OneCall\WeatherAggregate; use ProgrammatorDev\OpenWeatherMap\Entity\OneCall\WeatherLocation; use ProgrammatorDev\OpenWeatherMap\Entity\OneCall\OneCall; @@ -18,8 +18,8 @@ class OneCallEndpoint extends AbstractEndpoint { - use WithUnitSystemTrait; - use WithLanguageTrait; + use UnitSystemTrait; + use LanguageTrait; /** * @throws Exception diff --git a/src/Endpoint/Util/WithCacheTtlTrait.php b/src/Endpoint/Util/CacheTtlTrait.php similarity index 92% rename from src/Endpoint/Util/WithCacheTtlTrait.php rename to src/Endpoint/Util/CacheTtlTrait.php index 5919b01..e4cb69c 100644 --- a/src/Endpoint/Util/WithCacheTtlTrait.php +++ b/src/Endpoint/Util/CacheTtlTrait.php @@ -2,7 +2,7 @@ namespace ProgrammatorDev\OpenWeatherMap\Endpoint\Util; -trait WithCacheTtlTrait +trait CacheTtlTrait { public function withCacheTtl(int $seconds): static { diff --git a/src/Endpoint/Util/WithLanguageTrait.php b/src/Endpoint/Util/LanguageTrait.php similarity index 96% rename from src/Endpoint/Util/WithLanguageTrait.php rename to src/Endpoint/Util/LanguageTrait.php index 1666e62..63e6cec 100644 --- a/src/Endpoint/Util/WithLanguageTrait.php +++ b/src/Endpoint/Util/LanguageTrait.php @@ -6,7 +6,7 @@ use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException; use ProgrammatorDev\YetAnotherPhpValidator\Validator; -trait WithLanguageTrait +trait LanguageTrait { /** * @throws ValidationException diff --git a/src/Endpoint/Util/WithUnitSystemTrait.php b/src/Endpoint/Util/UnitSystemTrait.php similarity index 96% rename from src/Endpoint/Util/WithUnitSystemTrait.php rename to src/Endpoint/Util/UnitSystemTrait.php index 9160f3f..194249f 100644 --- a/src/Endpoint/Util/WithUnitSystemTrait.php +++ b/src/Endpoint/Util/UnitSystemTrait.php @@ -6,7 +6,7 @@ use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException; use ProgrammatorDev\YetAnotherPhpValidator\Validator; -trait WithUnitSystemTrait +trait UnitSystemTrait { /** * @throws ValidationException diff --git a/src/Endpoint/WeatherEndpoint.php b/src/Endpoint/WeatherEndpoint.php index 66a3a78..c389474 100644 --- a/src/Endpoint/WeatherEndpoint.php +++ b/src/Endpoint/WeatherEndpoint.php @@ -3,8 +3,8 @@ namespace ProgrammatorDev\OpenWeatherMap\Endpoint; use Http\Client\Exception; -use ProgrammatorDev\OpenWeatherMap\Endpoint\Util\WithLanguageTrait; -use ProgrammatorDev\OpenWeatherMap\Endpoint\Util\WithUnitSystemTrait; +use ProgrammatorDev\OpenWeatherMap\Endpoint\Util\LanguageTrait; +use ProgrammatorDev\OpenWeatherMap\Endpoint\Util\UnitSystemTrait; use ProgrammatorDev\OpenWeatherMap\Entity\Weather\WeatherLocation; use ProgrammatorDev\OpenWeatherMap\Entity\Weather\WeatherLocationList; use ProgrammatorDev\OpenWeatherMap\Exception\BadRequestException; @@ -17,8 +17,8 @@ class WeatherEndpoint extends AbstractEndpoint { - use WithUnitSystemTrait; - use WithLanguageTrait; + use UnitSystemTrait; + use LanguageTrait; private const NUM_RESULTS = 40; diff --git a/tests/WithCacheTtlTest.php b/tests/CacheTtlTraitTest.php similarity index 84% rename from tests/WithCacheTtlTest.php rename to tests/CacheTtlTraitTest.php index ffb764c..04f91f9 100644 --- a/tests/WithCacheTtlTest.php +++ b/tests/CacheTtlTraitTest.php @@ -2,7 +2,7 @@ namespace ProgrammatorDev\OpenWeatherMap\Test; -class WithCacheTtlTest extends AbstractTest +class CacheTtlTraitTest extends AbstractTest { public function testWithCacheTtl() { diff --git a/tests/WithLanguageTest.php b/tests/LanguageTraitTest.php similarity index 93% rename from tests/WithLanguageTest.php rename to tests/LanguageTraitTest.php index 7af67cb..b2e035e 100644 --- a/tests/WithLanguageTest.php +++ b/tests/LanguageTraitTest.php @@ -5,7 +5,7 @@ use PHPUnit\Framework\Attributes\DataProviderExternal; use ProgrammatorDev\OpenWeatherMap\Test\DataProvider\InvalidParamDataProvider; -class WithLanguageTest extends AbstractTest +class LanguageTraitTest extends AbstractTest { public function testWithLanguage() { diff --git a/tests/WithUnitSystemTest.php b/tests/UnitSystemTraitTest.php similarity index 93% rename from tests/WithUnitSystemTest.php rename to tests/UnitSystemTraitTest.php index 9ccc44a..ec5037b 100644 --- a/tests/WithUnitSystemTest.php +++ b/tests/UnitSystemTraitTest.php @@ -5,7 +5,7 @@ use PHPUnit\Framework\Attributes\DataProviderExternal; use ProgrammatorDev\OpenWeatherMap\Test\DataProvider\InvalidParamDataProvider; -class WithUnitSystemTest extends AbstractTest +class UnitSystemTraitTest extends AbstractTest { public function testWithUnitSystem() { From 872564b2314b4460e521f490521dd17d2f947952 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Pimpa=CC=83o?= Date: Tue, 29 Aug 2023 19:00:14 +0100 Subject: [PATCH 4/5] chore: moved endpoints access from properties to methods (again) --- README.md | 2 +- docs/01-usage.md | 2 +- docs/02-configuration.md | 14 +++++++------- docs/03-supported-apis.md | 28 ++++++++++++++-------------- docs/04-error-handling.md | 10 +++++----- src/Endpoint/AbstractEndpoint.php | 2 +- src/OpenWeatherMap.php | 29 ++++++++++++++++++++--------- tests/AbstractEndpointTest.php | 4 ++-- tests/AirPollutionEndpointTest.php | 18 +++++++++--------- tests/ApiErrorTest.php | 2 +- tests/CacheTtlTraitTest.php | 2 +- tests/GeocodingEndpointTest.php | 14 +++++++------- tests/LanguageTraitTest.php | 4 ++-- tests/OneCallEndpointTest.php | 16 ++++++++-------- tests/OpenWeatherMapTest.php | 10 +++++----- tests/UnitSystemTraitTest.php | 4 ++-- tests/WeatherEndpointTest.php | 10 +++++----- 17 files changed, 91 insertions(+), 80 deletions(-) diff --git a/README.md b/README.md index c16dc2e..e69e930 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ $openWeatherMap = new OpenWeatherMap( ); // Get current weather by coordinate (latitude, longitude) -$currentWeather = $openWeatherMap->weather->getCurrent(50, 50); +$currentWeather = $openWeatherMap->weather()->getCurrent(50, 50); // Show current temperature echo $currentWeather->getTemperature(); ``` diff --git a/docs/01-usage.md b/docs/01-usage.md index 59dc0d5..b2ab51d 100644 --- a/docs/01-usage.md +++ b/docs/01-usage.md @@ -44,7 +44,7 @@ $openWeatherMap = new OpenWeatherMap( ); // Get current weather by coordinate (latitude, longitude) -$currentWeather = $openWeatherMap->weather->getCurrent(50, 50); +$currentWeather = $openWeatherMap->weather()->getCurrent(50, 50); // Show current temperature echo $currentWeather->getTemperature(); ``` \ No newline at end of file diff --git a/docs/02-configuration.md b/docs/02-configuration.md index d8345c5..3bde134 100644 --- a/docs/02-configuration.md +++ b/docs/02-configuration.md @@ -187,7 +187,7 @@ It is possible to change the cache duration per request: ```php // Response will be cached for 1 hour -$currentWeather = $openWeatherMap->weather +$currentWeather = $openWeatherMap->weather() ->withCacheTtl(3600) ->getCurrent(50, 50); ``` @@ -244,20 +244,20 @@ $openWeatherMap = new OpenWeatherMap( // Using applicationKey as an example, // but getters and setters are available for all options -$openWeatherMap->config->getApplicationKey(); -$openWeatherMap->config->setApplicationKey('newappkey'); +$openWeatherMap->config()->getApplicationKey(); +$openWeatherMap->config()->setApplicationKey('newappkey'); ``` Just take into account that any change will affect any subsequent request globally: ```php // Using default 'metric' unit system -$openWeatherMap->weather->getCurrent(50, 50); +$openWeatherMap->weather()->getCurrent(50, 50); // Set new unit system -$openWeatherMap->config->setUnitSystem(UnitSystem::IMPERIAL); +$openWeatherMap->config()->setUnitSystem(UnitSystem::IMPERIAL); // Using 'imperial' unit system -$openWeatherMap->weather->getCurrent(50, 50); -$openWeatherMap->weather->getForecast(50, 50); +$openWeatherMap->weather()->getCurrent(50, 50); +$openWeatherMap->weather()->getForecast(50, 50); ``` \ No newline at end of file diff --git a/docs/03-supported-apis.md b/docs/03-supported-apis.md index 248dd16..7fc2c82 100644 --- a/docs/03-supported-apis.md +++ b/docs/03-supported-apis.md @@ -36,7 +36,7 @@ Get current and forecast (minutely, hourly and daily) weather data. Returns a [`OneCall`](05-objects.md#onecall) object: ```php -$weather = $openWeatherMap->oneCall->getWeather(50, 50); +$weather = $openWeatherMap->oneCall()->getWeather(50, 50); echo $weather->getCurrent()->getTemperature(); ``` @@ -52,7 +52,7 @@ Get weather data from a single moment in the past. Returns a [`WeatherLocation`](05-objects.md#weatherlocation) object: ```php -$weather = $openWeatherMap->oneCall->getHistoryMoment(50, 50, new \DateTime('2023-01-01 12:00:00')); +$weather = $openWeatherMap->oneCall()->getHistoryMoment(50, 50, new \DateTime('2023-01-01 12:00:00')); echo $weather->getTemperature(); ``` @@ -68,7 +68,7 @@ Get aggregated weather data from a single day in the past. Returns a [`WeatherAggregate`](05-objects.md#weatheraggregate) object: ```php -$weather = $openWeatherMap->oneCall->getHistoryAggregate(50, 50, new \DateTime('1985-07-19')); +$weather = $openWeatherMap->oneCall()->getHistoryAggregate(50, 50, new \DateTime('1985-07-19')); echo $weather->getTemperature(); ``` @@ -86,7 +86,7 @@ Get current weather data. Returns a [`WeatherLocation`](05-objects.md#weatherlocation-1) object: ```php -$weather = $openWeatherMap->weather->getCurrent(50, 50); +$weather = $openWeatherMap->weather()->getCurrent(50, 50); echo $weather->getTemperature(); ``` @@ -104,7 +104,7 @@ Returns a [`WeatherLocationList`](05-objects.md#weatherlocationlist) object: ```php // Since it returns 3-hour steps, // passing 8 as the numResults means it will return results for the next 24 hours -$weatherForecast = $openWeatherMap->weather->getForecast(50, 50, 8); +$weatherForecast = $openWeatherMap->weather()->getForecast(50, 50, 8); foreach ($weatherForecast->getList() as $weather) { echo $weather->getDateTime()->format('Y-m-d H:i:s'); @@ -125,7 +125,7 @@ Get current air pollution data. Returns a [`AirPollutionLocation`](05-objects.md#airpollutionlocation) object: ```php -$airPollution = $openWeatherMap->airPollution->getCurrent(50, 50); +$airPollution = $openWeatherMap->airPollution()->getCurrent(50, 50); echo $airPollution->getAirQuality()->getQualitativeName(); echo $airPollution->getCarbonMonoxide(); @@ -142,7 +142,7 @@ Get air pollution forecast data per 1-hour for the next 24 hours. Returns a [`AirPollutionLocationList`](05-objects.md#airpollutionlocationlist) object: ```php -$airPollutionForecast = $openWeatherMap->airPollution->getForecast(50, 50); +$airPollutionForecast = $openWeatherMap->airPollution()->getForecast(50, 50); foreach ($airPollutionForecast->getList() as $airPollution) { echo $airPollution->getDateTime()->format('Y-m-d H:i:s'); @@ -164,7 +164,7 @@ Returns a [`AirPollutionLocationList`](05-objects.md#airpollutionlocationlist) o ```php $startDate = new \DateTime('-7 days'); // 7 days ago $endDate = new \DateTime('-6 days'); // 6 days ago -$airPollutionHistory = $openWeatherMap->airPollution->getHistory(50, 50, $startDate, $endDate); +$airPollutionHistory = $openWeatherMap->airPollution()->getHistory(50, 50, $startDate, $endDate); foreach ($airPollutionHistory->getList() as $airPollution) { echo $airPollution->getDateTime()->format('Y-m-d H:i:s'); @@ -189,7 +189,7 @@ Get locations data by location name. Returns an array of [`Location`](05-objects.md#location) objects: ```php -$locations = $openWeatherMap->geocoding->getByLocationName('lisbon'); +$locations = $openWeatherMap->geocoding()->getByLocationName('lisbon'); foreach ($locations as $location) { echo $location->getName(); @@ -208,7 +208,7 @@ Get location data by zip/post code. Returns a [`ZipCodeLocation`](05-objects.md#zipcodelocation) object: ```php -$location = $openWeatherMap->geocoding->getByZipCode('1000-001', 'pt'); +$location = $openWeatherMap->geocoding()->getByZipCode('1000-001', 'pt'); echo $location->getName(); ``` @@ -227,7 +227,7 @@ Get locations data by coordinate. Returns an array of [`Location`](05-objects.md#location) objects: ```php -$locations = $openWeatherMap->geocoding->getByCoordinate(50, 50); +$locations = $openWeatherMap->geocoding()->getByCoordinate(50, 50); foreach ($locations as $location) { echo $location->getName(); @@ -251,7 +251,7 @@ Only available for [`OneCall`](#one-call) and [`Weather`](#weather) APIs. use ProgrammatorDev\OpenWeatherMap\UnitSystem\UnitSystem; // Uses 'imperial' unit system for this request alone -$openWeatherMap->weather +$openWeatherMap->weather() ->withUnitSystem(UnitSystem::IMPERIAL) ->getCurrent(50, 50); ``` @@ -270,7 +270,7 @@ Only available for [`OneCall`](#one-call) and [`Weather`](#weather) APIs. use ProgrammatorDev\OpenWeatherMap\Language\Language // Uses 'pt' language for this request alone -$openWeatherMap->weather +$openWeatherMap->weather() ->withLanguage(Language::PORTUGUESE) ->getCurrent(50, 50); ``` @@ -296,7 +296,7 @@ Available for all APIs if `cache` is enabled in the [configuration](02-configura use ProgrammatorDev\OpenWeatherMap\Language\Language // Cache will be saved for 1 hour for this request alone -$openWeatherMap->weather +$openWeatherMap->weather() ->withCacheTtl(3600) ->getCurrent(50, 50); ``` \ No newline at end of file diff --git a/docs/04-error-handling.md b/docs/04-error-handling.md index 1dfd90e..664ce67 100644 --- a/docs/04-error-handling.md +++ b/docs/04-error-handling.md @@ -15,9 +15,9 @@ use ProgrammatorDev\OpenWeatherMap\Exception\UnauthorizedException; use ProgrammatorDev\OpenWeatherMap\Exception\UnexpectedErrorException; try { - $location = $openWeatherMap->geocoding->getByZipCode('1000-001', 'pt'); + $location = $openWeatherMap->geocoding()->getByZipCode('1000-001', 'pt'); - $weather = $openWeatherMap->oneCall->getWeather( + $weather = $openWeatherMap->oneCall()->getWeather( $location->getCoordinate()->getLatitude(), $location->getCoordinate()->getLongitude() ); @@ -57,9 +57,9 @@ To catch all API errors with a single exception, `ApiErrorException` is availabl use ProgrammatorDev\OpenWeatherMap\Exception\ApiErrorException; try { - $location = $openWeatherMap->geocoding->getByZipCode('1000-001', 'pt'); + $location = $openWeatherMap->geocoding()->getByZipCode('1000-001', 'pt'); - $weather = $openWeatherMap->oneCall->getWeather( + $weather = $openWeatherMap->oneCall()->getWeather( $location->getCoordinate()->getLatitude(), $location->getCoordinate()->getLongitude() ); @@ -80,7 +80,7 @@ use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException; try { // An invalid latitude value is given - $weather = $openWeatherMap->weather->getCurrent(999, 50); + $weather = $openWeatherMap->weather()->getCurrent(999, 50); } catch (ValidationException $exception) { // Should print: diff --git a/src/Endpoint/AbstractEndpoint.php b/src/Endpoint/AbstractEndpoint.php index 4301d61..4cd7b92 100644 --- a/src/Endpoint/AbstractEndpoint.php +++ b/src/Endpoint/AbstractEndpoint.php @@ -42,7 +42,7 @@ class AbstractEndpoint public function __construct(protected OpenWeatherMap $api) { - $this->config = $this->api->config; + $this->config = $this->api->config(); $this->httpClientBuilder = $this->config->getHttpClientBuilder(); $this->cache = $this->config->getCache(); diff --git a/src/OpenWeatherMap.php b/src/OpenWeatherMap.php index bbe0dc0..1934b15 100644 --- a/src/OpenWeatherMap.php +++ b/src/OpenWeatherMap.php @@ -9,19 +9,30 @@ class OpenWeatherMap { - public OneCallEndpoint $oneCall; + public function __construct(private readonly Config $config) {} - public WeatherEndpoint $weather; + public function config(): Config + { + return $this->config; + } - public AirPollutionEndpoint $airPollution; + public function oneCall(): OneCallEndpoint + { + return new OneCallEndpoint($this); + } - public GeocodingEndpoint $geocoding; + public function weather(): WeatherEndpoint + { + return new WeatherEndpoint($this); + } + + public function airPollution(): AirPollutionEndpoint + { + return new AirPollutionEndpoint($this); + } - public function __construct(public readonly Config $config) + public function geocoding(): GeocodingEndpoint { - $this->oneCall = new OneCallEndpoint($this); - $this->weather = new WeatherEndpoint($this); - $this->airPollution = new AirPollutionEndpoint($this); - $this->geocoding = new GeocodingEndpoint($this); + return new GeocodingEndpoint($this); } } \ No newline at end of file diff --git a/tests/AbstractEndpointTest.php b/tests/AbstractEndpointTest.php index 71a599d..88bba5b 100644 --- a/tests/AbstractEndpointTest.php +++ b/tests/AbstractEndpointTest.php @@ -20,7 +20,7 @@ public function testAbstractEndpointWithCache() $cache->expects($this->once())->method('save'); $api = $this->givenApi(); - $api->config->setCache($cache); + $api->config()->setCache($cache); $this->mockSendRequest($api); } @@ -35,7 +35,7 @@ public function testAbstractEndpointWithLogger() $logger->expects($this->atLeastOnce())->method('info'); $api = $this->givenApi(); - $api->config->setLogger($logger); + $api->config()->setLogger($logger); $this->mockSendRequest($api); } diff --git a/tests/AirPollutionEndpointTest.php b/tests/AirPollutionEndpointTest.php index 6f85f09..af4a7bd 100644 --- a/tests/AirPollutionEndpointTest.php +++ b/tests/AirPollutionEndpointTest.php @@ -25,7 +25,7 @@ public function testAirPollutionGetCurrent() ) ); - $response = $this->givenApi()->airPollution->getCurrent(50, 50); + $response = $this->givenApi()->airPollution()->getCurrent(50, 50); $this->assertCurrentResponse($response); } @@ -33,7 +33,7 @@ public function testAirPollutionGetCurrent() public function testAirPollutionGetCurrentWithInvalidCoordinate(float $latitude, float $longitude, string $expectedException) { $this->expectException($expectedException); - $this->givenApi()->airPollution->getCurrent($latitude, $longitude); + $this->givenApi()->airPollution()->getCurrent($latitude, $longitude); } // --- FORECAST --- @@ -47,7 +47,7 @@ public function testAirPollutionGetForecast() ) ); - $response = $this->givenApi()->airPollution->getForecast(50, 50); + $response = $this->givenApi()->airPollution()->getForecast(50, 50); $this->assertForecastResponse($response); } @@ -55,7 +55,7 @@ public function testAirPollutionGetForecast() public function testAirPollutionGetForecastWithInvalidCoordinate(float $latitude, float $longitude, string $expectedException) { $this->expectException($expectedException); - $this->givenApi()->airPollution->getForecast($latitude, $longitude); + $this->givenApi()->airPollution()->getForecast($latitude, $longitude); } // --- HISTORY --- @@ -71,7 +71,7 @@ public function testAirPollutionGetHistory() $utcTimezone = new \DateTimeZone('UTC'); - $response = $this->givenApi()->airPollution->getHistory( + $response = $this->givenApi()->airPollution()->getHistory( 50, 50, new \DateTimeImmutable('-5 days', $utcTimezone), @@ -88,7 +88,7 @@ public function testAirPollutionGetHistoryWithInvalidCoordinate(float $latitude, $startDate = new \DateTimeImmutable('-5 days'); $endDate = new \DateTimeImmutable('-4 days'); - $this->givenApi()->airPollution->getHistory($latitude, $longitude, $startDate, $endDate); + $this->givenApi()->airPollution()->getHistory($latitude, $longitude, $startDate, $endDate); } #[DataProviderExternal(InvalidParamDataProvider::class, 'provideInvalidPastDateData')] @@ -98,7 +98,7 @@ public function testAirPollutionGetHistoryWithInvalidPastStartDate( ) { $this->expectException($expectedException); - $this->givenApi()->airPollution->getHistory( + $this->givenApi()->airPollution()->getHistory( 50, 50, $startDate, @@ -113,7 +113,7 @@ public function testAirPollutionGetHistoryWithInvalidPastEndDate( ) { $this->expectException($expectedException); - $this->givenApi()->airPollution->getHistory( + $this->givenApi()->airPollution()->getHistory( 50, 50, new \DateTimeImmutable('-5 days', new \DateTimeZone('UTC')), @@ -129,7 +129,7 @@ public function testAirPollutionGetHistoryWithInvalidDateRange( ) { $this->expectException($expectedException); - $this->givenApi()->airPollution->getHistory(50, 50, $startDate, $endDate); + $this->givenApi()->airPollution()->getHistory(50, 50, $startDate, $endDate); } // --- ASSERT METHODS EXIST --- diff --git a/tests/ApiErrorTest.php b/tests/ApiErrorTest.php index 90e17e8..25f0e4f 100644 --- a/tests/ApiErrorTest.php +++ b/tests/ApiErrorTest.php @@ -24,7 +24,7 @@ public function testApiError(int $statusCode, string $expectedException) ); $this->expectException($expectedException); - $this->givenApi()->weather->getCurrent(38.7077507, -9.1365919); + $this->givenApi()->weather()->getCurrent(38.7077507, -9.1365919); } public static function provideApiErrorData(): \Generator diff --git a/tests/CacheTtlTraitTest.php b/tests/CacheTtlTraitTest.php index 04f91f9..68dc358 100644 --- a/tests/CacheTtlTraitTest.php +++ b/tests/CacheTtlTraitTest.php @@ -8,7 +8,7 @@ public function testWithCacheTtl() { $this->assertSame( 60 * 60, - $this->givenApi()->weather->withCacheTtl(60 * 60)->getCacheTtl() + $this->givenApi()->weather()->withCacheTtl(60 * 60)->getCacheTtl() ); } } \ No newline at end of file diff --git a/tests/GeocodingEndpointTest.php b/tests/GeocodingEndpointTest.php index a60940a..d7b78da 100644 --- a/tests/GeocodingEndpointTest.php +++ b/tests/GeocodingEndpointTest.php @@ -25,14 +25,14 @@ public function testGeocodingGetByLocationName() ) ); - $response = $this->givenApi()->geocoding->getByLocationName('lisbon, pt'); + $response = $this->givenApi()->geocoding()->getByLocationName('lisbon, pt'); $this->assertLocationListResponse($response); } public function testGeocodingGetByLocationNameWithBlankValue() { $this->expectException(ValidationException::class); - $this->givenApi()->geocoding->getByLocationName(''); + $this->givenApi()->geocoding()->getByLocationName(''); } // --- BY ZIP CODE --- @@ -46,7 +46,7 @@ public function testGeocodingGetByZipCode() ) ); - $response = $this->givenApi()->geocoding->getByZipCode('1000-001', 'pt'); + $response = $this->givenApi()->geocoding()->getByZipCode('1000-001', 'pt'); $this->assertInstanceOf(ZipCodeLocation::class, $response); $this->assertSame('1000-001', $response->getZipCode()); @@ -63,7 +63,7 @@ public function testGeocodingGetByZipCode() public function testGeocodingGetByZipCodeWithInvalidValue(string $zipCode, string $countryCode) { $this->expectException(ValidationException::class); - $this->givenApi()->geocoding->getByZipCode($zipCode, $countryCode); + $this->givenApi()->geocoding()->getByZipCode($zipCode, $countryCode); } public static function provideGeocodingGetByZipCodeWithInvalidValueData(): \Generator @@ -84,7 +84,7 @@ public function testGeocodingGetByCoordinate() ) ); - $response = $this->givenApi()->geocoding->getByCoordinate(50, 50); + $response = $this->givenApi()->geocoding()->getByCoordinate(50, 50); $this->assertLocationListResponse($response); } @@ -96,7 +96,7 @@ public function testGeocodingGetByCoordinateWithInvalidCoordinate( ) { $this->expectException($expectedException); - $this->givenApi()->geocoding->getByCoordinate($latitude, $longitude); + $this->givenApi()->geocoding()->getByCoordinate($latitude, $longitude); } #[DataProviderExternal(InvalidParamDataProvider::class, 'provideInvalidNumResultsData')] @@ -106,7 +106,7 @@ public function testGeocodingGetByCoordinateWithInvalidNumResults( ) { $this->expectException($expectedException); - $this->givenApi()->geocoding->getByCoordinate(50, 50, $numResults); + $this->givenApi()->geocoding()->getByCoordinate(50, 50, $numResults); } // --- ASSERT METHODS EXIST --- diff --git a/tests/LanguageTraitTest.php b/tests/LanguageTraitTest.php index b2e035e..e8a6bb2 100644 --- a/tests/LanguageTraitTest.php +++ b/tests/LanguageTraitTest.php @@ -11,7 +11,7 @@ public function testWithLanguage() { $this->assertSame( 'pt', - $this->givenApi()->weather->withLanguage('pt')->getLanguage() + $this->givenApi()->weather()->withLanguage('pt')->getLanguage() ); } @@ -19,6 +19,6 @@ public function testWithLanguage() public function testWithLanguageWithInvalidValue(string $language, string $expectedException) { $this->expectException($expectedException); - $this->givenApi()->weather->withLanguage($language); + $this->givenApi()->weather()->withLanguage($language); } } \ No newline at end of file diff --git a/tests/OneCallEndpointTest.php b/tests/OneCallEndpointTest.php index 558cf29..93c9003 100644 --- a/tests/OneCallEndpointTest.php +++ b/tests/OneCallEndpointTest.php @@ -33,7 +33,7 @@ public function testOneCallGetWeather() ) ); - $response = $this->givenApi()->oneCall->getWeather(50, 50); + $response = $this->givenApi()->oneCall()->getWeather(50, 50); $this->assertWeatherResponse($response); } @@ -41,7 +41,7 @@ public function testOneCallGetWeather() public function testOneCallGetWeatherWithInvalidCoordinate(float $latitude, float $longitude, string $expectedException) { $this->expectException($expectedException); - $this->givenApi()->oneCall->getWeather($latitude, $longitude); + $this->givenApi()->oneCall()->getWeather($latitude, $longitude); } // --- HISTORY MOMENT --- @@ -55,7 +55,7 @@ public function testOneCallGetHistoryMoment() ) ); - $response = $this->givenApi()->oneCall->getHistoryMoment( + $response = $this->givenApi()->oneCall()->getHistoryMoment( 50, 50, new \DateTimeImmutable('2023-01-01 00:00:00') @@ -67,7 +67,7 @@ public function testOneCallGetHistoryMoment() public function testOneCallGetHistoryMomentWithInvalidCoordinate(float $latitude, float $longitude, string $expectedException) { $this->expectException($expectedException); - $this->givenApi()->oneCall->getHistoryMoment( + $this->givenApi()->oneCall()->getHistoryMoment( $latitude, $longitude, new \DateTimeImmutable('2023-01-01 00:00:00') @@ -78,7 +78,7 @@ public function testOneCallGetHistoryMomentWithInvalidCoordinate(float $latitude public function testOneCallGetHistoryMomentWithInvalidPastDate(\DateTimeImmutable $date, string $expectedException) { $this->expectException($expectedException); - $this->givenApi()->oneCall->getHistoryMoment(50, 50, $date); + $this->givenApi()->oneCall()->getHistoryMoment(50, 50, $date); } // --- HISTORY AGGREGATE --- @@ -92,7 +92,7 @@ public function testOneCallGetHistoryAggregate() ) ); - $response = $this->givenApi()->oneCall->getHistoryAggregate( + $response = $this->givenApi()->oneCall()->getHistoryAggregate( 50, 50, new \DateTimeImmutable('2023-01-01') @@ -104,7 +104,7 @@ public function testOneCallGetHistoryAggregate() public function testOneCallGetHistoryAggregateWithInvalidCoordinate(float $latitude, float $longitude, string $expectedException) { $this->expectException($expectedException); - $this->givenApi()->oneCall->getHistoryAggregate( + $this->givenApi()->oneCall()->getHistoryAggregate( $latitude, $longitude, new \DateTimeImmutable('2023-01-01') @@ -115,7 +115,7 @@ public function testOneCallGetHistoryAggregateWithInvalidCoordinate(float $latit public function testOneCallGetHistoryAggregateWithInvalidPastDate(\DateTimeImmutable $date, string $expectedException) { $this->expectException($expectedException); - $this->givenApi()->oneCall->getHistoryAggregate(50, 50, $date); + $this->givenApi()->oneCall()->getHistoryAggregate(50, 50, $date); } // --- ASSERT METHODS EXIST --- diff --git a/tests/OpenWeatherMapTest.php b/tests/OpenWeatherMapTest.php index 91368bb..13469b2 100644 --- a/tests/OpenWeatherMapTest.php +++ b/tests/OpenWeatherMapTest.php @@ -12,26 +12,26 @@ class OpenWeatherMapTest extends AbstractTest { public function testOpenWeatherMapConfig() { - $this->assertInstanceOf(Config::class, $this->givenApi()->config); + $this->assertInstanceOf(Config::class, $this->givenApi()->config()); } public function testOpenWeatherMapOneCall() { - $this->assertInstanceOf(OneCallEndpoint::class, $this->givenApi()->oneCall); + $this->assertInstanceOf(OneCallEndpoint::class, $this->givenApi()->oneCall()); } public function testOpenWeatherMapWeather() { - $this->assertInstanceOf(WeatherEndpoint::class, $this->givenApi()->weather); + $this->assertInstanceOf(WeatherEndpoint::class, $this->givenApi()->weather()); } public function testOpenWeatherMapAirPollution() { - $this->assertInstanceOf(AirPollutionEndpoint::class, $this->givenApi()->airPollution); + $this->assertInstanceOf(AirPollutionEndpoint::class, $this->givenApi()->airPollution()); } public function testOpenWeatherMapGeocoding() { - $this->assertInstanceOf(GeocodingEndpoint::class, $this->givenApi()->geocoding); + $this->assertInstanceOf(GeocodingEndpoint::class, $this->givenApi()->geocoding()); } } \ No newline at end of file diff --git a/tests/UnitSystemTraitTest.php b/tests/UnitSystemTraitTest.php index ec5037b..a257e5a 100644 --- a/tests/UnitSystemTraitTest.php +++ b/tests/UnitSystemTraitTest.php @@ -11,7 +11,7 @@ public function testWithUnitSystem() { $this->assertSame( 'imperial', - $this->givenApi()->weather->withUnitSystem('imperial')->getUnitSystem() + $this->givenApi()->weather()->withUnitSystem('imperial')->getUnitSystem() ); } @@ -19,6 +19,6 @@ public function testWithUnitSystem() public function testWithUnitSystemWithInvalidValue(string $unitSystem, string $expectedException) { $this->expectException($expectedException); - $this->givenApi()->weather->withUnitSystem($unitSystem); + $this->givenApi()->weather()->withUnitSystem($unitSystem); } } \ No newline at end of file diff --git a/tests/WeatherEndpointTest.php b/tests/WeatherEndpointTest.php index d324cb9..9d40ff3 100644 --- a/tests/WeatherEndpointTest.php +++ b/tests/WeatherEndpointTest.php @@ -32,7 +32,7 @@ public function testWeatherGetCurrent() ) ); - $response = $this->givenApi()->weather->getCurrent(50, 50); + $response = $this->givenApi()->weather()->getCurrent(50, 50); $this->assertCurrentResponse($response); } @@ -40,7 +40,7 @@ public function testWeatherGetCurrent() public function testWeatherGetCurrentWithInvalidCoordinate(float $latitude, float $longitude, string $expectedException) { $this->expectException($expectedException); - $this->givenApi()->weather->getCurrent($latitude, $longitude); + $this->givenApi()->weather()->getCurrent($latitude, $longitude); } // --- FORECAST --- @@ -54,7 +54,7 @@ public function testWeatherGetForecast() ) ); - $response = $this->givenApi()->weather->getForecast(50, 50, 1); + $response = $this->givenApi()->weather()->getForecast(50, 50, 1); $this->assertForecastResponse($response); } @@ -62,14 +62,14 @@ public function testWeatherGetForecast() public function testWeatherGetForecastWithInvalidCoordinate(float $latitude, float $longitude, string $expectedException) { $this->expectException($expectedException); - $this->givenApi()->weather->getForecast($latitude, $longitude, 10); + $this->givenApi()->weather()->getForecast($latitude, $longitude, 10); } #[DataProviderExternal(InvalidParamDataProvider::class, 'provideInvalidNumResultsData')] public function testWeatherGetForecastWithInvalidNumResults(int $numResults, string $expectedException) { $this->expectException($expectedException); - $this->givenApi()->weather->getForecast(50, 50, $numResults); + $this->givenApi()->weather()->getForecast(50, 50, $numResults); } // --- ASSERT METHODS EXIST --- From b4003962761f1252914b4139d3e194a676f68e26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Pimpa=CC=83o?= Date: Tue, 29 Aug 2023 19:22:16 +0100 Subject: [PATCH 5/5] chore: changes traits tests names to be coherent with other tests --- tests/CacheTtlTraitTest.php | 2 +- tests/LanguageTraitTest.php | 4 ++-- tests/UnitSystemTraitTest.php | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/CacheTtlTraitTest.php b/tests/CacheTtlTraitTest.php index 68dc358..a056116 100644 --- a/tests/CacheTtlTraitTest.php +++ b/tests/CacheTtlTraitTest.php @@ -4,7 +4,7 @@ class CacheTtlTraitTest extends AbstractTest { - public function testWithCacheTtl() + public function testCacheTtlTraitWithCacheTtl() { $this->assertSame( 60 * 60, diff --git a/tests/LanguageTraitTest.php b/tests/LanguageTraitTest.php index e8a6bb2..b85a06e 100644 --- a/tests/LanguageTraitTest.php +++ b/tests/LanguageTraitTest.php @@ -7,7 +7,7 @@ class LanguageTraitTest extends AbstractTest { - public function testWithLanguage() + public function testLanguageTraitWithLanguage() { $this->assertSame( 'pt', @@ -16,7 +16,7 @@ public function testWithLanguage() } #[DataProviderExternal(InvalidParamDataProvider::class, 'provideInvalidLanguageData')] - public function testWithLanguageWithInvalidValue(string $language, string $expectedException) + public function testLanguageTraitWithLanguageWithInvalidValue(string $language, string $expectedException) { $this->expectException($expectedException); $this->givenApi()->weather()->withLanguage($language); diff --git a/tests/UnitSystemTraitTest.php b/tests/UnitSystemTraitTest.php index a257e5a..add86d7 100644 --- a/tests/UnitSystemTraitTest.php +++ b/tests/UnitSystemTraitTest.php @@ -7,7 +7,7 @@ class UnitSystemTraitTest extends AbstractTest { - public function testWithUnitSystem() + public function testUnitSystemTraitWithUnitSystem() { $this->assertSame( 'imperial', @@ -16,7 +16,7 @@ public function testWithUnitSystem() } #[DataProviderExternal(InvalidParamDataProvider::class, 'provideInvalidUnitSystemData')] - public function testWithUnitSystemWithInvalidValue(string $unitSystem, string $expectedException) + public function testUnitSystemTraitWithUnitSystemWithInvalidValue(string $unitSystem, string $expectedException) { $this->expectException($expectedException); $this->givenApi()->weather()->withUnitSystem($unitSystem);