-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from php-http/message_factories
Add message factories
- Loading branch information
Showing
17 changed files
with
494 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
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,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" | ||
} | ||
} | ||
} | ||
} |
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,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'); | ||
} | ||
} |
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,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'); | ||
} | ||
} |
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,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'); | ||
} | ||
} |
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,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'); | ||
} | ||
} |
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,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'); | ||
} | ||
} |
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,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'); | ||
} | ||
} |
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,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); | ||
} | ||
} |
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,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); | ||
} | ||
} |
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 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); | ||
} | ||
} |
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,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); | ||
} | ||
} |
Oops, something went wrong.