Skip to content

Commit fe0a33d

Browse files
committed
chore: refactored OneCallEndpoint tests
1 parent 7d61c15 commit fe0a33d

File tree

2 files changed

+63
-166
lines changed

2 files changed

+63
-166
lines changed

tests/AirPollutionEndpointTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public static function provideEndpointInvalidResponseData(): \Generator
6565
yield 'get history, longitude greater than 180' => ['airPollution', 'getHistory',
6666
[50, 181, new \DateTime('yesterday'), new \DateTime('today')]
6767
];
68-
yield 'get history, future end date' => ['airPollution', 'getHistory',
68+
yield 'get history, invalid past end date' => ['airPollution', 'getHistory',
6969
[50, 50, new \DateTime('yesterday'), new \DateTime('tomorrow')]
7070
];
7171
yield 'get history, end date before start date' => ['airPollution', 'getHistory',

tests/OneCallEndpointTest.php

Lines changed: 62 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace ProgrammatorDev\OpenWeatherMap\Test;
44

5-
use Nyholm\Psr7\Response;
6-
use PHPUnit\Framework\Attributes\DataProviderExternal;
75
use ProgrammatorDev\OpenWeatherMap\Endpoint\OneCallEndpoint;
86
use ProgrammatorDev\OpenWeatherMap\Entity\Coordinate;
97
use ProgrammatorDev\OpenWeatherMap\Entity\Icon;
@@ -18,121 +16,68 @@
1816
use ProgrammatorDev\OpenWeatherMap\Entity\Timezone;
1917
use ProgrammatorDev\OpenWeatherMap\Entity\WeatherCondition;
2018
use ProgrammatorDev\OpenWeatherMap\Entity\Wind;
21-
use ProgrammatorDev\OpenWeatherMap\Test\DataProvider\InvalidParamDataProvider;
19+
use ProgrammatorDev\OpenWeatherMap\Test\Util\TestEndpointInvalidResponseTrait;
20+
use ProgrammatorDev\OpenWeatherMap\Test\Util\TestEndpointSuccessResponseTrait;
2221

2322
class OneCallEndpointTest extends AbstractTest
2423
{
25-
// --- WEATHER ---
24+
use TestEndpointSuccessResponseTrait;
25+
use TestEndpointInvalidResponseTrait;
2626

27-
public function testOneCallGetWeather()
27+
public static function provideEndpointSuccessResponseData(): \Generator
2828
{
29-
$this->mockHttpClient->addResponse(
30-
new Response(
31-
status: 200,
32-
body: MockResponse::ONE_CALL_WEATHER
33-
)
34-
);
35-
36-
$response = $this->givenApi()->oneCall()->getWeather(50, 50);
37-
$this->assertWeatherResponse($response);
29+
yield 'get weather' => [
30+
MockResponse::ONE_CALL_WEATHER,
31+
'oneCall',
32+
'getWeather',
33+
[50, 50],
34+
'assertGetWeatherResponse'
35+
];
36+
yield 'get history moment' => [
37+
MockResponse::ONE_CALL_HISTORY_MOMENT,
38+
'oneCall',
39+
'getHistoryMoment',
40+
[50, 50, new \DateTime('yesterday')],
41+
'assertGetHistoryMomentResponse'
42+
];
43+
yield 'get history aggregate' => [
44+
MockResponse::ONE_CALL_HISTORY_AGGREGATE,
45+
'oneCall',
46+
'getHistoryAggregate',
47+
[50, 50, new \DateTime('yesterday')],
48+
'assertGetHistoryAggregateResponse'
49+
];
3850
}
3951

40-
#[DataProviderExternal(InvalidParamDataProvider::class, 'provideInvalidCoordinateData')]
41-
public function testOneCallGetWeatherWithInvalidCoordinate(float $latitude, float $longitude, string $expectedException)
52+
public static function provideEndpointInvalidResponseData(): \Generator
4253
{
43-
$this->expectException($expectedException);
44-
$this->givenApi()->oneCall()->getWeather($latitude, $longitude);
54+
yield 'get weather, latitude lower than -90' => ['oneCall', 'getWeather', [-91, 50]];
55+
yield 'get weather, latitude greater than 90' => ['oneCall', 'getWeather', [91, 50]];
56+
yield 'get weather, longitude lower than -180' => ['oneCall', 'getWeather', [50, -181]];
57+
yield 'get weather, longitude greater than 180' => ['oneCall', 'getWeather', [50, 181]];
58+
59+
yield 'get history moment, latitude lower than -90' => ['oneCall', 'getHistoryMoment', [-91, 50, new \DateTime('yesterday')]];
60+
yield 'get history moment, latitude greater than 90' => ['oneCall', 'getHistoryMoment', [91, 50, new \DateTime('yesterday')]];
61+
yield 'get history moment, longitude lower than -180' => ['oneCall', 'getHistoryMoment', [50, -181, new \DateTime('yesterday')]];
62+
yield 'get history moment, longitude greater than 180' => ['oneCall', 'getHistoryMoment', [50, 181, new \DateTime('yesterday')]];
63+
yield 'get history moment, invalid past date' => ['oneCall', 'getHistoryMoment', [50, 50, new \DateTime('tomorrow')]];
64+
65+
yield 'get history aggregate, latitude lower than -90' => ['oneCall', 'getHistoryAggregate', [-91, 50, new \DateTime('yesterday')]];
66+
yield 'get history aggregate, latitude greater than 90' => ['oneCall', 'getHistoryAggregate', [91, 50, new \DateTime('yesterday')]];
67+
yield 'get history aggregate, longitude lower than -180' => ['oneCall', 'getHistoryAggregate', [50, -181, new \DateTime('yesterday')]];
68+
yield 'get history aggregate, longitude greater than 180' => ['oneCall', 'getHistoryAggregate', [50, 181, new \DateTime('yesterday')]];
69+
yield 'get history aggregate, invalid past date' => ['oneCall', 'getHistoryAggregate', [50, 50, new \DateTime('tomorrow')]];
4570
}
4671

47-
// --- HISTORY MOMENT ---
48-
49-
public function testOneCallGetHistoryMoment()
50-
{
51-
$this->mockHttpClient->addResponse(
52-
new Response(
53-
status: 200,
54-
body: MockResponse::ONE_CALL_HISTORY_MOMENT
55-
)
56-
);
57-
58-
$response = $this->givenApi()->oneCall()->getHistoryMoment(
59-
50,
60-
50,
61-
new \DateTimeImmutable('2023-01-01 00:00:00')
62-
);
63-
$this->assertHistoryMomentResponse($response);
64-
}
65-
66-
#[DataProviderExternal(InvalidParamDataProvider::class, 'provideInvalidCoordinateData')]
67-
public function testOneCallGetHistoryMomentWithInvalidCoordinate(float $latitude, float $longitude, string $expectedException)
68-
{
69-
$this->expectException($expectedException);
70-
$this->givenApi()->oneCall()->getHistoryMoment(
71-
$latitude,
72-
$longitude,
73-
new \DateTimeImmutable('2023-01-01 00:00:00')
74-
);
75-
}
76-
77-
#[DataProviderExternal(InvalidParamDataProvider::class, 'provideInvalidPastDateData')]
78-
public function testOneCallGetHistoryMomentWithInvalidPastDate(\DateTimeImmutable $date, string $expectedException)
79-
{
80-
$this->expectException($expectedException);
81-
$this->givenApi()->oneCall()->getHistoryMoment(50, 50, $date);
82-
}
83-
84-
// --- HISTORY AGGREGATE ---
85-
86-
public function testOneCallGetHistoryAggregate()
87-
{
88-
$this->mockHttpClient->addResponse(
89-
new Response(
90-
status: 200,
91-
body: MockResponse::ONE_CALL_HISTORY_AGGREGATE
92-
)
93-
);
94-
95-
$response = $this->givenApi()->oneCall()->getHistoryAggregate(
96-
50,
97-
50,
98-
new \DateTimeImmutable('2023-01-01')
99-
);
100-
$this->assertHistoryAggregateResponse($response);
101-
}
102-
103-
#[DataProviderExternal(InvalidParamDataProvider::class, 'provideInvalidCoordinateData')]
104-
public function testOneCallGetHistoryAggregateWithInvalidCoordinate(float $latitude, float $longitude, string $expectedException)
105-
{
106-
$this->expectException($expectedException);
107-
$this->givenApi()->oneCall()->getHistoryAggregate(
108-
$latitude,
109-
$longitude,
110-
new \DateTimeImmutable('2023-01-01')
111-
);
112-
}
113-
114-
#[DataProviderExternal(InvalidParamDataProvider::class, 'provideInvalidPastDateData')]
115-
public function testOneCallGetHistoryAggregateWithInvalidPastDate(\DateTimeImmutable $date, string $expectedException)
116-
{
117-
$this->expectException($expectedException);
118-
$this->givenApi()->oneCall()->getHistoryAggregate(50, 50, $date);
119-
}
120-
121-
// --- ASSERT METHODS EXIST ---
122-
12372
public function testOneCallMethodsExist()
12473
{
12574
$this->assertSame(true, method_exists(OneCallEndpoint::class, 'withUnitSystem'));
12675
$this->assertSame(true, method_exists(OneCallEndpoint::class, 'withLanguage'));
12776
$this->assertSame(true, method_exists(OneCallEndpoint::class, 'withCacheTtl'));
12877
}
12978

130-
// --- ASSERT RESPONSES ---
131-
132-
private function assertWeatherResponse(OneCall $response): void
79+
private function assertGetWeatherResponse(OneCall $response): void
13380
{
134-
$this->assertInstanceOf(OneCall::class, $response);
135-
13681
// Coordinate
13782
$coordinate = $response->getCoordinate();
13883
$this->assertInstanceOf(Coordinate::class, $coordinate);
@@ -163,6 +108,9 @@ private function assertWeatherResponse(OneCall $response): void
163108
$this->assertSame(null, $current->getPrecipitationProbability());
164109
$this->assertSame(null, $current->getRain());
165110
$this->assertSame(null, $current->getSnow());
111+
$this->assertSame('2023-07-03 11:35:39', $current->getDateTime()->format('Y-m-d H:i:s'));
112+
$this->assertSame('2023-07-03 05:16:08', $current->getSunriseAt()->format('Y-m-d H:i:s'));
113+
$this->assertSame('2023-07-03 20:04:57', $current->getSunsetAt()->format('Y-m-d H:i:s'));
166114

167115
$currentWind = $current->getWind();
168116
$this->assertInstanceOf(Wind::class, $currentWind);
@@ -182,26 +130,11 @@ private function assertWeatherResponse(OneCall $response): void
182130
$this->assertSame('02d', $currentWeatherConditionsIcon->getId());
183131
$this->assertSame('https://openweathermap.org/img/wn/02d@4x.png', $currentWeatherConditionsIcon->getImageUrl());
184132

185-
$currentDateTime = $current->getDateTime();
186-
$this->assertInstanceOf(\DateTimeImmutable::class, $currentDateTime);
187-
$this->assertSame('2023-07-03 11:35:39', $currentDateTime->format('Y-m-d H:i:s'));
188-
189-
$currentSunriseAt = $current->getSunriseAt();
190-
$this->assertInstanceOf(\DateTimeImmutable::class, $currentSunriseAt);
191-
$this->assertSame('2023-07-03 05:16:08', $currentSunriseAt->format('Y-m-d H:i:s'));
192-
193-
$currentSunsetAt = $current->getSunsetAt();
194-
$this->assertInstanceOf(\DateTimeImmutable::class, $currentSunsetAt);
195-
$this->assertSame('2023-07-03 20:04:57', $currentSunsetAt->format('Y-m-d H:i:s'));
196-
197133
// MinutelyForecast
198134
$minutelyForecast = $response->getMinutelyForecast();
199135
$this->assertContainsOnlyInstancesOf(MinuteForecast::class, $minutelyForecast);
200136
$this->assertSame(0.0, $minutelyForecast[0]->getPrecipitation());
201-
202-
$minutelyForecastDateTime = $minutelyForecast[0]->getDateTime();
203-
$this->assertInstanceOf(\DateTimeImmutable::class, $minutelyForecastDateTime);
204-
$this->assertSame('2023-07-03 11:36:00', $minutelyForecastDateTime->format('Y-m-d H:i:s'));
137+
$this->assertSame('2023-07-03 11:36:00', $minutelyForecast[0]->getDateTime()->format('Y-m-d H:i:s'));
205138

206139
// HourlyForecast
207140
$hourlyForecast = $response->getHourlyForecast();
@@ -223,6 +156,7 @@ private function assertWeatherResponse(OneCall $response): void
223156
$this->assertSame(0, $hourlyForecast[0]->getPrecipitationProbability());
224157
$this->assertSame(null, $hourlyForecast[0]->getRain());
225158
$this->assertSame(null, $hourlyForecast[0]->getSnow());
159+
$this->assertSame('2023-07-03 11:00:00', $hourlyForecast[0]->getDateTime()->format('Y-m-d H:i:s'));
226160

227161
$hourlyForecastWind = $hourlyForecast[0]->getWind();
228162
$this->assertInstanceOf(Wind::class, $hourlyForecastWind);
@@ -242,10 +176,6 @@ private function assertWeatherResponse(OneCall $response): void
242176
$this->assertSame('02d', $hourlyForecastWeatherConditionsIcon->getId());
243177
$this->assertSame('https://openweathermap.org/img/wn/02d@4x.png', $hourlyForecastWeatherConditionsIcon->getImageUrl());
244178

245-
$hourlyForecastDateTIme = $hourlyForecast[0]->getDateTime();
246-
$this->assertInstanceOf(\DateTimeImmutable::class, $hourlyForecastDateTIme);
247-
$this->assertSame('2023-07-03 11:00:00', $hourlyForecastDateTIme->format('Y-m-d H:i:s'));
248-
249179
// DailyForecast
250180
$dailyForecast = $response->getDailyForecast();
251181
$this->assertContainsOnlyInstancesOf(Weather::class, $dailyForecast);
@@ -259,6 +189,11 @@ private function assertWeatherResponse(OneCall $response): void
259189
$this->assertSame(0, $dailyForecast[0]->getPrecipitationProbability());
260190
$this->assertSame(null, $dailyForecast[0]->getRain());
261191
$this->assertSame(null, $dailyForecast[0]->getSnow());
192+
$this->assertSame('2023-07-03 12:00:00', $dailyForecast[0]->getDateTime()->format('Y-m-d H:i:s'));
193+
$this->assertSame('2023-07-03 05:16:08', $dailyForecast[0]->getSunriseAt()->format('Y-m-d H:i:s'));
194+
$this->assertSame('2023-07-03 20:04:57', $dailyForecast[0]->getSunsetAt()->format('Y-m-d H:i:s'));
195+
$this->assertSame('2023-07-03 20:45:00', $dailyForecast[0]->getMoonriseAt()->format('Y-m-d H:i:s'));
196+
$this->assertSame('2023-07-03 04:44:00', $dailyForecast[0]->getMoonsetAt()->format('Y-m-d H:i:s'));
262197

263198
$dailyForecastTemperature = $dailyForecast[0]->getTemperature();
264199
$this->assertInstanceOf(Temperature::class, $dailyForecastTemperature);
@@ -296,26 +231,6 @@ private function assertWeatherResponse(OneCall $response): void
296231
$this->assertSame('02d', $dailyForecastWeatherConditionsIcon->getId());
297232
$this->assertSame('https://openweathermap.org/img/wn/02d@4x.png', $dailyForecastWeatherConditionsIcon->getImageUrl());
298233

299-
$dailyForecastDateTime = $dailyForecast[0]->getDateTime();
300-
$this->assertInstanceOf(\DateTimeImmutable::class, $dailyForecastDateTime);
301-
$this->assertSame('2023-07-03 12:00:00', $dailyForecastDateTime->format('Y-m-d H:i:s'));
302-
303-
$dailyForecastSunriseAt = $dailyForecast[0]->getSunriseAt();
304-
$this->assertInstanceOf(\DateTimeImmutable::class, $dailyForecastSunriseAt);
305-
$this->assertSame('2023-07-03 05:16:08', $dailyForecastSunriseAt->format('Y-m-d H:i:s'));
306-
307-
$dailyForecastSunsetAt = $dailyForecast[0]->getSunsetAt();
308-
$this->assertInstanceOf(\DateTimeImmutable::class, $dailyForecastSunsetAt);
309-
$this->assertSame('2023-07-03 20:04:57', $dailyForecastSunsetAt->format('Y-m-d H:i:s'));
310-
311-
$dailyForecastMoonriseAt = $dailyForecast[0]->getMoonriseAt();
312-
$this->assertInstanceOf(\DateTimeImmutable::class, $dailyForecastMoonriseAt);
313-
$this->assertSame('2023-07-03 20:45:00', $dailyForecastMoonriseAt->format('Y-m-d H:i:s'));
314-
315-
$dailyForecastMoonsetAt = $dailyForecast[0]->getMoonsetAt();
316-
$this->assertInstanceOf(\DateTimeImmutable::class, $dailyForecastMoonsetAt);
317-
$this->assertSame('2023-07-03 04:44:00', $dailyForecastMoonsetAt->format('Y-m-d H:i:s'));
318-
319234
$dailyForecastMoonPhase = $dailyForecast[0]->getMoonPhase();
320235
$this->assertInstanceOf(MoonPhase::class, $dailyForecastMoonPhase);
321236
$this->assertSame(0.5, $dailyForecastMoonPhase->getValue());
@@ -329,17 +244,11 @@ private function assertWeatherResponse(OneCall $response): void
329244
$this->assertSame('Heat Advisory', $alerts[0]->getEventName());
330245
$this->assertStringStartsWith('...HEAT ADVISORY REMAINS', $alerts[0]->getDescription());
331246
$this->assertIsArray($alerts[0]->getTags());
332-
333-
$alertsStartsAt = $alerts[0]->getStartsAt();
334-
$this->assertInstanceOf(\DateTimeImmutable::class, $alertsStartsAt);
335-
$this->assertSame('2023-07-04 17:00:00', $alertsStartsAt->format('Y-m-d H:i:s'));
336-
337-
$alertsEndsAt = $alerts[0]->getEndsAt();
338-
$this->assertInstanceOf(\DateTimeImmutable::class, $alertsEndsAt);
339-
$this->assertSame('2023-07-06 06:00:00', $alertsEndsAt->format('Y-m-d H:i:s'));
247+
$this->assertSame('2023-07-04 17:00:00', $alerts[0]->getStartsAt()->format('Y-m-d H:i:s'));
248+
$this->assertSame('2023-07-06 06:00:00', $alerts[0]->getEndsAt()->format('Y-m-d H:i:s'));
340249
}
341250

342-
private function assertHistoryMomentResponse(WeatherLocation $response): void
251+
private function assertGetHistoryMomentResponse(WeatherLocation $response): void
343252
{
344253
$this->assertInstanceOf(WeatherLocation::class, $response);
345254

@@ -358,6 +267,9 @@ private function assertHistoryMomentResponse(WeatherLocation $response): void
358267
$this->assertSame(null, $response->getPrecipitationProbability());
359268
$this->assertSame(null, $response->getRain());
360269
$this->assertSame(null, $response->getSnow());
270+
$this->assertSame('2023-01-01 00:00:00', $response->getDateTime()->format('Y-m-d H:i:s'));
271+
$this->assertSame('2023-01-01 07:54:31', $response->getSunriseAt()->format('Y-m-d H:i:s'));
272+
$this->assertSame('2023-01-01 17:25:02', $response->getSunsetAt()->format('Y-m-d H:i:s'));
361273

362274
$wind = $response->getWind();
363275
$this->assertInstanceOf(Wind::class, $wind);
@@ -377,18 +289,6 @@ private function assertHistoryMomentResponse(WeatherLocation $response): void
377289
$this->assertSame('02n', $weatherConditionsIcon->getId());
378290
$this->assertSame('https://openweathermap.org/img/wn/02n@4x.png', $weatherConditionsIcon->getImageUrl());
379291

380-
$dateTime = $response->getDateTime();
381-
$this->assertInstanceOf(\DateTimeImmutable::class, $dateTime);
382-
$this->assertSame('2023-01-01 00:00:00', $dateTime->format('Y-m-d H:i:s'));
383-
384-
$sunriseAt = $response->getSunriseAt();
385-
$this->assertInstanceOf(\DateTimeImmutable::class, $sunriseAt);
386-
$this->assertSame('2023-01-01 07:54:31', $sunriseAt->format('Y-m-d H:i:s'));
387-
388-
$sunsetAt = $response->getSunsetAt();
389-
$this->assertInstanceOf(\DateTimeImmutable::class, $sunsetAt);
390-
$this->assertSame('2023-01-01 17:25:02', $sunsetAt->format('Y-m-d H:i:s'));
391-
392292
$coordinate = $response->getCoordinate();
393293
$this->assertInstanceOf(Coordinate::class, $coordinate);
394294
$this->assertSame(38.7078, $coordinate->getLatitude());
@@ -400,14 +300,15 @@ private function assertHistoryMomentResponse(WeatherLocation $response): void
400300
$this->assertSame(0, $timezone->getOffset());
401301
}
402302

403-
private function assertHistoryAggregateResponse(WeatherAggregate $response): void
303+
private function assertGetHistoryAggregateResponse(WeatherAggregate $response): void
404304
{
405305
$this->assertInstanceOf(WeatherAggregate::class, $response);
406306

407307
$this->assertSame(75, $response->getCloudiness());
408308
$this->assertSame(71, $response->getHumidity());
409309
$this->assertSame(2.53, $response->getPrecipitation());
410310
$this->assertSame(1017, $response->getAtmosphericPressure());
311+
$this->assertSame('2023-01-01 00:00:00', $response->getDateTime()->format('Y-m-d H:i:s'));
411312

412313
$coordinate = $response->getCoordinate();
413314
$this->assertInstanceOf(Coordinate::class, $coordinate);
@@ -419,10 +320,6 @@ private function assertHistoryAggregateResponse(WeatherAggregate $response): voi
419320
$this->assertSame(null, $timezone->getIdentifier());
420321
$this->assertSame(0, $timezone->getOffset());
421322

422-
$dateTime = $response->getDateTime();
423-
$this->assertInstanceOf(\DateTimeImmutable::class, $dateTime);
424-
$this->assertSame('2023-01-01 00:00:00', $dateTime->format('Y-m-d H:i:s'));
425-
426323
$temperature = $response->getTemperature();
427324
$this->assertInstanceOf(Temperature::class, $temperature);
428325
$this->assertSame(17.23, $temperature->getMorning());

0 commit comments

Comments
 (0)