GpsCoordinateUtilsTest.php 6.1 KB

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