Skip to content

Commit f88670c

Browse files
committed
add tests for network issue, no results, invalid proxy
1 parent 7e1dac1 commit f88670c

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

tests/GeocoderTest.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,52 @@ public function testDisabledKey(): void
8989
$this->assertStringContainsString('disabled', $result['status']['message']);
9090
}
9191

92+
// https://opencagedata.com/api#testingkeys - query 'NOWHERE-INTERESTING' returns 200 with zero results
93+
public function testNoResults(): void
94+
{
95+
$geocoder = new Geocoder(self::OPENCAGE_TEST_APIKEY_200);
96+
$result = $geocoder->geocode('NOWHERE-INTERESTING');
97+
$this->assertEquals(200, $result['status']['code']);
98+
$this->assertEquals(0, $result['total_results']);
99+
$this->assertEmpty($result['results']);
100+
}
101+
102+
public function testWithOptionalParams(): void
103+
{
104+
$geocoder = new Geocoder(self::OPENCAGE_TEST_APIKEY_200);
105+
$result = $geocoder->geocode('82 Clerkenwell Road, London', [
106+
'language' => 'fr',
107+
'countrycode' => 'gb'
108+
]);
109+
$this->assertEquals(200, $result['status']['code']);
110+
}
111+
112+
public function testReverseGeocode(): void
113+
{
114+
$geocoder = new Geocoder(self::OPENCAGE_TEST_APIKEY_200);
115+
$result = $geocoder->geocode('51.5227,-0.1025');
116+
$this->assertEquals(200, $result['status']['code']);
117+
$this->assertGreaterThan(0, $result['total_results']);
118+
}
119+
120+
public function testInvalidProxy(): void
121+
{
122+
$geocoder = new Geocoder(self::OPENCAGE_TEST_APIKEY_200);
123+
$this->expectException(\Exception::class);
124+
$this->expectExceptionMessage('Invalid proxy URL');
125+
$geocoder->setProxy('not-a-valid-proxy');
126+
}
127+
128+
public function testSetTimeout(): void
129+
{
130+
$geocoder = new Geocoder(self::OPENCAGE_TEST_APIKEY_200);
131+
$geocoder->setTimeout(1);
132+
$geocoder->setHost('doesnotexist.opencagedata.com');
133+
$result = $geocoder->geocode('London');
134+
$this->assertEquals(498, $result['status']['code']);
135+
$this->assertStringContainsString('network issue', $result['status']['message']);
136+
}
137+
92138
public function testLondon(): void
93139
{
94140
$geocoder = new Geocoder(self::OPENCAGE_TEST_APIKEY_200);
@@ -124,6 +170,36 @@ public function testAsyncNetworkError(): void
124170
$this->assertStringContainsString('network issue', $result['status']['message']);
125171
}
126172

173+
public function testAsyncOverQuota(): void
174+
{
175+
$geocoder = new Geocoder(self::OPENCAGE_TEST_APIKEY_402);
176+
$promise = $geocoder->geocodeAsync('Johannesburg');
177+
/** @var array{status: array{code: int, message: string}} $result */
178+
$result = $promise->wait();
179+
180+
$this->assertEquals(402, $result['status']['code']);
181+
$this->assertEquals('quota exceeded', $result['status']['message']);
182+
}
183+
184+
public function testAsyncDisabledKey(): void
185+
{
186+
$geocoder = new Geocoder(self::OPENCAGE_TEST_APIKEY_403_DISABLED);
187+
$promise = $geocoder->geocodeAsync('Johannesburg');
188+
/** @var array{status: array{code: int, message: string}} $result */
189+
$result = $promise->wait();
190+
191+
$this->assertEquals(403, $result['status']['code']);
192+
$this->assertStringContainsString('disabled', $result['status']['message']);
193+
}
194+
195+
public function testAsyncMissingKey(): void
196+
{
197+
$geocoder = new Geocoder();
198+
$this->expectException(\Exception::class);
199+
$this->expectExceptionMessage('Missing API key');
200+
$geocoder->geocodeAsync('London');
201+
}
202+
127203
public function testProxy(): void
128204
{
129205
$proxy = getenv('PROXY');

0 commit comments

Comments
 (0)