Change file name
This commit is contained in:
75
tests/Service/GeocoderTest.php
Normal file
75
tests/Service/GeocoderTest.php
Normal file
@@ -0,0 +1,75 @@
|
||||
<?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;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user