From 2a792c774df48c14cdbb70bcf19674f4a4e237c6 Mon Sep 17 00:00:00 2001 From: Nagy Attila Gabor Date: Tue, 24 Oct 2023 18:46:46 +0200 Subject: [PATCH 1/5] [Pelias] Fix admin level ordering Order of the adminLeves in Pelias did not have any particular order. I have changed the order according to this ticket: https://github.com/geocoder-php/Geocoder/issues/852 Please note that this might be a breaking change for someone. --- src/Provider/Pelias/CHANGELOG.md | 1 + src/Provider/Pelias/Pelias.php | 2 +- src/Provider/Pelias/Tests/AdminLevelTest.php | 74 ++++++++++++++++++++ 3 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 src/Provider/Pelias/Tests/AdminLevelTest.php diff --git a/src/Provider/Pelias/CHANGELOG.md b/src/Provider/Pelias/CHANGELOG.md index 0ae88d80a..dc4ddf41d 100644 --- a/src/Provider/Pelias/CHANGELOG.md +++ b/src/Provider/Pelias/CHANGELOG.md @@ -16,6 +16,7 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" betwee ### Changed - Migrate from PHP-HTTP to PSR-18 client +- Admin levels are numbered in a strict top->down order. 1 is the Country level, 5 is the locality level. ## 1.3.0 diff --git a/src/Provider/Pelias/Pelias.php b/src/Provider/Pelias/Pelias.php index 3bf9a79cb..65c3c03c7 100644 --- a/src/Provider/Pelias/Pelias.php +++ b/src/Provider/Pelias/Pelias.php @@ -151,7 +151,7 @@ protected function executeQuery(string $url): AddressCollection $props = $location['properties']; $adminLevels = []; - foreach (['region', 'county', 'locality', 'macroregion', 'country'] as $i => $component) { + foreach (['country', 'macroregion', 'region', 'county', 'locality'] as $i => $component) { if (isset($props[$component])) { $adminLevels[] = ['name' => $props[$component], 'level' => $i + 1]; } diff --git a/src/Provider/Pelias/Tests/AdminLevelTest.php b/src/Provider/Pelias/Tests/AdminLevelTest.php new file mode 100644 index 000000000..4a0ab42db --- /dev/null +++ b/src/Provider/Pelias/Tests/AdminLevelTest.php @@ -0,0 +1,74 @@ +getMockedHttpClient($response), 'http://localhost/'); + $result = $provider->geocodeQuery(GeocodeQuery::create('foobar')); + + $this->assertInstanceOf(Collection::class, $result); + $this->assertEquals(1, $result->count()); + $address = $result->get(0); + + $expectedOrder= [ + 'COUNTRY', + 'MACROREGION', + 'REGION', + 'COUNTY', + 'LOCALITY', + ]; + + foreach ($address->getAdminLevels()->all() as $key => $adminLevel) { + $this->assertEquals($expectedOrder[$key-1], $adminLevel->getName()); + $this->assertEquals($key, $adminLevel->getLevel(), 'Invalid admin level number for level '.$adminLevel->getName()); + } + } +} \ No newline at end of file From a573a8587b01d6c1d65e107de2e32fee2ace58ac Mon Sep 17 00:00:00 2001 From: Nagy Attila Gabor Date: Thu, 26 Oct 2023 10:40:09 +0200 Subject: [PATCH 2/5] [OpenRouteService] updated unit tests due to changes in Pelias provider AdminLevels are now ordered according to the following principles https://github.com/geocoder-php/Geocoder/issues/852 --- src/Provider/OpenRouteService/CHANGELOG.md | 7 +++++++ .../Tests/OpenRouteServiceTest.php | 20 +++++++++---------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/Provider/OpenRouteService/CHANGELOG.md b/src/Provider/OpenRouteService/CHANGELOG.md index 78add6be9..e56d8dc23 100644 --- a/src/Provider/OpenRouteService/CHANGELOG.md +++ b/src/Provider/OpenRouteService/CHANGELOG.md @@ -2,6 +2,13 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release. +## 1.4.0 + +### Changed + + - Admin levels are numbered in a strict top->down order. 1 is the Country level, 5 is the locality level. + + ## 1.3.0 ### Added diff --git a/src/Provider/OpenRouteService/Tests/OpenRouteServiceTest.php b/src/Provider/OpenRouteService/Tests/OpenRouteServiceTest.php index 938794b7e..7887478b2 100644 --- a/src/Provider/OpenRouteService/Tests/OpenRouteServiceTest.php +++ b/src/Provider/OpenRouteService/Tests/OpenRouteServiceTest.php @@ -60,7 +60,7 @@ public function testGeocodeWithRealAddress(): void $this->assertEquals('Acklam Road', $result->getStreetName()); $this->assertEquals('London', $result->getLocality()); $this->assertCount(4, $result->getAdminLevels()); - $this->assertEquals('London', $result->getAdminLevels()->get(3)->getName()); + $this->assertEquals('London', $result->getAdminLevels()->get(5)->getName()); $this->assertEquals('United Kingdom', $result->getCountry()->getName()); $this->assertEquals('GBR', $result->getCountry()->getCode()); } @@ -87,8 +87,8 @@ public function testReverseWithRealCoordinates(): void $this->assertEquals('LA1 1UH', $result->getPostalCode()); $this->assertEquals('Lancaster', $result->getLocality()); $this->assertCount(5, $result->getAdminLevels()); - $this->assertEquals('Lancashire', $result->getAdminLevels()->get(1)->getName()); - $this->assertEquals('England', $result->getAdminLevels()->get(4)->getName()); + $this->assertEquals('England', $result->getAdminLevels()->get(2)->getName()); + $this->assertEquals('Lancashire', $result->getAdminLevels()->get(4)->getName()); $this->assertEquals('United Kingdom', $result->getCountry()->getName()); $this->assertEquals('GBR', $result->getCountry()->getCode()); } @@ -130,8 +130,8 @@ public function testGeocodeWithCity(): void $this->assertEqualsWithDelta(9.787455, $result->getCoordinates()->getLongitude(), 0.01); $this->assertEquals('Hanover', $result->getLocality()); $this->assertCount(4, $result->getAdminLevels()); - $this->assertEquals('Niedersachsen', $result->getAdminLevels()->get(1)->getName()); - $this->assertEquals('Hanover', $result->getAdminLevels()->get(3)->getName()); + $this->assertEquals('Niedersachsen', $result->getAdminLevels()->get(3)->getName()); + $this->assertEquals('Hanover', $result->getAdminLevels()->get(5)->getName()); $this->assertEquals('Germany', $result->getCountry()->getName()); /** @var \Geocoder\Model\Address $result */ @@ -140,7 +140,7 @@ public function testGeocodeWithCity(): void $this->assertEqualsWithDelta(52.37362, $result->getCoordinates()->getLatitude(), 0.01); $this->assertEqualsWithDelta(9.73711, $result->getCoordinates()->getLongitude(), 0.01); $this->assertCount(3, $result->getAdminLevels()); - $this->assertEquals('Niedersachsen', $result->getAdminLevels()->get(1)->getName()); + $this->assertEquals('Niedersachsen', $result->getAdminLevels()->get(3)->getName()); $this->assertEquals('Germany', $result->getCountry()->getName()); /** @var \Geocoder\Model\Address $result */ @@ -150,7 +150,7 @@ public function testGeocodeWithCity(): void $this->assertEqualsWithDelta(-78.107687, $result->getCoordinates()->getLongitude(), 0.01); $this->assertNull($result->getLocality()); $this->assertCount(2, $result->getAdminLevels()); - $this->assertEquals('Hanover', $result->getAdminLevels()->get(1)->getName()); + $this->assertEquals('Hanover', $result->getAdminLevels()->get(3)->getName()); $this->assertEquals('Jamaica', $result->getCountry()->getName()); /** @var \Geocoder\Model\Address $result */ @@ -160,7 +160,7 @@ public function testGeocodeWithCity(): void $this->assertEqualsWithDelta(-76.724140000000006, $result->getCoordinates()->getLongitude(), 0.01); $this->assertEquals('Hanover', $result->getLocality()); $this->assertCount(4, $result->getAdminLevels()); - $this->assertEquals('Hanover', $result->getAdminLevels()->get(3)->getName()); + $this->assertEquals('Hanover', $result->getAdminLevels()->get(5)->getName()); $this->assertEquals('United States', $result->getCountry()->getName()); } @@ -186,8 +186,8 @@ public function testGeocodeWithCityDistrict(): void $this->assertEquals(60437, $result->getPostalCode()); $this->assertEquals('Frankfurt', $result->getLocality()); $this->assertCount(4, $result->getAdminLevels()); - $this->assertEquals('Frankfurt', $result->getAdminLevels()->get(2)->getName()); - $this->assertEquals('Hessen', $result->getAdminLevels()->get(1)->getName()); + $this->assertEquals('Frankfurt', $result->getAdminLevels()->get(4)->getName()); + $this->assertEquals('Hessen', $result->getAdminLevels()->get(3)->getName()); $this->assertNull($result->getAdminLevels()->get(1)->getCode()); $this->assertEquals('Germany', $result->getCountry()->getName()); $this->assertEquals('DEU', $result->getCountry()->getCode()); From 4645cf5bc375f0026d00d81538c6f358539eaedd Mon Sep 17 00:00:00 2001 From: Nagy Attila Gabor Date: Thu, 26 Oct 2023 10:53:59 +0200 Subject: [PATCH 3/5] [GeocodeEarth] updated unit tests due to changes in Pelias provider AdminLevels are now ordered according to the following principles https://github.com/geocoder-php/Geocoder/issues/852 --- src/Provider/GeocodeEarth/CHANGELOG.md | 1 + .../GeocodeEarth/Tests/GeocodeEarthTest.php | 20 +++++++++---------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/Provider/GeocodeEarth/CHANGELOG.md b/src/Provider/GeocodeEarth/CHANGELOG.md index bf55e9c58..3e0044067 100644 --- a/src/Provider/GeocodeEarth/CHANGELOG.md +++ b/src/Provider/GeocodeEarth/CHANGELOG.md @@ -16,6 +16,7 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" betwee ### Changed - Migrate from PHP-HTTP to PSR-18 client +- Admin levels are numbered in a strict top->down order. 1 is the Country level, 5 is the locality level. ## 1.3.0 diff --git a/src/Provider/GeocodeEarth/Tests/GeocodeEarthTest.php b/src/Provider/GeocodeEarth/Tests/GeocodeEarthTest.php index ec72b5383..617a810cd 100644 --- a/src/Provider/GeocodeEarth/Tests/GeocodeEarthTest.php +++ b/src/Provider/GeocodeEarth/Tests/GeocodeEarthTest.php @@ -60,7 +60,7 @@ public function testGeocodeWithRealAddress(): void $this->assertEquals('Acklam Road', $result->getStreetName()); $this->assertEquals('London', $result->getLocality()); $this->assertCount(3, $result->getAdminLevels()); - $this->assertEquals('London', $result->getAdminLevels()->get(3)->getName()); + $this->assertEquals('London', $result->getAdminLevels()->get(5)->getName()); $this->assertEquals('United Kingdom', $result->getCountry()->getName()); $this->assertEquals('GBR', $result->getCountry()->getCode()); } @@ -87,8 +87,8 @@ public function testReverseWithRealCoordinates(): void $this->assertEquals('LA1 1UH', $result->getPostalCode()); $this->assertEquals('Lancaster', $result->getLocality()); $this->assertCount(4, $result->getAdminLevels()); - $this->assertEquals('Lancashire', $result->getAdminLevels()->get(1)->getName()); - $this->assertEquals('England', $result->getAdminLevels()->get(4)->getName()); + $this->assertEquals('England', $result->getAdminLevels()->get(2)->getName()); + $this->assertEquals('Lancashire', $result->getAdminLevels()->get(3)->getName()); $this->assertEquals('United Kingdom', $result->getCountry()->getName()); $this->assertEquals('GBR', $result->getCountry()->getCode()); } @@ -130,8 +130,8 @@ public function testGeocodeWithCity(): void $this->assertEqualsWithDelta(9.787455, $result->getCoordinates()->getLongitude(), 0.01); $this->assertEquals('Hanover', $result->getLocality()); $this->assertCount(4, $result->getAdminLevels()); - $this->assertEquals('Niedersachsen', $result->getAdminLevels()->get(1)->getName()); - $this->assertEquals('Hanover', $result->getAdminLevels()->get(3)->getName()); + $this->assertEquals('Niedersachsen', $result->getAdminLevels()->get(3)->getName()); + $this->assertEquals('Hanover', $result->getAdminLevels()->get(5)->getName()); $this->assertEquals('Germany', $result->getCountry()->getName()); /** @var \Geocoder\Model\Address $result */ @@ -140,7 +140,7 @@ public function testGeocodeWithCity(): void $this->assertEqualsWithDelta(52.37362, $result->getCoordinates()->getLatitude(), 0.01); $this->assertEqualsWithDelta(9.73711, $result->getCoordinates()->getLongitude(), 0.01); $this->assertCount(3, $result->getAdminLevels()); - $this->assertEquals('Niedersachsen', $result->getAdminLevels()->get(1)->getName()); + $this->assertEquals('Niedersachsen', $result->getAdminLevels()->get(3)->getName()); $this->assertEquals('Germany', $result->getCountry()->getName()); /** @var \Geocoder\Model\Address $result */ @@ -150,7 +150,7 @@ public function testGeocodeWithCity(): void $this->assertEqualsWithDelta(-78.107687, $result->getCoordinates()->getLongitude(), 0.01); $this->assertNull($result->getLocality()); $this->assertCount(2, $result->getAdminLevels()); - $this->assertEquals('Hanover', $result->getAdminLevels()->get(1)->getName()); + $this->assertEquals('Hanover', $result->getAdminLevels()->get(3)->getName()); $this->assertEquals('Jamaica', $result->getCountry()->getName()); /** @var \Geocoder\Model\Address $result */ @@ -160,7 +160,7 @@ public function testGeocodeWithCity(): void $this->assertEqualsWithDelta(-76.724140000000006, $result->getCoordinates()->getLongitude(), 0.01); $this->assertEquals('Hanover', $result->getLocality()); $this->assertCount(4, $result->getAdminLevels()); - $this->assertEquals('Hanover', $result->getAdminLevels()->get(3)->getName()); + $this->assertEquals('Hanover', $result->getAdminLevels()->get(5)->getName()); $this->assertEquals('United States', $result->getCountry()->getName()); } @@ -186,8 +186,8 @@ public function testGeocodeWithCityDistrict(): void $this->assertEquals(60437, $result->getPostalCode()); $this->assertEquals('Frankfurt', $result->getLocality()); $this->assertCount(4, $result->getAdminLevels()); - $this->assertEquals('Frankfurt', $result->getAdminLevels()->get(2)->getName()); - $this->assertEquals('Hessen', $result->getAdminLevels()->get(1)->getName()); + $this->assertEquals('Frankfurt', $result->getAdminLevels()->get(4)->getName()); + $this->assertEquals('Hessen', $result->getAdminLevels()->get(3)->getName()); $this->assertNull($result->getAdminLevels()->get(1)->getCode()); $this->assertEquals('Germany', $result->getCountry()->getName()); $this->assertEquals('DEU', $result->getCountry()->getCode()); From 8c4c54135add2155dea9000ced3ccaf8f0cb74b1 Mon Sep 17 00:00:00 2001 From: Nagy Attila Gabor Date: Fri, 27 Oct 2023 01:39:13 +0200 Subject: [PATCH 4/5] [Pelias] changes in unit tests Using assertSame instead of assertEquals --- src/Provider/Pelias/Tests/AdminLevelTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Provider/Pelias/Tests/AdminLevelTest.php b/src/Provider/Pelias/Tests/AdminLevelTest.php index 4a0ab42db..80aa62b1c 100644 --- a/src/Provider/Pelias/Tests/AdminLevelTest.php +++ b/src/Provider/Pelias/Tests/AdminLevelTest.php @@ -55,10 +55,10 @@ public function testAdminLevelsOrder() $result = $provider->geocodeQuery(GeocodeQuery::create('foobar')); $this->assertInstanceOf(Collection::class, $result); - $this->assertEquals(1, $result->count()); + $this->assertSame(1, $result->count()); $address = $result->get(0); - $expectedOrder= [ + $expectedOrder = [ 'COUNTRY', 'MACROREGION', 'REGION', @@ -67,8 +67,8 @@ public function testAdminLevelsOrder() ]; foreach ($address->getAdminLevels()->all() as $key => $adminLevel) { - $this->assertEquals($expectedOrder[$key-1], $adminLevel->getName()); - $this->assertEquals($key, $adminLevel->getLevel(), 'Invalid admin level number for level '.$adminLevel->getName()); + $this->assertSame($expectedOrder[$key-1], $adminLevel->getName()); + $this->assertSame($key, $adminLevel->getLevel(), 'Invalid admin level number for level '.$adminLevel->getName()); } } } \ No newline at end of file From 5953b92e513b77ed58b8d6615313f7b66b679bdd Mon Sep 17 00:00:00 2001 From: Nagy Attila Gabor Date: Fri, 27 Oct 2023 23:04:19 +0200 Subject: [PATCH 5/5] [Pelias] phpstan and csfixer changes on modified code --- src/Provider/Pelias/Tests/AdminLevelTest.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Provider/Pelias/Tests/AdminLevelTest.php b/src/Provider/Pelias/Tests/AdminLevelTest.php index 80aa62b1c..8f291acce 100644 --- a/src/Provider/Pelias/Tests/AdminLevelTest.php +++ b/src/Provider/Pelias/Tests/AdminLevelTest.php @@ -19,13 +19,12 @@ class AdminLevelTest extends BaseTestCase { - protected function getCacheDir(): string { return __DIR__.'/.cached_responses'; } - public function testAdminLevelsOrder() + public function testAdminLevelsOrder(): void { $response = ' { @@ -55,7 +54,7 @@ public function testAdminLevelsOrder() $result = $provider->geocodeQuery(GeocodeQuery::create('foobar')); $this->assertInstanceOf(Collection::class, $result); - $this->assertSame(1, $result->count()); + $this->assertCount(1, $result); $address = $result->get(0); $expectedOrder = [ @@ -67,8 +66,8 @@ public function testAdminLevelsOrder() ]; foreach ($address->getAdminLevels()->all() as $key => $adminLevel) { - $this->assertSame($expectedOrder[$key-1], $adminLevel->getName()); + $this->assertSame($expectedOrder[$key - 1], $adminLevel->getName()); $this->assertSame($key, $adminLevel->getLevel(), 'Invalid admin level number for level '.$adminLevel->getName()); } } -} \ No newline at end of file +}