tests/Geocoder/GeocoderTest.php
<?php
namespace Geocoder;
use PHPUnit\Framework\TestCase;
class GeocoderTest extends TestCase
{
const VALID_ADDRESS = 'Melbourne, Australia';
const VALID_ADDRESS_MULTIPLE_RESULT = 'springfield';
const UNKNOWN_ADDRESS = 'ReallyDummyAddress';
const GEOCODE_FUNCTION = 'geocode';
public function testGeocode()
{
$stub = $this->createMock(Geocoder::class);
$stub->method(self::GEOCODE_FUNCTION)
->willReturn($this->getExpectedData());
$result = $stub->geocode(self::VALID_ADDRESS);
$this->assertEquals(
$this->getExpectedData(),
$result
);
}
public function testGeocodeInvalidApiKey()
{
$geocoder = new Geocoder('invalidApiKey');
$this->assertEquals(
$this->getExpectedInvalidKeyData(),
$geocoder->geocode(self::VALID_ADDRESS)
);
$this->assertEquals(
'The provided API key is invalid.',
$geocoder->getErrorMessage()
);
}
// public function testGeocodeMultipleResult()
// {
// $result = (new Geocoder())->geocode(self::VALID_ADDRESS_MULTIPLE_RESULT);
// $result = json_decode($result, true);
//
// $this->assertEquals(
// 6,
// count($result['results'])
// );
// }
public function testGeocodeUnknownAddress()
{
$stub = $this->createMock(Geocoder::class);
$stub->method(self::GEOCODE_FUNCTION)
->willReturn($this->getExpectedNullData());
$this->assertEquals(
$this->getExpectedNullData(),
$stub->geocode(self::UNKNOWN_ADDRESS)
);
}
public function testGeocodeInvalidFormat()
{
$this->expectException('\Exception');
$this->expectExceptionMessage("'invalidFormat' is not a valid format");
(new Geocoder())->geocode(self::VALID_ADDRESS, '', 'invalidFormat');
}
public function testGeocodeWithRegion()
{
$stub = $this->createMock(Geocoder::class);
$stub->method(self::GEOCODE_FUNCTION)
->willReturn($this->getExpectedData());
$result = $stub->geocode(self::VALID_ADDRESS, 'au');
$this->assertEquals(
$this->getExpectedData(),
$result
);
}
public function testGeocodeWithInvalidApiKey()
{
$this->assertEquals(
'{
"error_message" : "The provided API key is invalid.",
"results" : [],
"status" : "REQUEST_DENIED"
}
',
(new Geocoder('invalidApiKey'))->geocode(self::VALID_ADDRESS, 'au')
);
}
public function testGetLatLngUnknownAddress()
{
$this->assertEquals(
null,
(new Geocoder())->getLatLng(self::UNKNOWN_ADDRESS)
);
}
// public function testGetLatLng()
// {
// $this->assertEquals(
// [
// [
// 'lat' => '-37.8136276',
// 'lng' => '144.9630576',
// ],
// ],
// (new Geocoder())->getLatLng(self::VALID_ADDRESS)
// );
// }
// public function testGetLatLngMultipleResult()
// {
// $this->assertEquals(
// 6,
// count((new Geocoder())->getLatLng(self::VALID_ADDRESS_MULTIPLE_RESULT))
// );
// }
// public function testIsAddressValid()
// {
// $this->assertTrue((new Geocoder())->isAddressValid(self::VALID_ADDRESS));
// $this->assertFalse((new Geocoder())->isAddressValid(self::UNKNOWN_ADDRESS));
// }
// public function testHasMultipleResults()
// {
// $geocoder = new Geocoder();
// $geocoder->geocode(self::VALID_ADDRESS);
// $this->assertFalse($geocoder->hasMultipleResults());
//
// $geocoder->geocode(self::VALID_ADDRESS_MULTIPLE_RESULT);
// $this->assertTrue($geocoder->hasMultipleResults());
//
// $geocoder->geocode(self::UNKNOWN_ADDRESS);
// $this->assertFalse($geocoder->hasMultipleResults());
// }
private function getExpectedInvalidKeyData()
{
return '{
"error_message" : "The provided API key is invalid.",
"results" : [],
"status" : "REQUEST_DENIED"
}
';
}
private function getExpectedNullData()
{
return '{
"results" : [],
"status" : "ZERO_RESULTS"
}
';
}
private function getExpectedData()
{
return '{
"results" : [
{
"address_components" : [
{
"long_name" : "Melbourne",
"short_name" : "Melbourne",
"types" : [ "colloquial_area", "locality", "political" ]
},
{
"long_name" : "Victoria",
"short_name" : "VIC",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Australia",
"short_name" : "AU",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Melbourne VIC, Australia",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : -37.5112737,
"lng" : 145.5125288
},
"southwest" : {
"lat" : -38.4338593,
"lng" : 144.5937418
}
},
"location" : {
"lat" : -37.8136276,
"lng" : 144.9630576
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : -37.5112737,
"lng" : 145.5125288
},
"southwest" : {
"lat" : -38.4338593,
"lng" : 144.5937418
}
}
},
"place_id" : "ChIJ90260rVG1moRkM2MIXVWBAQ",
"types" : [ "colloquial_area", "locality", "political" ]
}
],
"status" : "OK"
}
';
}
}