Compare commits

..

7 Commits

Author SHA1 Message Date
35a37a1eae Improve method names 2024-09-26 10:19:07 +01:00
9a7e6b01cb Change tests to accept an interface 2024-09-26 10:17:44 +01:00
35d5c324e2 Add method to get compiler to stop crying 2024-09-26 10:17:30 +01:00
dd2f8a76d6 Change geocoder to use geocode data 2024-09-26 09:59:10 +01:00
ab4bbd0574 Change file name 2024-09-26 09:58:43 +01:00
2aa5cb731e Add get geocode data method to interface 2024-09-26 09:51:21 +01:00
2f54e01fdd Update docblock 2024-09-26 09:44:48 +01:00
4 changed files with 52 additions and 22 deletions

View File

@@ -18,7 +18,14 @@ interface GeocodeInterface
public function setLongitude(float $lon): self; public function setLongitude(float $lon): self;
/** /**
* @return bool True if both latitude and longitude is set * Used by {@see Pcm\GeocodeBundle\Service\Geocoder} to retrieve raw data for geocoding.
*
* @return string Data to use when geocodind an entity
*/
public function getGeocodeData(): string;
/**
* @return bool True if both latitude and longitude are set and are not (0, 0)
*/ */
public function isGeocoded(): bool; public function isGeocoded(): bool;
} }

View File

@@ -6,6 +6,7 @@ namespace Pcm\GeocodeBundle\Service;
use Pcm\GeocodeBundle\Exception\ApiErrorException; use Pcm\GeocodeBundle\Exception\ApiErrorException;
use Pcm\GeocodeBundle\Exception\NoResultsFoundException; use Pcm\GeocodeBundle\Exception\NoResultsFoundException;
use Pcm\GeocodeBundle\Interface\Entity\GeocodeInterface;
use Pcm\GeocodeBundle\Model\GeoCoordinates; use Pcm\GeocodeBundle\Model\GeoCoordinates;
use Symfony\Contracts\HttpClient\HttpClientInterface; use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface; use Symfony\Contracts\HttpClient\ResponseInterface;
@@ -20,27 +21,28 @@ final class Geocoder
public function __construct(private HttpClientInterface $client) {} public function __construct(private HttpClientInterface $client) {}
/** /**
* Find and return the geo-coordinates of a postcode * Find and return the geo-coordinates of an entity
* *
* @param string $postcode * @param GeocodeInterface $entity
* *
* @return GeoCoordinates * @return GeoCoordinates
* *
* @throws NoResultsFoundException when no results were found for the provided postcode * @throws NoResultsFoundException when no results were found for the provided postcode
* @throws ApiErrorException when the API response contains an error * @throws ApiErrorException when the API response contains an error
*/ */
public function geocodePostcode(string $postcode): GeoCoordinates public function geocode(GeocodeInterface $entity): GeoCoordinates
{ {
$geocodeData = $entity->getGeocodeData();
$client = $this->createClient(); $client = $this->createClient();
$response = $this->makeApiRequest($client, $postcode); $response = $this->makeApiRequest($client, $geocodeData);
$data = $this->getDataFromResponse($response); $data = $this->getDataFromResponse($response);
if (array_key_exists('error', $data)) { if (array_key_exists('error', $data)) {
throw new ApiErrorException($postcode, $data['error']['message']); throw new ApiErrorException($geocodeData, $data['error']['message']);
} }
if (empty($data)) { if (empty($data)) {
throw new NoResultsFoundException($postcode, "No results found."); throw new NoResultsFoundException($geocodeData, "No results found.");
} }
return $this->createGeoCoordinates($data); return $this->createGeoCoordinates($data);
@@ -53,7 +55,7 @@ final class Geocoder
]); ]);
} }
private function makeApiRequest(HttpClientInterface $client, string $postcode): ResponseInterface private function makeApiRequest(HttpClientInterface $client, string $geocodeData): ResponseInterface
{ {
return $client->request( return $client->request(
method: 'GET', method: 'GET',
@@ -61,7 +63,7 @@ final class Geocoder
options: [ options: [
'query' => [ 'query' => [
'format' => 'json', 'format' => 'json',
'postalcode' => $postcode 'q' => $geocodeData
] ]
] ]
); );

View File

@@ -16,7 +16,7 @@ use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
/** /**
* We sleep 1 second after API calls to prevent breaking the API T&Cs. * We sleep 1 second after API calls to prevent breaking the API T&Cs.
*/ */
final class GeocodeTest extends KernelTestCase final class GeocoderTest extends KernelTestCase
{ {
// Buckingham Palace // Buckingham Palace
private const string POSTCODE = 'SW1A 1AA'; private const string POSTCODE = 'SW1A 1AA';
@@ -30,38 +30,36 @@ final class GeocodeTest extends KernelTestCase
$this->geocoder = $kernel->getContainer()->get('pcm_geocode.geocoder'); $this->geocoder = $kernel->getContainer()->get('pcm_geocode.geocoder');
} }
public function testGeocodeInstance(): void public function testGeocodeThrowsOnEmptyInput(): void
{
$this->assertInstanceOf(Geocoder::class, $this->geocoder);
}
public function testGeocodePostcodeThrowsOnEmptyInput(): void
{ {
sleep(1); sleep(1);
$this->expectException(ApiErrorException::class); $this->expectException(ApiErrorException::class);
$entity = $this->createEntity('');
$this->expectExceptionMessageMatches("/Nothing to search for.$/"); $this->expectExceptionMessageMatches("/Nothing to search for.$/");
$this->geocoder->geocodePostcode(''); $this->geocoder->geocode($entity);
} }
public function testGeocodePostcodeThrowsOnInvalidPostcode(): void public function testGeocodeThrowsOnInvalidPostcode(): void
{ {
sleep(1); sleep(1);
$this->expectException(NoResultsFoundException::class); $this->expectException(NoResultsFoundException::class);
$this->expectExceptionMessageMatches("/No results found.$/"); $this->expectExceptionMessageMatches("/No results found.$/");
$this->geocoder->geocodePostcode('ZZZZZZZZZZZZ'); $entity = $this->createEntity('Invalid Postcode');
$this->geocoder->geocode($entity);
} }
public function testGeocodePostcodeReturnsGeoCoordinates(): GeoCoordinates public function testGeocodeReturnsGeoCoordinates(): GeoCoordinates
{ {
sleep(1); sleep(1);
$geoCoords = $this->geocoder->geocodePostcode(self::POSTCODE); $entity = $this->createEntity(self::POSTCODE);
$geoCoords = $this->geocoder->geocode($entity);
$this->assertNotNull($geoCoords); $this->assertNotNull($geoCoords);
return $geoCoords; return $geoCoords;
} }
/** /**
* @depends testGeocodePostcodeReturnsGeoCoordinates * @depends testGeocodeReturnsGeoCoordinates
*/ */
public function testCoordinatesAreSet(GeoCoordinates $geoCoordinates): void public function testCoordinatesAreSet(GeoCoordinates $geoCoordinates): void
{ {
@@ -76,5 +74,23 @@ final class GeocodeTest extends KernelTestCase
use GeocodeTrait; use GeocodeTrait;
}; };
} }
private function createEntity(string $data): GeocodeInterface
{
$entity = new class implements GeocodeInterface {
use GeocodeTrait;
public string $data;
public function getGeocodeData(): string
{
return $this->data;
}
};
$entity->data = $data;
return $entity;
}
} }

View File

@@ -94,6 +94,11 @@ final class GeocodeTraitTest extends TestCase
return new class implements GeocodeInterface return new class implements GeocodeInterface
{ {
use GeocodeTrait; use GeocodeTrait;
public function getGeocodeData(): string
{
return '';
}
}; };
} }
} }