Skip to content

Commit f811f37

Browse files
authored
Provide PHP 8.0 compatibility (#335)
* Provide PHP 7.1 - 8.0 compatibility The packaging gymnastics to support Guzzle 6 + 7 is only needed if PHP 7.1 support is still desired. Guzzle 6 supports PHP <= 7.4 and Guzzle 7 supports PHP 7.2 - 8.0. * refactor: Use mock client instead of Guzzle for tests - Use the error plugin to check for error responses — this is the default setting when a HTTP client is not explicitly set. - Remove 'testExtendedClient' because it tests pure Guzzle functionality. This test was added when this library was coupled to Guzzle, but now it doesn't add any value over 'testBasicClient'.
1 parent 613c8b2 commit f811f37

File tree

4 files changed

+41
-94
lines changed

4 files changed

+41
-94
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ composer.lock
44
# PHPUnit
55
phpunit.phar
66
phpunit.xml
7+
.phpunit.result.cache

composer.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@
4343
"psr/http-message": "^1.0"
4444
},
4545
"require-dev": {
46-
"php-http/guzzle6-adapter": "^1.0 || ^2.0",
47-
"phpunit/phpunit": "^7.0",
46+
"guzzlehttp/psr7": "^1.7.0",
47+
"php-http/mock-client": "^1.4",
48+
"phpunit/phpunit": "^7.5 || ^8.5 || ^9.3",
4849
"squizlabs/php_codesniffer": "^3.1"
4950
},
5051
"config": {

tests/IntercomClientTest.php

+36-91
Original file line numberDiff line numberDiff line change
@@ -3,57 +3,29 @@
33
namespace Intercom\Test;
44

55
use DateTimeImmutable;
6-
use GuzzleHttp\Client as GuzzleClient;
7-
use GuzzleHttp\Handler\MockHandler;
8-
use GuzzleHttp\HandlerStack;
9-
use GuzzleHttp\Middleware;
106
use GuzzleHttp\Psr7\Response;
11-
use Http\Adapter\Guzzle6\Client;
7+
use Http\Client\Common\Plugin\ErrorPlugin;
8+
use Http\Client\Common\PluginClient;
129
use Http\Client\Exception;
10+
use Http\Discovery\HttpClientDiscovery;
11+
use Http\Discovery\Strategy\MockClientStrategy;
12+
use Http\Mock\Client;
1313
use Intercom\IntercomClient;
14-
use PHPUnit\Framework\TestCase;
1514
use stdClass;
1615

1716
class IntercomClientTest extends TestCase
1817
{
19-
public function testBasicClient()
18+
protected function setUp(): void
2019
{
21-
$mock = new MockHandler([
22-
new Response(200, ['X-Foo' => 'Bar'], "{\"foo\":\"bar\"}")
23-
]);
24-
25-
$container = [];
26-
$history = Middleware::history($container);
27-
$stack = HandlerStack::create($mock);
28-
$stack->push($history);
29-
30-
$httpClient = new Client(new GuzzleClient(['handler' => $stack]));
31-
32-
$client = new IntercomClient('u', 'p');
33-
$client->setHttpClient($httpClient);
34-
35-
$client->users->create([
36-
'email' => '[email protected]'
37-
]);
38-
39-
foreach ($container as $transaction) {
40-
$basic = $transaction['request']->getHeaders()['Authorization'][0];
41-
$this->assertSame("Basic dTpw", $basic);
42-
}
20+
HttpClientDiscovery::prependStrategy(MockClientStrategy::class);
4321
}
4422

45-
public function testExtendedClient()
23+
public function testBasicClient()
4624
{
47-
$mock = new MockHandler([
25+
$httpClient = new Client();
26+
$httpClient->addResponse(
4827
new Response(200, ['X-Foo' => 'Bar'], "{\"foo\":\"bar\"}")
49-
]);
50-
51-
$container = [];
52-
$history = Middleware::history($container);
53-
$stack = HandlerStack::create($mock);
54-
$stack->push($history);
55-
56-
$httpClient = new Client(new GuzzleClient(['handler' => $stack, 'connect_timeout' => 10]));
28+
);
5729

5830
$client = new IntercomClient('u', 'p');
5931
$client->setHttpClient($httpClient);
@@ -62,24 +34,18 @@ public function testExtendedClient()
6234
'email' => '[email protected]'
6335
]);
6436

65-
foreach ($container as $transaction) {
66-
$options = $transaction['options'];
67-
$this->assertSame(10, $options['connect_timeout']);
37+
foreach ($httpClient->getRequests() as $request) {
38+
$basic = $request->getHeaders()['Authorization'][0];
39+
$this->assertSame("Basic dTpw", $basic);
6840
}
6941
}
7042

7143
public function testClientWithExtraHeaders()
7244
{
73-
$mock = new MockHandler([
45+
$httpClient = new Client();
46+
$httpClient->addResponse(
7447
new Response(200, ['X-Foo' => 'Bar'], "{\"foo\":\"bar\"}")
75-
]);
76-
77-
$container = [];
78-
$history = Middleware::history($container);
79-
$stack = HandlerStack::create($mock);
80-
$stack->push($history);
81-
82-
$httpClient = new Client(new GuzzleClient(['handler' => $stack]));
48+
);
8349

8450
$client = new IntercomClient('u', 'p', ['Custom-Header' => 'value']);
8551
$client->setHttpClient($httpClient);
@@ -88,8 +54,8 @@ public function testClientWithExtraHeaders()
8854
'email' => '[email protected]'
8955
]);
9056

91-
foreach ($container as $transaction) {
92-
$headers = $transaction['request']->getHeaders();
57+
foreach ($httpClient->getRequests() as $request) {
58+
$headers = $request->getHeaders();
9359
$this->assertSame('application/json', $headers['Accept'][0]);
9460
$this->assertSame('application/json', $headers['Content-Type'][0]);
9561
$this->assertSame('value', $headers['Custom-Header'][0]);
@@ -98,16 +64,11 @@ public function testClientWithExtraHeaders()
9864

9965
public function testClientErrorHandling()
10066
{
101-
$mock = new MockHandler([
67+
$httpClient = new Client();
68+
$httpClient->addResponse(
10269
new Response(404)
103-
]);
104-
105-
$container = [];
106-
$history = Middleware::history($container);
107-
$stack = HandlerStack::create($mock);
108-
$stack->push($history);
109-
110-
$httpClient = new Client(new GuzzleClient(['handler' => $stack]));
70+
);
71+
$httpClient = new PluginClient($httpClient, [new ErrorPlugin()]);
11172

11273
$client = new IntercomClient('u', 'p');
11374
$client->setHttpClient($httpClient);
@@ -120,16 +81,11 @@ public function testClientErrorHandling()
12081

12182
public function testServerErrorHandling()
12283
{
123-
$mock = new MockHandler([
84+
$httpClient = new Client();
85+
$httpClient->addResponse(
12486
new Response(500)
125-
]);
126-
127-
$container = [];
128-
$history = Middleware::history($container);
129-
$stack = HandlerStack::create($mock);
130-
$stack->push($history);
131-
132-
$httpClient = new Client(new GuzzleClient(['handler' => $stack]));
87+
);
88+
$httpClient = new PluginClient($httpClient, [new ErrorPlugin()]);
13389

13490
$client = new IntercomClient('u', 'p');
13591
$client->setHttpClient($httpClient);
@@ -142,16 +98,10 @@ public function testServerErrorHandling()
14298

14399
public function testPaginationHelper()
144100
{
145-
$mock = new MockHandler([
101+
$httpClient = new Client();
102+
$httpClient->addResponse(
146103
new Response(200, ['X-Foo' => 'Bar'], "{\"foo\":\"bar\"}")
147-
]);
148-
149-
$container = [];
150-
$history = Middleware::history($container);
151-
$stack = HandlerStack::create($mock);
152-
$stack->push($history);
153-
154-
$httpClient = new Client(new GuzzleClient(['handler' => $stack]));
104+
);
155105

156106
$client = new IntercomClient('u', 'p');
157107
$client->setHttpClient($httpClient);
@@ -161,8 +111,8 @@ public function testPaginationHelper()
161111

162112
$client->nextPage($pages);
163113

164-
foreach ($container as $transaction) {
165-
$host = $transaction['request']->getUri()->getHost();
114+
foreach ($httpClient->getRequests() as $request) {
115+
$host = $request->getUri()->getHost();
166116
$this->assertSame("foo.com", $host);
167117
}
168118
}
@@ -171,7 +121,9 @@ public function testRateLimitDetails()
171121
{
172122
date_default_timezone_set('UTC');
173123
$time = time() + 7;
174-
$mock = new MockHandler([
124+
125+
$httpClient = new Client();
126+
$httpClient->addResponse(
175127
new Response(
176128
200,
177129
[
@@ -181,14 +133,7 @@ public function testRateLimitDetails()
181133
],
182134
"{\"foo\":\"bar\"}"
183135
)
184-
]);
185-
186-
$container = [];
187-
$history = Middleware::history($container);
188-
$stack = HandlerStack::create($mock);
189-
$stack->push($history);
190-
191-
$httpClient = new Client(new GuzzleClient(['handler' => $stack]));
136+
);
192137

193138
$client = new IntercomClient('u', 'p');
194139
$client->setHttpClient($httpClient);

tests/TestCase.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ abstract class TestCase extends BaseTestCase
1212
*/
1313
protected $client;
1414

15-
protected function setUp()
15+
protected function setUp(): void
1616
{
1717
parent::setUp();
1818

0 commit comments

Comments
 (0)