1111namespace Geocoder \IntegrationTest ;
1212
1313use Geocoder \Collection ;
14+ use Geocoder \Exception \InvalidCredentials ;
15+ use Geocoder \Exception \InvalidServerResponse ;
16+ use Geocoder \Exception \QuotaExceeded ;
1417use Geocoder \Location ;
1518use Geocoder \Model \AdminLevelCollection ;
1619use Geocoder \Model \Bounds ;
@@ -36,6 +39,12 @@ abstract class ProviderIntegrationTest extends TestCase
3639 */
3740 protected $ skippedTests = [];
3841
42+ protected $ testAddress = true ;
43+ protected $ testReverse = true ;
44+ protected $ testIpv4 = true ;
45+ protected $ testIpv6 = true ;
46+ protected $ testHttpProvider = true ;
47+
3948 /**
4049 * @return Provider that is used in the tests.
4150 */
@@ -91,6 +100,9 @@ public function testGeocodeQuery()
91100 if (isset ($ this ->skippedTests [__FUNCTION__ ])) {
92101 $ this ->markTestSkipped ($ this ->skippedTests [__FUNCTION__ ]);
93102 }
103+ if (!$ this ->testAddress ) {
104+ $ this ->markTestSkipped ('Geocoding address is not supported by this provider ' );
105+ }
94106
95107 $ provider = $ this ->createProvider ($ this ->getCachedHttpClient ());
96108 $ query = GeocodeQuery::create ('10 Downing St, London, UK ' )->withLocale ('en ' );
@@ -113,6 +125,9 @@ public function testGeocodeQueryWithNoResults()
113125 if (isset ($ this ->skippedTests [__FUNCTION__ ])) {
114126 $ this ->markTestSkipped ($ this ->skippedTests [__FUNCTION__ ]);
115127 }
128+ if (!$ this ->testAddress ) {
129+ $ this ->markTestSkipped ('Geocoding address is not supported by this provider ' );
130+ }
116131
117132 $ provider = $ this ->createProvider ($ this ->getCachedHttpClient ());
118133 $ query = GeocodeQuery::create ('jsajhgsdkfjhsfkjhaldkadjaslgldasd ' )->withLocale ('en ' );
@@ -126,6 +141,9 @@ public function testReverseQuery()
126141 if (isset ($ this ->skippedTests [__FUNCTION__ ])) {
127142 $ this ->markTestSkipped ($ this ->skippedTests [__FUNCTION__ ]);
128143 }
144+ if (!$ this ->testReverse ) {
145+ $ this ->markTestSkipped ('Reverse geocoding address is not supported by this provider ' );
146+ }
129147
130148 $ provider = $ this ->createProvider ($ this ->getCachedHttpClient ());
131149
@@ -134,36 +152,14 @@ public function testReverseQuery()
134152 $ this ->assertWellFormattedResult ($ result );
135153 }
136154
137- /**
138- * @expectedException \Geocoder\Exception\InvalidArgument
139- */
140- public function testEmptyQuery ()
141- {
142- if (isset ($ this ->skippedTests [__FUNCTION__ ])) {
143- $ this ->markTestSkipped ($ this ->skippedTests [__FUNCTION__ ]);
144- }
145-
146- $ provider = $ this ->createProvider ($ this ->getCachedHttpClient ());
147- $ provider ->geocodeQuery (GeocodeQuery::create ('' ));
148- }
149-
150- /**
151- * @expectedException \Geocoder\Exception\InvalidArgument
152- */
153- public function testNullQuery ()
155+ public function testReverseQueryWithNoResults ()
154156 {
155157 if (isset ($ this ->skippedTests [__FUNCTION__ ])) {
156158 $ this ->markTestSkipped ($ this ->skippedTests [__FUNCTION__ ]);
157159 }
158160
159- $ provider = $ this ->createProvider ($ this ->getCachedHttpClient ());
160- $ provider ->geocodeQuery (GeocodeQuery::create (null ));
161- }
162-
163- public function testEmptyReverseQuery ()
164- {
165- if (isset ($ this ->skippedTests [__FUNCTION__ ])) {
166- $ this ->markTestSkipped ($ this ->skippedTests [__FUNCTION__ ]);
161+ if (!$ this ->testReverse ) {
162+ $ this ->markTestSkipped ('Reverse geocoding address is not supported by this provider ' );
167163 }
168164
169165 $ provider = $ this ->createProvider ($ this ->getCachedHttpClient ());
@@ -172,134 +168,109 @@ public function testEmptyReverseQuery()
172168 $ this ->assertEquals (0 , $ result ->count ());
173169 }
174170
175- /**
176- * @expectedException \Geocoder\Exception\InvalidServerResponse
177- */
178- public function testServer500Error ()
171+ public function testGeocodeIpv4 ()
179172 {
180173 if (isset ($ this ->skippedTests [__FUNCTION__ ])) {
181174 $ this ->markTestSkipped ($ this ->skippedTests [__FUNCTION__ ]);
182175 }
183176
184- $ provider = $ this ->createProvider ($ this ->getHttpClient (new Response (500 )));
185- $ provider ->geocodeQuery (GeocodeQuery::create ('foo ' ));
186- }
187-
188- /**
189- * @expectedException \Geocoder\Exception\InvalidServerResponse
190- */
191- public function testServer500ErrorReverse ()
192- {
193- if (isset ($ this ->skippedTests [__FUNCTION__ ])) {
194- $ this ->markTestSkipped ($ this ->skippedTests [__FUNCTION__ ]);
177+ if (!$ this ->testIpv4 ) {
178+ $ this ->markTestSkipped ('Geocoding IPv4 is not supported by this provider ' );
195179 }
196180
197- $ provider = $ this ->createProvider ($ this ->getHttpClient (new Response (500 )));
198- $ provider ->reverseQuery (ReverseQuery::fromCoordinates (0 , 0 ));
181+ $ provider = $ this ->createProvider ($ this ->getCachedHttpClient ());
182+ $ result = $ provider ->geocodeQuery (GeocodeQuery::create ('83.227.123.8 ' )->withLocale ('en ' ));
183+ $ this ->assertWellFormattedResult ($ result );
199184 }
200185
201- /**
202- * @expectedException \Geocoder\Exception\InvalidServerResponse
203- */
204- public function testServer400Error ()
186+ public function testGeocodeIpv6 ()
205187 {
206188 if (isset ($ this ->skippedTests [__FUNCTION__ ])) {
207189 $ this ->markTestSkipped ($ this ->skippedTests [__FUNCTION__ ]);
208190 }
209191
210- $ provider = $ this ->createProvider ($ this ->getHttpClient (new Response (400 )));
211- $ provider ->geocodeQuery (GeocodeQuery::create ('foo ' ));
212- }
213-
214- /**
215- * @expectedException \Geocoder\Exception\InvalidServerResponse
216- */
217- public function testServer400ErrorReverse ()
218- {
219- if (isset ($ this ->skippedTests [__FUNCTION__ ])) {
220- $ this ->markTestSkipped ($ this ->skippedTests [__FUNCTION__ ]);
192+ if (!$ this ->testIpv6 ) {
193+ $ this ->markTestSkipped ('Geocoding IPv6 is not supported by this provider ' );
221194 }
222195
223- $ provider = $ this ->createProvider ($ this ->getHttpClient (new Response (400 )));
224- $ provider ->reverseQuery (ReverseQuery::fromCoordinates (0 , 0 ));
196+ $ provider = $ this ->createProvider ($ this ->getCachedHttpClient ());
197+ $ result = $ provider ->geocodeQuery (GeocodeQuery::create ('2001:0db8:0000:0042:0000:8a2e:0370:7334 ' )->withLocale ('en ' ));
198+ $ this ->assertWellFormattedResult ($ result );
225199 }
226200
227201 /**
228- * @expectedException \Geocoder\Exception\InvalidServerResponse
202+ * @dataProvider exceptionDataProvider
203+ *
204+ * @param GeocodeQuery|ReverseQuery $query
205+ * @param string $exceptionClass
206+ * @param ResponseInterface|null $response
207+ * @param string $message
229208 */
230- public function testServerEmptyResponse ( )
209+ public function testExceptions ( $ query , string $ exceptionClass , ResponseInterface $ response = null , string $ message = '' )
231210 {
232211 if (isset ($ this ->skippedTests [__FUNCTION__ ])) {
233212 $ this ->markTestSkipped ($ this ->skippedTests [__FUNCTION__ ]);
234213 }
235214
236- $ provider = $ this ->createProvider ($ this ->getHttpClient (new Response (200 )));
237- $ provider ->geocodeQuery (GeocodeQuery::create ('foo ' ));
238- }
239-
240- /**
241- * @expectedException \Geocoder\Exception\InvalidServerResponse
242- */
243- public function testServerEmptyResponseReverse ()
244- {
245- if (isset ($ this ->skippedTests [__FUNCTION__ ])) {
246- $ this ->markTestSkipped ($ this ->skippedTests [__FUNCTION__ ]);
215+ if (null === $ response ) {
216+ $ provider = $ this ->createProvider ($ this ->getCachedHttpClient ());
217+ } else {
218+ $ provider = $ this ->createProvider ($ this ->getHttpClient ($ response ));
247219 }
248220
249- $ provider = $ this ->createProvider ($ this ->getHttpClient (new Response (200 )));
250- $ provider ->reverseQuery (ReverseQuery::fromCoordinates (0 , 0 ));
251- }
252-
253- /**
254- * @expectedException \Geocoder\Exception\InvalidCredentials
255- */
256- public function testInvalidCredentialsResponse ()
257- {
258- if (isset ($ this ->skippedTests [__FUNCTION__ ])) {
259- $ this ->markTestSkipped ($ this ->skippedTests [__FUNCTION__ ]);
221+ $ this ->expectException ($ exceptionClass );
222+ if ($ query instanceof ReverseQuery) {
223+ $ provider ->reverseQuery ($ query );
224+ } else {
225+ $ provider ->geocodeQuery ($ query );
260226 }
261-
262- $ provider = $ this ->createProvider ($ this ->getHttpClient (new Response (401 )));
263- $ provider ->geocodeQuery (GeocodeQuery::create ('foo ' ));
264227 }
265228
266- /**
267- * @expectedException \Geocoder\Exception\InvalidCredentials
268- */
269- public function testInvalidCredentialsResponseReverse ()
229+ public function exceptionDataProvider ()
270230 {
271- if (isset ($ this ->skippedTests [__FUNCTION__ ])) {
272- $ this ->markTestSkipped ($ this ->skippedTests [__FUNCTION__ ]);
231+ $ testData = [];
232+
233+ if (!$ this ->testHttpProvider ) {
234+ return $ testData ;
273235 }
274236
275- $ provider = $ this ->createProvider ($ this ->getHttpClient (new Response (401 )));
276- $ provider ->reverseQuery (ReverseQuery::fromCoordinates (0 , 0 ));
277- }
237+ if ($ this ->testAddress ) {
238+ $ q = GeocodeQuery::create ('foo ' );
239+ $ testData [] = [$ q , InvalidServerResponse::class, new Response (500 ), 'Server 500 ' ];
240+ $ testData [] = [$ q , InvalidServerResponse::class, new Response (400 ), 'Server 400 ' ];
241+ $ testData [] = [$ q , InvalidCredentials::class, new Response (401 ), 'Invalid credentials response ' ];
242+ $ testData [] = [$ q , QuotaExceeded::class, new Response (429 ), 'Quota exceeded response ' ];
243+ $ testData [] = [$ q , InvalidServerResponse::class, new Response (200 ), 'Empty response ' ];
244+ }
278245
279- /**
280- * @expectedException \Geocoder\Exception\QuotaExceeded
281- */
282- public function testQuotaExceededResponse ()
283- {
284- if ( isset ( $ this -> skippedTests [ __FUNCTION__ ])) {
285- $ this -> markTestSkipped ( $ this -> skippedTests [ __FUNCTION__ ]) ;
246+ if ( $ this -> testReverse ) {
247+ $ q = ReverseQuery:: fromCoordinates ( 0 , 0 );
248+ $ testData [] = [ $ q , InvalidServerResponse::class, new Response ( 500 ), ' Server 500 ' ];
249+ $ testData [] = [ $ q , InvalidServerResponse::class, new Response ( 400 ), ' Server 400 ' ];
250+ $ testData [] = [ $ q , InvalidCredentials::class, new Response ( 401 ), ' Invalid credentials response ' ];
251+ $ testData [] = [ $ q , QuotaExceeded::class, new Response ( 429 ), ' Quota exceeded response ' ];
252+ $ testData [] = [ $ q , InvalidServerResponse::class, new Response ( 200 ), ' Empty response ' ] ;
286253 }
287254
288- $ provider = $ this ->createProvider ($ this ->getHttpClient (new Response (429 )));
289- $ provider ->geocodeQuery (GeocodeQuery::create ('foo ' ));
290- }
255+ if ($ this ->testIpv4 ) {
256+ $ q = GeocodeQuery::create ('123.123.123.123 ' );
257+ $ testData [] = [$ q , InvalidServerResponse::class, new Response (500 ), 'Server 500 ' ];
258+ $ testData [] = [$ q , InvalidServerResponse::class, new Response (400 ), 'Server 400 ' ];
259+ $ testData [] = [$ q , InvalidServerResponse::class, new Response (401 ), 'Invalid credentials response ' ];
260+ $ testData [] = [$ q , QuotaExceeded::class, new Response (429 ), 'Quota exceeded response ' ];
261+ $ testData [] = [$ q , InvalidCredentials::class, new Response (200 ), 'Empty response ' ];
262+ }
291263
292- /**
293- * @expectedException \Geocoder\Exception\QuotaExceeded
294- */
295- public function testQuotaExceededResponseReverse ()
296- {
297- if ( isset ( $ this -> skippedTests [ __FUNCTION__ ])) {
298- $ this -> markTestSkipped ( $ this -> skippedTests [ __FUNCTION__ ]) ;
264+ if ( $ this -> testIpv6 ) {
265+ $ q = GeocodeQuery:: create ( ' 2001:0db8:0000:0042:0000:8a2e:0370:7334 ' );
266+ $ testData [] = [ $ q , InvalidServerResponse::class, new Response ( 500 ), ' Server 500 ' ];
267+ $ testData [] = [ $ q , InvalidServerResponse::class, new Response ( 400 ), ' Server 400 ' ];
268+ $ testData [] = [ $ q , InvalidServerResponse::class, new Response ( 401 ), ' Invalid credentials response ' ];
269+ $ testData [] = [ $ q , QuotaExceeded::class, new Response ( 429 ), ' Quota exceeded response ' ];
270+ $ testData [] = [ $ q , InvalidCredentials::class, new Response ( 200 ), ' Empty response ' ] ;
299271 }
300272
301- $ provider = $ this ->createProvider ($ this ->getHttpClient (new Response (429 )));
302- $ provider ->reverseQuery (ReverseQuery::fromCoordinates (0 , 0 ));
273+ return $ testData ;
303274 }
304275
305276 /**
0 commit comments