null, jamais d'exception). * * @internal */ final class BanGeocoderTest extends TestCase { public function testGeocodeMapsBanFeatureToCoordinates(): void { $geocoder = $this->geocoderReturning([ 'features' => [[ 'geometry' => ['coordinates' => [0.3404333, 46.5802596]], 'properties' => ['score' => 0.97], ]], ]); $coordinates = $geocoder->geocode('1 rue du Test, 86100 Châtellerault'); self::assertNotNull($coordinates); // GeoJSON = [longitude, latitude] : l'ordre doit etre inverse. self::assertSame('46.5802596', $coordinates->latitude); self::assertSame('0.3404333', $coordinates->longitude); } public function testGeocodeRoundsToSevenDecimals(): void { $geocoder = $this->geocoderReturning([ 'features' => [[ 'geometry' => ['coordinates' => [0.34043337777, 46.58025961111]], 'properties' => ['score' => 0.9], ]], ]); $coordinates = $geocoder->geocode('1 rue du Test'); self::assertNotNull($coordinates); self::assertSame('46.5802596', $coordinates->latitude); self::assertSame('0.3404334', $coordinates->longitude); } public function testGeocodeReturnsNullWhenScoreTooLow(): void { // Score < 0.4 : resultat juge non fiable (adresse etrangere, lieu-dit // inconnu) -> pas de coordonnees plutot qu'une position fantaisiste. $geocoder = $this->geocoderReturning([ 'features' => [[ 'geometry' => ['coordinates' => [0.34, 46.58]], 'properties' => ['score' => 0.12], ]], ]); self::assertNull($geocoder->geocode('Bahnhofstrasse 1, Zürich')); } public function testGeocodeReturnsNullWhenNoFeature(): void { $geocoder = $this->geocoderReturning(['features' => []]); self::assertNull($geocoder->geocode('zzz introuvable')); } public function testGeocodeReturnsNullOnServerError(): void { $client = new MockHttpClient(new MockResponse('oops', ['http_code' => 500])); $geocoder = new BanGeocoder($client, new NullLogger()); self::assertNull($geocoder->geocode('1 rue du Test')); } public function testGeocodeReturnsNullOnBlankAddressWithoutHttpCall(): void { $client = new MockHttpClient(static function (): never { self::fail('Aucun appel HTTP attendu pour une adresse vide.'); }); $geocoder = new BanGeocoder($client, new NullLogger()); self::assertNull($geocoder->geocode(' ')); } /** Fabrique un BanGeocoder repondant le payload JSON donne (HTTP 200). */ private function geocoderReturning(array $payload): BanGeocoder { $client = new MockHttpClient(new MockResponse( json_encode($payload, JSON_THROW_ON_ERROR), ['response_headers' => ['content-type' => 'application/json']], )); return new BanGeocoder($client, new NullLogger()); } }