diff --git a/src/Endpoint/AirPollutionEndpoint.php b/src/Endpoint/AirPollutionEndpoint.php index 19d0eea..fbc7291 100644 --- a/src/Endpoint/AirPollutionEndpoint.php +++ b/src/Endpoint/AirPollutionEndpoint.php @@ -10,6 +10,7 @@ use ProgrammatorDev\OpenWeatherMap\Exception\TooManyRequestsException; use ProgrammatorDev\OpenWeatherMap\Exception\UnauthorizedException; use ProgrammatorDev\OpenWeatherMap\Exception\UnexpectedErrorException; +use ProgrammatorDev\OpenWeatherMap\Exception\ValidationException; use ProgrammatorDev\OpenWeatherMap\Validator\CoordinateValidatorTrait; use ProgrammatorDev\OpenWeatherMap\Validator\LessThanValidatorTrait; use ProgrammatorDev\OpenWeatherMap\Validator\RangeValidatorTrait; @@ -35,6 +36,7 @@ class AirPollutionEndpoint extends AbstractEndpoint * @throws UnauthorizedException * @throws UnexpectedErrorException * @throws InvalidArgumentException + * @throws ValidationException */ public function getCurrent(float $latitude, float $longitude): CurrentAirPollution { @@ -60,6 +62,7 @@ public function getCurrent(float $latitude, float $longitude): CurrentAirPolluti * @throws UnauthorizedException * @throws UnexpectedErrorException * @throws InvalidArgumentException + * @throws ValidationException */ public function getForecast(float $latitude, float $longitude): AirPollutionList { @@ -85,6 +88,7 @@ public function getForecast(float $latitude, float $longitude): AirPollutionList * @throws UnauthorizedException * @throws UnexpectedErrorException * @throws InvalidArgumentException + * @throws ValidationException */ public function getHistory( float $latitude, diff --git a/src/Endpoint/GeocodingEndpoint.php b/src/Endpoint/GeocodingEndpoint.php index 237be14..7025bc9 100644 --- a/src/Endpoint/GeocodingEndpoint.php +++ b/src/Endpoint/GeocodingEndpoint.php @@ -10,6 +10,7 @@ use ProgrammatorDev\OpenWeatherMap\Exception\TooManyRequestsException; use ProgrammatorDev\OpenWeatherMap\Exception\UnauthorizedException; use ProgrammatorDev\OpenWeatherMap\Exception\UnexpectedErrorException; +use ProgrammatorDev\OpenWeatherMap\Exception\ValidationException; use ProgrammatorDev\OpenWeatherMap\Util\CreateEntityListTrait; use ProgrammatorDev\OpenWeatherMap\Validator\BlankValidatorTrait; use ProgrammatorDev\OpenWeatherMap\Validator\CoordinateValidatorTrait; @@ -42,6 +43,7 @@ class GeocodingEndpoint extends AbstractEndpoint * @throws UnauthorizedException * @throws UnexpectedErrorException * @throws InvalidArgumentException + * @throws ValidationException */ public function getCoordinatesByLocationName(string $locationName, int $numResults = self::NUM_RESULTS): array { @@ -68,6 +70,7 @@ public function getCoordinatesByLocationName(string $locationName, int $numResul * @throws UnauthorizedException * @throws UnexpectedErrorException * @throws InvalidArgumentException + * @throws ValidationException */ public function getCoordinatesByZipCode(string $zipCode, string $countryCode): ZipCodeLocation { @@ -94,6 +97,7 @@ public function getCoordinatesByZipCode(string $zipCode, string $countryCode): Z * @throws UnauthorizedException * @throws UnexpectedErrorException * @throws InvalidArgumentException + * @throws ValidationException */ public function getLocationNameByCoordinates(float $latitude, float $longitude, int $numResults = self::NUM_RESULTS): array { diff --git a/src/Endpoint/OneCallEndpoint.php b/src/Endpoint/OneCallEndpoint.php index 515962d..be54fbd 100644 --- a/src/Endpoint/OneCallEndpoint.php +++ b/src/Endpoint/OneCallEndpoint.php @@ -13,6 +13,7 @@ use ProgrammatorDev\OpenWeatherMap\Exception\TooManyRequestsException; use ProgrammatorDev\OpenWeatherMap\Exception\UnauthorizedException; use ProgrammatorDev\OpenWeatherMap\Exception\UnexpectedErrorException; +use ProgrammatorDev\OpenWeatherMap\Exception\ValidationException; use ProgrammatorDev\OpenWeatherMap\Validator\CoordinateValidatorTrait; use ProgrammatorDev\OpenWeatherMap\Validator\LessThanValidatorTrait; use Psr\Cache\InvalidArgumentException; @@ -38,6 +39,7 @@ class OneCallEndpoint extends AbstractEndpoint * @throws UnauthorizedException * @throws UnexpectedErrorException * @throws InvalidArgumentException + * @throws ValidationException */ public function getWeather(float $latitude, float $longitude): OneCall { @@ -65,6 +67,7 @@ public function getWeather(float $latitude, float $longitude): OneCall * @throws UnauthorizedException * @throws UnexpectedErrorException * @throws InvalidArgumentException + * @throws ValidationException */ public function getHistoryMoment(float $latitude, float $longitude, \DateTimeInterface $dateTime): HistoryMoment { @@ -94,6 +97,7 @@ public function getHistoryMoment(float $latitude, float $longitude, \DateTimeInt * @throws UnauthorizedException * @throws UnexpectedErrorException * @throws InvalidArgumentException + * @throws ValidationException */ public function getHistoryDaySummary(float $latitude, float $longitude, \DateTimeInterface $dateTime): HistoryDaySummary { diff --git a/src/Endpoint/WeatherEndpoint.php b/src/Endpoint/WeatherEndpoint.php index a07aa3a..b7fc9b9 100644 --- a/src/Endpoint/WeatherEndpoint.php +++ b/src/Endpoint/WeatherEndpoint.php @@ -12,6 +12,7 @@ use ProgrammatorDev\OpenWeatherMap\Exception\TooManyRequestsException; use ProgrammatorDev\OpenWeatherMap\Exception\UnauthorizedException; use ProgrammatorDev\OpenWeatherMap\Exception\UnexpectedErrorException; +use ProgrammatorDev\OpenWeatherMap\Exception\ValidationException; use ProgrammatorDev\OpenWeatherMap\Validator\CoordinateValidatorTrait; use ProgrammatorDev\OpenWeatherMap\Validator\GreaterThanValidatorTrait; use Psr\Cache\InvalidArgumentException; @@ -37,6 +38,7 @@ class WeatherEndpoint extends AbstractEndpoint * @throws UnauthorizedException * @throws UnexpectedErrorException * @throws InvalidArgumentException + * @throws ValidationException */ public function getCurrent(float $latitude, float $longitude): CurrentWeather { @@ -64,6 +66,7 @@ public function getCurrent(float $latitude, float $longitude): CurrentWeather * @throws UnauthorizedException * @throws UnexpectedErrorException * @throws InvalidArgumentException + * @throws ValidationException */ public function getForecast(float $latitude, float $longitude, int $numResults = self::NUM_RESULTS): WeatherList { diff --git a/src/Exception/ValidationException.php b/src/Exception/ValidationException.php new file mode 100644 index 0000000..ed907c0 --- /dev/null +++ b/src/Exception/ValidationException.php @@ -0,0 +1,5 @@ +getTimestamp() < $startConstraint->getTimestamp() || $value->getTimestamp() > $endConstraint->getTimestamp()) { $dateFormat = 'Y-m-d H:i:s'; - throw new \UnexpectedValueException( + throw new ValidationException( \sprintf( 'The "%s" date "%s" is invalid. Must be between "%s" and "%s".', $name, @@ -42,7 +47,7 @@ private function ValidateBetween( } if ($value < $startConstraint || $value > $endConstraint) { - throw new \UnexpectedValueException( + throw new ValidationException( \sprintf( 'The "%s" value "%d" is invalid. Must be between "%d" and "%d".', $name, diff --git a/src/Validator/BlankValidatorTrait.php b/src/Validator/BlankValidatorTrait.php index db078f7..ac72da7 100644 --- a/src/Validator/BlankValidatorTrait.php +++ b/src/Validator/BlankValidatorTrait.php @@ -2,12 +2,17 @@ namespace ProgrammatorDev\OpenWeatherMap\Validator; +use ProgrammatorDev\OpenWeatherMap\Exception\ValidationException; + trait BlankValidatorTrait { + /** + * @throws ValidationException + */ private function validateBlank(string $name, string $value): void { if (empty($value)) { - throw new \UnexpectedValueException( + throw new ValidationException( \sprintf('The "%s" value should not be blank.', $name) ); } diff --git a/src/Validator/ChoiceValidatorTrait.php b/src/Validator/ChoiceValidatorTrait.php index 5dd8e88..93cc97f 100644 --- a/src/Validator/ChoiceValidatorTrait.php +++ b/src/Validator/ChoiceValidatorTrait.php @@ -2,12 +2,17 @@ namespace ProgrammatorDev\OpenWeatherMap\Validator; +use ProgrammatorDev\OpenWeatherMap\Exception\ValidationException; + trait ChoiceValidatorTrait { + /** + * @throws ValidationException + */ private function validateChoice(string $name, mixed $value, array $options): void { if ( ! in_array($value, $options)) { - throw new \UnexpectedValueException( + throw new ValidationException( \sprintf( 'The "%s" value "%s" is invalid. Accepted values are: "%s".', $name, diff --git a/src/Validator/CoordinateValidatorTrait.php b/src/Validator/CoordinateValidatorTrait.php index 9e58b9f..e9176d8 100644 --- a/src/Validator/CoordinateValidatorTrait.php +++ b/src/Validator/CoordinateValidatorTrait.php @@ -2,10 +2,15 @@ namespace ProgrammatorDev\OpenWeatherMap\Validator; +use ProgrammatorDev\OpenWeatherMap\Exception\ValidationException; + trait CoordinateValidatorTrait { use BetweenValidatorTrait; + /** + * @throws ValidationException + */ private function validateCoordinate(float $latitude, float $longitude): void { $this->validateBetween('latitude', $latitude, -90, 90); diff --git a/src/Validator/GreaterThanValidatorTrait.php b/src/Validator/GreaterThanValidatorTrait.php index 229e55c..c2ce9c6 100644 --- a/src/Validator/GreaterThanValidatorTrait.php +++ b/src/Validator/GreaterThanValidatorTrait.php @@ -2,8 +2,13 @@ namespace ProgrammatorDev\OpenWeatherMap\Validator; +use ProgrammatorDev\OpenWeatherMap\Exception\ValidationException; + trait GreaterThanValidatorTrait { + /** + * @throws ValidationException + */ private function validateGreaterThan( string $name, \DateTimeInterface|int|float $value, @@ -22,7 +27,7 @@ private function validateGreaterThan( if ($value->getTimestamp() <= $constraint->getTimestamp()) { $dateFormat = 'Y-m-d H:i:s'; - throw new \UnexpectedValueException( + throw new ValidationException( \sprintf( 'The "%s" value "%s" is invalid. Must be greater than "%s".', $name, @@ -34,7 +39,7 @@ private function validateGreaterThan( } if ($value <= $constraint) { - throw new \UnexpectedValueException( + throw new ValidationException( \sprintf( 'The "%s" value "%d" is invalid. Must be greater than "%d".', $name, diff --git a/src/Validator/LessThanValidatorTrait.php b/src/Validator/LessThanValidatorTrait.php index 0498777..f651d2b 100644 --- a/src/Validator/LessThanValidatorTrait.php +++ b/src/Validator/LessThanValidatorTrait.php @@ -2,8 +2,13 @@ namespace ProgrammatorDev\OpenWeatherMap\Validator; +use ProgrammatorDev\OpenWeatherMap\Exception\ValidationException; + trait LessThanValidatorTrait { + /** + * @throws ValidationException + */ private function validateLessThan( string $name, \DateTimeInterface|int|float $value, @@ -22,7 +27,7 @@ private function validateLessThan( if ($value->getTimestamp() >= $constraint->getTimestamp()) { $dateFormat = 'Y-m-d H:i:s'; - throw new \UnexpectedValueException( + throw new ValidationException( \sprintf( 'The "%s" value "%s" is invalid. Must be less than "%s".', $name, @@ -34,7 +39,7 @@ private function validateLessThan( } if ($value >= $constraint) { - throw new \UnexpectedValueException( + throw new ValidationException( \sprintf( 'The "%s" value "%d" is invalid. Must be less than "%d".', $name, diff --git a/src/Validator/RangeValidatorTrait.php b/src/Validator/RangeValidatorTrait.php index 584bb59..d90d7b7 100644 --- a/src/Validator/RangeValidatorTrait.php +++ b/src/Validator/RangeValidatorTrait.php @@ -2,10 +2,15 @@ namespace ProgrammatorDev\OpenWeatherMap\Validator; +use ProgrammatorDev\OpenWeatherMap\Exception\ValidationException; + trait RangeValidatorTrait { use LessThanValidatorTrait; + /** + * @throws ValidationException + */ public function validateRange( string $startName, string $endName, @@ -23,11 +28,11 @@ public function validateRange( try { $this->validateLessThan('startValue', $startValue, $endValue); } - catch (\UnexpectedValueException) { + catch (ValidationException) { if ($startValue instanceof \DateTimeInterface) { $dateFormat = 'Y-m-d H:i:s'; - throw new \UnexpectedValueException( + throw new ValidationException( \sprintf( 'The "%s" value "%s" should be less than the "%s" value "%s".', $startName, @@ -38,7 +43,7 @@ public function validateRange( ); } - throw new \UnexpectedValueException( + throw new ValidationException( \sprintf( 'The "%s" value "%d" should be less than the "%s" value "%d".', $startName, diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index 7a4f737..6644272 100644 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php @@ -6,6 +6,7 @@ use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\DataProviderExternal; use ProgrammatorDev\OpenWeatherMap\Config; +use ProgrammatorDev\OpenWeatherMap\Exception\ValidationException; use ProgrammatorDev\OpenWeatherMap\HttpClient\HttpClientBuilder; use ProgrammatorDev\OpenWeatherMap\Test\DataProvider\InvalidParamDataProvider; use Psr\Cache\CacheItemPoolInterface; @@ -100,7 +101,7 @@ public function testConfigSetApplicationKey() public function testConfigSetApplicationKeyWithBlankValue() { - $this->expectException(\UnexpectedValueException::class); + $this->expectException(ValidationException::class); $this->config->setApplicationKey(''); } diff --git a/tests/DataProvider/InvalidParamDataProvider.php b/tests/DataProvider/InvalidParamDataProvider.php index 02cc1fd..845c79f 100644 --- a/tests/DataProvider/InvalidParamDataProvider.php +++ b/tests/DataProvider/InvalidParamDataProvider.php @@ -2,21 +2,23 @@ namespace ProgrammatorDev\OpenWeatherMap\Test\DataProvider; +use ProgrammatorDev\OpenWeatherMap\Exception\ValidationException; + class InvalidParamDataProvider { public static function provideInvalidCoordinateData(): \Generator { - yield 'latitude lower than -90' => [-91, -9.1365919, \UnexpectedValueException::class]; - yield 'latitude greater than 90' => [91, -9.1365919, \UnexpectedValueException::class]; - yield 'longitude lower than -180' => [38.7077507, -181, \UnexpectedValueException::class]; - yield 'longitude greater than 180' => [38.7077507, 181, \UnexpectedValueException::class]; + yield 'latitude lower than -90' => [-91, -9.1365919, ValidationException::class]; + yield 'latitude greater than 90' => [91, -9.1365919, ValidationException::class]; + yield 'longitude lower than -180' => [38.7077507, -181, ValidationException::class]; + yield 'longitude greater than 180' => [38.7077507, 181, ValidationException::class]; } public static function provideInvalidPastDateData(): \Generator { yield 'invalid past date' => [ new \DateTimeImmutable('1 days'), - \UnexpectedValueException::class + ValidationException::class ]; } @@ -25,23 +27,23 @@ public static function provideInvalidDateRangeData(): \Generator yield 'start date greater than end date' => [ new \DateTimeImmutable('-4 days'), new \DateTimeImmutable('-5 days'), - \UnexpectedValueException::class + ValidationException::class ]; } public static function provideInvalidNumResultsData(): \Generator { - yield 'equal to zero num results' => [0, \UnexpectedValueException::class]; - yield 'negative num results' => [-1, \UnexpectedValueException::class]; + yield 'equal to zero num results' => [0, ValidationException::class]; + yield 'negative num results' => [-1, ValidationException::class]; } public static function provideInvalidMeasurementSystemData(): \Generator { - yield 'not allowed measurement system' => ['invalid', \UnexpectedValueException::class]; + yield 'not allowed measurement system' => ['invalid', ValidationException::class]; } public static function provideInvalidLanguageData(): \Generator { - yield 'not allowed language' => ['invalid', \UnexpectedValueException::class]; + yield 'not allowed language' => ['invalid', ValidationException::class]; } } \ No newline at end of file diff --git a/tests/GeocodingEndpointTest.php b/tests/GeocodingEndpointTest.php index 75ff306..eaebc55 100644 --- a/tests/GeocodingEndpointTest.php +++ b/tests/GeocodingEndpointTest.php @@ -8,6 +8,7 @@ use ProgrammatorDev\OpenWeatherMap\Entity\Coordinate; use ProgrammatorDev\OpenWeatherMap\Entity\Geocoding\ZipCodeLocation; use ProgrammatorDev\OpenWeatherMap\Entity\Location; +use ProgrammatorDev\OpenWeatherMap\Exception\ValidationException; use ProgrammatorDev\OpenWeatherMap\Test\DataProvider\InvalidParamDataProvider; class GeocodingEndpointTest extends AbstractTest @@ -27,7 +28,7 @@ public function testGeocodingGetCoordinatesByLocationName() public function testGeocodingGetCoordinatesByLocationNameWithBlankValue() { - $this->expectException(\UnexpectedValueException::class); + $this->expectException(ValidationException::class); $this->getApi()->getGeocoding()->getCoordinatesByLocationName(''); } @@ -56,7 +57,7 @@ public function testGeocodingGetCoordinatesByZipCode() #[DataProvider('provideGeocodingGetCoordinatesByZipCodeWithBlankValueData')] public function testGeocodingGetCoordinatesByZipCodeWithBlankValue(string $zipCode, string $countryCode) { - $this->expectException(\UnexpectedValueException::class); + $this->expectException(ValidationException::class); $this->getApi()->getGeocoding()->getCoordinatesByZipCode($zipCode, $countryCode); }