GpsCoordinateUtilsTest.php 5.7 KB

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