Skip to content

Commit 0130c5a

Browse files
authored
Expect a PSR-18 client instead of a PHP-HTTP client (#18)
1 parent 19cf45b commit 0130c5a

File tree

7 files changed

+28
-76
lines changed

7 files changed

+28
-76
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ composer require --dev geocoder-php/provider-integration-tests:dev-master
1818
Create a test that looks like this:
1919

2020
```php
21-
use Http\Client\HttpClient;
2221
use Geocoder\IntegrationTest\ProviderIntegrationTest;
2322
use Geocoder\Provider\GoogleMaps\GoogleMaps;
23+
use Psr\Http\Client\ClientInterface;
2424

2525
class IntegrationTest extends ProviderIntegrationTest
2626
{
27-
protected function createProvider(HttpClient $httpClient)
27+
protected function createProvider(ClientInterface $httpClient)
2828
{
2929
return new GoogleMaps($httpClient);
3030
}

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"php": "^7.3 || ^8.0",
1818
"phpunit/phpunit": "^9.5",
1919
"php-http/mock-client": "^1.2",
20+
"psr/http-client": "^1.0",
2021
"nyholm/psr7": "^1.0"
2122
},
2223
"require-dev": {

src/BaseTestCase.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
namespace Geocoder\IntegrationTest;
1212

1313
use Http\Client\Curl\Client as HttplugClient;
14-
use Http\Client\HttpClient;
1514
use Http\Mock\Client as MockedHttpClient;
1615
use Nyholm\Psr7\Response;
1716
use PHPUnit\Framework\TestCase;
17+
use Psr\Http\Client\ClientInterface;
1818
use Psr\Http\Message\RequestInterface;
1919
use Psr\Http\Message\ResponseInterface;
2020

@@ -31,7 +31,7 @@ abstract protected function getCacheDir();
3131
/**
3232
* Get a real HTTP client. If a cache dir is set to a path it will use cached responses.
3333
*
34-
* @return HttpClient
34+
* @return ClientInterface
3535
*/
3636
protected function getHttpClient($apiKey = null)
3737
{
@@ -48,7 +48,7 @@ protected function getHttpClient($apiKey = null)
4848
* @param string|null $body
4949
* @param int $statusCode
5050
*
51-
* @return HttpClient
51+
* @return ClientInterface
5252
*/
5353
protected function getMockedHttpClient($body = null, $statusCode = 200)
5454
{
@@ -64,11 +64,11 @@ protected function getMockedHttpClient($body = null, $statusCode = 200)
6464
* @param string|null $body
6565
* @param int $statusCode
6666
*
67-
* @return HttpClient
67+
* @return ClientInterface
6868
*/
6969
protected function getMockedHttpClientCallback(callable $requestCallback)
7070
{
71-
$client = $this->getMockBuilder(HttpClient::class)->getMock();
71+
$client = $this->getMockBuilder(ClientInterface::class)->getMock();
7272

7373
$client
7474
->expects($this->once())

src/CachedResponseClient.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,21 @@
1010

1111
namespace Geocoder\IntegrationTest;
1212

13-
use Http\Client\HttpClient;
1413
use Nyholm\Psr7\Factory\HttplugFactory;
1514
use Nyholm\Psr7\Response;
15+
use Psr\Http\Client\ClientInterface;
1616
use Psr\Http\Message\RequestInterface;
17+
use Psr\Http\Message\ResponseInterface;
1718

1819
/**
1920
* Serve responses from local file cache.
2021
*
2122
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
2223
*/
23-
class CachedResponseClient implements HttpClient
24+
class CachedResponseClient implements ClientInterface
2425
{
25-
use HttpClientTrait;
26-
2726
/**
28-
* @var HttpClient
27+
* @var ClientInterface
2928
*/
3029
private $delegate;
3130

@@ -45,12 +44,12 @@ class CachedResponseClient implements HttpClient
4544
private $cacheDir;
4645

4746
/**
48-
* @param HttpClient $delegate
49-
* @param string $cacheDir
50-
* @param string|null $apiKey
51-
* @param string|null $appCode
47+
* @param ClientInterface $delegate
48+
* @param string $cacheDir
49+
* @param string|null $apiKey
50+
* @param string|null $appCode
5251
*/
53-
public function __construct(HttpClient $delegate, $cacheDir, $apiKey = null, $appCode = null)
52+
public function __construct(ClientInterface $delegate, $cacheDir, $apiKey = null, $appCode = null)
5453
{
5554
$this->delegate = $delegate;
5655
$this->cacheDir = $cacheDir;
@@ -61,7 +60,7 @@ public function __construct(HttpClient $delegate, $cacheDir, $apiKey = null, $ap
6160
/**
6261
* {@inheritdoc}
6362
*/
64-
protected function doSendRequest(RequestInterface $request)
63+
public function sendRequest(RequestInterface $request): ResponseInterface
6564
{
6665
$host = (string) $request->getUri()->getHost();
6766
$cacheKey = (string) $request->getUri();

src/HttpClientTrait.php

Lines changed: 0 additions & 48 deletions
This file was deleted.

src/ProviderIntegrationTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
use Geocoder\Provider\Provider;
2323
use Geocoder\Query\GeocodeQuery;
2424
use Geocoder\Query\ReverseQuery;
25-
use Http\Client\HttpClient;
26-
use Http\Discovery\HttpClientDiscovery;
25+
use Http\Discovery\Psr18ClientDiscovery;
2726
use Nyholm\Psr7\Response;
2827
use PHPUnit\Framework\TestCase;
28+
use Psr\Http\Client\ClientInterface;
2929
use Psr\Http\Message\ResponseInterface;
3030

3131
/**
@@ -47,7 +47,7 @@ abstract class ProviderIntegrationTest extends TestCase
4747
/**
4848
* @return Provider that is used in the tests.
4949
*/
50-
abstract protected function createProvider(HttpClient $httpClient);
50+
abstract protected function createProvider(ClientInterface $httpClient);
5151

5252
/**
5353
* @return string the directory where cached responses are stored
@@ -62,11 +62,11 @@ abstract protected function getApiKey();
6262
/**
6363
* @param ResponseInterface $response
6464
*
65-
* @return \PHPUnit_Framework_MockObject_MockObject|HttpClient
65+
* @return \PHPUnit_Framework_MockObject_MockObject|ClientInterface
6666
*/
6767
private function getHttpClient(ResponseInterface $response)
6868
{
69-
$client = $this->getMockForAbstractClass(HttpClient::class);
69+
$client = $this->getMockForAbstractClass(ClientInterface::class);
7070

7171
$client
7272
->expects($this->any())
@@ -84,9 +84,9 @@ private function getHttpClient(ResponseInterface $response)
8484
private function getCachedHttpClient()
8585
{
8686
try {
87-
$client = HttpClientDiscovery::find();
88-
} catch (\Http\Discovery\NotFoundException $e) {
89-
$client = $this->getMockForAbstractClass(HttpClient::class);
87+
$client = Psr18ClientDiscovery::find();
88+
} catch (\Http\Discovery\Exception\NotFoundException $e) {
89+
$client = $this->getMockForAbstractClass(ClientInterface::class);
9090

9191
$client
9292
->expects($this->any())

tests/NominatimTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
use Geocoder\IntegrationTest\ProviderIntegrationTest;
1414
use Geocoder\Provider\Nominatim\Nominatim;
15-
use Http\Client\HttpClient;
15+
use Psr\Http\Client\ClientInterface;
1616

1717
/**
1818
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
@@ -22,7 +22,7 @@ class NominatimTest extends ProviderIntegrationTest
2222
protected $testIpv4 = false;
2323
protected $testIpv6 = false;
2424

25-
protected function createProvider(HttpClient $httpClient)
25+
protected function createProvider(ClientInterface $httpClient)
2626
{
2727
return Nominatim::withOpenStreetMapServer($httpClient, 'Geocoder PHP/Nominatim Provider/Nominatim Test');
2828
}

0 commit comments

Comments
 (0)