Compare commits

...

10 Commits

@ -1,5 +1,5 @@
{
"name": "tobias/laravel-geo",
"name": "ae3/laravel-geo-layer",
"description": "Biblioteca para converter endereços em coordenadas geográficas (latitude e longitude) e vice-versa.",
"type": "project",
"license": "MIT",
@ -16,11 +16,11 @@
}
],
"require": {
"php": "^7.4",
"laravel/framework": ">=5.8",
"spatie/geocoder": "^3.15"
"php": "^8.1",
"laravel/framework": ">=9.19",
"spatie/geocoder": "^3.15",
"spatie/laravel-data": "^3.11"
},
"minimum-stability": "stable",
"extra": {
"laravel": {
"providers": [
@ -29,7 +29,7 @@
}
},
"require-dev": {
"phpunit/phpunit": "^9.6",
"orchestra/testbench": "^6.41"
"orchestra/testbench": "^7.38",
"phpunit/phpunit": "^9.6"
}
}

3510
composer.lock generated

File diff suppressed because it is too large Load Diff

@ -1,24 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
<php>
<server name="APP_ENV" value="testing"/>
</php>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" bootstrap="vendor/autoload.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
</testsuites>
<php>
<server name="APP_ENV" value="testing"/>
</php>
</phpunit>

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
<php>
<server name="APP_ENV" value="testing"/>
</php>
</phpunit>

@ -0,0 +1,31 @@
<?php
namespace Ae3\LaravelGeoLayer\app\DataTransferObjects;
use Spatie\LaravelData\Data;
class GeocodingResponseData extends Data
{
/**
* @param float $lat
* @param float $lng
* @param string $accuracy
* @param string $formatted_address
* @param array $viewport
* @param array $address_components
* @param bool $partial_match
* @param string $place_id
* @param array $types
*/
public function __construct(
public float $lat,
public float $lng,
public string $accuracy,
public string $formatted_address,
public array $viewport,
public array $address_components,
public bool $partial_match,
public string $place_id,
public array $types
) {}
}

@ -2,6 +2,8 @@
namespace Ae3\LaravelGeoLayer\app\Providers;
use Ae3\LaravelGeoLayer\app\Services\Contracts\GeocodingServiceContract;
use Ae3\LaravelGeoLayer\app\Services\GeocodingService;
use Illuminate\Support\ServiceProvider;
class LaravelGeoServiceProvider extends ServiceProvider
@ -28,5 +30,7 @@ class LaravelGeoServiceProvider extends ServiceProvider
__DIR__ . '/../../config/config.php' => config_path('laravel-geo-layer.php'),
], 'config');
}
$this->app->bind(GeocodingServiceContract::class, GeocodingService::class);
}
}

@ -2,18 +2,26 @@
namespace Ae3\LaravelGeoLayer\app\Services\Contracts;
use Ae3\LaravelGeoLayer\app\DataTransferObjects\GeocodingResponseData;
interface GeocodingServiceContract
{
/**
* @param string $address
* @return array
* Retrieves the coordinates for the given address.
*
* @param string $address The address to retrieve coordinates for.
*
* @return GeocodingResponseData An object containing the latitude and longitude coordinates.
*/
public function getCoordinatesForAddress(string $address): array;
public function getCoordinatesForAddress(string $address): GeocodingResponseData;
/**
* @param float $latitude
* @param float $longitude
* @return array
* Retrieves the address for the given coordinates.
*
* @param float $latitude The latitude of the coordinates.
* @param float $longitude The longitude of the coordinates.
*
* @return GeocodingResponseData The address information for the given coordinates.
*/
public function getAddressForCoordinates(float $latitude, float $longitude): array;
public function getAddressForCoordinates(float $latitude, float $longitude): GeocodingResponseData;
}

@ -2,6 +2,7 @@
namespace Ae3\LaravelGeoLayer\app\Services;
use Ae3\LaravelGeoLayer\app\DataTransferObjects\GeocodingResponseData;
use Ae3\LaravelGeoLayer\app\Services\Contracts\GeocodingServiceContract;
use GuzzleHttp\Client;
use Spatie\Geocoder\Geocoder;
@ -16,9 +17,10 @@ class GeocodingService implements GeocodingServiceContract
/**
* Constructs a new instance of the class.
*/
public function __construct()
public function __construct(
protected readonly Client $client
)
{
$client = new Client();
$this->geocoder = new Geocoder($client);
$this->geocoder->setApiKey(config('laravel-geo-layer.key'));
$this->geocoder->setRegion(config('laravel-geo-layer.region'));
@ -31,11 +33,21 @@ class GeocodingService implements GeocodingServiceContract
*
* @param string $address The address to retrieve coordinates for.
*
* @return array An array containing the latitude and longitude coordinates.
* @return GeocodingResponseData An array containing the latitude and longitude coordinates.
*/
public function getCoordinatesForAddress(string $address): array
public function getCoordinatesForAddress(string $address): GeocodingResponseData
{
return $this->geocoder->getCoordinatesForAddress($address);
$response = $this->geocoder->getCoordinatesForAddress($address);
$viewport = array_key_exists('viewport', $response)
? get_object_vars($response['viewport'])
: [];
$response['viewport'] = $viewport;
return GeocodingResponseData::from(
$response
);
}
/**
@ -44,10 +56,20 @@ class GeocodingService implements GeocodingServiceContract
* @param float $latitude The latitude of the coordinates.
* @param float $longitude The longitude of the coordinates.
*
* @return array The address information for the given coordinates.
* @return GeocodingResponseData The address information for the given coordinates.
*/
public function getAddressForCoordinates(float $latitude, float $longitude): array
public function getAddressForCoordinates(float $latitude, float $longitude): GeocodingResponseData
{
return $this->geocoder->getAddressForCoordinates($latitude, $longitude);
$response = $this->geocoder->getAddressForCoordinates($latitude, $longitude);
$viewport = array_key_exists('viewport', $response)
? get_object_vars($response['viewport'])
: [];
$response['viewport'] = $viewport;
return GeocodingResponseData::from(
$response
);
}
}

@ -2,8 +2,8 @@
return [
/*
* The api key used when sending Geocoding requests to Google.
*/
* The api key used when sending Geocoding requests to Google.
*/
'key' => env('GOOGLE_MAPS_GEOCODING_API_KEY', ''),
/*
@ -12,26 +12,26 @@ return [
* More info: https://developers.google.com/maps/faq#languagesupport
*/
'language' => '',
'language' => env('GOOGLE_MAPS_GEOCODING_LANGUAGE', ''),
/*
* The region param used to finetune the geocoding process.
*
* More info: https://developers.google.com/maps/documentation/geocoding/requests-geocoding#RegionCodes
*/
'region' => 'br',
'region' => env('GOOGLE_MAPS_GEOCODING_REGION', 'br'),
/*
* The bounds param used to finetune the geocoding process.
*
* More info: https://developers.google.com/maps/documentation/geocoding/requests-geocoding#Viewports
*/
'bounds' => 'Roraima',
'bounds' => env('GOOGLE_MAPS_GEOCODING_BOUNDS', 'Roraima'),
/*
* The country param used to limit results to a specific country.
*
* More info: https://developers.google.com/maps/documentation/javascript/geocoding#GeocodingRequests
*/
'country' => 'Brazil',
'country' => env('GOOGLE_MAPS_GEOCODING_COUNTRY', 'Brazil'),
];

@ -1,67 +0,0 @@
<?php
namespace Unit;
use Ae3\LaravelGeoLayer\app\Services\GeocodingService;
use Ae3\LaravelGeoLayer\Tests\TestCase;
use Mockery;
class GeocodingServiceTest extends TestCase
{
/**
* @var GeocodingService
*/
private GeocodingService $geocodingService;
private $mockedGeocoder;
/**
* Set up the test
*/
public function setUp(): void
{
parent::setUp();
$this->mockedGeocoder = Mockery::mock('overload:Spatie\Geocoder\Geocoder');
// Configurando o mock para esperar a chamada de setApiKey
$this->mockedGeocoder->shouldReceive('setApiKey')->once()->withAnyArgs();
$this->mockedGeocoder->shouldReceive('setRegion')->once()->withAnyArgs();
$this->mockedGeocoder->shouldReceive('setCountry')->once()->withAnyArgs();
$this->mockedGeocoder->shouldReceive('setBounds')->once()->withAnyArgs();
// Configurando o mock para esperar a chamada de getCoordinatesForAddress
$this->mockedGeocoder->shouldReceive('getCoordinatesForAddress')
->once()
->with('Brussels, Belgium')
->andReturn(['lat' => '50.85045', 'lng' => '4.34878']);
$this->geocodingService = new GeocodingService();
}
/**
* Test getCoordinatesForAddress function
*/
public function testGetCoordinatesForAddress(): void
{
// Mocking Geocoder response
$this->mockedGeocoder->shouldReceive('getCoordinatesForAddress')
->once()
->with('Brussels, Belgium')
->andReturn(['lat' => '50.85045', 'lng' => '4.34878']);
// Testing the function
$result = $this->geocodingService->getCoordinatesForAddress('Brussels, Belgium');
$this->assertEquals(['lat' => '50.85045', 'lng' => '4.34878'], $result);
}
/**
* Tear down the test
*/
public function tearDown(): void
{
Mockery::close();
parent::tearDown();
}
}
Loading…
Cancel
Save