From 998a1e64afeccacf37d97e0c4163923fb9a378ed Mon Sep 17 00:00:00 2001 From: smiley Date: Sat, 25 Nov 2023 17:36:07 +0100 Subject: [PATCH] :octocat: re-added http-interop/http-factory-tests (https://github.com/http-interop/http-factory-tests/pull/49) --- composer.json | 7 +- phpunit.xml.dist | 2 +- tests/Psr17/RequestFactoryTest.php | 50 ------- tests/Psr17/ResponseFactoryTest.php | 37 ----- tests/Psr17/ServerRequestFactoryTest.php | 130 ------------------ tests/Psr17/StreamFactoryTest.php | 164 ----------------------- tests/Psr17/UploadedFileFactoryTest.php | 62 --------- tests/Psr17/UriFactoryTest.php | 34 ----- tests/Psr17/streaminput.txt | 1 - 9 files changed, 5 insertions(+), 482 deletions(-) delete mode 100644 tests/Psr17/RequestFactoryTest.php delete mode 100644 tests/Psr17/ResponseFactoryTest.php delete mode 100644 tests/Psr17/ServerRequestFactoryTest.php delete mode 100644 tests/Psr17/StreamFactoryTest.php delete mode 100644 tests/Psr17/UploadedFileFactoryTest.php delete mode 100644 tests/Psr17/UriFactoryTest.php delete mode 100644 tests/Psr17/streaminput.txt diff --git a/composer.json b/composer.json index 10b9e53f..a8a34288 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,7 @@ "psr/http-server-handler-implementation": "1.0", "psr/http-server-middleware-implementation": "1.0" }, - "minimum-stability": "dev", + "minimum-stability": "stable", "prefer-stable": true, "require": { "php": "^8.1", @@ -32,7 +32,7 @@ "ext-json": "*", "ext-simplexml": "*", "ext-zlib": "*", - "chillerlan/php-http-message-utils": "dev-main#d1a5a12369a9075100beffe20af16976e3203be8", + "chillerlan/php-http-message-utils": "^2.0", "chillerlan/php-settings-container": "^2.1.4 || ^3.1", "fig/http-message-util": "^1.1.5", "psr/http-client": "^1.0", @@ -43,9 +43,10 @@ "psr/log": "^1.1 || ^2.0 || ^3.0" }, "require-dev": { + "http-interop/http-factory-tests": "2.1.0", "phan/phan": "^5.4", "phpmd/phpmd": "^2.13", - "phpunit/phpunit": "^10.2", + "phpunit/phpunit": "^10.4", "squizlabs/php_codesniffer": "^3.7" }, "autoload": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index c2f3a3bc..a8ee5224 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,6 +1,6 @@ ['GET'], - 'POST' => ['POST'], - 'PUT' => ['PUT'], - 'DELETE' => ['DELETE'], - 'OPTIONS' => ['OPTIONS'], - 'HEAD' => ['HEAD'], - ]; - } - - #[DataProvider('dataMethods')] - public function testCreateRequest(string $method):void{ - $uri = 'https://example.com/'; - $request = $this->requestFactory->createRequest($method, $uri); - - $this::assertInstanceOf(RequestInterface::class, $request); - $this::assertSame($method, $request->getMethod()); - $this::assertSame($uri, (string)$request->getUri()); - } - - public function testCreateRequestWithUri():void{ - $method = 'GET'; - $uri = 'https://example.com/'; - $request = $this->requestFactory->createRequest($method, $this->uriFactory->createUri($uri)); - - $this::assertSame($method, $request->getMethod()); - $this::assertSame($uri, (string)$request->getUri()); - } - -} diff --git a/tests/Psr17/ResponseFactoryTest.php b/tests/Psr17/ResponseFactoryTest.php deleted file mode 100644 index 1a5c6f34..00000000 --- a/tests/Psr17/ResponseFactoryTest.php +++ /dev/null @@ -1,37 +0,0 @@ - [200], - '301' => [301], - '404' => [404], - '500' => [500], - ]; - } - - #[DataProvider('dataCodes')] - public function testCreateResponse($code){ - $response = $this->responseFactory->createResponse($code); - - $this::assertInstanceOf(ResponseInterface::class, $response); - $this::assertSame($code, $response->getStatusCode()); - } - -} diff --git a/tests/Psr17/ServerRequestFactoryTest.php b/tests/Psr17/ServerRequestFactoryTest.php deleted file mode 100644 index 2c5e4d1b..00000000 --- a/tests/Psr17/ServerRequestFactoryTest.php +++ /dev/null @@ -1,130 +0,0 @@ - $method, - 'REQUEST_URI' => '/test', - 'QUERY_STRING' => 'foo=1&bar=true', - 'HTTP_HOST' => 'example.org', - ], - ]; - } - - return $data; - } - - #[DataProvider('dataServer')] - public function testCreateServerRequest(array $server):void{ - $method = $server['REQUEST_METHOD']; - $uri = sprintf('https://%s%s?%s', $server['HTTP_HOST'], $server['REQUEST_URI'], $server['QUERY_STRING']); - $request = $this->serverRequestFactory->createServerRequest($method, $uri); - - $this::assertInstanceOf(ServerRequestInterface::class, $request); - $this::assertSame($method, $request->getMethod()); - $this::assertSame($uri, (string)$request->getUri()); - } - - #[DataProvider('dataServer')] - public function testCreateServerRequestFromArray(array $server):void{ - $method = $server['REQUEST_METHOD']; - $uri = sprintf('https://%s%s?%s', $server['HTTP_HOST'], $server['REQUEST_URI'], $server['QUERY_STRING']); - $request = $this->serverRequestFactory->createServerRequest($method, $uri, $server); - - $this::assertSame($method, $request->getMethod()); - $this::assertSame($uri, (string)$request->getUri()); - } - - #[DataProvider('dataServer')] - public function testCreateServerRequestWithUriObject(array $server):void{ - $method = $server['REQUEST_METHOD']; - $uri = sprintf('https://%s%s?%s', $server['HTTP_HOST'], $server['REQUEST_URI'], $server['QUERY_STRING']); - $request = $this->serverRequestFactory->createServerRequest($method, $this->uriFactory->createUri($uri)); - - $this::assertSame($method, $request->getMethod()); - $this::assertSame($uri, (string)$request->getUri()); - } - - #[BackupGlobals(true)] - public function testCreateServerRequestDoesNotReadServerSuperglobal():void{ - $_SERVER = ['HTTP_X_FOO' => 'bar']; - - $server = [ - 'REQUEST_METHOD' => 'PUT', - 'REQUEST_URI' => '/test', - 'QUERY_STRING' => 'super=0', - 'HTTP_HOST' => 'example.org', - ]; - - $request = $this->serverRequestFactory->createServerRequest('PUT', '/test', $server); - $serverParams = $request->getServerParams(); - - $this::assertNotEquals($_SERVER, $serverParams); - $this::assertArrayNotHasKey('HTTP_X_FOO', $serverParams); - } - - public function testCreateServerRequestDoesNotReadCookieSuperglobal():void{ - $_COOKIE = ['foo' => 'bar']; - - $request = $this->serverRequestFactory->createServerRequest('POST', 'https://example.org/test'); - - $this::assertEmpty($request->getCookieParams()); - } - - public function testCreateServerRequestDoesNotReadGetSuperglobal():void{ - $_GET = ['foo' => 'bar']; - - $request = $this->serverRequestFactory->createServerRequest('POST', 'https://example.org/test'); - - $this::assertEmpty($request->getQueryParams()); - } - - public function testCreateServerRequestDoesNotReadFilesSuperglobal():void{ - $_FILES = [ - [ - 'name' => 'foobar.dat', - 'type' => 'application/octet-stream', - 'tmp_name' => '/tmp/php45sd3f', - 'error' => UPLOAD_ERR_OK, - 'size' => 4, - ], - ]; - - $request = $this->serverRequestFactory->createServerRequest('POST', 'https://example.org/test'); - - $this::assertEmpty($request->getUploadedFiles()); - } - - public function testCreateServerRequestDoesNotReadPostSuperglobal():void{ - $_POST = ['foo' => 'bar']; - - $request = $this->serverRequestFactory->createServerRequest('POST', 'https://example.org/test'); - - $this::assertEmpty($request->getParsedBody()); - } - -} diff --git a/tests/Psr17/StreamFactoryTest.php b/tests/Psr17/StreamFactoryTest.php deleted file mode 100644 index 3b3b230c..00000000 --- a/tests/Psr17/StreamFactoryTest.php +++ /dev/null @@ -1,164 +0,0 @@ -createTemporaryFile(); - $resource = fopen($file, 'r+'); - - if($content){ - fwrite($resource, $content); - rewind($resource); - } - - return $resource; - } - - public function testCreateStreamWithoutArgument():void{ - $stream = $this->streamFactory->createStream(); - - $this::assertInstanceOf(StreamInterface::class, $stream); - $this::assertSame('', (string)$stream); - } - - public function testCreateStreamWithEmptyString():void{ - $string = ''; - - $stream = $this->streamFactory->createStream($string); - - $this::assertSame($string, (string)$stream); - } - - public function testCreateStreamWithASCIIString():void{ - $string = 'would you like some crumpets?'; - - $stream = $this->streamFactory->createStream($string); - - $this::assertSame($string, (string)$stream); - } - - public function testCreateStreamWithMultiByteMultiLineString():void{ - $string = "would you\r\nlike some\n\u{1F950}?"; - - $stream = $this->streamFactory->createStream($string); - - $this::assertSame($string, (string)$stream); - } - - public function testCreateStreamFromFile():void{ - $string = 'would you like some crumpets?'; - $filename = $this->createTemporaryFile(); - - file_put_contents($filename, $string); - - $stream = $this->streamFactory->createStreamFromFile($filename); - - $this::assertSame($string, (string)$stream); - } - - public function testCreateStreamFromResource():void{ - $string = 'would you like some crumpets?'; - $resource = $this->createTemporaryResource($string); - - $stream = $this->streamFactory->createStreamFromResource($resource); - - $this::assertSame($string, (string)$stream); - } - - public function testCreateStreamFromNonExistingFile():void{ - $filename = $this->createTemporaryFile(); - unlink($filename); - - $this->expectException(RuntimeException::class); - $stream = $this->streamFactory->createStreamFromFile($filename); - } - - public function testCreateStreamFromInvalidFileName():void{ - $this->expectException(RuntimeException::class); - $stream = $this->streamFactory->createStreamFromFile(''); - } - - public function testCreateStreamFromFileIsReadOnlyByDefault():void{ - $string = 'would you like some crumpets?'; - $filename = $this->createTemporaryFile(); - - $stream = $this->streamFactory->createStreamFromFile($filename); - - $this->expectException(RuntimeException::class); - $stream->write($string); - } - - public function testCreateStreamFromFileWithWriteOnlyMode():void{ - $filename = $this->createTemporaryFile(); - - $stream = $this->streamFactory->createStreamFromFile($filename, 'w'); - - $this->expectException(RuntimeException::class); - $stream->read(1); - } - - public function testCreateStreamFromFileWithNoMode():void{ - $filename = $this->createTemporaryFile(); - - if(file_exists($filename)){ - unlink($filename); - } - - $this->expectException(Exception::class); - $stream = $this->streamFactory->createStreamFromFile($filename, ''); - } - - public function testCreateStreamFromFileWithInvalidMode():void{ - $filename = $this->createTemporaryFile(); - - $this->expectException(Exception::class); - $stream = $this->streamFactory->createStreamFromFile($filename, "\u{2620}"); - } - -} diff --git a/tests/Psr17/UploadedFileFactoryTest.php b/tests/Psr17/UploadedFileFactoryTest.php deleted file mode 100644 index 91150831..00000000 --- a/tests/Psr17/UploadedFileFactoryTest.php +++ /dev/null @@ -1,62 +0,0 @@ -getStream()); - $this::assertSame(($size ?? strlen($content)), $file->getSize()); - $this::assertSame(($error ?? UPLOAD_ERR_OK), $file->getError()); - $this::assertSame($clientFilename, $file->getClientFilename()); - $this::assertSame($clientMediaType, $file->getClientMediaType()); - } - - public function testCreateUploadedFileWithClientFilenameAndMediaType():void{ - $content = 'this is your capitan speaking'; - $upload = $this->streamFactory->createStream($content); - $error = UPLOAD_ERR_OK; - $clientFilename = 'test.txt'; - $clientMediaType = 'text/plain'; - - $file = $this->uploadedFileFactory->createUploadedFile($upload, null, $error, $clientFilename, $clientMediaType); - - $this::assertUploadedFile($file, $content, null, $error, $clientFilename, $clientMediaType); - } - - public function testCreateUploadedFileWithError():void{ - $upload = $this->streamFactory->createStream('foobar'); - $error = UPLOAD_ERR_NO_FILE; - - $file = $this->uploadedFileFactory->createUploadedFile($upload, null, $error); - - // Cannot use assertUploadedFile() here because the error prevents - // fetching the content stream. - $this::assertInstanceOf(UploadedFileInterface::class, $file); - $this::assertSame($error, $file->getError()); - } - -} diff --git a/tests/Psr17/UriFactoryTest.php b/tests/Psr17/UriFactoryTest.php deleted file mode 100644 index 72e82d5e..00000000 --- a/tests/Psr17/UriFactoryTest.php +++ /dev/null @@ -1,34 +0,0 @@ -uriFactory->createUri($uriString); - - $this::assertInstanceOf(UriInterface::class, $uri); - $this::assertSame($uriString, (string)$uri); - } - - public function testExceptionWhenUriIsInvalid():void{ - $this->expectException(InvalidArgumentException::class); - $this->uriFactory->createUri(':'); - } - -} diff --git a/tests/Psr17/streaminput.txt b/tests/Psr17/streaminput.txt deleted file mode 100644 index c226a808..00000000 --- a/tests/Psr17/streaminput.txt +++ /dev/null @@ -1 +0,0 @@ -filetest