diff --git a/src/Resource/AirPollutionResource.php b/src/Resource/AirPollutionResource.php index 7400468..7a1ff14 100644 --- a/src/Resource/AirPollutionResource.php +++ b/src/Resource/AirPollutionResource.php @@ -5,14 +5,11 @@ use ProgrammatorDev\Api\Method; use ProgrammatorDev\OpenWeatherMap\Entity\AirPollution\AirPollution; use ProgrammatorDev\OpenWeatherMap\Entity\AirPollution\AirPollutionCollection; -use ProgrammatorDev\OpenWeatherMap\Resource\Util\ValidationTrait; use ProgrammatorDev\Validator\Exception\ValidationException; use Psr\Http\Client\ClientExceptionInterface; class AirPollutionResource extends Resource { - use ValidationTrait; - /** * @throws ValidationException * @throws ClientExceptionInterface diff --git a/src/Resource/GeocodingResource.php b/src/Resource/GeocodingResource.php index fa8ef48..c0539ba 100644 --- a/src/Resource/GeocodingResource.php +++ b/src/Resource/GeocodingResource.php @@ -5,7 +5,6 @@ use ProgrammatorDev\Api\Method; use ProgrammatorDev\OpenWeatherMap\Entity\Geocoding\ZipLocation; use ProgrammatorDev\OpenWeatherMap\Entity\Location; -use ProgrammatorDev\OpenWeatherMap\Resource\Util\ValidationTrait; use ProgrammatorDev\OpenWeatherMap\Util\EntityTrait; use ProgrammatorDev\Validator\Exception\ValidationException; use Psr\Http\Client\ClientExceptionInterface; @@ -13,7 +12,6 @@ class GeocodingResource extends Resource { use EntityTrait; - use ValidationTrait; private const NUM_RESULTS = 5; diff --git a/src/Resource/Resource.php b/src/Resource/Resource.php index d17aef8..b9f59e7 100644 --- a/src/Resource/Resource.php +++ b/src/Resource/Resource.php @@ -4,10 +4,12 @@ use ProgrammatorDev\OpenWeatherMap\OpenWeatherMap; use ProgrammatorDev\OpenWeatherMap\Resource\Util\CacheTrait; +use ProgrammatorDev\OpenWeatherMap\Resource\Util\ValidationTrait; class Resource { use CacheTrait; + use ValidationTrait; public function __construct(protected OpenWeatherMap $api) {} } \ No newline at end of file diff --git a/src/Resource/Util/ValidationTrait.php b/src/Resource/Util/ValidationTrait.php index 382ce7c..33dd7d6 100644 --- a/src/Resource/Util/ValidationTrait.php +++ b/src/Resource/Util/ValidationTrait.php @@ -12,7 +12,7 @@ trait ValidationTrait /** * @throws ValidationException */ - private function validateQuery(string $query, string $name): void + protected function validateQuery(string $query, string $name): void { Validator::notBlank()->assert($query, $name); } @@ -20,7 +20,7 @@ private function validateQuery(string $query, string $name): void /** * @throws ValidationException */ - private function validatePositive(int $number, string $name): void + protected function validatePositive(int $number, string $name): void { Validator::greaterThan(0)->assert($number, $name); } @@ -28,7 +28,7 @@ private function validatePositive(int $number, string $name): void /** * @throws ValidationException */ - private function validateCoordinate(float $latitude, float $longitude): void + protected function validateCoordinate(float $latitude, float $longitude): void { Validator::range(-90, 90)->assert($latitude, 'latitude'); Validator::range(-180, 180)->assert($longitude, 'longitude'); @@ -37,7 +37,7 @@ private function validateCoordinate(float $latitude, float $longitude): void /** * @throws ValidationException */ - private function validateCountryCode(string $countryCode): void + protected function validateCountryCode(string $countryCode): void { Validator::country()->assert($countryCode, 'countryCode'); } @@ -45,7 +45,7 @@ private function validateCountryCode(string $countryCode): void /** * @throws ValidationException */ - private function validateLanguage(string $language): void + protected function validateLanguage(string $language): void { Validator::choice(Language::getOptions())->assert($language, 'language'); } @@ -53,7 +53,7 @@ private function validateLanguage(string $language): void /** * @throws ValidationException */ - private function validateUnitSystem(string $unitSystem): void + protected function validateUnitSystem(string $unitSystem): void { Validator::choice(UnitSystem::getOptions())->assert($unitSystem, 'unitSystem'); } @@ -61,7 +61,7 @@ private function validateUnitSystem(string $unitSystem): void /** * @throws ValidationException */ - private function validateDateOrder(\DateTimeInterface $startDate, \DateTimeInterface $endDate): void + protected function validateDateOrder(\DateTimeInterface $startDate, \DateTimeInterface $endDate): void { Validator::greaterThan( constraint: $startDate, diff --git a/src/Resource/WeatherResource.php b/src/Resource/WeatherResource.php index 66803a4..73ba121 100644 --- a/src/Resource/WeatherResource.php +++ b/src/Resource/WeatherResource.php @@ -7,7 +7,6 @@ use ProgrammatorDev\OpenWeatherMap\Entity\Weather\WeatherCollection; use ProgrammatorDev\OpenWeatherMap\Resource\Util\LanguageTrait; use ProgrammatorDev\OpenWeatherMap\Resource\Util\UnitSystemTrait; -use ProgrammatorDev\OpenWeatherMap\Resource\Util\ValidationTrait; use ProgrammatorDev\Validator\Exception\ValidationException; use Psr\Http\Client\ClientExceptionInterface; @@ -15,7 +14,6 @@ class WeatherResource extends Resource { use LanguageTrait; use UnitSystemTrait; - use ValidationTrait; private const NUM_RESULTS = 40; diff --git a/tests/Unit/AirPollution/AirPollutionCollectionTest.php b/tests/Unit/AirPollution/AirPollutionCollectionTest.php new file mode 100644 index 0000000..f7757a9 --- /dev/null +++ b/tests/Unit/AirPollution/AirPollutionCollectionTest.php @@ -0,0 +1,42 @@ + [ + 'lat' => 50, + 'lon' => 50 + ], + 'list' => [ + [ + 'dt' => 1715279409, + 'main' => [ + 'aqi' => 1 + ], + 'components' => [ + 'co' => 100, + 'no' => 0, + 'no2' => 1, + 'o3' => 100, + 'so2' => 1, + 'pm2_5' => 1, + 'pm10' => 1, + 'nh3' => 1 + ] + ] + ] + ]); + + $this->assertInstanceOf(Coordinate::class, $entity->getCoordinate()); + $this->assertContainsOnlyInstancesOf(AirPollutionData::class, $entity->getData()); + } +} \ No newline at end of file diff --git a/tests/Unit/AirPollution/AirPollutionDataTest.php b/tests/Unit/AirPollution/AirPollutionDataTest.php new file mode 100644 index 0000000..68da780 --- /dev/null +++ b/tests/Unit/AirPollution/AirPollutionDataTest.php @@ -0,0 +1,41 @@ + 1715279409, + 'main' => [ + 'aqi' => 1 + ], + 'components' => [ + 'co' => 100, + 'no' => 0, + 'no2' => 1, + 'o3' => 100, + 'so2' => 1, + 'pm2_5' => 1, + 'pm10' => 1, + 'nh3' => 1 + ] + ]); + + $this->assertInstanceOf(\DateTimeImmutable::class, $entity->getDateTime()); + $this->assertInstanceOf(AirQuality::class, $entity->getAirQuality()); + $this->assertSame(100.0, $entity->getCarbonMonoxide()); + $this->assertSame(0.0, $entity->getNitrogenMonoxide()); + $this->assertSame(1.0, $entity->getNitrogenDioxide()); + $this->assertSame(100.0, $entity->getOzone()); + $this->assertSame(1.0, $entity->getSulphurDioxide()); + $this->assertSame(1.0, $entity->getFineParticulateMatter()); + $this->assertSame(1.0, $entity->getCoarseParticulateMatter()); + $this->assertSame(1.0, $entity->getAmmonia()); + } +} \ No newline at end of file diff --git a/tests/Unit/AirPollution/AirPollutionTest.php b/tests/Unit/AirPollution/AirPollutionTest.php new file mode 100644 index 0000000..76902f7 --- /dev/null +++ b/tests/Unit/AirPollution/AirPollutionTest.php @@ -0,0 +1,51 @@ + [ + 'lat' => 50, + 'lon' => 50 + ], + 'list' => [ + [ + 'dt' => 1715279409, + 'main' => [ + 'aqi' => 1 + ], + 'components' => [ + 'co' => 100, + 'no' => 0, + 'no2' => 1, + 'o3' => 100, + 'so2' => 1, + 'pm2_5' => 1, + 'pm10' => 1, + 'nh3' => 1 + ] + ] + ] + ]); + + $this->assertInstanceOf(Coordinate::class, $entity->getCoordinate()); + $this->assertInstanceOf(\DateTimeImmutable::class, $entity->getDateTime()); + $this->assertInstanceOf(AirQuality::class, $entity->getAirQuality()); + $this->assertSame(100.0, $entity->getCarbonMonoxide()); + $this->assertSame(0.0, $entity->getNitrogenMonoxide()); + $this->assertSame(1.0, $entity->getNitrogenDioxide()); + $this->assertSame(100.0, $entity->getOzone()); + $this->assertSame(1.0, $entity->getSulphurDioxide()); + $this->assertSame(1.0, $entity->getFineParticulateMatter()); + $this->assertSame(1.0, $entity->getCoarseParticulateMatter()); + $this->assertSame(1.0, $entity->getAmmonia()); + } +} \ No newline at end of file diff --git a/tests/Unit/AirPollution/AirQualityTest.php b/tests/Unit/AirPollution/AirQualityTest.php new file mode 100644 index 0000000..cae7f29 --- /dev/null +++ b/tests/Unit/AirPollution/AirQualityTest.php @@ -0,0 +1,19 @@ + 1 + ]); + + $this->assertSame(1, $entity->getIndex()); + $this->assertSame('Good', $entity->getQualitativeName()); + } +} \ No newline at end of file