66
77class GeocoderTest extends \PHPUnit \Framework \TestCase
88{
9+ // https://opencagedata.com/api#testingkeys
10+ private const OPENCAGE_TEST_APIKEY_200 = '6d0e711d72d74daeb2b0bfd2a5cdfdba ' ;
11+ private const OPENCAGE_TEST_APIKEY_402 = '4372eff77b8343cebfc843eb4da4ddc4 ' ;
12+ private const OPENCAGE_TEST_APIKEY_403_DISABLED = '2e10e5e828262eb243ec0b54681d699a ' ;
13+ private const OPENCAGE_TEST_APIKEY_403_IP_REJECTED = '6c79ee8e1ca44ad58ad1fc493ba9542f ' ;
14+ private const OPENCAGE_TEST_APIKEY_429 = 'd6d0f0065f4348a4bdfe4587ba02714b ' ;
15+
916 public function testMissingKey (): void
1017 {
1118 $ geocoder = new Geocoder ();
@@ -29,8 +36,7 @@ public function testInvalidKey(): void
2936
3037 public function testInvalidHost (): void
3138 {
32- // https://opencagedata.com/api#testingkeys
33- $ geocoder = new Geocoder ('6d0e711d72d74daeb2b0bfd2a5cdfdba ' );
39+ $ geocoder = new Geocoder (self ::OPENCAGE_TEST_APIKEY_200 );
3440 try {
3541 $ geocoder ->setHost ('www.example.com ' );
3642 } catch (\Exception $ e ) {
@@ -42,8 +48,7 @@ public function testInvalidHost(): void
4248
4349 public function testNetworkRequestError (): void
4450 {
45- // https://opencagedata.com/api#testingkeys
46- $ geocoder = new Geocoder ('6d0e711d72d74daeb2b0bfd2a5cdfdba ' );
51+ $ geocoder = new Geocoder (self ::OPENCAGE_TEST_APIKEY_200 );
4752 $ geocoder ->setHost ('doesnotexist.opencagedata.com ' );
4853 $ result = $ geocoder ->geocode ('London ' );
4954 // print_r($result);
@@ -55,17 +60,38 @@ public function testNetworkRequestError(): void
5560
5661 public function testOverQuota (): void
5762 {
58- // https://opencagedata.com/api#testingkeys
59- $ geocoder = new Geocoder ('4372eff77b8343cebfc843eb4da4ddc4 ' );
63+ $ geocoder = new Geocoder (self ::OPENCAGE_TEST_APIKEY_402 );
6064 $ result = $ geocoder ->geocode ('Johannesburg ' );
6165 $ this ->assertEquals (402 , $ result ['status ' ]['code ' ]);
6266 $ this ->assertEquals ('quota exceeded ' , $ result ['status ' ]['message ' ]);
6367 }
6468
69+ public function testRateLimited (): void
70+ {
71+ $ geocoder = new Geocoder (self ::OPENCAGE_TEST_APIKEY_429 );
72+ $ result = $ geocoder ->geocode ('Johannesburg ' );
73+ $ this ->assertEquals (429 , $ result ['status ' ]['code ' ]);
74+ }
75+
76+ public function testIpRejected (): void
77+ {
78+ $ geocoder = new Geocoder (self ::OPENCAGE_TEST_APIKEY_403_IP_REJECTED );
79+ $ result = $ geocoder ->geocode ('Johannesburg ' );
80+ $ this ->assertEquals (403 , $ result ['status ' ]['code ' ]);
81+ $ this ->assertStringContainsString ('IP address rejected ' , $ result ['status ' ]['message ' ]);
82+ }
83+
84+ public function testDisabledKey (): void
85+ {
86+ $ geocoder = new Geocoder (self ::OPENCAGE_TEST_APIKEY_403_DISABLED );
87+ $ result = $ geocoder ->geocode ('Johannesburg ' );
88+ $ this ->assertEquals (403 , $ result ['status ' ]['code ' ]);
89+ $ this ->assertStringContainsString ('disabled ' , $ result ['status ' ]['message ' ]);
90+ }
91+
6592 public function testLondon (): void
6693 {
67- // https://opencagedata.com/api#testingkeys
68- $ geocoder = new Geocoder ('6d0e711d72d74daeb2b0bfd2a5cdfdba ' );
94+ $ geocoder = new Geocoder (self ::OPENCAGE_TEST_APIKEY_200 );
6995 $ query = "82 Clerkenwell Road, London " ;
7096 $ result = $ geocoder ->geocode ($ query );
7197
@@ -77,8 +103,7 @@ public function testLondon(): void
77103
78104 public function testAsyncLondon (): void
79105 {
80- // https://opencagedata.com/api#testingkeys
81- $ geocoder = new Geocoder ('6d0e711d72d74daeb2b0bfd2a5cdfdba ' );
106+ $ geocoder = new Geocoder (self ::OPENCAGE_TEST_APIKEY_200 );
82107 $ promise = $ geocoder ->geocodeAsync ("82 Clerkenwell Road, London " );
83108 /** @var array{status: array{code: int, message: string}} $result */
84109 $ result = $ promise ->wait ();
@@ -89,8 +114,7 @@ public function testAsyncLondon(): void
89114
90115 public function testAsyncNetworkError (): void
91116 {
92- // https://opencagedata.com/api#testingkeys
93- $ geocoder = new Geocoder ('6d0e711d72d74daeb2b0bfd2a5cdfdba ' );
117+ $ geocoder = new Geocoder (self ::OPENCAGE_TEST_APIKEY_200 );
94118 $ geocoder ->setHost ('doesnotexist.opencagedata.com ' );
95119 $ promise = $ geocoder ->geocodeAsync ('London ' );
96120 /** @var array{status: array{code: int, message: string}} $result */
@@ -106,8 +130,7 @@ public function testProxy(): void
106130 if (!$ proxy ) {
107131 $ this ->markTestSkipped ('PROXY environment variable not set ' );
108132 }
109- // https://opencagedata.com/api#testingkeys
110- $ geocoder = new Geocoder ('6d0e711d72d74daeb2b0bfd2a5cdfdba ' );
133+ $ geocoder = new Geocoder (self ::OPENCAGE_TEST_APIKEY_200 );
111134 try {
112135 $ geocoder ->setProxy ($ proxy );
113136 } catch (\Exception $ e ) {
0 commit comments