Skip to content

Commit

Permalink
Initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
Nyholm committed Aug 25, 2016
0 parents commit b5108c7
Show file tree
Hide file tree
Showing 16 changed files with 481 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
composer.phar
composer.lock
phpunit.xml
build/
vendor/
21 changes: 21 additions & 0 deletions LICENSE
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.
3 changes: 3 additions & 0 deletions README.md
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).
37 changes: 37 additions & 0 deletions composer.json
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"
}
]
}
17 changes: 17 additions & 0 deletions phpunit.xml.dist
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>
14 changes: 14 additions & 0 deletions src/RequestFactory.php
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);
}
}
14 changes: 14 additions & 0 deletions src/ResponseFactory.php
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);
}
}
20 changes: 20 additions & 0 deletions src/ServerRequestFactory.php
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();
}
}
13 changes: 13 additions & 0 deletions src/StreamFactory.php
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);
}
}
28 changes: 28 additions & 0 deletions src/UploadedFileFactory.php
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);
}
}
14 changes: 14 additions & 0 deletions src/UriFactory.php
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);
}
}
59 changes: 59 additions & 0 deletions tests/RequestFactoryTest.php
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);
}
}
42 changes: 42 additions & 0 deletions tests/ResponseFactoryTest.php
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);
}
}
76 changes: 76 additions & 0 deletions tests/ServerRequestFactoryTest.php
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);
}
}
Loading

0 comments on commit b5108c7

Please sign in to comment.