Skip to content

Commit

Permalink
tests: added air pollution entities tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andrepimpao committed May 9, 2024
1 parent e8a6665 commit e35e1e2
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 14 deletions.
3 changes: 0 additions & 3 deletions src/Resource/AirPollutionResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions src/Resource/GeocodingResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
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;

class GeocodingResource extends Resource
{
use EntityTrait;
use ValidationTrait;

private const NUM_RESULTS = 5;

Expand Down
2 changes: 2 additions & 0 deletions src/Resource/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
}
14 changes: 7 additions & 7 deletions src/Resource/Util/ValidationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ 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);
}

/**
* @throws ValidationException
*/
private function validatePositive(int $number, string $name): void
protected function validatePositive(int $number, string $name): void
{
Validator::greaterThan(0)->assert($number, $name);
}

/**
* @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');
Expand All @@ -37,31 +37,31 @@ 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');
}

/**
* @throws ValidationException
*/
private function validateLanguage(string $language): void
protected function validateLanguage(string $language): void
{
Validator::choice(Language::getOptions())->assert($language, 'language');
}

/**
* @throws ValidationException
*/
private function validateUnitSystem(string $unitSystem): void
protected function validateUnitSystem(string $unitSystem): void
{
Validator::choice(UnitSystem::getOptions())->assert($unitSystem, 'unitSystem');
}

/**
* @throws ValidationException
*/
private function validateDateOrder(\DateTimeInterface $startDate, \DateTimeInterface $endDate): void
protected function validateDateOrder(\DateTimeInterface $startDate, \DateTimeInterface $endDate): void
{
Validator::greaterThan(
constraint: $startDate,
Expand Down
2 changes: 0 additions & 2 deletions src/Resource/WeatherResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@
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;

class WeatherResource extends Resource
{
use LanguageTrait;
use UnitSystemTrait;
use ValidationTrait;

private const NUM_RESULTS = 40;

Expand Down
42 changes: 42 additions & 0 deletions tests/Unit/AirPollution/AirPollutionCollectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace ProgrammatorDev\OpenWeatherMap\Test\Unit\AirPollution;

use ProgrammatorDev\OpenWeatherMap\Entity\AirPollution\AirPollutionCollection;
use ProgrammatorDev\OpenWeatherMap\Entity\AirPollution\AirPollutionData;
use ProgrammatorDev\OpenWeatherMap\Entity\Coordinate;
use ProgrammatorDev\OpenWeatherMap\Test\AbstractTest;

class AirPollutionCollectionTest extends AbstractTest
{
public function testMethods()
{
$entity = new AirPollutionCollection([
'coord' => [
'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());
}
}
41 changes: 41 additions & 0 deletions tests/Unit/AirPollution/AirPollutionDataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace ProgrammatorDev\OpenWeatherMap\Test\Unit\AirPollution;

use ProgrammatorDev\OpenWeatherMap\Entity\AirPollution\AirPollutionData;
use ProgrammatorDev\OpenWeatherMap\Entity\AirPollution\AirQuality;
use ProgrammatorDev\OpenWeatherMap\Test\AbstractTest;

class AirPollutionDataTest extends AbstractTest
{
public function testMethods()
{
$entity = new AirPollutionData([
'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(\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());
}
}
51 changes: 51 additions & 0 deletions tests/Unit/AirPollution/AirPollutionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace ProgrammatorDev\OpenWeatherMap\Test\Unit\AirPollution;

use ProgrammatorDev\OpenWeatherMap\Entity\AirPollution\AirPollution;
use ProgrammatorDev\OpenWeatherMap\Entity\AirPollution\AirQuality;
use ProgrammatorDev\OpenWeatherMap\Entity\Coordinate;
use ProgrammatorDev\OpenWeatherMap\Test\AbstractTest;

class AirPollutionTest extends AbstractTest
{
public function testMethods()
{
$entity = new AirPollution([
'coord' => [
'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());
}
}
19 changes: 19 additions & 0 deletions tests/Unit/AirPollution/AirQualityTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace ProgrammatorDev\OpenWeatherMap\Test\Unit\AirPollution;

use ProgrammatorDev\OpenWeatherMap\Entity\AirPollution\AirQuality;
use ProgrammatorDev\OpenWeatherMap\Test\AbstractTest;

class AirQualityTest extends AbstractTest
{
public function testMethods()
{
$entity = new AirQuality([
'aqi' => 1
]);

$this->assertSame(1, $entity->getIndex());
$this->assertSame('Good', $entity->getQualitativeName());
}
}

0 comments on commit e35e1e2

Please sign in to comment.