-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit b5108c7
Showing
16 changed files
with
481 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
composer.phar | ||
composer.lock | ||
phpunit.xml | ||
build/ | ||
vendor/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# HTTP Factory for Guzzle | ||
|
||
HTTP factory implemented for [Guzzle](https://github.com/guzzle/psr7). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<phpunit backupGlobals="false" bootstrap="./vendor/autoload.php" colors="true"> | ||
<testsuites> | ||
<testsuite> | ||
<directory>./tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<filter> | ||
<whitelist> | ||
<directory>./src/</directory> | ||
</whitelist> | ||
</filter> | ||
<logging> | ||
<log type="coverage-text" target="php://stdout"/> | ||
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/> | ||
<log type="coverage-clover" target="build/logs/clover.xml"/> | ||
</logging> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Http\Factory\Guzzle; | ||
|
||
use GuzzleHttp\Psr7\Request; | ||
use Interop\Http\Factory\RequestFactoryInterface; | ||
|
||
class RequestFactory implements RequestFactoryInterface | ||
{ | ||
public function createRequest($method, $uri) | ||
{ | ||
return new Request($method, $uri); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Http\Factory\Guzzle; | ||
|
||
use GuzzleHttp\Psr7\Response; | ||
use Interop\Http\Factory\ResponseFactoryInterface; | ||
|
||
class ResponseFactory implements ResponseFactoryInterface | ||
{ | ||
public function createResponse($code = 200) | ||
{ | ||
return new Response($code); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace Http\Factory\Guzzle; | ||
|
||
use GuzzleHttp\Psr7\ServerRequest; | ||
use Interop\Http\Factory\ServerRequestFactoryInterface; | ||
use Interop\Http\Factory\ServerRequestFromGlobalsFactoryInterface; | ||
|
||
class ServerRequestFactory implements ServerRequestFactoryInterface, ServerRequestFromGlobalsFactoryInterface | ||
{ | ||
public function createServerRequest($method, $uri) | ||
{ | ||
return new ServerRequest($method, $uri); | ||
} | ||
|
||
public function createServerRequestFromGlobals() | ||
{ | ||
return ServerRequest::fromGlobals(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Http\Factory\Guzzle; | ||
|
||
use Interop\Http\Factory\StreamFactoryInterface; | ||
|
||
class StreamFactory implements StreamFactoryInterface | ||
{ | ||
public function createStream($resource) | ||
{ | ||
return \GuzzleHttp\Psr7\stream_for($resource); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace Http\Factory\Guzzle; | ||
|
||
use GuzzleHttp\Psr7\UploadedFile; | ||
use Interop\Http\Factory\UploadedFileFactoryInterface; | ||
|
||
class UploadedFileFactory implements UploadedFileFactoryInterface | ||
{ | ||
public function createUploadedFile( | ||
$file, | ||
$size = null, | ||
$error = \UPLOAD_ERR_OK, | ||
$clientFilename = null, | ||
$clientMediaType = null | ||
) { | ||
if ($size === null) { | ||
if (is_string($file)) { | ||
$size = filesize($file); | ||
} else { | ||
$stats = fstat($file); | ||
$size = $stats['size']; | ||
} | ||
} | ||
|
||
return new UploadedFile($file, $size, $error, $clientFilename, $clientMediaType); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Http\Factory\Guzzle; | ||
|
||
use GuzzleHttp\Psr7\Uri; | ||
use Interop\Http\Factory\UriFactoryInterface; | ||
|
||
class UriFactory implements UriFactoryInterface | ||
{ | ||
public function createUri($uri = '') | ||
{ | ||
return new Uri($uri); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace Http\Factory\Guzzle; | ||
|
||
use PHPUnit_Framework_TestCase as TestCase; | ||
use Psr\Http\Message\RequestInterface; | ||
|
||
class RequestFactoryTest extends TestCase | ||
{ | ||
private $factory; | ||
|
||
public function setUp() | ||
{ | ||
$this->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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace Http\Factory\Guzzle; | ||
|
||
use PHPUnit_Framework_TestCase as TestCase; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
class ResponseFactoryTest extends TestCase | ||
{ | ||
private $factory; | ||
|
||
public function setUp() | ||
{ | ||
$this->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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
namespace Http\Factory\Guzzle; | ||
|
||
use PHPUnit_Framework_TestCase as TestCase; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
class ServerRequestFactoryTest extends TestCase | ||
{ | ||
private $factory; | ||
|
||
public function setUp() | ||
{ | ||
$this->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); | ||
} | ||
} |
Oops, something went wrong.