├── .gitignore
├── test
├── UriFactoryTest.php
├── StreamFactoryTest.php
├── ResponseFactoryTest.php
├── RequestFactoryTest.php
├── UploadedFileFactoryTest.php
├── ServerRequestFactoryTest.php
├── UriFactoryTestCase.php
├── StreamHelper.php
├── ResponseFactoryTestCase.php
├── RequestFactoryTestCase.php
├── UploadedFileFactoryTestCase.php
├── ServerRequestFactoryTestCase.php
└── StreamFactoryTestCase.php
├── CHANGELOG.md
├── composer.json
├── README.md
└── LICENSE
/.gitignore:
--------------------------------------------------------------------------------
1 | composer.phar
2 | composer.lock
3 | phpunit.xml
4 | build/
5 | vendor/
6 |
--------------------------------------------------------------------------------
/test/UriFactoryTest.php:
--------------------------------------------------------------------------------
1 |
7 |
8 |
9 | ./vendor/http-interop/http-factory-tests/test
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 | ```
24 |
--------------------------------------------------------------------------------
/test/RequestFactoryTest.php:
--------------------------------------------------------------------------------
1 | createUri($uri);
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/test/UploadedFileFactoryTest.php:
--------------------------------------------------------------------------------
1 | createStream($content);
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/test/ServerRequestFactoryTest.php:
--------------------------------------------------------------------------------
1 | createUri($uri);
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2016 Woody Gilk
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/test/UriFactoryTestCase.php:
--------------------------------------------------------------------------------
1 | factory = $this->createUriFactory();
25 | }
26 |
27 | protected function assertUri($uri, $uriString)
28 | {
29 | static::assertInstanceOf(UriInterface::class, $uri);
30 | static::assertSame($uriString, (string) $uri);
31 | }
32 |
33 | public function testCreateUri()
34 | {
35 | $uriString = 'http://example.com/';
36 |
37 | $uri = $this->factory->createUri($uriString);
38 |
39 | $this->assertUri($uri, $uriString);
40 | }
41 |
42 | public function testExceptionWhenUriIsInvalid()
43 | {
44 | $this->expectException(InvalidArgumentException::class);
45 | $this->factory->createUri(':');
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/test/StreamHelper.php:
--------------------------------------------------------------------------------
1 | createTemporaryFile();
34 | $resource = fopen($file, 'r+');
35 |
36 | if ($content) {
37 | fwrite($resource, $content);
38 | rewind($resource);
39 | }
40 |
41 | return $resource;
42 | }
43 |
44 | public static function tearDownAfterClass(): void
45 | {
46 | foreach (static::$tempFiles as $tempFile) {
47 | if (is_file($tempFile)) {
48 | unlink($tempFile);
49 | }
50 | }
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/test/ResponseFactoryTestCase.php:
--------------------------------------------------------------------------------
1 | factory = $this->createResponseFactory();
25 | }
26 |
27 | protected function assertResponse($response, $code)
28 | {
29 | static::assertInstanceOf(ResponseInterface::class, $response);
30 | static::assertSame($code, $response->getStatusCode());
31 | }
32 |
33 | public static function dataCodes()
34 | {
35 | return [
36 | 'HTTP/200' => [200],
37 | 'HTTP/301' => [301],
38 | 'HTTP/404' => [404],
39 | 'HTTP/500' => [500],
40 | ];
41 | }
42 |
43 | #[DataProvider('dataCodes')]
44 | public function testCreateResponse($code)
45 | {
46 | $response = $this->factory->createResponse($code);
47 |
48 | $this->assertResponse($response, $code);
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/test/RequestFactoryTestCase.php:
--------------------------------------------------------------------------------
1 | factory = $this->createRequestFactory();
33 | }
34 |
35 | protected function assertRequest($request, $method, $uri)
36 | {
37 | static::assertInstanceOf(RequestInterface::class, $request);
38 | static::assertSame($method, $request->getMethod());
39 | static::assertSame($uri, (string) $request->getUri());
40 | }
41 |
42 | public static function dataMethods()
43 | {
44 | return [
45 | 'GET' => ['GET'],
46 | 'POST' => ['POST'],
47 | 'PUT' => ['PUT'],
48 | 'DELETE' => ['DELETE'],
49 | 'OPTIONS' => ['OPTIONS'],
50 | 'HEAD' => ['HEAD'],
51 | ];
52 | }
53 |
54 | #[DataProvider('dataMethods')]
55 | public function testCreateRequest($method)
56 | {
57 | $uri = 'http://example.com/';
58 |
59 | $request = $this->factory->createRequest($method, $uri);
60 |
61 | $this->assertRequest($request, $method, $uri);
62 | }
63 |
64 | public function testCreateRequestWithUri()
65 | {
66 | $method = 'GET';
67 | $uri = 'http://example.com/';
68 |
69 | $request = $this->factory->createRequest($method, $this->createUri($uri));
70 |
71 | $this->assertRequest($request, $method, $uri);
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/test/UploadedFileFactoryTestCase.php:
--------------------------------------------------------------------------------
1 | factory = $this->createUploadedFileFactory();
33 | }
34 |
35 | protected function assertUploadedFile(
36 | $file,
37 | $content,
38 | $size = null,
39 | $error = null,
40 | $clientFilename = null,
41 | $clientMediaType = null
42 | ) {
43 | static::assertInstanceOf(UploadedFileInterface::class, $file);
44 | static::assertSame($content, (string) $file->getStream());
45 | static::assertSame(($size ?? strlen($content)), $file->getSize());
46 | static::assertSame(($error ?? UPLOAD_ERR_OK), $file->getError());
47 | static::assertSame($clientFilename, $file->getClientFilename());
48 | static::assertSame($clientMediaType, $file->getClientMediaType());
49 | }
50 |
51 | public function testCreateUploadedFileWithClientFilenameAndMediaType()
52 | {
53 | $content = 'this is your capitan speaking';
54 | $upload = $this->createStream($content);
55 | $error = UPLOAD_ERR_OK;
56 | $clientFilename = 'test.txt';
57 | $clientMediaType = 'text/plain';
58 |
59 | $file = $this->factory->createUploadedFile($upload, null, $error, $clientFilename, $clientMediaType);
60 |
61 | $this->assertUploadedFile($file, $content, null, $error, $clientFilename, $clientMediaType);
62 | }
63 |
64 | public function testCreateUploadedFileWithError()
65 | {
66 | $upload = $this->createStream('foobar');
67 | $error = UPLOAD_ERR_NO_FILE;
68 |
69 | $file = $this->factory->createUploadedFile($upload, null, $error);
70 |
71 | // Cannot use assertUploadedFile() here because the error prevents
72 | // fetching the content stream.
73 | static::assertInstanceOf(UploadedFileInterface::class, $file);
74 | static::assertSame($error, $file->getError());
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/test/ServerRequestFactoryTestCase.php:
--------------------------------------------------------------------------------
1 | factory = $this->createServerRequestFactory();
38 | }
39 |
40 | protected function assertServerRequest($request, $method, $uri)
41 | {
42 | static::assertInstanceOf(ServerRequestInterface::class, $request);
43 | static::assertSame($method, $request->getMethod());
44 | static::assertSame($uri, (string) $request->getUri());
45 | }
46 |
47 | public static function dataMethods()
48 | {
49 | return [
50 | ['GET'],
51 | ['POST'],
52 | ['PUT'],
53 | ['DELETE'],
54 | ['OPTIONS'],
55 | ['HEAD'],
56 | ];
57 | }
58 |
59 | public static function dataServer()
60 | {
61 | $data = [];
62 |
63 | foreach (static::dataMethods() as $methodData) {
64 | $data[$methodData[0]] = [
65 | [
66 | 'REQUEST_METHOD' => $methodData[0],
67 | 'REQUEST_URI' => '/test',
68 | 'QUERY_STRING' => 'foo=1&bar=true',
69 | 'HTTP_HOST' => 'example.org',
70 | ]
71 | ];
72 | }
73 |
74 | return $data;
75 | }
76 |
77 | #[DataProvider('dataServer')]
78 | public function testCreateServerRequest($server)
79 | {
80 | $method = $server['REQUEST_METHOD'];
81 | $uri = "http://{$server['HTTP_HOST']}{$server['REQUEST_URI']}?{$server['QUERY_STRING']}";
82 |
83 | $request = $this->factory->createServerRequest($method, $uri);
84 |
85 | $this->assertServerRequest($request, $method, $uri);
86 | }
87 |
88 | #[DataProvider('dataServer')]
89 | public function testCreateServerRequestFromArray(array $server)
90 | {
91 | $method = $server['REQUEST_METHOD'];
92 | $uri = "http://{$server['HTTP_HOST']}{$server['REQUEST_URI']}?{$server['QUERY_STRING']}";
93 |
94 | $request = $this->factory->createServerRequest($method, $uri, $server);
95 |
96 | $this->assertServerRequest($request, $method, $uri);
97 | }
98 |
99 | #[DataProvider('dataServer')]
100 | public function testCreateServerRequestWithUriObject($server)
101 | {
102 | $method = $server['REQUEST_METHOD'];
103 | $uri = "http://{$server['HTTP_HOST']}{$server['REQUEST_URI']}?{$server['QUERY_STRING']}";
104 |
105 | $request = $this->factory->createServerRequest($method, $this->createUri($uri));
106 |
107 | $this->assertServerRequest($request, $method, $uri);
108 | }
109 |
110 | #[BackupGlobals(true)]
111 | public function testCreateServerRequestDoesNotReadServerSuperglobal()
112 | {
113 | $_SERVER = ['HTTP_X_FOO' => 'bar'];
114 |
115 | $server = [
116 | 'REQUEST_METHOD' => 'PUT',
117 | 'REQUEST_URI' => '/test',
118 | 'QUERY_STRING' => 'super=0',
119 | 'HTTP_HOST' => 'example.org',
120 | ];
121 |
122 | $request = $this->factory->createServerRequest('PUT', '/test', $server);
123 |
124 | $serverParams = $request->getServerParams();
125 |
126 | static::assertNotEquals($_SERVER, $serverParams);
127 | static::assertArrayNotHasKey('HTTP_X_FOO', $serverParams);
128 | }
129 |
130 | public function testCreateServerRequestDoesNotReadCookieSuperglobal()
131 | {
132 | $_COOKIE = ['foo' => 'bar'];
133 |
134 | $request = $this->factory->createServerRequest('POST', 'http://example.org/test');
135 |
136 | static::assertEmpty($request->getCookieParams());
137 | }
138 |
139 | public function testCreateServerRequestDoesNotReadGetSuperglobal()
140 | {
141 | $_GET = ['foo' => 'bar'];
142 |
143 | $request = $this->factory->createServerRequest('POST', 'http://example.org/test');
144 |
145 | static::assertEmpty($request->getQueryParams());
146 | }
147 |
148 | public function testCreateServerRequestDoesNotReadFilesSuperglobal()
149 | {
150 | $_FILES = [['name' => 'foobar.dat', 'type' => 'application/octet-stream', 'tmp_name' => '/tmp/php45sd3f', 'error' => UPLOAD_ERR_OK, 'size' => 4]];
151 |
152 | $request = $this->factory->createServerRequest('POST', 'http://example.org/test');
153 |
154 | static::assertEmpty($request->getUploadedFiles());
155 | }
156 |
157 | public function testCreateServerRequestDoesNotReadPostSuperglobal()
158 | {
159 | $_POST = ['foo' => 'bar'];
160 |
161 | $request = $this->factory->createServerRequest('POST', 'http://example.org/test');
162 |
163 | static::assertEmpty($request->getParsedBody());
164 | }
165 | }
166 |
--------------------------------------------------------------------------------
/test/StreamFactoryTestCase.php:
--------------------------------------------------------------------------------
1 | factory = $this->createStreamFactory();
37 | }
38 |
39 | protected function assertStream($stream, $content)
40 | {
41 | static::assertInstanceOf(StreamInterface::class, $stream);
42 | static::assertSame($content, (string) $stream);
43 | }
44 |
45 | public function testCreateStreamWithoutArgument()
46 | {
47 | $stream = $this->factory->createStream();
48 |
49 | $this->assertStream($stream, '');
50 | }
51 |
52 | public function testCreateStreamWithEmptyString()
53 | {
54 | $string = '';
55 |
56 | $stream = $this->factory->createStream($string);
57 |
58 | $this->assertStream($stream, $string);
59 | }
60 |
61 | public function testCreateStreamWithASCIIString()
62 | {
63 | $string = 'would you like some crumpets?';
64 |
65 | $stream = $this->factory->createStream($string);
66 |
67 | $this->assertStream($stream, $string);
68 | }
69 |
70 | public function testCreateStreamWithMultiByteMultiLineString()
71 | {
72 | $string = "would you\r\nlike some\n\u{1F950}?";
73 |
74 | $stream = $this->factory->createStream($string);
75 |
76 | $this->assertStream($stream, $string);
77 | }
78 |
79 | /**
80 | * @noinspection PhpUnreachableStatementInspection
81 | */
82 | public function testCreateStreamCursorPosition()
83 | {
84 | $this->markTestIncomplete('This behaviour has not been specified by PHP-FIG yet.');
85 |
86 | $string = 'would you like some crumpets?';
87 |
88 | $stream = $this->factory->createStream($string);
89 |
90 | static::assertSame(strlen($string), $stream->tell());
91 | }
92 |
93 | public function testCreateStreamFromFile()
94 | {
95 | $string = 'would you like some crumpets?';
96 | $filename = $this->createTemporaryFile();
97 |
98 | file_put_contents($filename, $string);
99 |
100 | $stream = $this->factory->createStreamFromFile($filename);
101 |
102 | $this->assertStream($stream, $string);
103 | }
104 |
105 | public function testCreateStreamFromNonExistingFile()
106 | {
107 | $filename = $this->createTemporaryFile();
108 | unlink($filename);
109 |
110 | $this->expectException(RuntimeException::class);
111 | $this->factory->createStreamFromFile($filename);
112 | }
113 |
114 | public function testCreateStreamFromInvalidFileName()
115 | {
116 | $this->expectException(RuntimeException::class);
117 | $this->factory->createStreamFromFile('');
118 | }
119 |
120 | public function testCreateStreamFromFileIsReadOnlyByDefault()
121 | {
122 | $string = 'would you like some crumpets?';
123 | $filename = $this->createTemporaryFile();
124 |
125 | $stream = $this->factory->createStreamFromFile($filename);
126 |
127 | $this->expectException(RuntimeException::class);
128 | $stream->write($string);
129 | }
130 |
131 | public function testCreateStreamFromFileWithWriteOnlyMode()
132 | {
133 | $filename = $this->createTemporaryFile();
134 |
135 | $stream = $this->factory->createStreamFromFile($filename, 'w');
136 |
137 | $this->expectException(RuntimeException::class);
138 | $stream->read(1);
139 | }
140 |
141 | public function testCreateStreamFromFileWithNoMode()
142 | {
143 | $filename = $this->createTemporaryFile();
144 |
145 | $this->expectException(Exception::class);
146 | $this->factory->createStreamFromFile($filename, '');
147 | }
148 |
149 | public function testCreateStreamFromFileWithInvalidMode()
150 | {
151 | $filename = $this->createTemporaryFile();
152 |
153 | $this->expectException(Exception::class);
154 | $this->factory->createStreamFromFile($filename, "\u{2620}");
155 | }
156 |
157 | public function testCreateStreamFromFileCursorPosition()
158 | {
159 | $string = 'would you like some crumpets?';
160 | $filename = $this->createTemporaryFile();
161 |
162 | file_put_contents($filename, $string);
163 |
164 | $resource = fopen($filename, 'r');
165 | $fopenTell = ftell($resource);
166 | fclose($resource);
167 |
168 | $stream = $this->factory->createStreamFromFile($filename);
169 |
170 | static::assertSame($fopenTell, $stream->tell());
171 | }
172 |
173 | public function testCreateStreamFromResource()
174 | {
175 | $string = 'would you like some crumpets?';
176 | $resource = $this->createTemporaryResource($string);
177 |
178 | $stream = $this->factory->createStreamFromResource($resource);
179 |
180 | $this->assertStream($stream, $string);
181 | }
182 |
183 | public function testCreateStreamFromResourceCursorPosition()
184 | {
185 | $string = 'would you like some crumpets?';
186 |
187 | $resource1 = $this->createTemporaryResource($string);
188 | fseek($resource1, 0, SEEK_SET);
189 | $stream1 = $this->factory->createStreamFromResource($resource1);
190 | static::assertSame(0, $stream1->tell());
191 |
192 | $resource2 = $this->createTemporaryResource($string);
193 | fseek($resource2, 0, SEEK_END);
194 | $stream2 = $this->factory->createStreamFromResource($resource2);
195 | static::assertSame(strlen($string), $stream2->tell());
196 |
197 | $resource3 = $this->createTemporaryResource($string);
198 | fseek($resource3, 15, SEEK_SET);
199 | $stream3 = $this->factory->createStreamFromResource($resource3);
200 | static::assertSame(15, $stream3->tell());
201 | }
202 | }
203 |
--------------------------------------------------------------------------------