Skip to content

Commit a702e41

Browse files
authored
Merge pull request #54 from programmatordev/OPA-51-refactor-tests
Refactor tests
2 parents 91e6c6f + b2357a6 commit a702e41

9 files changed

+365
-614
lines changed

src/Endpoint/Util/ValidationTrait.php

+13-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,19 @@ private function validateCoordinate(float $latitude, float $longitude): void
2121
*/
2222
private function validateDateRange(\DateTimeInterface $startDate, \DateTimeInterface $endDate): void
2323
{
24-
Validator::lessThan(new \DateTime('now'))->assert($endDate, 'endDate');
25-
Validator::greaterThan($startDate)->assert($endDate, 'endDate');
24+
$nowDate = new \DateTime('now');
25+
$nowDate->setTime($nowDate->format('H'), 0);
26+
27+
// Start date must be less or equal to end date
28+
Validator::lessThanOrEqual(
29+
constraint: $endDate,
30+
message: 'The "{{ name }}" value should be less than or equal to the "endDate"'
31+
)->assert($startDate, 'startDate');
32+
33+
// End date must be less or equal to today date
34+
Validator::lessThanOrEqual(
35+
constraint: $nowDate
36+
)->assert($endDate, 'endDate');
2637
}
2738

2839
/**

tests/AirPollutionEndpointTest.php

+76-156
Original file line numberDiff line numberDiff line change
@@ -2,187 +2,116 @@
22

33
namespace ProgrammatorDev\OpenWeatherMap\Test;
44

5-
use Nyholm\Psr7\Response;
6-
use PHPUnit\Framework\Attributes\DataProviderExternal;
75
use ProgrammatorDev\OpenWeatherMap\Endpoint\AirPollutionEndpoint;
86
use ProgrammatorDev\OpenWeatherMap\Entity\AirPollution\AirPollution;
97
use ProgrammatorDev\OpenWeatherMap\Entity\AirPollution\AirPollutionLocationList;
108
use ProgrammatorDev\OpenWeatherMap\Entity\AirPollution\AirQuality;
119
use ProgrammatorDev\OpenWeatherMap\Entity\AirPollution\AirPollutionLocation;
1210
use ProgrammatorDev\OpenWeatherMap\Entity\Coordinate;
13-
use ProgrammatorDev\OpenWeatherMap\Test\DataProvider\InvalidParamDataProvider;
11+
use ProgrammatorDev\OpenWeatherMap\Test\Util\TestEndpointInvalidResponseTrait;
12+
use ProgrammatorDev\OpenWeatherMap\Test\Util\TestEndpointSuccessResponseTrait;
1413

1514
class AirPollutionEndpointTest extends AbstractTest
1615
{
17-
// --- CURRENT ---
16+
use TestEndpointSuccessResponseTrait;
17+
use TestEndpointInvalidResponseTrait;
1818

19-
public function testAirPollutionGetCurrent()
19+
public static function provideEndpointSuccessResponseData(): \Generator
2020
{
21-
$this->mockHttpClient->addResponse(
22-
new Response(
23-
status: 200,
24-
body: MockResponse::AIR_POLLUTION_CURRENT
25-
)
26-
);
27-
28-
$response = $this->givenApi()->airPollution()->getCurrent(50, 50);
29-
$this->assertCurrentResponse($response);
21+
yield 'get current' => [
22+
MockResponse::AIR_POLLUTION_CURRENT,
23+
'airPollution',
24+
'getCurrent',
25+
[50, 50],
26+
'assertGetCurrentResponse'
27+
];
28+
yield 'get forecast' => [
29+
MockResponse::AIR_POLLUTION_FORECAST,
30+
'airPollution',
31+
'getForecast',
32+
[50, 50],
33+
'assertGetForecastResponse'
34+
];
35+
yield 'get history' => [
36+
MockResponse::AIR_POLLUTION_HISTORY,
37+
'airPollution',
38+
'getHistory',
39+
[50, 50, new \DateTime('yesterday'), new \DateTime('today')],
40+
'assertGetHistoryResponse'
41+
];
3042
}
3143

32-
#[DataProviderExternal(InvalidParamDataProvider::class, 'provideInvalidCoordinateData')]
33-
public function testAirPollutionGetCurrentWithInvalidCoordinate(float $latitude, float $longitude, string $expectedException)
44+
public static function provideEndpointInvalidResponseData(): \Generator
3445
{
35-
$this->expectException($expectedException);
36-
$this->givenApi()->airPollution()->getCurrent($latitude, $longitude);
46+
yield 'get current, latitude lower than -90' => ['airPollution', 'getCurrent', [-91, 50]];
47+
yield 'get current, latitude greater than 90' => ['airPollution', 'getCurrent', [91, 50]];
48+
yield 'get current, longitude lower than -180' => ['airPollution', 'getCurrent', [50, -181]];
49+
yield 'get current, longitude greater than 180' => ['airPollution', 'getCurrent', [50, 181]];
50+
51+
yield 'get forecast, latitude lower than -90' => ['airPollution', 'getForecast', [-91, 50]];
52+
yield 'get forecast, latitude greater than 90' => ['airPollution', 'getForecast', [91, 50]];
53+
yield 'get forecast, longitude lower than -180' => ['airPollution', 'getForecast', [50, -181]];
54+
yield 'get forecast, longitude greater than 180' => ['airPollution', 'getForecast', [50, 181]];
55+
56+
yield 'get history, latitude lower than -90' => ['airPollution', 'getHistory',
57+
[-91, 50, new \DateTime('yesterday'), new \DateTime('today')]
58+
];
59+
yield 'get history, latitude greater than 90' => ['airPollution', 'getHistory',
60+
[91, 50, new \DateTime('yesterday'), new \DateTime('today')]
61+
];
62+
yield 'get history, longitude lower than -180' => ['airPollution', 'getHistory',
63+
[50, -181, new \DateTime('yesterday'), new \DateTime('today')]
64+
];
65+
yield 'get history, longitude greater than 180' => ['airPollution', 'getHistory',
66+
[50, 181, new \DateTime('yesterday'), new \DateTime('today')]
67+
];
68+
yield 'get history, invalid past end date' => ['airPollution', 'getHistory',
69+
[50, 50, new \DateTime('yesterday'), new \DateTime('tomorrow')]
70+
];
71+
yield 'get history, end date before start date' => ['airPollution', 'getHistory',
72+
[50, 50, new \DateTime('yesterday'), new \DateTime('-2 days')]
73+
];
3774
}
3875

39-
// --- FORECAST ---
40-
41-
public function testAirPollutionGetForecast()
42-
{
43-
$this->mockHttpClient->addResponse(
44-
new Response(
45-
status: 200,
46-
body: MockResponse::AIR_POLLUTION_FORECAST
47-
)
48-
);
49-
50-
$response = $this->givenApi()->airPollution()->getForecast(50, 50);
51-
$this->assertForecastResponse($response);
52-
}
53-
54-
#[DataProviderExternal(InvalidParamDataProvider::class, 'provideInvalidCoordinateData')]
55-
public function testAirPollutionGetForecastWithInvalidCoordinate(float $latitude, float $longitude, string $expectedException)
56-
{
57-
$this->expectException($expectedException);
58-
$this->givenApi()->airPollution()->getForecast($latitude, $longitude);
59-
}
60-
61-
// --- HISTORY ---
62-
63-
public function testAirPollutionGetHistory()
64-
{
65-
$this->mockHttpClient->addResponse(
66-
new Response(
67-
status: 200,
68-
body: MockResponse::AIR_POLLUTION_HISTORY
69-
)
70-
);
71-
72-
$utcTimezone = new \DateTimeZone('UTC');
73-
74-
$response = $this->givenApi()->airPollution()->getHistory(
75-
50,
76-
50,
77-
new \DateTimeImmutable('-5 days', $utcTimezone),
78-
new \DateTimeImmutable('-4 days', $utcTimezone)
79-
);
80-
$this->assertHistoryResponse($response);
81-
}
82-
83-
#[DataProviderExternal(InvalidParamDataProvider::class, 'provideInvalidCoordinateData')]
84-
public function testAirPollutionGetHistoryWithInvalidCoordinate(float $latitude, float $longitude, string $expectedException)
85-
{
86-
$this->expectException($expectedException);
87-
88-
$startDate = new \DateTimeImmutable('-5 days');
89-
$endDate = new \DateTimeImmutable('-4 days');
90-
91-
$this->givenApi()->airPollution()->getHistory($latitude, $longitude, $startDate, $endDate);
92-
}
93-
94-
#[DataProviderExternal(InvalidParamDataProvider::class, 'provideInvalidPastDateData')]
95-
public function testAirPollutionGetHistoryWithInvalidPastStartDate(
96-
\DateTimeImmutable $startDate,
97-
string $expectedException
98-
)
99-
{
100-
$this->expectException($expectedException);
101-
$this->givenApi()->airPollution()->getHistory(
102-
50,
103-
50,
104-
$startDate,
105-
new \DateTimeImmutable('-5 days', new \DateTimeZone('UTC'))
106-
);
107-
}
108-
109-
#[DataProviderExternal(InvalidParamDataProvider::class, 'provideInvalidPastDateData')]
110-
public function testAirPollutionGetHistoryWithInvalidPastEndDate(
111-
\DateTimeImmutable $endDate,
112-
string $expectedException
113-
)
114-
{
115-
$this->expectException($expectedException);
116-
$this->givenApi()->airPollution()->getHistory(
117-
50,
118-
50,
119-
new \DateTimeImmutable('-5 days', new \DateTimeZone('UTC')),
120-
$endDate
121-
);
122-
}
123-
124-
#[DataProviderExternal(InvalidParamDataProvider::class, 'provideInvalidDateRangeData')]
125-
public function testAirPollutionGetHistoryWithInvalidDateRange(
126-
\DateTimeImmutable $startDate,
127-
\DateTimeImmutable $endDate,
128-
string $expectedException
129-
)
130-
{
131-
$this->expectException($expectedException);
132-
$this->givenApi()->airPollution()->getHistory(50, 50, $startDate, $endDate);
133-
}
134-
135-
// --- ASSERT METHODS EXIST ---
136-
13776
public function testAirPollutionMethodsExist()
13877
{
13978
$this->assertSame(false, method_exists(AirPollutionEndpoint::class, 'withUnitSystem'));
14079
$this->assertSame(false, method_exists(AirPollutionEndpoint::class, 'withLanguage'));
14180
$this->assertSame(true, method_exists(AirPollutionEndpoint::class, 'withCacheTtl'));
14281
}
14382

144-
// --- ASSERT RESPONSES ---
145-
146-
private function assertCurrentResponse(AirPollutionLocation $response): void
83+
private function assertGetCurrentResponse(AirPollutionLocation $airPollutionLocation): void
14784
{
148-
$this->assertInstanceOf(AirPollutionLocation::class, $response);
149-
150-
$this->assertSame(196.93, $response->getCarbonMonoxide());
151-
$this->assertSame(0.65, $response->getNitrogenMonoxide());
152-
$this->assertSame(3.98, $response->getNitrogenDioxide());
153-
$this->assertSame(107.29, $response->getOzone());
154-
$this->assertSame(1.46, $response->getSulphurDioxide());
155-
$this->assertSame(8.58, $response->getFineParticulateMatter());
156-
$this->assertSame(13.5, $response->getCoarseParticulateMatter());
157-
$this->assertSame(2.03, $response->getAmmonia());
158-
159-
$coordinate = $response->getCoordinate();
85+
$this->assertSame(196.93, $airPollutionLocation->getCarbonMonoxide());
86+
$this->assertSame(0.65, $airPollutionLocation->getNitrogenMonoxide());
87+
$this->assertSame(3.98, $airPollutionLocation->getNitrogenDioxide());
88+
$this->assertSame(107.29, $airPollutionLocation->getOzone());
89+
$this->assertSame(1.46, $airPollutionLocation->getSulphurDioxide());
90+
$this->assertSame(8.58, $airPollutionLocation->getFineParticulateMatter());
91+
$this->assertSame(13.5, $airPollutionLocation->getCoarseParticulateMatter());
92+
$this->assertSame(2.03, $airPollutionLocation->getAmmonia());
93+
$this->assertSame('2023-06-23 17:21:57', $airPollutionLocation->getDateTime()->format('Y-m-d H:i:s'));
94+
95+
$coordinate = $airPollutionLocation->getCoordinate();
16096
$this->assertInstanceOf(Coordinate::class, $coordinate);
16197
$this->assertSame(38.7078, $coordinate->getLatitude());
16298
$this->assertSame(-9.1366, $coordinate->getLongitude());
16399

164-
$dateTime = $response->getDateTime();
165-
$this->assertInstanceOf(\DateTimeImmutable::class, $dateTime);
166-
$this->assertSame('2023-06-23 17:21:57', $dateTime->format('Y-m-d H:i:s'));
167-
168-
$airQuality = $response->getAirQuality();
100+
$airQuality = $airPollutionLocation->getAirQuality();
169101
$this->assertInstanceOf(AirQuality::class, $airQuality);
170102
$this->assertSame(3, $airQuality->getIndex());
171103
$this->assertSame('Moderate', $airQuality->getQualitativeName());
172104
}
173105

174-
private function assertForecastResponse(AirPollutionLocationList $response): void
106+
private function assertGetForecastResponse(AirPollutionLocationList $airPollutionLocationList): void
175107
{
176-
$this->assertInstanceOf(AirPollutionLocationList::class, $response);
177-
178-
$coordinate = $response->getCoordinate();
108+
$coordinate = $airPollutionLocationList->getCoordinate();
179109
$this->assertInstanceOf(Coordinate::class, $coordinate);
180110
$this->assertSame(38.7078, $coordinate->getLatitude());
181111
$this->assertSame(-9.1366, $coordinate->getLongitude());
182112

183-
$list = $response->getList();
113+
$list = $airPollutionLocationList->getList();
184114
$this->assertContainsOnlyInstancesOf(AirPollution::class, $list);
185-
186115
$this->assertSame(196.93, $list[0]->getCarbonMonoxide());
187116
$this->assertSame(0.65, $list[0]->getNitrogenMonoxide());
188117
$this->assertSame(3.98, $list[0]->getNitrogenDioxide());
@@ -191,29 +120,23 @@ private function assertForecastResponse(AirPollutionLocationList $response): voi
191120
$this->assertSame(8.58, $list[0]->getFineParticulateMatter());
192121
$this->assertSame(13.5, $list[0]->getCoarseParticulateMatter());
193122
$this->assertSame(2.03, $list[0]->getAmmonia());
194-
195-
$dateTime = $list[0]->getDateTime();
196-
$this->assertInstanceOf(\DateTimeImmutable::class, $dateTime);
197-
$this->assertSame('2023-06-23 17:00:00', $dateTime->format('Y-m-d H:i:s'));
123+
$this->assertSame('2023-06-23 17:00:00', $list[0]->getDateTime()->format('Y-m-d H:i:s'));
198124

199125
$airQuality = $list[0]->getAirQuality();
200126
$this->assertInstanceOf(AirQuality::class, $airQuality);
201127
$this->assertSame(3, $airQuality->getIndex());
202128
$this->assertSame('Moderate', $airQuality->getQualitativeName());
203129
}
204130

205-
private function assertHistoryResponse(AirPollutionLocationList $response): void
131+
private function assertGetHistoryResponse(AirPollutionLocationList $airPollutionLocationList): void
206132
{
207-
$this->assertInstanceOf(AirPollutionLocationList::class, $response);
208-
209-
$coordinate = $response->getCoordinate();
133+
$coordinate = $airPollutionLocationList->getCoordinate();
210134
$this->assertInstanceOf(Coordinate::class, $coordinate);
211135
$this->assertSame(38.7078, $coordinate->getLatitude());
212136
$this->assertSame(-9.1366, $coordinate->getLongitude());
213137

214-
$list = $response->getList();
138+
$list = $airPollutionLocationList->getList();
215139
$this->assertContainsOnlyInstancesOf(AirPollution::class, $list);
216-
217140
$this->assertSame(220.3, $list[0]->getCarbonMonoxide());
218141
$this->assertSame(0.12, $list[0]->getNitrogenMonoxide());
219142
$this->assertSame(3.3, $list[0]->getNitrogenDioxide());
@@ -222,10 +145,7 @@ private function assertHistoryResponse(AirPollutionLocationList $response): void
222145
$this->assertSame(1.62, $list[0]->getFineParticulateMatter());
223146
$this->assertSame(2.94, $list[0]->getCoarseParticulateMatter());
224147
$this->assertSame(0.38, $list[0]->getAmmonia());
225-
226-
$dateTime = $list[0]->getDateTime();
227-
$this->assertInstanceOf(\DateTimeImmutable::class, $dateTime);
228-
$this->assertSame('2023-06-18 18:00:00', $dateTime->format('Y-m-d H:i:s'));
148+
$this->assertSame('2023-06-18 18:00:00', $list[0]->getDateTime()->format('Y-m-d H:i:s'));
229149

230150
$airQuality = $list[0]->getAirQuality();
231151
$this->assertInstanceOf(AirQuality::class, $airQuality);

tests/DataProvider/InvalidParamDataProvider.php

-31
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,6 @@
66

77
class InvalidParamDataProvider
88
{
9-
public static function provideInvalidCoordinateData(): \Generator
10-
{
11-
yield 'latitude lower than -90' => [-91, -9.1365919, ValidationException::class];
12-
yield 'latitude greater than 90' => [91, -9.1365919, ValidationException::class];
13-
yield 'longitude lower than -180' => [38.7077507, -181, ValidationException::class];
14-
yield 'longitude greater than 180' => [38.7077507, 181, ValidationException::class];
15-
}
16-
17-
public static function provideInvalidPastDateData(): \Generator
18-
{
19-
yield 'invalid past date' => [
20-
new \DateTimeImmutable('1 days'),
21-
ValidationException::class
22-
];
23-
}
24-
25-
public static function provideInvalidDateRangeData(): \Generator
26-
{
27-
yield 'start date greater than end date' => [
28-
new \DateTimeImmutable('-4 days'),
29-
new \DateTimeImmutable('-5 days'),
30-
ValidationException::class
31-
];
32-
}
33-
34-
public static function provideInvalidNumResultsData(): \Generator
35-
{
36-
yield 'equal to zero num results' => [0, ValidationException::class];
37-
yield 'negative num results' => [-1, ValidationException::class];
38-
}
39-
409
public static function provideInvalidUnitSystemData(): \Generator
4110
{
4211
yield 'not allowed unit system' => ['invalid', ValidationException::class];

0 commit comments

Comments
 (0)