From b5108c79b45d0fe9ce0014bdb9dc1b1c024d7087 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Thu, 25 Aug 2016 17:41:15 +0200 Subject: [PATCH] Initial import --- .gitignore | 5 ++ LICENSE | 21 +++++++++ README.md | 3 ++ composer.json | 37 +++++++++++++++ phpunit.xml.dist | 17 +++++++ src/RequestFactory.php | 14 ++++++ src/ResponseFactory.php | 14 ++++++ src/ServerRequestFactory.php | 20 ++++++++ src/StreamFactory.php | 13 +++++ src/UploadedFileFactory.php | 28 +++++++++++ src/UriFactory.php | 14 ++++++ tests/RequestFactoryTest.php | 59 +++++++++++++++++++++++ tests/ResponseFactoryTest.php | 42 +++++++++++++++++ tests/ServerRequestFactoryTest.php | 76 ++++++++++++++++++++++++++++++ tests/StreamFactoryTest.php | 43 +++++++++++++++++ tests/UploadedFileFactoryTest.php | 75 +++++++++++++++++++++++++++++ 16 files changed, 481 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 composer.json create mode 100644 phpunit.xml.dist create mode 100644 src/RequestFactory.php create mode 100644 src/ResponseFactory.php create mode 100644 src/ServerRequestFactory.php create mode 100644 src/StreamFactory.php create mode 100644 src/UploadedFileFactory.php create mode 100644 src/UriFactory.php create mode 100644 tests/RequestFactoryTest.php create mode 100644 tests/ResponseFactoryTest.php create mode 100644 tests/ServerRequestFactoryTest.php create mode 100644 tests/StreamFactoryTest.php create mode 100644 tests/UploadedFileFactoryTest.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e2c2927 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +composer.phar +composer.lock +phpunit.xml +build/ +vendor/ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..efb5a19 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The 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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..6df50bc --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# HTTP Factory for Guzzle + +HTTP factory implemented for [Guzzle](https://github.com/guzzle/psr7). diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..9f4a6f3 --- /dev/null +++ b/composer.json @@ -0,0 +1,37 @@ +{ + "name": "http-interop/http-factory-guzzle", + "description": "An HTTP Factory using Guzzle PSR7", + "keywords": [ + "psr-7", + "psr-17", + "http", + "factory" + ], + "license": "MIT", + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "require": { + "http-interop/http-factory": "dev-master", + "guzzlehttp/psr7": "^1.3.1" + }, + "autoload": { + "psr-4": { + "Http\\Factory\\Guzzle\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "Http\\Factory\\Guzzle\\": "tests/" + } + }, + "repositories": [ + { + "type": "git", + "url": "https://github.com/http-interop/http-factory.git" + } + ] +} diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..48ea434 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,17 @@ + + + + ./tests + + + + + ./src/ + + + + + + + + diff --git a/src/RequestFactory.php b/src/RequestFactory.php new file mode 100644 index 0000000..2f3d1c1 --- /dev/null +++ b/src/RequestFactory.php @@ -0,0 +1,14 @@ +factory = new RequestFactory(); + } + + private function assertRequest($request, $method, $uri) + { + $this->assertInstanceOf(RequestInterface::class, $request); + $this->assertSame($method, $request->getMethod()); + $this->assertSame($uri, (string) $request->getUri()); + } + + public function dataMethods() + { + return [ + ['GET'], + ['POST'], + ['PUT'], + ['DELETE'], + ['OPTIONS'], + ['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() + { + $uriFactory = new UriFactory(); + + $method = 'GET'; + $uri = 'http://example.com/'; + + $request = $this->factory->createRequest($method, $uriFactory->createUri($uri)); + + $this->assertRequest($request, $method, $uri); + } +} diff --git a/tests/ResponseFactoryTest.php b/tests/ResponseFactoryTest.php new file mode 100644 index 0000000..d968f31 --- /dev/null +++ b/tests/ResponseFactoryTest.php @@ -0,0 +1,42 @@ +factory = new ResponseFactory(); + } + + private function assertResponse($response, $code) + { + $this->assertInstanceOf(ResponseInterface::class, $response); + $this->assertSame($code, $response->getStatusCode()); + } + + public function dataCodes() + { + return [ + [200], + [301], + [404], + [500], + ]; + } + + /** + * @dataProvider dataCodes + */ + public function testCreateResponse($code) + { + $response = $this->factory->createResponse($code); + + $this->assertResponse($response, $code); + } +} diff --git a/tests/ServerRequestFactoryTest.php b/tests/ServerRequestFactoryTest.php new file mode 100644 index 0000000..7dd93c2 --- /dev/null +++ b/tests/ServerRequestFactoryTest.php @@ -0,0 +1,76 @@ +factory = new ServerRequestFactory(); + } + + private function assertServerRequest($request, $method, $uri) + { + $this->assertInstanceOf(ServerRequestInterface::class, $request); + $this->assertSame($method, $request->getMethod()); + $this->assertSame($uri, (string) $request->getUri()); + } + + public function dataMethods() + { + return [ + ['GET'], + ['POST'], + ['PUT'], + ['DELETE'], + ['OPTIONS'], + ['HEAD'], + ]; + } + + /** + * @dataProvider dataMethods + */ + public function testCreateServerRequest($method) + { + $uri = 'http://example.com/'; + + $request = $this->factory->createServerRequest($method, $uri); + + $this->assertServerRequest($request, $method, $uri); + } + + public function testCreateServerRequestWithUri() + { + $uriFactory = new UriFactory(); + + $method = 'GET'; + $uri = 'http://example.com/'; + + $request = $this->factory->createServerRequest($method, $uriFactory->createUri($uri)); + + $this->assertServerRequest($request, $method, $uri); + } + + /** + * @runInSeparateProcess + * @preserveGlobalState disabled + */ + public function testCreateServerRequestFromGlobals() + { + $_SERVER['REQUEST_METHOD'] = $method = 'GET'; + $_SERVER['QUERY_STRING'] = $qs = 'foo=1&bar=true'; + $_SERVER['HTTP_HOST'] = $host = 'example.org'; + + $uri = "http://{$host}/?$qs"; + + $request = $this->factory->createServerRequestFromGlobals(); + + $this->assertServerRequest($request, $method, $uri); + } +} diff --git a/tests/StreamFactoryTest.php b/tests/StreamFactoryTest.php new file mode 100644 index 0000000..93e0837 --- /dev/null +++ b/tests/StreamFactoryTest.php @@ -0,0 +1,43 @@ +factory = new StreamFactory(); + } + + private function assertStream($stream, $content) + { + $this->assertInstanceOf(StreamInterface::class, $stream); + $this->assertSame($content, (string) $stream); + } + + public function testCreateStream() + { + $resource = tmpfile(); + + $stream = $this->factory->createStream($resource); + + $this->assertStream($stream, ''); + } + + public function testCreateStreamWithContent() + { + $string = 'would you like some crumpets?'; + + $resource = tmpfile(); + fwrite($resource, $string); + + $stream = $this->factory->createStream($resource); + + $this->assertStream($stream, $string); + } +} diff --git a/tests/UploadedFileFactoryTest.php b/tests/UploadedFileFactoryTest.php new file mode 100644 index 0000000..3f9312b --- /dev/null +++ b/tests/UploadedFileFactoryTest.php @@ -0,0 +1,75 @@ +factory = new UploadedFileFactory(); + } + + private function assertUploadedFile( + $file, + $content, + $size = null, + $error = null, + $clientFilename = null, + $clientMediaType = null + ) { + $this->assertInstanceOf(UploadedFileInterface::class, $file); + $this->assertSame($content, (string) $file->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 testCreateUploadedFileWithString() + { + $filename = tempnam(sys_get_temp_dir(), 'http-factory-test'); + $content = 'i made this!'; + $size = strlen($content); + + file_put_contents($filename, $content); + + $file = $this->factory->createUploadedFile($filename); + + $this->assertUploadedFile($file, $content, $size); + + unlink($filename); + } + + public function testCreateUploadedFileWithClientFilenameAndMediaType() + { + $upload = tmpfile(); + $content = 'this is your capitan speaking'; + $error = UPLOAD_ERR_OK; + $clientFilename = 'test.txt'; + $clientMediaType = 'text/plain'; + + fwrite($upload, $content); + + $file = $this->factory->createUploadedFile($upload, null, $error, $clientFilename, $clientMediaType); + + $this->assertUploadedFile($file, $content, null, $error, $clientFilename, $clientMediaType); + } + + public function testCreateUploadedFileWithError() + { + $upload = tmpfile(); + $error = UPLOAD_ERR_NO_FILE; + + $file = $this->factory->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()); + } +}