Skip to content

Commit

Permalink
Merge pull request #2 from php-http/message_factories
Browse files Browse the repository at this point in the history
Add message factories
  • Loading branch information
sagikazarmark committed Dec 21, 2015
2 parents 62eba76 + c37fb62 commit 9fc3a41
Show file tree
Hide file tree
Showing 17 changed files with 494 additions and 0 deletions.
6 changes: 6 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@
"php-http/message-factory": "^1.0"
},
"require-dev": {
"zendframework/zend-diactoros": "^1.0",
"guzzlehttp/psr7": "^1.0",
"phpspec/phpspec": "^2.4",
"henrikbjorn/phpspec-code-coverage" : "^1.0"
},
"suggest": {
"zendframework/zend-diactoros": "Used with Diactoros Factories",
"guzzlehttp/psr7": "Used with Guzzle PSR-7 Factories"
},
"autoload": {
"psr-4": {
"Http\\Message\\": "src/"
Expand Down
53 changes: 53 additions & 0 deletions puli.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"version": "1.0",
"bindings": {
"064d003d-78a1-48c4-8f3b-1f92ff25da69": {
"_class": "Puli\\Discovery\\Binding\\ClassBinding",
"class": "Http\\Message\\MessageFactory\\DiactorosMessageFactory",
"type": "Http\\Message\\MessageFactory",
"parameters": {
"depends": "Zend\\Diactoros\\Request"
}
},
"2438c2d0-0658-441f-8855-ddaf0f87d54d": {
"_class": "Puli\\Discovery\\Binding\\ClassBinding",
"class": "Http\\Message\\MessageFactory\\GuzzleMessageFactory",
"type": "Http\\Message\\MessageFactory",
"parameters": {
"depends": "GuzzleHttp\\Psr7\\Request"
}
},
"273a34f9-62f4-4ba1-9801-b1284d49ff89": {
"_class": "Puli\\Discovery\\Binding\\ClassBinding",
"class": "Http\\Message\\StreamFactory\\GuzzleStreamFactory",
"type": "Http\\Message\\StreamFactory",
"parameters": {
"depends": "GuzzleHttp\\Psr7\\Stream"
}
},
"304b83db-b594-4d83-ae75-1f633adf92f7": {
"_class": "Puli\\Discovery\\Binding\\ClassBinding",
"class": "Http\\Message\\UriFactory\\GuzzleUriFactory",
"type": "Http\\Message\\UriFactory",
"parameters": {
"depends": "GuzzleHttp\\Psr7\\Uri"
}
},
"3f4bc1cd-aa95-4702-9fa7-65408e471691": {
"_class": "Puli\\Discovery\\Binding\\ClassBinding",
"class": "Http\\Message\\UriFactory\\DiactorosUriFactory",
"type": "Http\\Message\\UriFactory",
"parameters": {
"depends": "Zend\\Diactoros\\Uri"
}
},
"95c1be8f-39fe-4abd-8351-92cb14379a75": {
"_class": "Puli\\Discovery\\Binding\\ClassBinding",
"class": "Http\\Message\\StreamFactory\\DiactorosStreamFactory",
"type": "Http\\Message\\StreamFactory",
"parameters": {
"depends": "Zend\\Diactoros\\Stream"
}
}
}
}
16 changes: 16 additions & 0 deletions spec/MessageFactory/DiactorosMessageFactorySpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace spec\Http\Message\MessageFactory;

use PhpSpec\ObjectBehavior;
use spec\Http\Message\MessageFactoryBehavior;

class DiactorosMessageFactorySpec extends ObjectBehavior
{
use MessageFactoryBehavior;

function it_is_initializable()
{
$this->shouldHaveType('Http\Message\MessageFactory\DiactorosMessageFactory');
}
}
16 changes: 16 additions & 0 deletions spec/MessageFactory/GuzzleMessageFactorySpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace spec\Http\Message\MessageFactory;

use PhpSpec\ObjectBehavior;
use spec\Http\Message\MessageFactoryBehavior;

class GuzzleMessageFactorySpec extends ObjectBehavior
{
use MessageFactoryBehavior;

function it_is_initializable()
{
$this->shouldHaveType('Http\Message\MessageFactory\GuzzleMessageFactory');
}
}
27 changes: 27 additions & 0 deletions spec/MessageFactoryBehavior.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace spec\Http\Message;

trait MessageFactoryBehavior
{
function it_is_a_message_factory()
{
$this->shouldImplement('Http\Message\MessageFactory');
}

function it_creates_a_request()
{
$request = $this->createRequest('GET', '/');

$request->shouldHaveType('Psr\Http\Message\RequestInterface');
$request->getMethod()->shouldReturn('GET');
$request->getRequestTarget()->shouldReturn('/');
}

function it_creates_a_response()
{
$response = $this->createResponse();

$response->shouldHaveType('Psr\Http\Message\ResponseInterface');
}
}
23 changes: 23 additions & 0 deletions spec/StreamFactory/DiactorosStreamFactorySpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace spec\Http\Message\StreamFactory;

use Zend\Diactoros\Stream;
use PhpSpec\ObjectBehavior;
use spec\Http\Message\StreamFactoryBehavior;

class DiactorosStreamFactorySpec extends ObjectBehavior
{
use StreamFactoryBehavior;

function it_is_initializable()
{
$this->shouldHaveType('Http\Message\StreamFactory\DiactorosStreamFactory');
}

function it_creates_a_stream_from_stream()
{
$this->createStream(new Stream('php://memory'))
->shouldHaveType('Psr\Http\Message\StreamInterface');
}
}
23 changes: 23 additions & 0 deletions spec/StreamFactory/GuzzleStreamFactorySpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace spec\Http\Message\StreamFactory;

use GuzzleHttp\Psr7\Stream;
use PhpSpec\ObjectBehavior;
use spec\Http\Message\StreamFactoryBehavior;

class GuzzleStreamFactorySpec extends ObjectBehavior
{
use StreamFactoryBehavior;

public function it_is_initializable()
{
$this->shouldHaveType('Http\Message\StreamFactory\GuzzleStreamFactory');
}

public function it_creates_a_stream_from_stream()
{
$this->createStream(new Stream(fopen('php://memory', 'rw')))
->shouldHaveType('Psr\Http\Message\StreamInterface');
}
}
27 changes: 27 additions & 0 deletions spec/StreamFactoryBehavior.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace spec\Http\Message;

trait StreamFactoryBehavior
{
function it_is_a_stream_factory()
{
$this->shouldImplement('Http\Message\StreamFactory');
}

function it_creates_a_stream_from_string()
{
$this->createStream('foo')->shouldHaveType('Psr\Http\Message\StreamInterface');
}

function it_creates_a_stream_from_resource()
{
$this->createStream(fopen('php://memory', 'rw'))
->shouldHaveType('Psr\Http\Message\StreamInterface');
}

function it_creates_a_stream_from_null()
{
$this->createStream(null)->shouldHaveType('Psr\Http\Message\StreamInterface');
}
}
25 changes: 25 additions & 0 deletions spec/UriFactory/DiactorosUriFactorySpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace spec\Http\Message\UriFactory;

use Psr\Http\Message\UriInterface;
use PhpSpec\ObjectBehavior;
use spec\Http\Message\UriFactoryBehavior;

class DiactorosUriFactorySpec extends ObjectBehavior
{
use UriFactoryBehavior;

function it_is_initializable()
{
$this->shouldHaveType('Http\Message\UriFactory\DiactorosUriFactory');
}

/**
* TODO: Remove this when https://github.com/phpspec/phpspec/issues/825 is resolved
*/
function it_creates_a_uri_from_uri(UriInterface $uri)
{
$this->createUri($uri)->shouldReturn($uri);
}
}
25 changes: 25 additions & 0 deletions spec/UriFactory/GuzzleUriFactorySpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace spec\Http\Message\UriFactory;

use Psr\Http\Message\UriInterface;
use PhpSpec\ObjectBehavior;
use spec\Http\Message\UriFactoryBehavior;

class GuzzleUriFactorySpec extends ObjectBehavior
{
use UriFactoryBehavior;

function it_is_initializable()
{
$this->shouldHaveType('Http\Message\UriFactory\GuzzleUriFactory');
}

/**
* TODO: Remove this when https://github.com/phpspec/phpspec/issues/825 is resolved
*/
function it_creates_a_uri_from_uri(UriInterface $uri)
{
$this->createUri($uri)->shouldReturn($uri);
}
}
28 changes: 28 additions & 0 deletions spec/UriFactoryBehavior.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace spec\Http\Message;

use Psr\Http\Message\UriInterface;

trait UriFactoryBehavior
{
function it_is_a_uri_factory()
{
$this->shouldImplement('Http\Message\UriFactory');
}

function it_creates_a_uri_from_string()
{
$this->createUri('http://php-http.org')->shouldHaveType('Psr\Http\Message\UriInterface');
}

function it_creates_a_uri_from_uri(UriInterface $uri)
{
$this->createUri($uri)->shouldReturn($uri);
}

function it_throws_an_exception_when_uri_is_invalid()
{
$this->shouldThrow('InvalidArgumentException')->duringCreateUri(null);
}
}
61 changes: 61 additions & 0 deletions src/MessageFactory/DiactorosMessageFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Http\Message\MessageFactory;

use Http\Message\StreamFactory\DiactorosStreamFactory;
use Http\Message\MessageFactory;
use Zend\Diactoros\Request;
use Zend\Diactoros\Response;

/**
* Creates Diactoros messages.
*
* @author GeLo <geloen.eric@gmail.com>
*/
final class DiactorosMessageFactory implements MessageFactory
{
/**
* @var DiactorosStreamFactory
*/
private $streamFactory;

public function __construct()
{
$this->streamFactory = new DiactorosStreamFactory();
}

/**
* {@inheritdoc}
*/
public function createRequest(
$method,
$uri,
array $headers = [],
$body = null,
$protocolVersion = '1.1'
) {
return (new Request(
$uri,
$method,
$this->streamFactory->createStream($body),
$headers
))->withProtocolVersion($protocolVersion);
}

/**
* {@inheritdoc}
*/
public function createResponse(
$statusCode = 200,
$reasonPhrase = null,
array $headers = [],
$body = null,
$protocolVersion = '1.1'
) {
return (new Response(
$this->streamFactory->createStream($body),
$statusCode,
$headers
))->withProtocolVersion($protocolVersion);
}
}

0 comments on commit 9fc3a41

Please sign in to comment.