GpsCoordinateUtilsTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace App\Tests\Service\Utils;
  3. use App\ApiResources\Utils\GpsCoordinate;
  4. use App\Service\Utils\GpsCoordinateUtils;
  5. use PHPUnit\Framework\TestCase;
  6. use Symfony\Component\HttpClient\MockHttpClient;
  7. use Symfony\Component\HttpClient\Response\MockResponse;
  8. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  9. class GpsCoordinateUtilsTest extends TestCase
  10. {
  11. private string $response;
  12. private string $responseReverse;
  13. public function setUp(): void
  14. {
  15. $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}]';
  16. $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"]}';
  17. }
  18. /**
  19. * @see GpsCoordinateUtils::searchGpsCoordinates()
  20. */
  21. public function testSearchGpsCoordinates():void
  22. {
  23. $responses = [new MockResponse($this->response, ['http_code' => 200])];
  24. $client = new MockHttpClient($responses, 'https://nominatim.openstreetmap.org/');
  25. $gpsCoordinateUtils = new GpsCoordinateUtils($client);
  26. $content = $gpsCoordinateUtils->searchGpsCoordinates('11 chemin des rirtes', '74300', 'nancy-sur-cluses');
  27. $this->assertIsArray($content);
  28. $this->assertCount(1, $content);
  29. $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']);
  30. }
  31. /**
  32. * @see GpsCoordinateUtils::searchGpsCoordinates()
  33. */
  34. public function testSearchGpsCoordinatesFailed():void
  35. {
  36. $responses = [new MockResponse('...', ['http_code' => 404])];
  37. $client = new MockHttpClient($responses, 'https://nominatim.openstreetmap.org/');
  38. $gpsCoordinateUtils = new GpsCoordinateUtils($client);
  39. $this->expectException(NotFoundHttpException::class);
  40. $gpsCoordinateUtils->searchGpsCoordinates('...', '74300', '...');
  41. }
  42. /**
  43. * @see GpsCoordinateUtils::reverseGpsCoordinates()
  44. */
  45. public function testReverseGpsCoordinates():void
  46. {
  47. $responses = [new MockResponse($this->responseReverse, ['http_code' => 200])];
  48. $client = new MockHttpClient($responses, 'https://nominatim.openstreetmap.org/');
  49. $gpsCoordinateUtils = new GpsCoordinateUtils($client);
  50. $content = $gpsCoordinateUtils->reverseGpsCoordinates(46.04085160266434, 6.585583549999978);
  51. $this->assertIsArray($content);
  52. $this->assertArrayHasKey('place_id', $content);
  53. $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']);
  54. }
  55. /**
  56. * @see GpsCoordinateUtils::reverseGpsCoordinates()
  57. */
  58. public function testReverseGpsCoordinatesFailed():void
  59. {
  60. $responses = [new MockResponse('...', ['http_code' => 404])];
  61. $client = new MockHttpClient($responses, 'https://nominatim.openstreetmap.org/');
  62. $gpsCoordinateUtils = new GpsCoordinateUtils($client);
  63. $this->expectException(NotFoundHttpException::class);
  64. $gpsCoordinateUtils->reverseGpsCoordinates(0000, 0000);
  65. }
  66. /**
  67. * @see GpsCoordinateUtils::createGpsCoordinate()
  68. */
  69. public function testCreateGpsCoordinate():void
  70. {
  71. $responses = [new MockResponse('...', ['http_code' => 200])];
  72. $client = new MockHttpClient($responses, 'https://nominatim.openstreetmap.org/');
  73. $gpsCoordinateUtils = new GpsCoordinateUtils($client);
  74. $this->assertInstanceOf(GpsCoordinate::class, $gpsCoordinateUtils->createGpsCoordinate(json_decode($this->responseReverse, true)));
  75. }
  76. /**
  77. * @see GpsCoordinateUtils::transformAddress()
  78. */
  79. public function testTransformAddress():void
  80. {
  81. $responses = [new MockResponse('...', ['http_code' => 200])];
  82. $client = new MockHttpClient($responses, 'https://nominatim.openstreetmap.org/');
  83. $gpsCoordinateUtils = new GpsCoordinateUtils($client);
  84. $arrayAddress = json_decode($this->responseReverse, true);
  85. $addressTransformed = $gpsCoordinateUtils->transformAddress($arrayAddress['address']);
  86. $this->assertEquals([
  87. "streetAddress" => "Chemin des Larrets",
  88. "streetAddressSecond" => "La Frasse",
  89. "city" => "Romme",
  90. "cp" => "74300"
  91. ], $addressTransformed);
  92. }
  93. }