Skip to content

Commit 22ac1c3

Browse files
committed
🚿 use PSR-17 factories in tests
1 parent f27e22a commit 22ac1c3

File tree

5 files changed

+111
-76
lines changed

5 files changed

+111
-76
lines changed

tests/CurlUtils/CurlHandleTest.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
namespace chillerlan\HTTPTest\CurlUtils;
1212

1313
use chillerlan\HTTP\HTTPOptions;
14-
use chillerlan\HTTP\Psr17\RequestFactory;
14+
use chillerlan\HTTP\Psr17\{RequestFactory, StreamFactory};
1515
use chillerlan\HTTP\Psr18\CurlClient;
1616
use chillerlan\HTTP\Psr7\Request;
1717
use Exception;
1818
use PHPUnit\Framework\TestCase;
19-
2019
use Psr\Http\Client\ClientInterface;
21-
use function chillerlan\HTTP\Psr17\create_stream;
20+
use Psr\Http\Message\{RequestFactoryInterface, StreamFactoryInterface};
21+
2222
use function chillerlan\HTTP\Psr7\get_json;
2323
use function str_repeat, strlen, strtolower;
2424

@@ -28,8 +28,8 @@
2828
class CurlHandleTest extends TestCase{
2929

3030
protected ClientInterface $http;
31-
32-
protected RequestFactory $requestFactory;
31+
protected RequestFactoryInterface $requestFactory;
32+
protected StreamFactoryInterface $streamFactory;
3333

3434
protected function setUp():void{
3535
$options = new HTTPOptions([
@@ -38,6 +38,7 @@ protected function setUp():void{
3838

3939
$this->http = new CurlClient($options);
4040
$this->requestFactory = new RequestFactory;
41+
$this->streamFactory = new StreamFactory;
4142
}
4243

4344
public function requestMethodProvider():array{
@@ -102,7 +103,7 @@ public function testRequestMethodsWithFormBody(string $method):void{
102103
$request = $this->requestFactory->createRequest($method, $url)
103104
->withHeader('Content-type', 'x-www-form-urlencoded')
104105
->withHeader('Content-Length', strlen($body))
105-
->withBody(create_stream($body))
106+
->withBody($this->streamFactory->createStream($body))
106107
;
107108

108109
$response = $this->http->sendRequest($request);
@@ -137,7 +138,7 @@ public function testRequestMethodsWithJsonBody(string $method):void{
137138
$body = '{"foo":"bar"}';
138139
$request = $this->requestFactory->createRequest($method, $url)
139140
->withHeader('Content-type', 'application/json')
140-
->withBody(create_stream($body))
141+
->withBody($this->streamFactory->createStream($body))
141142
;
142143

143144
$response = $this->http->sendRequest($request);
@@ -168,7 +169,7 @@ public function testLargeBody():void{
168169
$request = $this->requestFactory->createRequest('POST', 'https://httpbin.org/post')
169170
->withHeader('Content-type', 'text/plain')
170171
->withHeader('Content-Length', strlen($body))
171-
->withBody(create_stream($body))
172+
->withBody($this->streamFactory->createStream($body))
172173
;
173174

174175
$response = $this->http->sendRequest($request);

tests/Psr7/MessageHelpersTest.php

Lines changed: 67 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010

1111
namespace chillerlan\HTTPTest\Psr7;
1212

13-
use chillerlan\HTTP\Psr7\{Request, Response, Uri};
13+
use chillerlan\HTTP\Psr17\{RequestFactory, ResponseFactory, StreamFactory, UriFactory};
14+
use Psr\Http\Message\{RequestFactoryInterface, ResponseFactoryInterface, StreamFactoryInterface, UriFactoryInterface};
1415
use PHPUnit\Framework\TestCase;
1516
use Psr\Http\Message\MessageInterface;
1617
use TypeError;
1718

18-
use function chillerlan\HTTP\Psr17\create_stream;
1919
use function chillerlan\HTTP\Psr7\{
2020
decompress_content, get_json, get_xml, message_to_string, r_rawurlencode,
2121
uriIsAbsolute, uriIsAbsolutePathReference, uriIsNetworkPathReference,
@@ -25,6 +25,18 @@
2525

2626
class MessageHelpersTest extends TestCase{
2727

28+
protected RequestFactoryInterface $requestFactory;
29+
protected ResponseFactoryInterface $responseFactory;
30+
protected StreamFactoryInterface $streamFactory;
31+
protected UriFactoryInterface $uriFactory;
32+
33+
protected function setUp():void{
34+
$this->requestFactory = new RequestFactory;
35+
$this->responseFactory = new ResponseFactory;
36+
$this->streamFactory = new StreamFactory;
37+
$this->uriFactory = new UriFactory;
38+
}
39+
2840
public function rawurlencodeDataProvider():array{
2941
return [
3042
'null' => [null, ''],
@@ -59,7 +71,7 @@ public function testRawurlencodeTypeErrorException():void{
5971

6072
public function testGetJSON():void{
6173

62-
$r = (new Response)->withBody(create_stream('{"foo":"bar"}'));
74+
$r = $this->responseFactory->createResponse()->withBody($this->streamFactory->createStream('{"foo":"bar"}'));
6375

6476
$this::assertSame('bar', get_json($r)->foo);
6577

@@ -70,7 +82,9 @@ public function testGetJSON():void{
7082

7183
public function testGetXML():void{
7284

73-
$r = (new Response)->withBody(create_stream('<?xml version="1.0" encoding="UTF-8"?><root><foo>bar</foo></root>'));
85+
$r = $this->responseFactory
86+
->createResponse()
87+
->withBody($this->streamFactory->createStream('<?xml version="1.0" encoding="UTF-8"?><root><foo>bar</foo></root>'));
7488

7589
$this::assertSame('bar', get_xml($r)->foo->__toString());
7690

@@ -79,23 +93,29 @@ public function testGetXML():void{
7993
$this::assertSame('bar', get_xml($r, true)['foo']);
8094
}
8195

82-
public function messageDataProvider():array{
83-
return [
84-
'Request' => [new Request('GET', 'https://localhost/foo'), 'GET /foo HTTP/1.1'."\r\n".'Host: localhost'."\r\n".'foo: bar'."\r\n\r\n".'testbody'],
85-
'Response' => [new Response, 'HTTP/1.1 200 OK'."\r\n".'foo: bar'."\r\n\r\n".'testbody'],
86-
];
87-
}
96+
public function testMessageToString():void{
97+
$body = $this->streamFactory->createStream('testbody');
98+
99+
$request = $this->requestFactory
100+
->createRequest('GET', 'https://localhost/foo')
101+
->withAddedHeader('foo', 'bar')
102+
->withBody($body)
103+
;
104+
105+
$this::assertSame(
106+
'GET /foo HTTP/1.1'."\r\n".'Host: localhost'."\r\n".'foo: bar'."\r\n\r\n".'testbody',
107+
message_to_string($request)
108+
);
109+
110+
$response = $this->responseFactory
111+
->createResponse()
112+
->withAddedHeader('foo', 'bar')
113+
->withBody($body)
114+
;
88115

89-
/**
90-
* @dataProvider messageDataProvider
91-
*
92-
* @param \Psr\Http\Message\MessageInterface $message
93-
* @param string $expected
94-
*/
95-
public function testMessageToString(MessageInterface $message, string $expected):void{
96116
$this::assertSame(
97-
$expected,
98-
message_to_string($message->withAddedHeader('foo', 'bar')->withBody(create_stream('testbody')))
117+
'HTTP/1.1 200 OK'."\r\n".'foo: bar'."\r\n\r\n".'testbody',
118+
message_to_string($response)
99119
);
100120
}
101121

@@ -113,50 +133,50 @@ public function decompressDataProvider():array{
113133
*/
114134
public function testDecompressContent(string $fn, string $encoding):void{
115135
$data = $expected = str_repeat('compressed string ', 100);
116-
$response = (new Response);
136+
$response = $this->responseFactory->createResponse();
117137

118138
if($fn){
119139
$data = $fn($data);
120140
$response = $response->withHeader('Content-Encoding', $encoding);
121141
}
122142

123-
$response = $response->withBody(create_stream($data));
143+
$response = $response->withBody($this->streamFactory->createStream($data));
124144

125145
$this::assertSame($expected, decompress_content($response));
126146
}
127147

128148
public function testUriIsAbsolute():void{
129-
$this::assertTrue(uriIsAbsolute(new Uri('http://example.org')));
130-
$this::assertFalse(uriIsAbsolute(new Uri('//example.org')));
131-
$this::assertFalse(uriIsAbsolute(new Uri('/abs-path')));
132-
$this::assertFalse(uriIsAbsolute(new Uri('rel-path')));
149+
$this::assertTrue(uriIsAbsolute($this->uriFactory->createUri('http://example.org')));
150+
$this::assertFalse(uriIsAbsolute($this->uriFactory->createUri('//example.org')));
151+
$this::assertFalse(uriIsAbsolute($this->uriFactory->createUri('/abs-path')));
152+
$this::assertFalse(uriIsAbsolute($this->uriFactory->createUri('rel-path')));
133153
}
134154

135155
public function testUriIsNetworkPathReference():void{
136-
$this::assertFalse(uriIsNetworkPathReference(new Uri('http://example.org')));
137-
$this::assertTrue(uriIsNetworkPathReference(new Uri('//example.org')));
138-
$this::assertFalse(uriIsNetworkPathReference(new Uri('/abs-path')));
139-
$this::assertFalse(uriIsNetworkPathReference(new Uri('rel-path')));
156+
$this::assertFalse(uriIsNetworkPathReference($this->uriFactory->createUri('http://example.org')));
157+
$this::assertTrue(uriIsNetworkPathReference($this->uriFactory->createUri('//example.org')));
158+
$this::assertFalse(uriIsNetworkPathReference($this->uriFactory->createUri('/abs-path')));
159+
$this::assertFalse(uriIsNetworkPathReference($this->uriFactory->createUri('rel-path')));
140160
}
141161

142162
public function testUriIsAbsolutePathReference():void{
143-
$this::assertFalse(uriIsAbsolutePathReference(new Uri('http://example.org')));
144-
$this::assertFalse(uriIsAbsolutePathReference(new Uri('//example.org')));
145-
$this::assertTrue(uriIsAbsolutePathReference(new Uri('/abs-path')));
146-
$this::assertTrue(uriIsAbsolutePathReference(new Uri('/')));
147-
$this::assertFalse(uriIsAbsolutePathReference(new Uri('rel-path')));
163+
$this::assertFalse(uriIsAbsolutePathReference($this->uriFactory->createUri('http://example.org')));
164+
$this::assertFalse(uriIsAbsolutePathReference($this->uriFactory->createUri('//example.org')));
165+
$this::assertTrue(uriIsAbsolutePathReference($this->uriFactory->createUri('/abs-path')));
166+
$this::assertTrue(uriIsAbsolutePathReference($this->uriFactory->createUri('/')));
167+
$this::assertFalse(uriIsAbsolutePathReference($this->uriFactory->createUri('rel-path')));
148168
}
149169

150170
public function testUriIsRelativePathReference():void{
151-
$this::assertFalse(uriIsRelativePathReference(new Uri('http://example.org')));
152-
$this::assertFalse(uriIsRelativePathReference(new Uri('//example.org')));
153-
$this::assertFalse(uriIsRelativePathReference(new Uri('/abs-path')));
154-
$this::assertTrue(uriIsRelativePathReference(new Uri('rel-path')));
155-
$this::assertTrue(uriIsRelativePathReference(new Uri('')));
171+
$this::assertFalse(uriIsRelativePathReference($this->uriFactory->createUri('http://example.org')));
172+
$this::assertFalse(uriIsRelativePathReference($this->uriFactory->createUri('//example.org')));
173+
$this::assertFalse(uriIsRelativePathReference($this->uriFactory->createUri('/abs-path')));
174+
$this::assertTrue(uriIsRelativePathReference($this->uriFactory->createUri('rel-path')));
175+
$this::assertTrue(uriIsRelativePathReference($this->uriFactory->createUri('')));
156176
}
157177

158178
public function testUriAddAndRemoveQueryValues():void{
159-
$uri = new Uri;
179+
$uri = $this->uriFactory->createUri();
160180

161181
$uri = uriWithQueryValue($uri, 'a', 'b');
162182
$uri = uriWithQueryValue($uri, 'c', 'd');
@@ -172,7 +192,7 @@ public function testUriAddAndRemoveQueryValues():void{
172192
}
173193

174194
public function testUriWithQueryValueReplacesSameKeys():void{
175-
$uri = new Uri;
195+
$uri = $this->uriFactory->createUri();
176196

177197
$uri = uriWithQueryValue($uri, 'a', 'b');
178198
$uri = uriWithQueryValue($uri, 'c', 'd');
@@ -181,37 +201,37 @@ public function testUriWithQueryValueReplacesSameKeys():void{
181201
}
182202

183203
public function testUriWithoutQueryValueRemovesAllSameKeys():void{
184-
$uri = (new Uri)->withQuery('a=b&c=d&a=e');
204+
$uri = $this->uriFactory->createUri()->withQuery('a=b&c=d&a=e');
185205

186206
$uri = uriWithoutQueryValue($uri, 'a');
187207
$this::assertSame('c=d', $uri->getQuery());
188208
}
189209

190210
public function testUriRemoveNonExistingQueryValue():void{
191-
$uri = new Uri;
211+
$uri = $this->uriFactory->createUri();
192212
$uri = uriWithQueryValue($uri, 'a', 'b');
193213
$uri = uriWithoutQueryValue($uri, 'c');
194214
$this::assertSame('a=b', $uri->getQuery());
195215
}
196216

197217
public function testUriWithQueryValueHandlesEncoding():void{
198-
$uri = new Uri;
218+
$uri = $this->uriFactory->createUri();
199219
$uri = uriWithQueryValue($uri, 'E=mc^2', 'ein&stein');
200220
$this::assertSame('E%3Dmc%5E2=ein%26stein', $uri->getQuery(), 'Decoded key/value get encoded');
201221

202-
$uri = new Uri;
222+
$uri = $this->uriFactory->createUri();
203223
$uri = uriWithQueryValue($uri, 'E%3Dmc%5e2', 'ein%26stein');
204224
$this::assertSame('E%3Dmc%5e2=ein%26stein', $uri->getQuery(), 'Encoded key/value do not get double-encoded');
205225
}
206226

207227
public function testUriWithoutQueryValueHandlesEncoding():void{
208228
// It also tests that the case of the percent-encoding does not matter,
209229
// i.e. both lowercase "%3d" and uppercase "%5E" can be removed.
210-
$uri = (new Uri)->withQuery('E%3dmc%5E2=einstein&foo=bar');
230+
$uri = $this->uriFactory->createUri()->withQuery('E%3dmc%5E2=einstein&foo=bar');
211231
$uri = uriWithoutQueryValue($uri, 'E=mc^2');
212232
$this::assertSame('foo=bar', $uri->getQuery(), 'Handles key in decoded form');
213233

214-
$uri = (new Uri)->withQuery('E%3dmc%5E2=einstein&foo=bar');
234+
$uri = $this->uriFactory->createUri()->withQuery('E%3dmc%5E2=einstein&foo=bar');
215235
$uri = uriWithoutQueryValue($uri, 'E%3Dmc%5e2');
216236
$this::assertSame('foo=bar', $uri->getQuery(), 'Handles key in encoded form');
217237

tests/Psr7/ResponseTest.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,19 @@
1212

1313
namespace chillerlan\HTTPTest\Psr7;
1414

15+
use chillerlan\HTTP\Psr17\StreamFactory;
1516
use chillerlan\HTTP\Psr7\Response;
16-
use Psr\Http\Message\StreamInterface;
17+
use Psr\Http\Message\{StreamFactoryInterface, StreamInterface};
1718
use PHPUnit\Framework\TestCase;
1819

19-
use function chillerlan\HTTP\Psr17\create_stream;
20-
2120
class ResponseTest extends TestCase{
2221

22+
protected StreamFactoryInterface $streamFactory;
23+
24+
protected function setUp():void{
25+
$this->streamFactory = new StreamFactory;
26+
}
27+
2328
public function testDefaultConstructor():void{
2429
$r = new Response;
2530

@@ -132,7 +137,7 @@ public function testSameInstanceWhenSameProtocol():void{
132137
}
133138

134139
public function testWithBody():void{
135-
$r = (new Response)->withBody(create_stream('0'));
140+
$r = (new Response)->withBody($this->streamFactory->createStream('0'));
136141
$this::assertInstanceOf(StreamInterface::class, $r->getBody());
137142
$this::assertSame('0', (string) $r->getBody());
138143
}

0 commit comments

Comments
 (0)