Compare commits
3 Commits
issue/6-re
...
issue/13-r
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8b662ebfa3 | ||
|
|
a6c8c4cc0c | ||
|
|
fcda1e931c |
10
src/Exception/ApiErrorException.php
Normal file
10
src/Exception/ApiErrorException.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pcm\GeocodeBundle\Exception;
|
||||
|
||||
class ApiErrorException extends GeocodeException
|
||||
{
|
||||
}
|
||||
|
||||
16
src/Exception/GeocodeException.php
Normal file
16
src/Exception/GeocodeException.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pcm\GeocodeBundle\Exception;
|
||||
|
||||
class GeocodeException extends \RuntimeException
|
||||
{
|
||||
public function __construct(string $postcode, string $message)
|
||||
{
|
||||
$formattedMessage = sprintf("Failed to geocode a postcode \"%s\": %s", $postcode, $message);
|
||||
|
||||
parent::__construct($formattedMessage);
|
||||
}
|
||||
}
|
||||
|
||||
16
src/Exception/NoResultsFoundException.php
Normal file
16
src/Exception/NoResultsFoundException.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pcm\GeocodeBundle\Exception;
|
||||
|
||||
class NoResultsFoundException extends GeocodeException
|
||||
{
|
||||
private const string MESSAGE = "No results found.";
|
||||
|
||||
public function __construct(string $postcode)
|
||||
{
|
||||
parent::__construct($postcode, self::MESSAGE);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pcm\GeocodeBundle\Entity\Interface;
|
||||
namespace Pcm\GeocodeBundle\Interface\Entity;
|
||||
|
||||
interface MappableInterface
|
||||
interface GeocodeInterface
|
||||
{
|
||||
public function getLatitude(): ?float;
|
||||
|
||||
@@ -4,6 +4,8 @@ declare(strict_types=1);
|
||||
|
||||
namespace Pcm\GeocodeBundle\Service;
|
||||
|
||||
use Pcm\GeocodeBundle\Exception\ApiErrorException;
|
||||
use Pcm\GeocodeBundle\Exception\NoResultsFoundException;
|
||||
use Pcm\GeocodeBundle\Model\GeoCoordinates;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
use Symfony\Contracts\HttpClient\ResponseInterface;
|
||||
@@ -26,13 +28,12 @@ final class Geocoder
|
||||
$response = $this->makeApiRequest($client, $postcode);
|
||||
$data = $this->getDataFromResponse($response);
|
||||
|
||||
// @todo remove check, I don't think it's needed
|
||||
if (empty($data)) {
|
||||
return null;
|
||||
if (array_key_exists('error', $data)) {
|
||||
throw new ApiErrorException($postcode, $data['error']['message']);
|
||||
}
|
||||
|
||||
if (array_key_exists('error', $data)) {
|
||||
return null;
|
||||
if (empty($data)) {
|
||||
throw new NoResultsFoundException($postcode, "No results found.");
|
||||
}
|
||||
|
||||
return $this->createGeoCoordinates($data);
|
||||
|
||||
@@ -2,18 +2,16 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pcm\GeocodeBundle\Entity\Trait;
|
||||
namespace Pcm\GeocodeBundle\Trait\Entity;
|
||||
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
/**
|
||||
* Allows an entity to be mapped via latitude and longitude coordinates
|
||||
*
|
||||
* Use MappableInterface
|
||||
* Implementation of {@see Pcm\GeocodeBundle\Interface\Entity\GeocodeInterface}
|
||||
*
|
||||
* @package Pcm\GeocodeBundle
|
||||
*/
|
||||
trait MappableTrait
|
||||
trait GeocodeTrait
|
||||
{
|
||||
#[ORM\Column(type: 'decimal', precision: 10, scale: 6, nullable: true)]
|
||||
private ?float $latitude = null;
|
||||
@@ -55,3 +53,4 @@ trait MappableTrait
|
||||
return null !== $this->getLatitude() && null !== $this->getLongitude();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,8 +4,10 @@ declare(strict_types=1);
|
||||
|
||||
namespace Pcm\GeocodeBundle\Tests;
|
||||
|
||||
use Pcm\GeocodeBundle\Entity\Interface\MappableInterface;
|
||||
use Pcm\GeocodeBundle\Entity\Trait\MappableTrait;
|
||||
use Pcm\GeocodeBundle\Interface\Entity\GeocodeInterface;
|
||||
use Pcm\GeocodeBundle\Trait\Entity\GeocodeTrait;
|
||||
use Pcm\GeocodeBundle\Exception\ApiErrorException;
|
||||
use Pcm\GeocodeBundle\Exception\NoResultsFoundException;
|
||||
use Pcm\GeocodeBundle\Model\GeoCoordinates;
|
||||
use Pcm\GeocodeBundle\Service\Geocoder;
|
||||
use Pcm\GeocodeBundle\Tests\AppKernel;
|
||||
@@ -33,10 +35,20 @@ final class GeocodeTest extends KernelTestCase
|
||||
$this->assertInstanceOf(Geocoder::class, $this->geocoder);
|
||||
}
|
||||
|
||||
public function testGeocodePostcodeReturnsNullOnInvalidInput(): void
|
||||
public function testGeocodePostcodeThrowsOnEmptyInput(): void
|
||||
{
|
||||
sleep(1);
|
||||
$this->assertNull($this->geocoder->geocodePostcode(''));
|
||||
$this->expectException(ApiErrorException::class);
|
||||
$this->expectExceptionMessageMatches("/Nothing to search for.$/");
|
||||
$this->geocoder->geocodePostcode('');
|
||||
}
|
||||
|
||||
public function testGeocodePostcodeThrowsOnInvalidPostcode(): void
|
||||
{
|
||||
sleep(1);
|
||||
$this->expectException(NoResultsFoundException::class);
|
||||
$this->expectExceptionMessageMatches("/No results found.$/");
|
||||
$this->geocoder->geocodePostcode('ZZZZZZZZZZZZ');
|
||||
}
|
||||
|
||||
public function testGeocodePostcodeReturnsGeoCoordinates(): GeoCoordinates
|
||||
@@ -57,11 +69,11 @@ final class GeocodeTest extends KernelTestCase
|
||||
$this->assertIsFloat($geoCoordinates->longitude);
|
||||
}
|
||||
|
||||
private function getMappableEntity(): MappableInterface
|
||||
private function getGeocodableEntity(): GeocodeInterface
|
||||
{
|
||||
return new class implements MappableInterface
|
||||
return new class implements GeocodeInterface
|
||||
{
|
||||
use MappableTrait;
|
||||
use GeocodeTrait;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,15 +4,15 @@ declare(strict_types=1);
|
||||
|
||||
namespace Pcm\GeocodeBundle\Tests;
|
||||
|
||||
use Pcm\GeocodeBundle\Entity\Interface\MappableInterface;
|
||||
use Pcm\GeocodeBundle\Entity\Trait\MappableTrait;
|
||||
use Pcm\GeocodeBundle\Interface\Entity\GeocodeInterface;
|
||||
use Pcm\GeocodeBundle\Trait\Entity\GeocodeTrait;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class MappableTraitTest extends TestCase
|
||||
final class GeocodeTraitTest extends TestCase
|
||||
{
|
||||
private const float COORD = 123.456;
|
||||
|
||||
private MappableInterface $obj;
|
||||
private GeocodeInterface $obj;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
@@ -21,7 +21,7 @@ final class MappableTraitTest extends TestCase
|
||||
|
||||
public function testSetLatitude(): void
|
||||
{
|
||||
$this->assertInstanceOf(MappableInterface::class, $this->obj->setLatitude(self::COORD));
|
||||
$this->assertInstanceOf(GeocodeInterface::class, $this->obj->setLatitude(self::COORD));
|
||||
}
|
||||
|
||||
public function testGetLatitudeReturnsNull(): void
|
||||
@@ -37,7 +37,7 @@ final class MappableTraitTest extends TestCase
|
||||
|
||||
public function testSetLongitude(): void
|
||||
{
|
||||
$this->assertInstanceOf(MappableInterface::class, $this->obj->setLongitude(self::COORD));
|
||||
$this->assertInstanceOf(GeocodeInterface::class, $this->obj->setLongitude(self::COORD));
|
||||
}
|
||||
|
||||
public function testGetLongitudeReturnsNull(): void
|
||||
@@ -82,11 +82,11 @@ final class MappableTraitTest extends TestCase
|
||||
$this->assertTrue($this->obj->isGeocoded());
|
||||
}
|
||||
|
||||
private function getTraitObject(): MappableInterface
|
||||
private function getTraitObject(): GeocodeInterface
|
||||
{
|
||||
return new class implements MappableInterface
|
||||
return new class implements GeocodeInterface
|
||||
{
|
||||
use MappableTrait;
|
||||
use GeocodeTrait;
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user