pax_global_header00006660000000000000000000000064146315743620014525gustar00rootroot0000000000000052 comment=e71ce6f973df53ddfc80bdfd7ccc4c6a4ce77a0e http-factory-tests-2.2.0/000077500000000000000000000000001463157436200153125ustar00rootroot00000000000000http-factory-tests-2.2.0/.gitignore000066400000000000000000000000671463157436200173050ustar00rootroot00000000000000composer.phar composer.lock phpunit.xml build/ vendor/ http-factory-tests-2.2.0/CHANGELOG.md000066400000000000000000000010451463157436200171230ustar00rootroot00000000000000# Changelog All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [2.2.0] - 2024-06-10 - Allow PHPUnit 11.x #51 ## [2.1.0] - 2023-11-25 - Clean up testing changes #48, #49 ## [2.0.0] - 2023-10-21 - Require PHP 8.x and PHPUnit 10.x #47 ## [1.1.0] - 2023-10-21 - Final release that supports PHP 7.x. ## [1.0.0] - 2023-10-10 - Add tests for streams #39 http-factory-tests-2.2.0/LICENSE000066400000000000000000000020651463157436200163220ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2016 Woody Gilk Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. http-factory-tests-2.2.0/README.md000066400000000000000000000015751463157436200166010ustar00rootroot00000000000000# Unit tests for HTTP Factory implementations To use these unit tests you need to add some config to phpunit.xml. ```xml ./vendor/http-interop/http-factory-tests/test ``` http-factory-tests-2.2.0/composer.json000066400000000000000000000013471463157436200200410ustar00rootroot00000000000000{ "name": "http-interop/http-factory-tests", "description": "Unit tests for HTTP factories", "keywords": [ "psr-7", "psr-17", "http", "factory", "test" ], "license": "MIT", "authors": [ { "name": "PHP-FIG", "homepage": "https://www.php-fig.org/" }, { "name": "Contributors", "homepage": "https://github.com/http-interop/http-factory-tests/graphs/contributors" } ], "require": { "php": "^8.1", "psr/http-factory": "^1.0", "phpunit/phpunit": "^10.0 || ^11.0" }, "autoload": { "psr-4": { "Interop\\Http\\Factory\\": "test/" } } } http-factory-tests-2.2.0/test/000077500000000000000000000000001463157436200162715ustar00rootroot00000000000000http-factory-tests-2.2.0/test/RequestFactoryTest.php000066400000000000000000000014471463157436200226300ustar00rootroot00000000000000createUri($uri); } } http-factory-tests-2.2.0/test/RequestFactoryTestCase.php000066400000000000000000000034051463157436200234200ustar00rootroot00000000000000factory = $this->createRequestFactory(); } protected function assertRequest($request, $method, $uri) { static::assertInstanceOf(RequestInterface::class, $request); static::assertSame($method, $request->getMethod()); static::assertSame($uri, (string) $request->getUri()); } public static function dataMethods() { return [ 'GET' => ['GET'], 'POST' => ['POST'], 'PUT' => ['PUT'], 'DELETE' => ['DELETE'], 'OPTIONS' => ['OPTIONS'], 'HEAD' => ['HEAD'], ]; } #[DataProvider('dataMethods')] public function testCreateRequest($method) { $uri = 'http://example.com/'; $request = $this->factory->createRequest($method, $uri); $this->assertRequest($request, $method, $uri); } public function testCreateRequestWithUri() { $method = 'GET'; $uri = 'http://example.com/'; $request = $this->factory->createRequest($method, $this->createUri($uri)); $this->assertRequest($request, $method, $uri); } } http-factory-tests-2.2.0/test/ResponseFactoryTest.php000066400000000000000000000010061463157436200227650ustar00rootroot00000000000000factory = $this->createResponseFactory(); } protected function assertResponse($response, $code) { static::assertInstanceOf(ResponseInterface::class, $response); static::assertSame($code, $response->getStatusCode()); } public static function dataCodes() { return [ 'HTTP/200' => [200], 'HTTP/301' => [301], 'HTTP/404' => [404], 'HTTP/500' => [500], ]; } #[DataProvider('dataCodes')] public function testCreateResponse($code) { $response = $this->factory->createResponse($code); $this->assertResponse($response, $code); } } http-factory-tests-2.2.0/test/ServerRequestFactoryTest.php000066400000000000000000000015251463157436200240140ustar00rootroot00000000000000createUri($uri); } } http-factory-tests-2.2.0/test/ServerRequestFactoryTestCase.php000066400000000000000000000113621463157436200246100ustar00rootroot00000000000000factory = $this->createServerRequestFactory(); } protected function assertServerRequest($request, $method, $uri) { static::assertInstanceOf(ServerRequestInterface::class, $request); static::assertSame($method, $request->getMethod()); static::assertSame($uri, (string) $request->getUri()); } public static function dataMethods() { return [ ['GET'], ['POST'], ['PUT'], ['DELETE'], ['OPTIONS'], ['HEAD'], ]; } public static function dataServer() { $data = []; foreach (static::dataMethods() as $methodData) { $data[$methodData[0]] = [ [ 'REQUEST_METHOD' => $methodData[0], 'REQUEST_URI' => '/test', 'QUERY_STRING' => 'foo=1&bar=true', 'HTTP_HOST' => 'example.org', ] ]; } return $data; } #[DataProvider('dataServer')] public function testCreateServerRequest($server) { $method = $server['REQUEST_METHOD']; $uri = "http://{$server['HTTP_HOST']}{$server['REQUEST_URI']}?{$server['QUERY_STRING']}"; $request = $this->factory->createServerRequest($method, $uri); $this->assertServerRequest($request, $method, $uri); } #[DataProvider('dataServer')] public function testCreateServerRequestFromArray(array $server) { $method = $server['REQUEST_METHOD']; $uri = "http://{$server['HTTP_HOST']}{$server['REQUEST_URI']}?{$server['QUERY_STRING']}"; $request = $this->factory->createServerRequest($method, $uri, $server); $this->assertServerRequest($request, $method, $uri); } #[DataProvider('dataServer')] public function testCreateServerRequestWithUriObject($server) { $method = $server['REQUEST_METHOD']; $uri = "http://{$server['HTTP_HOST']}{$server['REQUEST_URI']}?{$server['QUERY_STRING']}"; $request = $this->factory->createServerRequest($method, $this->createUri($uri)); $this->assertServerRequest($request, $method, $uri); } #[BackupGlobals(true)] public function testCreateServerRequestDoesNotReadServerSuperglobal() { $_SERVER = ['HTTP_X_FOO' => 'bar']; $server = [ 'REQUEST_METHOD' => 'PUT', 'REQUEST_URI' => '/test', 'QUERY_STRING' => 'super=0', 'HTTP_HOST' => 'example.org', ]; $request = $this->factory->createServerRequest('PUT', '/test', $server); $serverParams = $request->getServerParams(); static::assertNotEquals($_SERVER, $serverParams); static::assertArrayNotHasKey('HTTP_X_FOO', $serverParams); } public function testCreateServerRequestDoesNotReadCookieSuperglobal() { $_COOKIE = ['foo' => 'bar']; $request = $this->factory->createServerRequest('POST', 'http://example.org/test'); static::assertEmpty($request->getCookieParams()); } public function testCreateServerRequestDoesNotReadGetSuperglobal() { $_GET = ['foo' => 'bar']; $request = $this->factory->createServerRequest('POST', 'http://example.org/test'); static::assertEmpty($request->getQueryParams()); } public function testCreateServerRequestDoesNotReadFilesSuperglobal() { $_FILES = [['name' => 'foobar.dat', 'type' => 'application/octet-stream', 'tmp_name' => '/tmp/php45sd3f', 'error' => UPLOAD_ERR_OK, 'size' => 4]]; $request = $this->factory->createServerRequest('POST', 'http://example.org/test'); static::assertEmpty($request->getUploadedFiles()); } public function testCreateServerRequestDoesNotReadPostSuperglobal() { $_POST = ['foo' => 'bar']; $request = $this->factory->createServerRequest('POST', 'http://example.org/test'); static::assertEmpty($request->getParsedBody()); } } http-factory-tests-2.2.0/test/StreamFactoryTest.php000066400000000000000000000007701463157436200224310ustar00rootroot00000000000000factory = $this->createStreamFactory(); } protected function assertStream($stream, $content) { static::assertInstanceOf(StreamInterface::class, $stream); static::assertSame($content, (string) $stream); } public function testCreateStreamWithoutArgument() { $stream = $this->factory->createStream(); $this->assertStream($stream, ''); } public function testCreateStreamWithEmptyString() { $string = ''; $stream = $this->factory->createStream($string); $this->assertStream($stream, $string); } public function testCreateStreamWithASCIIString() { $string = 'would you like some crumpets?'; $stream = $this->factory->createStream($string); $this->assertStream($stream, $string); } public function testCreateStreamWithMultiByteMultiLineString() { $string = "would you\r\nlike some\n\u{1F950}?"; $stream = $this->factory->createStream($string); $this->assertStream($stream, $string); } /** * @noinspection PhpUnreachableStatementInspection */ public function testCreateStreamCursorPosition() { $this->markTestIncomplete('This behaviour has not been specified by PHP-FIG yet.'); $string = 'would you like some crumpets?'; $stream = $this->factory->createStream($string); static::assertSame(strlen($string), $stream->tell()); } public function testCreateStreamFromFile() { $string = 'would you like some crumpets?'; $filename = $this->createTemporaryFile(); file_put_contents($filename, $string); $stream = $this->factory->createStreamFromFile($filename); $this->assertStream($stream, $string); } public function testCreateStreamFromNonExistingFile() { $filename = $this->createTemporaryFile(); unlink($filename); $this->expectException(RuntimeException::class); $this->factory->createStreamFromFile($filename); } public function testCreateStreamFromInvalidFileName() { $this->expectException(RuntimeException::class); $this->factory->createStreamFromFile(''); } public function testCreateStreamFromFileIsReadOnlyByDefault() { $string = 'would you like some crumpets?'; $filename = $this->createTemporaryFile(); $stream = $this->factory->createStreamFromFile($filename); $this->expectException(RuntimeException::class); $stream->write($string); } public function testCreateStreamFromFileWithWriteOnlyMode() { $filename = $this->createTemporaryFile(); $stream = $this->factory->createStreamFromFile($filename, 'w'); $this->expectException(RuntimeException::class); $stream->read(1); } public function testCreateStreamFromFileWithNoMode() { $filename = $this->createTemporaryFile(); $this->expectException(Exception::class); $this->factory->createStreamFromFile($filename, ''); } public function testCreateStreamFromFileWithInvalidMode() { $filename = $this->createTemporaryFile(); $this->expectException(Exception::class); $this->factory->createStreamFromFile($filename, "\u{2620}"); } public function testCreateStreamFromFileCursorPosition() { $string = 'would you like some crumpets?'; $filename = $this->createTemporaryFile(); file_put_contents($filename, $string); $resource = fopen($filename, 'r'); $fopenTell = ftell($resource); fclose($resource); $stream = $this->factory->createStreamFromFile($filename); static::assertSame($fopenTell, $stream->tell()); } public function testCreateStreamFromResource() { $string = 'would you like some crumpets?'; $resource = $this->createTemporaryResource($string); $stream = $this->factory->createStreamFromResource($resource); $this->assertStream($stream, $string); } public function testCreateStreamFromResourceCursorPosition() { $string = 'would you like some crumpets?'; $resource1 = $this->createTemporaryResource($string); fseek($resource1, 0, SEEK_SET); $stream1 = $this->factory->createStreamFromResource($resource1); static::assertSame(0, $stream1->tell()); $resource2 = $this->createTemporaryResource($string); fseek($resource2, 0, SEEK_END); $stream2 = $this->factory->createStreamFromResource($resource2); static::assertSame(strlen($string), $stream2->tell()); $resource3 = $this->createTemporaryResource($string); fseek($resource3, 15, SEEK_SET); $stream3 = $this->factory->createStreamFromResource($resource3); static::assertSame(15, $stream3->tell()); } } http-factory-tests-2.2.0/test/StreamHelper.php000066400000000000000000000021051463157436200213730ustar00rootroot00000000000000createTemporaryFile(); $resource = fopen($file, 'r+'); if ($content) { fwrite($resource, $content); rewind($resource); } return $resource; } public static function tearDownAfterClass(): void { foreach (static::$tempFiles as $tempFile) { if (is_file($tempFile)) { unlink($tempFile); } } } } http-factory-tests-2.2.0/test/UploadedFileFactoryTest.php000066400000000000000000000015031463157436200235260ustar00rootroot00000000000000createStream($content); } } http-factory-tests-2.2.0/test/UploadedFileFactoryTestCase.php000066400000000000000000000045141463157436200243270ustar00rootroot00000000000000factory = $this->createUploadedFileFactory(); } protected function assertUploadedFile( $file, $content, $size = null, $error = null, $clientFilename = null, $clientMediaType = null ) { static::assertInstanceOf(UploadedFileInterface::class, $file); static::assertSame($content, (string) $file->getStream()); static::assertSame(($size ?? strlen($content)), $file->getSize()); static::assertSame(($error ?? UPLOAD_ERR_OK), $file->getError()); static::assertSame($clientFilename, $file->getClientFilename()); static::assertSame($clientMediaType, $file->getClientMediaType()); } public function testCreateUploadedFileWithClientFilenameAndMediaType() { $content = 'this is your capitan speaking'; $upload = $this->createStream($content); $error = UPLOAD_ERR_OK; $clientFilename = 'test.txt'; $clientMediaType = 'text/plain'; $file = $this->factory->createUploadedFile($upload, null, $error, $clientFilename, $clientMediaType); $this->assertUploadedFile($file, $content, null, $error, $clientFilename, $clientMediaType); } public function testCreateUploadedFileWithError() { $upload = $this->createStream('foobar'); $error = UPLOAD_ERR_NO_FILE; $file = $this->factory->createUploadedFile($upload, null, $error); // Cannot use assertUploadedFile() here because the error prevents // fetching the content stream. static::assertInstanceOf(UploadedFileInterface::class, $file); static::assertSame($error, $file->getError()); } } http-factory-tests-2.2.0/test/UriFactoryTest.php000066400000000000000000000007431463157436200217350ustar00rootroot00000000000000factory = $this->createUriFactory(); } protected function assertUri($uri, $uriString) { static::assertInstanceOf(UriInterface::class, $uri); static::assertSame($uriString, (string) $uri); } public function testCreateUri() { $uriString = 'http://example.com/'; $uri = $this->factory->createUri($uriString); $this->assertUri($uri, $uriString); } public function testExceptionWhenUriIsInvalid() { $this->expectException(InvalidArgumentException::class); $this->factory->createUri(':'); } }