Skip to content

Commit 1f8fecc

Browse files
andrew-dembdbu
andauthored
Make package compatible with stream interface Compatibility stream interface v2 (#75)
* make compatible with stream interface * 🚨 Explicitly specify what features of HTTP client is not supported by current implementation to avoid having failed tests * 📦 Explicitly declare compatibility versions for `psr/http-message` There is implementation for interface from this package * 📦 `fread` requires to pass only `int<1, max>` - early return empty string in case of `0` length --------- Co-authored-by: David Buchmann <[email protected]>
1 parent 4a69954 commit 1f8fecc

9 files changed

+65
-41
lines changed

.github/workflows/ci.yml

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ name: CI
33
on:
44
push:
55
branches:
6-
- 2.x
6+
- '[0-9]+.x'
7+
- '[0-9]+.[0-9]+'
8+
- '[0-9]+.[0-9]+.x'
79
pull_request:
810

911
jobs:
@@ -91,7 +93,7 @@ jobs:
9193
- name: Setup PHP
9294
uses: shivammathur/setup-php@v2
9395
with:
94-
php-version: 7.4
96+
php-version: 8.3
9597
tools: composer
9698
coverage: xdebug
9799

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Change Log
22

3+
## 2.3.0
4+
5+
* Fixed compatibility with `psr/http-message` v2
6+
* The `Http\Client\Socket\Stream` has BC breaks if you extended it. It is not meant to be extended, declaring it as `@internal` now.
7+
38
## 2.2.0
49

510
* Allow installation with Symfony 7

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ First launch the http server:
2828
$ ./vendor/bin/http_test_server > /dev/null 2>&1 &
2929
```
3030

31-
Then generate ssh certificates:
31+
Then generate SSL certificates:
3232

3333
```bash
3434
$ composer gen-ssl

composer.json

+10-8
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,20 @@
99
}
1010
],
1111
"require": {
12-
"php": "^7.2 || ^8.0",
13-
"nyholm/psr7": "^1.3",
14-
"php-http/httplug": "^2.0",
12+
"php": "^8.1",
13+
"nyholm/psr7": "^1.8.1",
14+
"php-http/httplug": "^2.4",
1515
"psr/http-client": "^1.0",
16+
"psr/http-message": "^1.0 || ^2.0",
1617
"symfony/options-resolver": "^2.6 || ^3.4 || ^4.4 || ^5.0 || ^6.0 || ^7.0"
1718
},
1819
"require-dev": {
19-
"friendsofphp/php-cs-fixer": "^2.2 || ^3.0",
20-
"php-http/client-integration-tests": "^3.0",
21-
"php-http/message": "^1.9",
22-
"php-http/client-common": "^2.3",
23-
"phpunit/phpunit": "^8.5.23 || ~9.5"
20+
"friendsofphp/php-cs-fixer": "^3.51",
21+
"php-http/client-integration-tests": "^3.1.1",
22+
"php-http/message": "^1.16",
23+
"php-http/client-common": "^2.7",
24+
"phpunit/phpunit": "^8.5.23 || ~9.5",
25+
"php-http/message-factory": "^1.1"
2426
},
2527
"provide": {
2628
"php-http/client-implementation": "1.0",

phpunit.xml.dist

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
<testsuites>
99
<testsuite name="Socket Client Test Suite">
1010
<directory>tests/</directory>
11-
<exclude>tests/SocketClientFeatureTest.php</exclude>
1211
</testsuite>
1312
</testsuites>
1413
<php>

src/ResponseReader.php

-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Http\Client\Socket\Exception\BrokenPipeException;
66
use Http\Client\Socket\Exception\TimeoutException;
7-
use Http\Message\ResponseFactory;
87
use Nyholm\Psr7\Response;
98
use Psr\Http\Message\RequestInterface;
109
use Psr\Http\Message\ResponseInterface;
@@ -18,11 +17,6 @@
1817
*/
1918
trait ResponseReader
2019
{
21-
/**
22-
* @var ResponseFactory For creating response
23-
*/
24-
protected $responseFactory;
25-
2620
/**
2721
* Read a response from a socket.
2822
*

src/Stream.php

+18-19
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function __construct(RequestInterface $request, $socket, ?int $size = nul
6161
$this->request = $request;
6262
}
6363

64-
public function __toString()
64+
public function __toString(): string
6565
{
6666
try {
6767
return $this->getContents();
@@ -70,7 +70,7 @@ public function __toString()
7070
}
7171
}
7272

73-
public function close()
73+
public function close(): void
7474
{
7575
if ($this->isDetached || null === $this->socket) {
7676
throw new StreamException('Stream is detached');
@@ -93,12 +93,12 @@ public function detach()
9393
/**
9494
* @return int<0, max>|null
9595
*/
96-
public function getSize()
96+
public function getSize(): ?int
9797
{
9898
return $this->size;
9999
}
100100

101-
public function tell()
101+
public function tell(): int
102102
{
103103
if ($this->isDetached || null === $this->socket) {
104104
throw new StreamException('Stream is detached');
@@ -111,7 +111,7 @@ public function tell()
111111
return $tell;
112112
}
113113

114-
public function eof()
114+
public function eof(): bool
115115
{
116116
if ($this->isDetached || null === $this->socket) {
117117
throw new StreamException('Stream is detached');
@@ -120,50 +120,49 @@ public function eof()
120120
return feof($this->socket);
121121
}
122122

123-
public function isSeekable()
123+
public function isSeekable(): bool
124124
{
125125
return false;
126126
}
127127

128-
/**
129-
* @return void
130-
*/
131-
public function seek($offset, $whence = SEEK_SET)
128+
public function seek($offset, $whence = SEEK_SET): void
132129
{
133130
throw new StreamException('This stream is not seekable');
134131
}
135132

136-
/**
137-
* @return void
138-
*/
139-
public function rewind()
133+
public function rewind(): void
140134
{
141135
throw new StreamException('This stream is not seekable');
142136
}
143137

144-
public function isWritable()
138+
public function isWritable(): bool
145139
{
146140
return false;
147141
}
148142

149-
public function write($string)
143+
public function write($string): int
150144
{
151145
throw new StreamException('This stream is not writable');
152146
}
153147

154-
public function isReadable()
148+
public function isReadable(): bool
155149
{
156150
return true;
157151
}
158152

159153
/**
160154
* @param int<0, max> $length
161155
*/
162-
public function read($length)
156+
public function read($length): string
163157
{
158+
if (0 === $length) {
159+
return '';
160+
}
161+
164162
if ($this->isDetached || null === $this->socket) {
165163
throw new StreamException('Stream is detached');
166164
}
165+
167166
if (null === $this->getSize()) {
168167
$read = fread($this->socket, $length);
169168
if (false === $read) {
@@ -197,7 +196,7 @@ public function read($length)
197196
return $read;
198197
}
199198

200-
public function getContents()
199+
public function getContents(): string
201200
{
202201
if ($this->isDetached || null === $this->socket) {
203202
throw new StreamException('Stream is detached');

tests/SocketClientFeatureTest.php

+25
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,29 @@ protected function createClient(): ClientInterface
1212
{
1313
return new SocketHttpClient();
1414
}
15+
16+
public function testAutoSetContentLength(): void
17+
{
18+
$this->markTestSkipped('Feature is unsupported');
19+
}
20+
21+
public function testGzip(): void
22+
{
23+
$this->markTestSkipped('Feature is unsupported');
24+
}
25+
26+
public function testDeflate(): void
27+
{
28+
$this->markTestSkipped('Feature is unsupported');
29+
}
30+
31+
public function testChunked(): void
32+
{
33+
$this->markTestSkipped('Feature is unsupported');
34+
}
35+
36+
public function testRedirect(): void
37+
{
38+
$this->markTestSkipped('Feature is unsupported');
39+
}
1540
}

tests/SocketHttpClientTest.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@
66
use Http\Client\Socket\Client as SocketHttpClient;
77
use Http\Client\Socket\Exception\NetworkException;
88
use Http\Client\Socket\Exception\TimeoutException;
9-
use Http\Message\MessageFactory\GuzzleMessageFactory;
9+
use Nyholm\Psr7\Factory\Psr17Factory;
1010

1111
class SocketHttpClientTest extends BaseTestCase
1212
{
1313
public function createClient($options = [])
1414
{
15-
$messageFactory = new GuzzleMessageFactory();
16-
17-
return new HttpMethodsClient(new SocketHttpClient($options), $messageFactory);
15+
return new HttpMethodsClient(new SocketHttpClient($options), new Psr17Factory());
1816
}
1917

2018
public function testTcpSocketDomain()

0 commit comments

Comments
 (0)