Skip to content

Commit

Permalink
Add generic geocoder service
Browse files Browse the repository at this point in the history
  • Loading branch information
wimski committed Feb 25, 2022
1 parent 47bccdc commit f279eac
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ This changelog is initialized in release 1.0.0

## [Unreleased]

### Added
* Generic geocoder service

[Unreleased]: https://github.com/wimski/nominatim-geocoding-api-client/compare/v1.0.0...master
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ $service = new NominatimGeocoderService(
Services for the following providers are currently available:
* [Nominatim](https://nominatim.org/release-docs/latest/api/Overview/)
* [LocationIQ](https://locationiq.com/docs)
* Generic: without any implementation specific headers or parameters.

Custom services can easily be created by extending the `AbstractGeocoderService`
as long as the provider implements the Nominatim spec correctly.
Expand Down
11 changes: 11 additions & 0 deletions src/Config/GenericConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Wimski\Nominatim\Config;

use Wimski\Nominatim\Contracts\Config\GenericConfigInterface;

class GenericConfig extends AbstractConfig implements GenericConfigInterface
{
}
9 changes: 9 additions & 0 deletions src/Contracts/Config/GenericConfigInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Wimski\Nominatim\Contracts\Config;

interface GenericConfigInterface extends ConfigInterface
{
}
26 changes: 26 additions & 0 deletions src/GeocoderServices/GenericGeocoderService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Wimski\Nominatim\GeocoderServices;

use Wimski\Nominatim\Contracts\ClientInterface;
use Wimski\Nominatim\Contracts\Config\ConfigInterface;
use Wimski\Nominatim\Contracts\Config\GenericConfigInterface;
use Wimski\Nominatim\Contracts\Transformers\GeocodingResponseTransformerInterface;

class GenericGeocoderService extends AbstractGeocoderService
{
public function __construct(
ClientInterface $client,
GeocodingResponseTransformerInterface $responseTransformer,
protected GenericConfigInterface $config,
) {
parent::__construct($client, $responseTransformer);
}

protected function getConfig(): ConfigInterface
{
return $this->config;
}
}
29 changes: 29 additions & 0 deletions tests/Config/GenericConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Wimski\Nominatim\Tests\Config;

use Wimski\Nominatim\Config\GenericConfig;
use Wimski\Nominatim\Tests\AbstractTest;

class GenericConfigTest extends AbstractTest
{
/**
* @test
*/
public function it_returns_set_data(): void
{
$config = new GenericConfig(
'https://nominatim.myserver.com',
'/fowarding',
'/reversing',
'nl',
);

static::assertSame('https://nominatim.myserver.com', $config->getUrl());
static::assertSame('/fowarding', $config->getForwardGeocodingEndpoint());
static::assertSame('/reversing', $config->getReverseGeocodingEndpoint());
static::assertSame('nl', $config->getLanguage());
}
}
135 changes: 135 additions & 0 deletions tests/GeocoderServices/GenericGeocoderServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

declare(strict_types=1);

namespace Wimski\Nominatim\Tests\GeocoderServices;

use Mockery;
use Mockery\MockInterface;
use Psr\Http\Message\ResponseInterface;
use Wimski\Nominatim\Contracts\ClientInterface;
use Wimski\Nominatim\Contracts\Config\GenericConfigInterface;
use Wimski\Nominatim\Contracts\RequestParameters\ForwardGeocodingRequestParametersInterface;
use Wimski\Nominatim\Contracts\RequestParameters\ReverseGeocodingRequestParametersInterface;
use Wimski\Nominatim\Contracts\Responses\ForwardGeocodingResponseInterface;
use Wimski\Nominatim\Contracts\Responses\ReverseGeocodingResponseInterface;
use Wimski\Nominatim\Contracts\Transformers\GeocodingResponseTransformerInterface;
use Wimski\Nominatim\GeocoderServices\GenericGeocoderService;
use Wimski\Nominatim\Tests\AbstractTest;

class GenericGeocoderServiceTest extends AbstractTest
{
protected GenericGeocoderService $service;

/**
* @var ClientInterface|MockInterface
*/
protected $client;

/**
* @var GeocodingResponseTransformerInterface|MockInterface
*/
protected $responseTransformer;

/**
* @var GenericConfigInterface|MockInterface
*/
protected $config;

protected function setUp(): void
{
parent::setUp();

$this->client = Mockery::mock(ClientInterface::class);
$this->responseTransformer = Mockery::mock(GeocodingResponseTransformerInterface::class);
$this->config = Mockery::mock(GenericConfigInterface::class);

$this->config
->shouldReceive('getUrl')->once()->andReturn('https://generic.server/')->getMock()
->shouldReceive('getLanguage')->once()->andReturn('nl')->getMock();

$this->service = new GenericGeocoderService(
$this->client,
$this->responseTransformer,
$this->config,
);
}

/**
* @test
*/
public function it_makes_a_forward_geocoding_request(): void
{
$response = Mockery::mock(ResponseInterface::class);

$this->config
->shouldReceive('getForwardGeocodingEndpoint')
->once()
->andReturn('/forwards');

$this->client
->shouldReceive('request')
->once()
->with('https://generic.server/forwards', [
'Accept-Language' => 'nl',
], [
'param' => 'value',
'format' => 'json',
])
->andReturn($response);

$this->responseTransformer
->shouldReceive('transformForwardResponse')
->once()
->with($response)
->andReturn(Mockery::mock(ForwardGeocodingResponseInterface::class));

/** @var ForwardGeocodingRequestParametersInterface|MockInterface $parameters */
$parameters = Mockery::mock(ForwardGeocodingRequestParametersInterface::class)
->shouldReceive('toArray')
->once()
->andReturn(['param' => 'value'])
->getMock();

$this->service->requestForwardGeocoding($parameters);
}

/**
* @test
*/
public function it_makes_a_reverse_geocoding_request(): void
{
$response = Mockery::mock(ResponseInterface::class);

$this->config
->shouldReceive('getReverseGeocodingEndpoint')
->once()
->andReturn('/reversed');

$this->client
->shouldReceive('request')
->once()
->with('https://generic.server/reversed', [
'Accept-Language' => 'nl',
], [
'param' => 'value',
'format' => 'json',
])
->andReturn($response);

$this->responseTransformer
->shouldReceive('transformReverseResponse')
->once()
->with($response)
->andReturn(Mockery::mock(ReverseGeocodingResponseInterface::class));

/** @var ReverseGeocodingRequestParametersInterface|MockInterface $parameters */
$parameters = Mockery::mock(ReverseGeocodingRequestParametersInterface::class)
->shouldReceive('toArray')
->once()
->andReturn(['param' => 'value'])
->getMock();

$this->service->requestReverseGeocoding($parameters);
}
}

0 comments on commit f279eac

Please sign in to comment.