22
33namespace ProgrammatorDev \OpenWeatherMap \Test ;
44
5- use Nyholm \Psr7 \Response ;
6- use PHPUnit \Framework \Attributes \DataProviderExternal ;
75use ProgrammatorDev \OpenWeatherMap \Endpoint \AirPollutionEndpoint ;
86use ProgrammatorDev \OpenWeatherMap \Entity \AirPollution \AirPollution ;
97use ProgrammatorDev \OpenWeatherMap \Entity \AirPollution \AirPollutionLocationList ;
108use ProgrammatorDev \OpenWeatherMap \Entity \AirPollution \AirQuality ;
119use ProgrammatorDev \OpenWeatherMap \Entity \AirPollution \AirPollutionLocation ;
1210use 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
1514class 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 );
0 commit comments