Skip to content

Commit

Permalink
tests: added one call resource tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andrepimpao committed May 13, 2024
1 parent 898b88b commit 724c5c0
Show file tree
Hide file tree
Showing 15 changed files with 125 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/Entity/AirPollution/AirPollutionData.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public function __construct(array $data)
$this->ammonia = $data['components']['nh3'];
}

/**
* DateTime in UTC
*/
public function getDateTime(): \DateTimeImmutable
{
return $this->dateTime;
Expand Down
6 changes: 6 additions & 0 deletions src/Entity/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,17 @@ public function getTimezone(): ?Timezone
return $this->timezone;
}

/**
* Sunrise date in UTC
*/
public function getSunriseAt(): ?\DateTimeImmutable
{
return $this->sunriseAt;
}

/**
* Sunset date in UTC
*/
public function getSunsetAt(): ?\DateTimeImmutable
{
return $this->sunsetAt;
Expand Down
3 changes: 3 additions & 0 deletions src/Entity/OneCall/BaseWeather.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public function __construct(array $data)
$this->snowVolume = $data['snow']['1h'] ?? $data['snow']['3h'] ?? $data['snow'] ?? null;
}

/**
* DateTime in UTC
*/
public function getDateTime(): \DateTimeImmutable
{
return $this->dateTime;
Expand Down
12 changes: 12 additions & 0 deletions src/Entity/OneCall/DayData.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,33 @@ public function getMoonPhase(): MoonPhase
return $this->moonPhase;
}

/**
* Moonrise date in UTC
*/
public function getMoonriseAt(): \DateTimeImmutable
{
return $this->moonriseAt;
}

/**
* Moonset date in UTC
*/
public function getMoonsetAt(): \DateTimeImmutable
{
return $this->moonsetAt;
}

/**
* Sunrise date in UTC
*/
public function getSunriseAt(): \DateTimeImmutable
{
return $this->sunriseAt;
}

/**
* Sunset date in UTC
*/
public function getSunsetAt(): \DateTimeImmutable
{
return $this->sunsetAt;
Expand Down
3 changes: 3 additions & 0 deletions src/Entity/OneCall/MinuteData.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public function __construct(array $data)
$this->precipitation = $data['precipitation'];
}

/**
* DateTime in UTC
*/
public function getDateTime(): \DateTimeImmutable
{
return $this->dateTime;
Expand Down
6 changes: 6 additions & 0 deletions src/Entity/OneCall/WeatherData.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,17 @@ public function getVisibility(): ?int
return $this->visibility;
}

/**
* Sunrise date in UTC
*/
public function getSunriseAt(): ?\DateTimeImmutable
{
return $this->sunriseAt;
}

/**
* Sunset date in UTC
*/
public function getSunsetAt(): ?\DateTimeImmutable
{
return $this->sunsetAt;
Expand Down
3 changes: 3 additions & 0 deletions src/Entity/OneCall/WeatherSummary.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ public function getTimezone(): Timezone
return $this->timezone;
}

/**
* DateTime in UTC
*/
public function getDateTime(): \DateTimeImmutable
{
return $this->dateTime;
Expand Down
3 changes: 3 additions & 0 deletions src/Entity/Weather/WeatherData.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public function __construct(array $data)
$this->snowVolume = $data['snow']['1h'] ?? $data['snow']['3h'] ?? null;
}

/**
* DateTime in UTC
*/
public function getDateTime(): \DateTimeImmutable
{
return $this->dateTime;
Expand Down
6 changes: 6 additions & 0 deletions src/Resource/AirPollutionResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
class AirPollutionResource extends Resource
{
/**
* Get access to current air pollution data
*
* @throws ValidationException
* @throws ClientExceptionInterface
*/
Expand All @@ -31,6 +33,8 @@ public function getCurrent(float $latitude, float $longitude): AirPollution
}

/**
* Get access to air pollution forecast data
*
* @throws ValidationException
* @throws ClientExceptionInterface
*/
Expand All @@ -51,6 +55,8 @@ public function getForecast(float $latitude, float $longitude): AirPollutionColl
}

/**
* Get access to historical air pollution data
*
* @throws ValidationException
* @throws ClientExceptionInterface
*/
Expand Down
6 changes: 6 additions & 0 deletions src/Resource/GeocodingResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class GeocodingResource extends Resource
private const NUM_RESULTS = 5;

/**
* Get geographical coordinates (latitude, longitude) by using the name of the location (city name or area name)
*
* @return Location[]
* @throws ValidationException
* @throws ClientExceptionInterface
Expand All @@ -38,6 +40,8 @@ public function getByLocationName(string $locationName, int $numResults = self::
}

/**
* Get geographical coordinates (latitude, longitude) by using the zip/postal code
*
* @throws ValidationException
* @throws ClientExceptionInterface
*/
Expand All @@ -58,6 +62,8 @@ public function getByZipCode(string $zipCode, string $countryCode): ZipLocation
}

/**
* Get name of the location (city name or area name) by using geographical coordinates (latitude, longitude)
*
* @return Location[]
* @throws ValidationException
* @throws ClientExceptionInterface
Expand Down
10 changes: 8 additions & 2 deletions src/Resource/OneCallResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use ProgrammatorDev\Api\Method;
use ProgrammatorDev\OpenWeatherMap\Entity\OneCall\Weather;
use ProgrammatorDev\OpenWeatherMap\Entity\OneCall\WeatherData;
use ProgrammatorDev\OpenWeatherMap\Entity\OneCall\WeatherMoment;
use ProgrammatorDev\OpenWeatherMap\Entity\OneCall\WeatherSummary;
use ProgrammatorDev\OpenWeatherMap\Resource\Util\LanguageTrait;
Expand All @@ -18,6 +17,9 @@ class OneCallResource extends Resource
use UnitSystemTrait;

/**
* Get access to current weather, minute forecast for 1 hour, hourly forecast for 48 hours,
* daily forecast for 8 days and government weather alerts
*
* @throws ValidationException
* @throws ClientExceptionInterface
*/
Expand All @@ -38,10 +40,12 @@ public function getWeather(float $latitude, float $longitude): Weather
}

/**
* Get access to weather data for any datetime
*
* @throws ValidationException
* @throws ClientExceptionInterface
*/
public function getWeatherByDateTime(float $latitude, float $longitude, \DateTimeInterface $dateTime): WeatherMoment
public function getWeatherByDate(float $latitude, float $longitude, \DateTimeInterface $dateTime): WeatherMoment
{
$this->validateCoordinate($latitude, $longitude);

Expand All @@ -61,6 +65,8 @@ public function getWeatherByDateTime(float $latitude, float $longitude, \DateTim
}

/**
* Get access to aggregated weather data for a particular date
*
* @throws ValidationException
* @throws ClientExceptionInterface
*/
Expand Down
4 changes: 4 additions & 0 deletions src/Resource/WeatherResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class WeatherResource extends Resource
private const NUM_RESULTS = 40;

/**
* Get access to current weather data
*
* @throws ValidationException
* @throws ClientExceptionInterface
*/
Expand All @@ -38,6 +40,8 @@ public function getCurrent(float $latitude, float $longitude): Weather
}

/**
* Get access to 5-day weather forecast data with 3-hour steps
*
* @throws ValidationException
* @throws ClientExceptionInterface
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Test/MockResponse.php

Large diffs are not rendered by default.

58 changes: 58 additions & 0 deletions tests/Integration/OneCallResourceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace ProgrammatorDev\OpenWeatherMap\Test\Integration;

use ProgrammatorDev\OpenWeatherMap\Entity\OneCall\Weather;
use ProgrammatorDev\OpenWeatherMap\Entity\OneCall\WeatherMoment;
use ProgrammatorDev\OpenWeatherMap\Entity\OneCall\WeatherSummary;
use ProgrammatorDev\OpenWeatherMap\Test\AbstractTest;
use ProgrammatorDev\OpenWeatherMap\Test\MockResponse;
use ProgrammatorDev\OpenWeatherMap\Test\Util\TestValidationExceptionTrait;
use ProgrammatorDev\OpenWeatherMap\Test\Util\TestItemResponseTrait;

class OneCallResourceTest extends AbstractTest
{
use TestItemResponseTrait;
use TestValidationExceptionTrait;

public static function provideItemResponseData(): \Generator
{
yield 'get weather' => [
Weather::class,
MockResponse::ONE_CALL_WEATHER,
'oneCall',
'getWeather',
[50, 50]
];
yield 'get weather by date' => [
WeatherMoment::class,
MockResponse::ONE_CALL_TIMEMACHINE,
'oneCall',
'getWeatherByDate',
[50, 50, new \DateTime()]
];
yield 'get weather summary by date' => [
WeatherSummary::class,
MockResponse::ONE_CALL_DAY_SUMMARY,
'oneCall',
'getWeatherSummaryByDate',
[50, 50, new \DateTime()]
];
}

public static function provideValidationExceptionData(): \Generator
{
yield 'get weather, latitude lower than -90' => ['oneCall', 'getWeather', [-91, 50]];
yield 'get weather, latitude greater than 90' => ['oneCall', 'getWeather', [91, 50]];
yield 'get weather, longitude lower than -180' => ['oneCall', 'getWeather', [50, -181]];
yield 'get weather, longitude greater than 180' => ['oneCall', 'getWeather', [50, 181]];
yield 'get weather by date, latitude lower than -90' => ['oneCall', 'getWeatherByDate', [-91, 50, new \DateTime()]];
yield 'get weather by date, latitude greater than 90' => ['oneCall', 'getWeatherByDate', [91, 50, new \DateTime()]];
yield 'get weather by date, longitude lower than -180' => ['oneCall', 'getWeatherByDate', [50, -181, new \DateTime()]];
yield 'get weather by date, longitude greater than 180' => ['oneCall', 'getWeatherByDate', [50, 181, new \DateTime()]];
yield 'get weather summary by date, latitude lower than -90' => ['oneCall', 'getWeatherSummaryByDate', [-91, 50, new \DateTime()]];
yield 'get weather summary by date, latitude greater than 90' => ['oneCall', 'getWeatherSummaryByDate', [91, 50, new \DateTime()]];
yield 'get weather summary by date, longitude lower than -180' => ['oneCall', 'getWeatherSummaryByDate', [50, -181, new \DateTime()]];
yield 'get weather summary by date, longitude greater than 180' => ['oneCall', 'getWeatherSummaryByDate', [50, 181, new \DateTime()]];
}
}
2 changes: 2 additions & 0 deletions tests/Integration/OpenWeatherMapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

use ProgrammatorDev\OpenWeatherMap\Resource\AirPollutionResource;
use ProgrammatorDev\OpenWeatherMap\Resource\GeocodingResource;
use ProgrammatorDev\OpenWeatherMap\Resource\OneCallResource;
use ProgrammatorDev\OpenWeatherMap\Resource\WeatherResource;
use ProgrammatorDev\OpenWeatherMap\Test\AbstractTest;

class OpenWeatherMapTest extends AbstractTest
{
public function testMethods()
{
$this->assertInstanceOf(OneCallResource::class, $this->api->oneCall());
$this->assertInstanceOf(WeatherResource::class, $this->api->weather());
$this->assertInstanceOf(AirPollutionResource::class, $this->api->airPollution());
$this->assertInstanceOf(GeocodingResource::class, $this->api->geocoding());
Expand Down

0 comments on commit 724c5c0

Please sign in to comment.