|
|
@@ -0,0 +1,110 @@
|
|
|
+<?php
|
|
|
+namespace App\Tests\Service\Utils;
|
|
|
+
|
|
|
+use App\ApiResources\Utils\GpsCoordinate;
|
|
|
+use App\Service\Utils\GpsCoordinateUtils;
|
|
|
+use PHPUnit\Framework\TestCase;
|
|
|
+use Symfony\Component\HttpClient\MockHttpClient;
|
|
|
+use Symfony\Component\HttpClient\Response\MockResponse;
|
|
|
+use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
|
+
|
|
|
+class GpsCoordinateUtilsTest extends TestCase
|
|
|
+{
|
|
|
+ private $response;
|
|
|
+ private $responseReverse;
|
|
|
+
|
|
|
+ public function setUp(): void
|
|
|
+ {
|
|
|
+ $this->response = '[{"place_id":124047700,"licence":"Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright","osm_type":"way","osm_id":146744176,"boundingbox":["46.040309","46.0413942","6.584711","6.5864561"],"lat":"46.0405718","lon":"6.5857964","display_name":"Chemin des Rirets, La Frasse, Romme, Nancy-sur-Cluses, Bonneville, Haute-Savoie, Auvergne-Rhône-Alpes, France métropolitaine, 74300, France","place_rank":26,"category":"highway","type":"residential","importance":0.8099999999999999}]';
|
|
|
+ $this->responseReverse = '{"place_id":124302276,"licence":"Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright","osm_type":"way","osm_id":146744171,"lat":"46.04082792421903","lon":"6.585562827387761","place_rank":26,"category":"highway","type":"unclassified","importance":0.09999999999999998,"addresstype":"road","name":"Chemin des Larrets","display_name":"Chemin des Larrets, La Frasse, Romme, Nancy-sur-Cluses, Bonneville, Haute-Savoie, Auvergne-Rhône-Alpes, France métropolitaine, 74300, France","address":{"road":"Chemin des Larrets","hamlet":"La Frasse","village":"Romme","municipality":"Bonneville","county":"Haute-Savoie","state":"Auvergne-Rhône-Alpes","country":"France","postcode":"74300","country_code":"fr"},"boundingbox":["46.0404896","46.040881","6.585013","6.5874704"]}';
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @see GpsCoordinateUtils::searchGpsCoordinates()
|
|
|
+ */
|
|
|
+ public function testSearchGpsCoordinates():void
|
|
|
+ {
|
|
|
+ $responses = [new MockResponse($this->response, ['http_code' => 200])];
|
|
|
+ $client = new MockHttpClient($responses, 'https://nominatim.openstreetmap.org/');
|
|
|
+
|
|
|
+ $gpsCoordinateUtils = new GpsCoordinateUtils($client);
|
|
|
+ $content = $gpsCoordinateUtils->searchGpsCoordinates('11 chemin des rirtes', '74300', 'nancy-sur-cluses');
|
|
|
+
|
|
|
+ $this->assertIsArray($content);
|
|
|
+ $this->assertCount(1, $content);
|
|
|
+ $this->assertEquals("Chemin des Rirets, La Frasse, Romme, Nancy-sur-Cluses, Bonneville, Haute-Savoie, Auvergne-Rhône-Alpes, France métropolitaine, 74300, France", $content[0]['display_name']);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @see GpsCoordinateUtils::searchGpsCoordinates()
|
|
|
+ */
|
|
|
+ public function testSearchGpsCoordinatesFailed():void
|
|
|
+ {
|
|
|
+ $responses = [new MockResponse('...', ['http_code' => 404])];
|
|
|
+ $client = new MockHttpClient($responses, 'https://nominatim.openstreetmap.org/');
|
|
|
+
|
|
|
+ $gpsCoordinateUtils = new GpsCoordinateUtils($client);
|
|
|
+ $this->expectException(NotFoundHttpException::class);
|
|
|
+ $gpsCoordinateUtils->searchGpsCoordinates('...', '74300', '...');
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @see GpsCoordinateUtils::reverseGpsCoordinates()
|
|
|
+ */
|
|
|
+ public function testReverseGpsCoordinates():void
|
|
|
+ {
|
|
|
+ $responses = [new MockResponse($this->responseReverse, ['http_code' => 200])];
|
|
|
+ $client = new MockHttpClient($responses, 'https://nominatim.openstreetmap.org/');
|
|
|
+
|
|
|
+ $gpsCoordinateUtils = new GpsCoordinateUtils($client);
|
|
|
+ $content = $gpsCoordinateUtils->reverseGpsCoordinates(46.04085160266434, 6.585583549999978);
|
|
|
+
|
|
|
+ $this->assertIsArray($content);
|
|
|
+ $this->assertArrayHasKey('place_id', $content);
|
|
|
+ $this->assertEquals("Chemin des Larrets, La Frasse, Romme, Nancy-sur-Cluses, Bonneville, Haute-Savoie, Auvergne-Rhône-Alpes, France métropolitaine, 74300, France", $content['display_name']);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @see GpsCoordinateUtils::reverseGpsCoordinates()
|
|
|
+ */
|
|
|
+ public function testReverseGpsCoordinatesFailed():void
|
|
|
+ {
|
|
|
+ $responses = [new MockResponse('...', ['http_code' => 404])];
|
|
|
+ $client = new MockHttpClient($responses, 'https://nominatim.openstreetmap.org/');
|
|
|
+
|
|
|
+ $gpsCoordinateUtils = new GpsCoordinateUtils($client);
|
|
|
+ $this->expectException(NotFoundHttpException::class);
|
|
|
+ $gpsCoordinateUtils->reverseGpsCoordinates(0000, 0000);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @see GpsCoordinateUtils::createGpsCoordinate()
|
|
|
+ */
|
|
|
+ public function testCreateGpsCoordinate():void
|
|
|
+ {
|
|
|
+ $responses = [new MockResponse('...', ['http_code' => 200])];
|
|
|
+ $client = new MockHttpClient($responses, 'https://nominatim.openstreetmap.org/');
|
|
|
+
|
|
|
+ $gpsCoordinateUtils = new GpsCoordinateUtils($client);
|
|
|
+ $this->assertInstanceOf(GpsCoordinate::class, $gpsCoordinateUtils->createGpsCoordinate(json_decode($this->responseReverse, true)));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @see GpsCoordinateUtils::transformAddress()
|
|
|
+ */
|
|
|
+ public function testTransformAddress():void
|
|
|
+ {
|
|
|
+ $responses = [new MockResponse('...', ['http_code' => 200])];
|
|
|
+ $client = new MockHttpClient($responses, 'https://nominatim.openstreetmap.org/');
|
|
|
+
|
|
|
+ $gpsCoordinateUtils = new GpsCoordinateUtils($client);
|
|
|
+ $arrayAddress = json_decode($this->responseReverse, true);
|
|
|
+ $addressTransformed = $gpsCoordinateUtils->transformAddress($arrayAddress['address']);
|
|
|
+ $this->assertEquals([
|
|
|
+ "streetAddress" => "Chemin des Larrets",
|
|
|
+ "streetAddressSecond" => "La Frasse",
|
|
|
+ "city" => "Romme",
|
|
|
+ "cp" => "74300"
|
|
|
+ ], $addressTransformed);
|
|
|
+ }
|
|
|
+}
|