Skip to content

Commit b942332

Browse files
committed
tests: ✅ Switch to Pest for testing, replace PHPUnit with Pest, update test dependencies, add
LaravelHttpClient, and refactor tests to use Pest syntax and HTTP fakes.
1 parent 1347aba commit b942332

16 files changed

Lines changed: 432 additions & 418 deletions

.github/workflows/laravel.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ jobs:
5757
- name: Install Dependencies
5858
run: composer update --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
5959

60-
- name: Execute tests via PHPUnit
61-
run: vendor/bin/phpunit --configuration phpunit.xml ${{ matrix.php == '8.5' && matrix.laravel == '13.*' && '--coverage-clover coverage.xml' || '' }}
60+
- name: Execute tests via Pest
61+
run: vendor/bin/pest --configuration phpunit.xml ${{ matrix.php == '8.5' && matrix.laravel == '13.*' && '--coverage-clover coverage.xml' || '' }}
6262

6363
- name: Upload coverage to Codecov
6464
if: matrix.php == '8.5' && matrix.laravel == '13.*'

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ composer.phar
33
composer.lock
44
.DS_Store
55
.phpunit.result.cache
6+
.phpunit.cache

README.md

Lines changed: 60 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@
1010
> If you still use **Laravel 4**, please check out the `0.4.x` branch
1111
[here](https://github.com/geocoder-php/GeocoderLaravel/tree/0.4.x).
1212

13-
**Version 4.0.0 is a backwards-compatibility-breaking update. Please review
14-
this documentation, especially the _Usage_ section before installing.**
13+
**Version 5.0.0 is a backwards-compatibility-breaking update. Please review
14+
the _Upgrading_ section, especially the new default HTTP adapter, before
15+
installing.**
1516

1617
This package allows you to use [**Geocoder**](http://geocoder-php.org/Geocoder/)
17-
in [**Laravel 5**](http://laravel.com/).
18+
in [**Laravel**](http://laravel.com/).
1819

1920
## Requirements
20-
- PHP >= 7.1.3
21-
- Laravel >= 5.0
21+
- PHP >= 8.2
22+
- Laravel >= 11.0
2223

2324
## Installation
2425
1. Install the package via composer:
@@ -120,10 +121,10 @@ However, you are free to add or remove providers as needed, both inside the
120121
Chain provider, as well as along-side it. The following is the default
121122
configuration provided by the package:
122123
```php
124+
use Geocoder\Laravel\Http\LaravelHttpClient;
123125
use Geocoder\Provider\Chain\Chain;
124126
use Geocoder\Provider\GeoPlugin\GeoPlugin;
125127
use Geocoder\Provider\GoogleMaps\GoogleMaps;
126-
use Http\Client\Curl\Client;
127128

128129
return [
129130

@@ -173,16 +174,19 @@ return [
173174
| Adapter
174175
|--------------------------------------------------------------------------
175176
|
176-
| You can specify which PSR-7-compliant HTTP adapter you would like to use.
177-
| There are multiple options at your disposal: CURL, Guzzle, and others.
177+
| The HTTP adapter to use when communicating with geocoding services. By
178+
| default this package ships a PSR-18 client that delegates to Laravel's
179+
| `Http` facade — this gives you `Http::fake()` in tests, native retry
180+
| and timeout configuration, and any HTTP middleware you've registered.
178181
|
179-
| Please consult the official Geocoder documentation for more info.
180-
| https://github.com/geocoder-php/Geocoder#usage
182+
| Provide any class that implements `Psr\Http\Client\ClientInterface` to
183+
| swap in a different adapter (e.g., `Http\Client\Curl\Client` from
184+
| `php-http/curl-client`, which you would need to install separately).
181185
|
182-
| Default: Client::class (FQCN for CURL adapter)
186+
| Default: LaravelHttpClient::class
183187
|
184188
*/
185-
'adapter' => Client::class,
189+
'adapter' => LaravelHttpClient::class,
186190

187191
/*
188192
|--------------------------------------------------------------------------
@@ -206,11 +210,18 @@ return [
206210
```
207211

208212
### Adapters
209-
By default we provide a CURL adapter to get you running out of the box.
210-
However, if you have already installed Guzzle or any other PSR-7-compatible
211-
HTTP adapter, you are encouraged to replace the CURL adapter with it. Please
212-
see the [Geocoder Documentation](https://github.com/geocoder-php/Geocoder) for
213-
specific implementation details.
213+
By default we ship `Geocoder\Laravel\Http\LaravelHttpClient`, a thin PSR-18
214+
client that delegates every request to Laravel's `Http` facade. This means:
215+
216+
- `Http::fake()` and `Http::assertSent()` work in your tests with no extra setup
217+
- `Http::timeout()`, `Http::retry()`, `Http::withMiddleware()`, and any other
218+
Laravel HTTP client configuration applies to geocoder requests
219+
- One less third-party HTTP client to manage
220+
221+
If you need a different transport, set `'adapter'` in `config/geocoder.php` to
222+
any class that implements `Psr\Http\Client\ClientInterface`. To go back to the
223+
previous CURL adapter, install `php-http/curl-client` and set
224+
`'adapter' => Http\Client\Curl\Client::class`.
214225

215226
### Customization
216227
If you would like to make changes to the default configuration, publish and
@@ -263,6 +274,38 @@ Anytime you upgrade this package, please remember to clear your cache, to preven
263274
php artisan cache:clear
264275
```
265276

277+
### 4.x to 5.x
278+
Update your `composer.json`:
279+
```json
280+
"toin0u/geocoder-laravel": "^5.0",
281+
```
282+
283+
**Breaking: default HTTP adapter changed.** The default `'adapter'` in
284+
`config/geocoder.php` is now `Geocoder\Laravel\Http\LaravelHttpClient`
285+
instead of `Http\Client\Curl\Client`. The new adapter is a PSR-18 client
286+
that delegates to Laravel's `Http` facade, so `Http::fake()`, retries,
287+
timeouts, and middleware all work transparently.
288+
289+
If you have **published the geocoder config previously**, your config file
290+
still pins the old curl adapter and will fail at runtime once
291+
`php-http/curl-client` is no longer installed. Pick one:
292+
293+
- **Recommended:** delete (or rename) your published `config/geocoder.php`
294+
and re-publish it, then re-apply your customizations.
295+
- Or edit the existing file: change `use Http\Client\Curl\Client;` to
296+
`use Geocoder\Laravel\Http\LaravelHttpClient;` and change
297+
`'adapter' => Client::class` to `'adapter' => LaravelHttpClient::class`.
298+
- Or, if you want to keep the curl adapter, install it explicitly:
299+
`composer require php-http/curl-client`. The published config keeps working
300+
unchanged.
301+
302+
**Other notable changes in 5.x:**
303+
- Minimum PHP raised to 8.2; minimum Laravel raised to 11.x.
304+
- `php-http/curl-client` removed from required dependencies (install it
305+
yourself if you still need it).
306+
- `MaxMindBinary` provider support removed (the underlying PHP package was
307+
abandoned). Use `geocoder-php/geoip2-provider` for MaxMind data instead.
308+
266309
### 1.x to 4.x
267310
Update your composer.json file:
268311
```json

composer.json

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,18 @@
3030
"guzzlehttp/psr7": "*",
3131
"http-interop/http-factory-guzzle": "^1.0",
3232
"illuminate/cache": "^11.0|^12.0|^13.0",
33+
"illuminate/http": "^11.0|^12.0|^13.0",
3334
"illuminate/support": "^11.0|^12.0|^13.0",
34-
"php-http/curl-client": "*",
3535
"willdurand/geocoder": "^4.0|^5.0"
3636
},
3737
"require-dev": {
38-
"doctrine/dbal": "*",
38+
"geocoder-php/nominatim-provider": "^5.0",
3939
"geocoder-php/bing-maps-provider": "^4.0",
4040
"geocoder-php/geoip2-provider": "^4.0",
4141
"geocoder-php/maxmind-binary-provider": "^4.0",
4242
"orchestra/testbench": "^9.0|^10.0|^11.0",
43-
"orchestra/testbench-browser-kit": "^9.0|^10.0|^11.0",
44-
"orchestra/testbench-dusk": "^9.0|^10.0|^11.0",
45-
"php-coveralls/php-coveralls": "*",
46-
"phpunit/phpunit": "^10.5|^11.5.3|^12.5.12"
43+
"pestphp/pest": "^3.0|^4.0",
44+
"pestphp/pest-plugin-laravel": "^3.0|^4.0"
4745
},
4846
"autoload": {
4947
"psr-4": {
@@ -52,7 +50,6 @@
5250
},
5351
"autoload-dev": {
5452
"psr-4": {
55-
"App\\": "vendor/laravel/laravel/app/",
5653
"Geocoder\\Laravel\\Tests\\": "tests/"
5754
}
5855
},
@@ -69,7 +66,8 @@
6966
"preferred-install": "dist",
7067
"sort-packages": true,
7168
"allow-plugins": {
72-
"php-http/discovery": true
69+
"php-http/discovery": true,
70+
"pestphp/pest-plugin": true
7371
}
7472
}
7573
}

config/geocoder.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22

3+
use Geocoder\Laravel\Http\LaravelHttpClient;
34
use Geocoder\Provider\Chain\Chain;
45
use Geocoder\Provider\GeoPlugin\GeoPlugin;
56
use Geocoder\Provider\GoogleMaps\GoogleMaps;
6-
use Http\Client\Curl\Client;
77

88
return [
99
'cache' => [
@@ -70,16 +70,19 @@
7070
| Adapter
7171
|---------------------------------------------------------------------------
7272
|
73-
| You can specify which PSR-7-compliant HTTP adapter you would like to use.
74-
| There are multiple options at your disposal: CURL, Guzzle, and others.
73+
| The HTTP adapter to use when communicating with geocoding services. By
74+
| default this package ships a PSR-18 client that delegates to Laravel's
75+
| `Http` facade — this gives you `Http::fake()` in tests, native retry
76+
| and timeout configuration, and any HTTP middleware you've registered.
7577
|
76-
| Please consult the official Geocoder documentation for more info.
77-
| https://github.com/geocoder-php/Geocoder#usage
78+
| Provide any class that implements `Psr\Http\Client\ClientInterface` to
79+
| swap in a different adapter (e.g., `Http\Client\Curl\Client` from
80+
| `php-http/curl-client`, which you would need to install separately).
7881
|
79-
| Default: Client::class (FQCN for CURL adapter)
82+
| Default: LaravelHttpClient::class
8083
|
8184
*/
82-
'adapter' => Client::class,
85+
'adapter' => LaravelHttpClient::class,
8386

8487
/*
8588
|---------------------------------------------------------------------------

phpunit.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
<env name="CACHE_DRIVER" value="array"/>
2727
<env name="SESSION_DRIVER" value="array"/>
2828
<env name="QUEUE_DRIVER" value="sync"/>
29-
<env name="DB_CONNECTION" value="testing"/>
29+
<env name="DB_CONNECTION" value="sqlite"/>
30+
<env name="DB_DATABASE" value=":memory:"/>
3031
</php>
3132
</phpunit>

src/Http/LaravelHttpClient.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Geocoder\Laravel\Http;
4+
5+
use Illuminate\Support\Facades\Http;
6+
use Psr\Http\Client\ClientInterface;
7+
use Psr\Http\Message\RequestInterface;
8+
use Psr\Http\Message\ResponseInterface;
9+
10+
class LaravelHttpClient implements ClientInterface
11+
{
12+
public function sendRequest(RequestInterface $request): ResponseInterface
13+
{
14+
$headers = [];
15+
16+
foreach ($request->getHeaders() as $name => $values) {
17+
$headers[$name] = implode(', ', $values);
18+
}
19+
20+
$body = (string) $request->getBody();
21+
$pending = Http::withHeaders($headers);
22+
23+
if ($body !== '') {
24+
$pending = $pending->withBody($body, $request->getHeaderLine('Content-Type') ?: 'application/octet-stream');
25+
}
26+
27+
return $pending
28+
->send($request->getMethod(), (string) $request->getUri())
29+
->toPsrResponse();
30+
}
31+
}

src/ProviderAndDumperAggregator.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,6 @@ protected function getAdapterClass(string $provider) : string
227227
{
228228
$specificAdapters = collect([
229229
'Geocoder\Provider\GeoIP2\GeoIP2' => 'Geocoder\Provider\GeoIP2\GeoIP2Adapter',
230-
'Geocoder\Provider\MaxMindBinary\MaxMindBinary' => '',
231230
]);
232231

233232
if ($specificAdapters->has($provider)) {

tests/CreatesApplication.php

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,6 @@
44

55
trait CreatesApplication
66
{
7-
public function setUp() : void
8-
{
9-
parent::setUp();
10-
11-
$this->withFactories(__DIR__ . '/database/factories');
12-
$this->loadMigrationsFrom(__DIR__ . '/database/migrations');
13-
}
14-
157
/**
168
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
179
*/
@@ -26,19 +18,10 @@ protected function getEnvironmentSetUp($app)
2618
{
2719
$config = require(__DIR__ . '/config/testConfig.php');
2820
$app['config']->set('geocoder', $config);
29-
$app['config']->set('database.redis.default', [
30-
'host' => env('REDIS_HOST', '192.168.10.10'),
31-
]);
32-
$app['config']->set('database.redis.geocode-cache', [
33-
'host' => env('REDIS_HOST', '192.168.10.10'),
34-
'password' => env('REDIS_PASSWORD', null),
35-
'port' => env('REDIS_PORT', 6379),
36-
'database' => 1,
37-
]);
3821
$app['config']->set('cache.stores.geocode', [
39-
'driver' => 'redis',
40-
'connection' => 'geocode-cache',
22+
'driver' => 'array',
23+
'serialize' => false,
4124
]);
42-
$app['config']->set('geocoder.store', 'geocode');
25+
$app['config']->set('geocoder.cache.store', 'geocode');
4326
}
4427
}

0 commit comments

Comments
 (0)