Skip to content

Commit fb4f345

Browse files
committed
use psr factories and typed methods
1 parent ed1bff2 commit fb4f345

File tree

7 files changed

+117
-167
lines changed

7 files changed

+117
-167
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010

1111
- Allow PHPUnit > 9
1212
- Removed deprecated PhpUnitBackwardCompatibleTrait
13+
- Replaced deprecated HttpBaseTest::$messageFactory with $requestFactory and $streamFactory
1314

1415
# 3.x
1516

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
],
1717
"require": {
1818
"php": "^7.4 || ^8.0",
19+
"ext-json": "*",
1920
"phpunit/phpunit": "^9.6.17 || ^10.0 || ^11.0 || ^12.0",
2021
"php-http/message": "^1.0 || ^2.0",
21-
"php-http/message-factory": "^1.0",
2222
"guzzlehttp/psr7": "^1.9 || ^2.0",
2323
"th3n3rd/cartesian-product": "^0.3"
2424
},

src/HttpAsyncClientTest.php

Lines changed: 45 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,44 @@
22

33
namespace Http\Client\Tests;
44

5+
use Http\Client\Exception;
56
use Http\Client\HttpAsyncClient;
7+
use Http\Promise\Promise;
68
use PHPUnit\Framework\Attributes\DataProvider;
79
use PHPUnit\Framework\Attributes\Group;
810

911
abstract class HttpAsyncClientTest extends HttpBaseTest
1012
{
11-
/**
12-
* @var HttpAsyncClient
13-
*/
14-
protected $httpAsyncClient;
13+
protected HttpAsyncClient $httpAsyncClient;
1514

16-
/**
17-
* {@inheritdoc}
18-
*/
1915
protected function setUp(): void
2016
{
2117
$this->httpAsyncClient = $this->createHttpAsyncClient();
2218
}
2319

24-
/**
25-
* {@inheritdoc}
26-
*/
2720
protected function tearDown(): void
2821
{
2922
unset($this->httpAdapter);
3023
}
3124

3225
abstract protected function createHttpAsyncClient(): HttpAsyncClient;
3326

34-
public function testSuccessiveCallMustUseResponseInterface()
27+
public function testSuccessiveCallMustUseResponseInterface(): void
3528
{
36-
$request = self::$messageFactory->createRequest(
37-
'GET',
38-
self::getUri(),
39-
self::$defaultHeaders
40-
);
29+
$request = self::$requestFactory->createRequest('GET', self::getUri());
30+
foreach (self::$defaultHeaders as $name => $value) {
31+
$request = $request->withHeader($name, $value);
32+
}
33+
4134

4235
$promise = $this->httpAsyncClient->sendAsyncRequest($request);
43-
$this->assertInstanceOf('Http\Promise\Promise', $promise);
36+
$this->assertInstanceOf(Promise::class, $promise);
4437

4538
$response = null;
4639
$promise->then()->then()->then(function ($r) use (&$response) {
4740
$response = $r;
4841

49-
return $response;
42+
return $r;
5043
});
5144

5245
$promise->wait(false);
@@ -58,23 +51,22 @@ public function testSuccessiveCallMustUseResponseInterface()
5851
);
5952
}
6053

61-
public function testSuccessiveInvalidCallMustUseException()
54+
public function testSuccessiveInvalidCallMustUseException(): void
6255
{
63-
$request = self::$messageFactory->createRequest(
64-
'GET',
65-
$this->getInvalidUri(),
66-
self::$defaultHeaders
67-
);
56+
$request = self::$requestFactory->createRequest('GET', $this->getInvalidUri());
57+
foreach (self::$defaultHeaders as $name => $value) {
58+
$request = $request->withHeader($name, $value);
59+
}
6860

6961
$promise = $this->httpAsyncClient->sendAsyncRequest($request);
70-
$this->assertInstanceOf('Http\Promise\Promise', $promise);
62+
$this->assertInstanceOf(Promise::class, $promise);
7163

7264
$exception = null;
7365
$response = null;
7466
$promise->then()->then()->then(function ($r) use (&$response) {
7567
$response = $r;
7668

77-
return $response;
69+
return $r;
7870
}, function ($e) use (&$exception) {
7971
$exception = $e;
8072

@@ -85,7 +77,7 @@ public function testSuccessiveInvalidCallMustUseException()
8577

8678
$this->assertNull($response);
8779
$this->assertNotNull($exception);
88-
$this->assertInstanceOf('\Http\Client\Exception', $exception);
80+
$this->assertInstanceOf(Exception::class, $exception);
8981
}
9082

9183
/**
@@ -94,21 +86,22 @@ public function testSuccessiveInvalidCallMustUseException()
9486
*/
9587
#[DataProvider('requestProvider')]
9688
#[Group('integration')]
97-
public function testAsyncSendRequest($method, $uri, array $headers, $body)
89+
public function testAsyncSendRequest(string $method, string $uri, array $headers, ?string $body): void
9890
{
9991
if (null != $body) {
10092
$headers['Content-Length'] = (string) strlen($body);
10193
}
10294

103-
$request = self::$messageFactory->createRequest(
104-
$method,
105-
$uri,
106-
$headers,
107-
$body
108-
);
95+
$request = self::$requestFactory->createRequest($method, $uri);
96+
foreach ($headers as $name => $value) {
97+
$request = $request->withHeader($name, $value);
98+
}
99+
if (null !== $body) {
100+
$request = $request->withBody(self::$streamFactory->createStream($body));
101+
}
109102

110103
$promise = $this->httpAsyncClient->sendAsyncRequest($request);
111-
$this->assertInstanceOf('Http\Promise\Promise', $promise);
104+
$this->assertInstanceOf(Promise::class, $promise);
112105

113106
$response = null;
114107
$promise->then(function ($r) use (&$response) {
@@ -131,18 +124,17 @@ public function testAsyncSendRequest($method, $uri, array $headers, $body)
131124
* @group integration
132125
*/
133126
#[Group('integration')]
134-
public function testSendAsyncWithInvalidUri()
127+
public function testSendAsyncWithInvalidUri(): void
135128
{
136-
$request = self::$messageFactory->createRequest(
137-
'GET',
138-
$this->getInvalidUri(),
139-
self::$defaultHeaders
140-
);
129+
$request = self::$requestFactory->createRequest('GET', $this->getInvalidUri());
130+
foreach (self::$defaultHeaders as $name => $value) {
131+
$request = $request->withHeader($name, $value);
132+
}
141133

142134
$exception = null;
143135
$response = null;
144136
$promise = $this->httpAsyncClient->sendAsyncRequest($request);
145-
$this->assertInstanceOf('Http\Promise\Promise', $promise);
137+
$this->assertInstanceOf(Promise::class, $promise);
146138

147139
$promise->then(function ($r) use (&$response) {
148140
$response = $r;
@@ -157,7 +149,7 @@ public function testSendAsyncWithInvalidUri()
157149

158150
$this->assertNull($response);
159151
$this->assertNotNull($exception);
160-
$this->assertInstanceOf('\Http\Client\Exception', $exception);
152+
$this->assertInstanceOf(Exception::class, $exception);
161153
}
162154

163155
/**
@@ -166,7 +158,7 @@ public function testSendAsyncWithInvalidUri()
166158
*/
167159
#[Group('integration')]
168160
#[DataProvider('requestWithOutcomeProvider')]
169-
public function testSendAsyncRequestWithOutcome($uriAndOutcome, $protocolVersion, array $headers, $body)
161+
public function testSendAsyncRequestWithOutcome(array $uriAndOutcome, string $protocolVersion, array $headers, string $body): void
170162
{
171163
if ('1.0' === $protocolVersion) {
172164
$body = null;
@@ -176,13 +168,14 @@ public function testSendAsyncRequestWithOutcome($uriAndOutcome, $protocolVersion
176168
$headers['Content-Length'] = (string) strlen($body);
177169
}
178170

179-
$request = self::$messageFactory->createRequest(
180-
$method = 'GET',
181-
$uriAndOutcome[0],
182-
$headers,
183-
$body,
184-
$protocolVersion
185-
);
171+
$request = self::$requestFactory->createRequest($method = 'GET', $uriAndOutcome[0]);
172+
foreach ($headers as $name => $value) {
173+
$request = $request->withHeader($name, $value);
174+
}
175+
if (null !== $body) {
176+
$request = $request->withBody(self::$streamFactory->createStream($body));
177+
}
178+
$request = $request->withProtocolVersion($protocolVersion);
186179

187180
$outcome = $uriAndOutcome[1];
188181
$outcome['protocolVersion'] = $protocolVersion;
@@ -195,7 +188,7 @@ public function testSendAsyncRequestWithOutcome($uriAndOutcome, $protocolVersion
195188
return $response;
196189
});
197190

198-
$this->assertInstanceOf('Http\Promise\Promise', $promise);
191+
$this->assertInstanceOf(Promise::class, $promise);
199192
$promise->wait();
200193
$this->assertResponse(
201194
$response,

src/HttpBaseTest.php

Lines changed: 21 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,29 @@
22

33
namespace Http\Client\Tests;
44

5-
use Http\Message\MessageFactory;
6-
use Http\Message\MessageFactory\GuzzleMessageFactory;
5+
use GuzzleHttp\Psr7\HttpFactory;
76
use Nerd\CartesianProduct\CartesianProduct;
87
use PHPUnit\Framework\TestCase;
8+
use Psr\Http\Message\RequestFactoryInterface;
99
use Psr\Http\Message\ResponseInterface;
10+
use Psr\Http\Message\StreamFactoryInterface;
1011

1112
abstract class HttpBaseTest extends TestCase
1213
{
13-
/**
14-
* @var string
15-
*/
16-
private static $logPath;
14+
private static string $logPath;
1715

18-
/**
19-
* @var MessageFactory
20-
*/
21-
protected static $messageFactory;
16+
protected static RequestFactoryInterface $requestFactory;
17+
protected static StreamFactoryInterface $streamFactory;
2218

23-
/**
24-
* @var array
25-
*/
26-
protected $defaultOptions = [
19+
protected array $defaultOptions = [
2720
'protocolVersion' => '1.1',
2821
'statusCode' => 200,
2922
'reasonPhrase' => 'OK',
3023
'headers' => ['Content-Type' => 'text/html'],
3124
'body' => 'Ok',
3225
];
3326

34-
/**
35-
* @var array
36-
*/
37-
protected static $defaultHeaders = [
27+
protected static array $defaultHeaders = [
3828
'Connection' => 'close',
3929
'User-Agent' => 'PHP HTTP Adapter',
4030
'Content-Length' => '0',
@@ -46,7 +36,7 @@ abstract class HttpBaseTest extends TestCase
4636
public static function setUpBeforeClass(): void
4737
{
4838
self::$logPath = PHPUnitUtility::getFile(true, 'php-http-adapter.log');
49-
self::$messageFactory = new GuzzleMessageFactory();
39+
self::$requestFactory = self::$streamFactory = new HttpFactory();
5040
}
5141

5242
/**
@@ -114,25 +104,19 @@ private static function getMethods(): array
114104
*
115105
* @return string|null
116106
*/
117-
protected static function getUri(array $query = [])
107+
protected static function getUri(array $query = []): ?string
118108
{
119109
return !empty($query)
120110
? PHPUnitUtility::getUri().'?'.http_build_query($query, '', '&')
121111
: PHPUnitUtility::getUri();
122112
}
123113

124-
/**
125-
* @return string
126-
*/
127-
protected function getInvalidUri()
114+
protected function getInvalidUri(): string
128115
{
129116
return 'http://invalid.php-http.org';
130117
}
131118

132-
/**
133-
* @return array
134-
*/
135-
private static function getUrisAndOutcomes()
119+
private static function getUrisAndOutcomes(): array
136120
{
137121
return [
138122
[
@@ -160,18 +144,15 @@ private static function getUrisAndOutcomes()
160144
];
161145
}
162146

163-
/**
164-
* @return array
165-
*/
166-
private static function getProtocolVersions()
147+
private static function getProtocolVersions(): array
167148
{
168149
return ['1.1', '1.0'];
169150
}
170151

171152
/**
172153
* @return string[]
173154
*/
174-
private static function getHeaders()
155+
private static function getHeaders(): array
175156
{
176157
$headers = self::$defaultHeaders;
177158
$headers['Accept-Charset'] = 'utf-8';
@@ -183,21 +164,15 @@ private static function getHeaders()
183164
];
184165
}
185166

186-
/**
187-
* @return array
188-
*/
189-
private static function getBodies()
167+
private static function getBodies(): array
190168
{
191169
return [
192170
null,
193171
http_build_query(self::getData(), '', '&'),
194172
];
195173
}
196174

197-
/**
198-
* @return array
199-
*/
200-
private static function getData()
175+
private static function getData(): array
201176
{
202177
return ['param1' => 'foo', 'param2' => ['bar', ['baz']]];
203178
}
@@ -226,16 +201,13 @@ protected function assertResponse(ResponseInterface $response, array $options =
226201
}
227202

228203
/**
229-
* @param string $method
230204
* @param string[] $headers
231-
* @param string $body
232-
* @param string $protocolVersion
233205
*/
234206
protected function assertRequest(
235-
$method,
236-
array $headers = [],
237-
$body = null,
238-
$protocolVersion = '1.1'
207+
string $method,
208+
array $headers = [],
209+
?string $body = null,
210+
string $protocolVersion = '1.1'
239211
) {
240212
$request = $this->getRequest();
241213

@@ -266,10 +238,7 @@ protected function assertRequest(
266238
}
267239
}
268240

269-
/**
270-
* @return array
271-
*/
272-
protected function getRequest()
241+
protected function getRequest(): array
273242
{
274243
$file = fopen(self::$logPath, 'r');
275244
flock($file, LOCK_EX);

0 commit comments

Comments
 (0)