76 lines
2.1 KiB
PHP
76 lines
2.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Pcm\GeocodeBundle\Tests;
|
|
|
|
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;
|
|
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
|
|
|
/**
|
|
* We sleep 1 second after API calls to prevent breaking the API T&Cs.
|
|
*/
|
|
final class GeocoderTest extends KernelTestCase
|
|
{
|
|
// Buckingham Palace
|
|
private const string POSTCODE = 'SW1A 1AA';
|
|
|
|
private Geocoder $geocoder;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$kernel = new AppKernel('test', false);
|
|
$kernel->boot();
|
|
$this->geocoder = $kernel->getContainer()->get('pcm_geocode.geocoder');
|
|
}
|
|
|
|
public function testGeocodePostcodeThrowsOnEmptyInput(): void
|
|
{
|
|
sleep(1);
|
|
$this->expectException(ApiErrorException::class);
|
|
$this->expectExceptionMessageMatches("/Nothing to search for.$/");
|
|
$this->geocoder->geocode('');
|
|
}
|
|
|
|
public function testGeocodePostcodeThrowsOnInvalidPostcode(): void
|
|
{
|
|
sleep(1);
|
|
$this->expectException(NoResultsFoundException::class);
|
|
$this->expectExceptionMessageMatches("/No results found.$/");
|
|
$this->geocoder->geocode('ZZZZZZZZZZZZ');
|
|
}
|
|
|
|
public function testGeocodePostcodeReturnsGeoCoordinates(): GeoCoordinates
|
|
{
|
|
sleep(1);
|
|
$geoCoords = $this->geocoder->geocode(self::POSTCODE);
|
|
$this->assertNotNull($geoCoords);
|
|
|
|
return $geoCoords;
|
|
}
|
|
|
|
/**
|
|
* @depends testGeocodePostcodeReturnsGeoCoordinates
|
|
*/
|
|
public function testCoordinatesAreSet(GeoCoordinates $geoCoordinates): void
|
|
{
|
|
$this->assertIsFloat($geoCoordinates->latitude);
|
|
$this->assertIsFloat($geoCoordinates->longitude);
|
|
}
|
|
|
|
private function getGeocodableEntity(): GeocodeInterface
|
|
{
|
|
return new class implements GeocodeInterface
|
|
{
|
|
use GeocodeTrait;
|
|
};
|
|
}
|
|
}
|
|
|