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); } }