GpsCoordinateUtilsTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. use Symfony\Contracts\HttpClient\HttpClientInterface;
  10. use Symfony\Contracts\HttpClient\ResponseInterface;
  11. class GpsCoordinateUtilsTest extends TestCase
  12. {
  13. private HttpClientInterface $client;
  14. private string $response;
  15. private string $responseReverse;
  16. public function setUp(): void
  17. {
  18. $this->client = $this->getMockBuilder(HttpClientInterface::class)->disableOriginalConstructor()->getMock();
  19. $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}]';
  20. $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"]}';
  21. }
  22. /**
  23. * @see GpsCoordinateUtils::searchGpsCoordinates()
  24. */
  25. public function testSearchGpsCoordinates():void
  26. {
  27. $gpsCoordinateUtils = $this->getMockBuilder(GpsCoordinateUtils::class)
  28. ->setConstructorArgs([$this->client])
  29. ->setMethodsExcept(['searchGpsCoordinates'])
  30. ->getMock();
  31. $response = $this->getMockBuilder(ResponseInterface::class)->getMock();
  32. $response->method('getContent')->willReturn('{"data":1}'); // dummy response data
  33. $this->client
  34. ->expects(self::once())
  35. ->method('request')
  36. ->with(
  37. 'GET',
  38. 'search?addressdetails=1&format=json&limit=10&street=11 chemin des rirtes&postalcode=74300&city=nancy-sur-cluses'
  39. )
  40. ->willReturn($response);
  41. $content = $gpsCoordinateUtils->searchGpsCoordinates('11 chemin des rirtes', '74300', 'nancy-sur-cluses');
  42. $this->assertEquals(['data' => 1], $content);
  43. }
  44. /**
  45. * @see GpsCoordinateUtils::searchGpsCoordinates()
  46. */
  47. public function testSearchGpsCoordinatesFailed():void
  48. {
  49. $gpsCoordinateUtils = $this->getMockBuilder(GpsCoordinateUtils::class)
  50. ->setConstructorArgs([$this->client])
  51. ->setMethodsExcept(['searchGpsCoordinates'])
  52. ->getMock();
  53. $this->client
  54. ->expects(self::once())
  55. ->method('request')
  56. ->willThrowException(new \Exception());
  57. $this->expectException(NotFoundHttpException::class);
  58. $gpsCoordinateUtils->searchGpsCoordinates('...', '74300', '...');
  59. }
  60. /**
  61. * @see GpsCoordinateUtils::reverseGpsCoordinates()
  62. */
  63. public function testReverseGpsCoordinates():void
  64. {
  65. $gpsCoordinateUtils = $this->getMockBuilder(GpsCoordinateUtils::class)
  66. ->setConstructorArgs([$this->client])
  67. ->setMethodsExcept(['reverseGpsCoordinates'])
  68. ->getMock();
  69. $response = $this->getMockBuilder(ResponseInterface::class)->getMock();
  70. $response->method('getContent')->willReturn('{"data":1}'); // dummy response data
  71. $this->client
  72. ->expects(self::once())
  73. ->method('request')
  74. ->with(
  75. 'GET',
  76. 'reverse?addressdetails=1&format=json&lat=46.040851602664&lon=6.58558355'
  77. )
  78. ->willReturn($response);
  79. $content = $gpsCoordinateUtils->reverseGpsCoordinates(46.04085160266434, 6.585583549999978);
  80. $this->assertEquals(['data' => 1], $content);
  81. }
  82. /**
  83. * @see GpsCoordinateUtils::reverseGpsCoordinates()
  84. */
  85. public function testReverseGpsCoordinatesFailed():void
  86. {
  87. $gpsCoordinateUtils = $this->getMockBuilder(GpsCoordinateUtils::class)
  88. ->setConstructorArgs([$this->client])
  89. ->setMethodsExcept(['reverseGpsCoordinates'])
  90. ->getMock();
  91. $this->client
  92. ->expects(self::once())
  93. ->method('request')
  94. ->willThrowException(new \Exception());
  95. $this->expectException(NotFoundHttpException::class);
  96. $gpsCoordinateUtils->reverseGpsCoordinates(0000, 0000);
  97. }
  98. /**
  99. * @see GpsCoordinateUtils::createGpsCoordinate()
  100. */
  101. public function testCreateGpsCoordinate():void
  102. {
  103. $gpsCoordinateUtils = $this->getMockBuilder(GpsCoordinateUtils::class)
  104. ->setConstructorArgs([$this->client])
  105. ->setMethodsExcept(['createGpsCoordinate'])
  106. ->getMock();
  107. $gpsApiResponse = [
  108. 'address' => ['foo'],
  109. 'lat' => 123.4,
  110. 'lon' => 456.7,
  111. ];
  112. $gpsCoordinateUtils
  113. ->expects(self::once())
  114. ->method('transformAddress')
  115. ->with(['foo'])
  116. ->willReturn(
  117. [
  118. 'city' => 'bar',
  119. 'cp' => '12345',
  120. 'streetAddress' => '1 rue lambda',
  121. 'streetAddressSecond' => 'cedex 1'
  122. ]
  123. );
  124. $gpsCoordinate = $gpsCoordinateUtils->createGpsCoordinate($gpsApiResponse);
  125. $this->assertEquals(123.4, $gpsCoordinate->getLatitude());
  126. $this->assertEquals(456.7, $gpsCoordinate->getLongitude());
  127. $this->assertEquals('bar', $gpsCoordinate->getCity());
  128. $this->assertEquals('1 rue lambda', $gpsCoordinate->getStreetAddress());
  129. $this->assertEquals('cedex 1', $gpsCoordinate->getStreetAddressSecond());
  130. }
  131. /**
  132. * @see GpsCoordinateUtils::transformAddress()
  133. */
  134. public function testTransformAddress():void
  135. {
  136. $gpsCoordinateUtils = $this->getMockBuilder(GpsCoordinateUtils::class)
  137. ->setConstructorArgs([$this->client])
  138. ->setMethodsExcept(['transformAddress'])
  139. ->getMock();
  140. $arrayAddress = [
  141. 'road' => 'foo',
  142. 'hamlet' => 'bar',
  143. 'town' => 'alpha',
  144. 'postcode' => '12345'
  145. ];
  146. $addressTransformed = $gpsCoordinateUtils->transformAddress($arrayAddress);
  147. $this->assertEquals([
  148. "streetAddress" => "foo",
  149. "streetAddressSecond" => "bar",
  150. "city" => "alpha",
  151. "cp" => "12345"
  152. ], $addressTransformed);
  153. }
  154. }