| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- <?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;
- use Symfony\Contracts\HttpClient\HttpClientInterface;
- use Symfony\Contracts\HttpClient\ResponseInterface;
- class GpsCoordinateUtilsTest extends TestCase
- {
- private HttpClientInterface $client;
- private string $response;
- private string $responseReverse;
- public function setUp(): void
- {
- $this->client = $this->getMockBuilder(HttpClientInterface::class)->disableOriginalConstructor()->getMock();
- $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
- {
- $gpsCoordinateUtils = $this->getMockBuilder(GpsCoordinateUtils::class)
- ->setConstructorArgs([$this->client])
- ->setMethodsExcept(['searchGpsCoordinates'])
- ->getMock();
- $response = $this->getMockBuilder(ResponseInterface::class)->getMock();
- $response->method('getContent')->willReturn('{"data":1}'); // dummy response data
- $this->client
- ->expects(self::once())
- ->method('request')
- ->with(
- 'GET',
- 'search?addressdetails=1&format=json&limit=10&street=11 chemin des rirtes&postalcode=74300&city=nancy-sur-cluses'
- )
- ->willReturn($response);
- $content = $gpsCoordinateUtils->searchGpsCoordinates('11 chemin des rirtes', '74300', 'nancy-sur-cluses');
- $this->assertEquals(['data' => 1], $content);
- }
- /**
- * @see GpsCoordinateUtils::searchGpsCoordinates()
- */
- public function testSearchGpsCoordinatesFailed():void
- {
- $gpsCoordinateUtils = $this->getMockBuilder(GpsCoordinateUtils::class)
- ->setConstructorArgs([$this->client])
- ->setMethodsExcept(['searchGpsCoordinates'])
- ->getMock();
- $this->client
- ->expects(self::once())
- ->method('request')
- ->willThrowException(new \Exception());
- $this->expectException(NotFoundHttpException::class);
- $gpsCoordinateUtils->searchGpsCoordinates('...', '74300', '...');
- }
- /**
- * @see GpsCoordinateUtils::reverseGpsCoordinates()
- */
- public function testReverseGpsCoordinates():void
- {
- $gpsCoordinateUtils = $this->getMockBuilder(GpsCoordinateUtils::class)
- ->setConstructorArgs([$this->client])
- ->setMethodsExcept(['reverseGpsCoordinates'])
- ->getMock();
- $response = $this->getMockBuilder(ResponseInterface::class)->getMock();
- $response->method('getContent')->willReturn('{"data":1}'); // dummy response data
- $this->client
- ->expects(self::once())
- ->method('request')
- ->with(
- 'GET',
- 'reverse?addressdetails=1&format=json&lat=46.040851602664&lon=6.58558355'
- )
- ->willReturn($response);
- $content = $gpsCoordinateUtils->reverseGpsCoordinates(46.04085160266434, 6.585583549999978);
- $this->assertEquals(['data' => 1], $content);
- }
- /**
- * @see GpsCoordinateUtils::reverseGpsCoordinates()
- */
- public function testReverseGpsCoordinatesFailed():void
- {
- $gpsCoordinateUtils = $this->getMockBuilder(GpsCoordinateUtils::class)
- ->setConstructorArgs([$this->client])
- ->setMethodsExcept(['reverseGpsCoordinates'])
- ->getMock();
- $this->client
- ->expects(self::once())
- ->method('request')
- ->willThrowException(new \Exception());
- $this->expectException(NotFoundHttpException::class);
- $gpsCoordinateUtils->reverseGpsCoordinates(0000, 0000);
- }
- /**
- * @see GpsCoordinateUtils::createGpsCoordinate()
- */
- public function testCreateGpsCoordinate():void
- {
- $gpsCoordinateUtils = $this->getMockBuilder(GpsCoordinateUtils::class)
- ->setConstructorArgs([$this->client])
- ->setMethodsExcept(['createGpsCoordinate'])
- ->getMock();
- $gpsApiResponse = [
- 'address' => ['foo'],
- 'lat' => 123.4,
- 'lon' => 456.7,
- ];
- $gpsCoordinateUtils
- ->expects(self::once())
- ->method('transformAddress')
- ->with(['foo'])
- ->willReturn(
- [
- 'city' => 'bar',
- 'cp' => '12345',
- 'streetAddress' => '1 rue lambda',
- 'streetAddressSecond' => 'cedex 1'
- ]
- );
- $gpsCoordinate = $gpsCoordinateUtils->createGpsCoordinate($gpsApiResponse);
- $this->assertEquals(123.4, $gpsCoordinate->getLatitude());
- $this->assertEquals(456.7, $gpsCoordinate->getLongitude());
- $this->assertEquals('bar', $gpsCoordinate->getCity());
- $this->assertEquals('1 rue lambda', $gpsCoordinate->getStreetAddress());
- $this->assertEquals('cedex 1', $gpsCoordinate->getStreetAddressSecond());
- }
- /**
- * @see GpsCoordinateUtils::transformAddress()
- */
- public function testTransformAddress():void
- {
- $gpsCoordinateUtils = $this->getMockBuilder(GpsCoordinateUtils::class)
- ->setConstructorArgs([$this->client])
- ->setMethodsExcept(['transformAddress'])
- ->getMock();
- $arrayAddress = [
- 'road' => 'foo',
- 'hamlet' => 'bar',
- 'town' => 'alpha',
- 'postcode' => '12345'
- ];
- $addressTransformed = $gpsCoordinateUtils->transformAddress($arrayAddress);
- $this->assertEquals([
- "streetAddress" => "foo",
- "streetAddressSecond" => "bar",
- "city" => "alpha",
- "cp" => "12345"
- ], $addressTransformed);
- }
- }
|