22
33namespace ProgrammatorDev \OpenWeatherMap \Test ;
44
5- use Nyholm \Psr7 \Response ;
6- use PHPUnit \Framework \Attributes \DataProvider ;
7- use PHPUnit \Framework \Attributes \DataProviderExternal ;
85use ProgrammatorDev \OpenWeatherMap \Endpoint \GeocodingEndpoint ;
96use ProgrammatorDev \OpenWeatherMap \Entity \Coordinate ;
107use ProgrammatorDev \OpenWeatherMap \Entity \Geocoding \ZipCodeLocation ;
118use ProgrammatorDev \OpenWeatherMap \Entity \Location ;
12- use ProgrammatorDev \OpenWeatherMap \Test \DataProvider \ InvalidParamDataProvider ;
13- use ProgrammatorDev \YetAnotherPhpValidator \ Exception \ ValidationException ;
9+ use ProgrammatorDev \OpenWeatherMap \Test \Util \ TestEndpointInvalidResponseTrait ;
10+ use ProgrammatorDev \OpenWeatherMap \ Test \ Util \ TestEndpointSuccessResponseTrait ;
1411
1512class GeocodingEndpointTest extends AbstractTest
1613{
17- // --- BY LOCATION NAME ---
14+ use TestEndpointSuccessResponseTrait;
15+ use TestEndpointInvalidResponseTrait;
1816
19- public function testGeocodingGetByLocationName ()
17+ public static function provideEndpointSuccessResponseData (): \ Generator
2018 {
21- $ this ->mockHttpClient ->addResponse (
22- new Response (
23- status: 200 ,
24- body: MockResponse::GEOCODING_DIRECT
25- )
26- );
27-
28- $ response = $ this ->givenApi ()->geocoding ()->getByLocationName ('lisbon, pt ' );
29- $ this ->assertLocationListResponse ($ response );
19+ yield 'get by location name ' => [
20+ MockResponse::GEOCODING_DIRECT ,
21+ 'geocoding ' ,
22+ 'getByLocationName ' ,
23+ ['test ' ],
24+ 'assertGetByLocationResponse '
25+ ];
26+ yield 'get by coordinate ' => [
27+ MockResponse::GEOCODING_REVERSE ,
28+ 'geocoding ' ,
29+ 'getByCoordinate ' ,
30+ [50 , 50 ],
31+ 'assertGetByLocationResponse '
32+ ];
33+ yield 'get by zip code ' => [
34+ MockResponse::GEOCODING_ZIP ,
35+ 'geocoding ' ,
36+ 'getByZipCode ' ,
37+ ['1234-567 ' , 'pt ' ],
38+ 'assertGetByZipCodeResponse '
39+ ];
3040 }
3141
32- public function testGeocodingGetByLocationNameWithBlankValue ()
42+ public static function provideEndpointInvalidResponseData (): \ Generator
3343 {
34- $ this ->expectException (ValidationException::class);
35- $ this ->givenApi ()->geocoding ()->getByLocationName ('' );
44+ yield 'get by location name, blank value ' => ['geocoding ' , 'getByLocationName ' , ['' ]];
45+
46+ yield 'get by zip code, blank zip code ' => ['geocoding ' , 'getByZipCode ' , ['' , 'pt ' ]];
47+ yield 'get by zip code, blank country code ' => ['geocoding ' , 'getByZipCode ' , ['1234-567 ' , '' ]];
48+ yield 'get by zip code, invalid country code ' => ['geocoding ' , 'getByZipCode ' , ['1234-567 ' , 'invalid ' ]];
49+
50+ yield 'get by coordinate, latitude lower than -90 ' => ['geocoding ' , 'getByCoordinate ' , [-91 , 50 ]];
51+ yield 'get by coordinate, latitude greater than 90 ' => ['geocoding ' , 'getByCoordinate ' , [91 , 50 ]];
52+ yield 'get by coordinate, longitude lower than -180 ' => ['geocoding ' , 'getByCoordinate ' , [50 , -181 ]];
53+ yield 'get by coordinate, longitude greater than 180 ' => ['geocoding ' , 'getByCoordinate ' , [50 , 181 ]];
54+ yield 'get by coordinate, zero num results ' => ['geocoding ' , 'getByCoordinate ' , [50 , 50 , 0 ]];
55+ yield 'get by coordinate, negative num results ' => ['geocoding ' , 'getByCoordinate ' , [50 , 50 , -1 ]];
3656 }
3757
38- // --- BY ZIP CODE ---
39-
40- public function testGeocodingGetByZipCode ()
41- {
42- $ this ->mockHttpClient ->addResponse (
43- new Response (
44- status: 200 ,
45- body: MockResponse::GEOCODING_ZIP
46- )
47- );
48-
49- $ response = $ this ->givenApi ()->geocoding ()->getByZipCode ('1000-001 ' , 'pt ' );
50- $ this ->assertInstanceOf (ZipCodeLocation::class, $ response );
51-
52- $ this ->assertSame ('1000-001 ' , $ response ->getZipCode ());
53- $ this ->assertSame ('Lisbon ' , $ response ->getName ());
54- $ this ->assertSame ('PT ' , $ response ->getCountryCode ());
55-
56- $ coordinate = $ response ->getCoordinate ();
57- $ this ->assertInstanceOf (Coordinate::class, $ coordinate );
58- $ this ->assertSame (38.7167 , $ coordinate ->getLatitude ());
59- $ this ->assertSame (-9.1333 , $ coordinate ->getLongitude ());
60- }
61-
62- #[DataProvider('provideGeocodingGetByZipCodeWithInvalidValueData ' )]
63- public function testGeocodingGetByZipCodeWithInvalidValue (string $ zipCode , string $ countryCode )
64- {
65- $ this ->expectException (ValidationException::class);
66- $ this ->givenApi ()->geocoding ()->getByZipCode ($ zipCode , $ countryCode );
67- }
68-
69- public static function provideGeocodingGetByZipCodeWithInvalidValueData (): \Generator
70- {
71- yield 'blank zip code ' => ['' , 'pt ' ];
72- yield 'blank country code ' => ['1000-100 ' , '' ];
73- yield 'invalid country code ' => ['1000-100 ' , 'invalid ' ];
74- }
75-
76- // --- BY COORDINATE ---
77-
78- public function testGeocodingGetByCoordinate ()
79- {
80- $ this ->mockHttpClient ->addResponse (
81- new Response (
82- status: 200 ,
83- body: MockResponse::GEOCODING_REVERSE
84- )
85- );
86-
87- $ response = $ this ->givenApi ()->geocoding ()->getByCoordinate (50 , 50 );
88- $ this ->assertLocationListResponse ($ response );
89- }
90-
91- #[DataProviderExternal(InvalidParamDataProvider::class, 'provideInvalidCoordinateData ' )]
92- public function testGeocodingGetByCoordinateWithInvalidCoordinate (
93- float $ latitude ,
94- float $ longitude ,
95- string $ expectedException
96- )
97- {
98- $ this ->expectException ($ expectedException );
99- $ this ->givenApi ()->geocoding ()->getByCoordinate ($ latitude , $ longitude );
100- }
101-
102- #[DataProviderExternal(InvalidParamDataProvider::class, 'provideInvalidNumResultsData ' )]
103- public function testGeocodingGetByCoordinateWithInvalidNumResults (
104- int $ numResults ,
105- string $ expectedException
106- )
107- {
108- $ this ->expectException ($ expectedException );
109- $ this ->givenApi ()->geocoding ()->getByCoordinate (50 , 50 , $ numResults );
110- }
111-
112- // --- ASSERT METHODS EXIST ---
113-
11458 public function testGeocodingMethodsExist ()
11559 {
11660 $ this ->assertSame (false , method_exists (GeocodingEndpoint::class, 'withUnitSystem ' ));
11761 $ this ->assertSame (false , method_exists (GeocodingEndpoint::class, 'withLanguage ' ));
11862 $ this ->assertSame (true , method_exists (GeocodingEndpoint::class, 'withCacheTtl ' ));
11963 }
12064
121- // --- ASSERT RESPONSES ---
122-
123- /**
124- * @param Location[] $response
125- */
126- private function assertLocationListResponse (array $ response ): void
65+ /** @param Location[] $locations */
66+ private function assertGetByLocationResponse (array $ locations ): void
12767 {
128- $ this ->assertContainsOnlyInstancesOf (Location::class, $ response );
68+ $ this ->assertContainsOnlyInstancesOf (Location::class, $ locations );
12969
130- $ location = $ response [0 ];
70+ $ location = $ locations [0 ];
13171 $ this ->assertSame (null , $ location ->getId ());
13272 $ this ->assertSame ('Lisbon ' , $ location ->getName ());
13373 $ this ->assertSame (null , $ location ->getState ());
@@ -139,9 +79,21 @@ private function assertLocationListResponse(array $response): void
13979 $ this ->assertSame (null , $ location ->getSunriseAt ());
14080 $ this ->assertSame (null , $ location ->getSunsetAt ());
14181
142- $ coordinate = $ response [0 ]->getCoordinate ();
82+ $ coordinate = $ locations [0 ]->getCoordinate ();
14383 $ this ->assertInstanceOf (Coordinate::class, $ coordinate );
14484 $ this ->assertSame (38.7077507 , $ coordinate ->getLatitude ());
14585 $ this ->assertSame (-9.1365919 , $ coordinate ->getLongitude ());
14686 }
87+
88+ private function assertGetByZipCodeResponse (ZipCodeLocation $ zipCodeLocation ): void
89+ {
90+ $ this ->assertSame ('1000-001 ' , $ zipCodeLocation ->getZipCode ());
91+ $ this ->assertSame ('Lisbon ' , $ zipCodeLocation ->getName ());
92+ $ this ->assertSame ('PT ' , $ zipCodeLocation ->getCountryCode ());
93+
94+ $ coordinate = $ zipCodeLocation ->getCoordinate ();
95+ $ this ->assertInstanceOf (Coordinate::class, $ coordinate );
96+ $ this ->assertSame (38.7167 , $ coordinate ->getLatitude ());
97+ $ this ->assertSame (-9.1333 , $ coordinate ->getLongitude ());
98+ }
14799}
0 commit comments