2
2
3
3
namespace ProgrammatorDev \OpenWeatherMap \Test ;
4
4
5
- use Nyholm \Psr7 \Response ;
6
- use PHPUnit \Framework \Attributes \DataProviderExternal ;
7
5
use ProgrammatorDev \OpenWeatherMap \Endpoint \AirPollutionEndpoint ;
8
6
use ProgrammatorDev \OpenWeatherMap \Entity \AirPollution \AirPollution ;
9
7
use ProgrammatorDev \OpenWeatherMap \Entity \AirPollution \AirPollutionLocationList ;
10
8
use ProgrammatorDev \OpenWeatherMap \Entity \AirPollution \AirQuality ;
11
9
use ProgrammatorDev \OpenWeatherMap \Entity \AirPollution \AirPollutionLocation ;
12
10
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 ;
14
13
15
14
class AirPollutionEndpointTest extends AbstractTest
16
15
{
17
- // --- CURRENT ---
16
+ use TestEndpointSuccessResponseTrait;
17
+ use TestEndpointInvalidResponseTrait;
18
18
19
- public function testAirPollutionGetCurrent ()
19
+ public static function provideEndpointSuccessResponseData (): \ Generator
20
20
{
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
+ ];
30
42
}
31
43
32
- #[DataProviderExternal(InvalidParamDataProvider::class, 'provideInvalidCoordinateData ' )]
33
- public function testAirPollutionGetCurrentWithInvalidCoordinate (float $ latitude , float $ longitude , string $ expectedException )
44
+ public static function provideEndpointInvalidResponseData (): \Generator
34
45
{
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
+ ];
37
74
}
38
75
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
-
137
76
public function testAirPollutionMethodsExist ()
138
77
{
139
78
$ this ->assertSame (false , method_exists (AirPollutionEndpoint::class, 'withUnitSystem ' ));
140
79
$ this ->assertSame (false , method_exists (AirPollutionEndpoint::class, 'withLanguage ' ));
141
80
$ this ->assertSame (true , method_exists (AirPollutionEndpoint::class, 'withCacheTtl ' ));
142
81
}
143
82
144
- // --- ASSERT RESPONSES ---
145
-
146
- private function assertCurrentResponse (AirPollutionLocation $ response ): void
83
+ private function assertGetCurrentResponse (AirPollutionLocation $ airPollutionLocation ): void
147
84
{
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 ();
160
96
$ this ->assertInstanceOf (Coordinate::class, $ coordinate );
161
97
$ this ->assertSame (38.7078 , $ coordinate ->getLatitude ());
162
98
$ this ->assertSame (-9.1366 , $ coordinate ->getLongitude ());
163
99
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 ();
169
101
$ this ->assertInstanceOf (AirQuality::class, $ airQuality );
170
102
$ this ->assertSame (3 , $ airQuality ->getIndex ());
171
103
$ this ->assertSame ('Moderate ' , $ airQuality ->getQualitativeName ());
172
104
}
173
105
174
- private function assertForecastResponse (AirPollutionLocationList $ response ): void
106
+ private function assertGetForecastResponse (AirPollutionLocationList $ airPollutionLocationList ): void
175
107
{
176
- $ this ->assertInstanceOf (AirPollutionLocationList::class, $ response );
177
-
178
- $ coordinate = $ response ->getCoordinate ();
108
+ $ coordinate = $ airPollutionLocationList ->getCoordinate ();
179
109
$ this ->assertInstanceOf (Coordinate::class, $ coordinate );
180
110
$ this ->assertSame (38.7078 , $ coordinate ->getLatitude ());
181
111
$ this ->assertSame (-9.1366 , $ coordinate ->getLongitude ());
182
112
183
- $ list = $ response ->getList ();
113
+ $ list = $ airPollutionLocationList ->getList ();
184
114
$ this ->assertContainsOnlyInstancesOf (AirPollution::class, $ list );
185
-
186
115
$ this ->assertSame (196.93 , $ list [0 ]->getCarbonMonoxide ());
187
116
$ this ->assertSame (0.65 , $ list [0 ]->getNitrogenMonoxide ());
188
117
$ this ->assertSame (3.98 , $ list [0 ]->getNitrogenDioxide ());
@@ -191,29 +120,23 @@ private function assertForecastResponse(AirPollutionLocationList $response): voi
191
120
$ this ->assertSame (8.58 , $ list [0 ]->getFineParticulateMatter ());
192
121
$ this ->assertSame (13.5 , $ list [0 ]->getCoarseParticulateMatter ());
193
122
$ 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 ' ));
198
124
199
125
$ airQuality = $ list [0 ]->getAirQuality ();
200
126
$ this ->assertInstanceOf (AirQuality::class, $ airQuality );
201
127
$ this ->assertSame (3 , $ airQuality ->getIndex ());
202
128
$ this ->assertSame ('Moderate ' , $ airQuality ->getQualitativeName ());
203
129
}
204
130
205
- private function assertHistoryResponse (AirPollutionLocationList $ response ): void
131
+ private function assertGetHistoryResponse (AirPollutionLocationList $ airPollutionLocationList ): void
206
132
{
207
- $ this ->assertInstanceOf (AirPollutionLocationList::class, $ response );
208
-
209
- $ coordinate = $ response ->getCoordinate ();
133
+ $ coordinate = $ airPollutionLocationList ->getCoordinate ();
210
134
$ this ->assertInstanceOf (Coordinate::class, $ coordinate );
211
135
$ this ->assertSame (38.7078 , $ coordinate ->getLatitude ());
212
136
$ this ->assertSame (-9.1366 , $ coordinate ->getLongitude ());
213
137
214
- $ list = $ response ->getList ();
138
+ $ list = $ airPollutionLocationList ->getList ();
215
139
$ this ->assertContainsOnlyInstancesOf (AirPollution::class, $ list );
216
-
217
140
$ this ->assertSame (220.3 , $ list [0 ]->getCarbonMonoxide ());
218
141
$ this ->assertSame (0.12 , $ list [0 ]->getNitrogenMonoxide ());
219
142
$ this ->assertSame (3.3 , $ list [0 ]->getNitrogenDioxide ());
@@ -222,10 +145,7 @@ private function assertHistoryResponse(AirPollutionLocationList $response): void
222
145
$ this ->assertSame (1.62 , $ list [0 ]->getFineParticulateMatter ());
223
146
$ this ->assertSame (2.94 , $ list [0 ]->getCoarseParticulateMatter ());
224
147
$ 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 ' ));
229
149
230
150
$ airQuality = $ list [0 ]->getAirQuality ();
231
151
$ this ->assertInstanceOf (AirQuality::class, $ airQuality );
0 commit comments