Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add message factories #50

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
],
"require": {
"php": ">=5.4.0",
"psr/http-message": "~1.0"
"psr/http-message": "~1.0",
"php-http/message-factory": "~1.0@dev"
},
"require-dev": {
"phpunit/phpunit": "~4.0"
Expand Down
32 changes: 32 additions & 0 deletions src/MessageFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace GuzzleHttp\Psr7;

/**
* Creates Request and Response.
*
* @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
*/
final class MessageFactory implements \Http\Message\MessageFactory
{
use ResponseFactoryTrait;

/**
* {@inheritdoc}
*/
public function createRequest(
$method,
$uri,
array $headers = [],
$body = null,
$protocolVersion = '1.1'
) {
return new Request(
$method,
$uri,
$headers,
$body,
$protocolVersion
);
}
}
30 changes: 30 additions & 0 deletions src/ResponseFactoryTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace GuzzleHttp\Psr7;

/**
* Creates Response.
*
* @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
*/
trait ResponseFactoryTrait
{
/**
* {@inheritdoc}
*/
public function createResponse(
$statusCode = 200,
$reasonPhrase = null,
array $headers = [],
$body = null,
$protocolVersion = '1.1'
) {
return new Response(
$statusCode,
$headers,
$body,
$protocolVersion,
$reasonPhrase
);
}
}
19 changes: 19 additions & 0 deletions src/StreamFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace GuzzleHttp\Psr7;

/**
* Creates streams.
*
* @author Михаил Красильников <m.krasilnikov@yandex.ru>
*/
final class StreamFactory implements \Http\Message\StreamFactory
{
/**
* {@inheritdoc}
*/
public function createStream($body = null)
{
return stream_for($body);
}
}
19 changes: 19 additions & 0 deletions src/UriFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace GuzzleHttp\Psr7;

/**
* Creates URI.
*
* @author David de Boer <david@ddeboer.nl>
*/
final class UriFactory implements \Http\Message\UriFactory
{
/**
* {@inheritdoc}
*/
public function createUri($uri)
{
return uri_for($uri);
}
}
45 changes: 45 additions & 0 deletions tests/MessageFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace GuzzleHttp\Tests\Psr7;

use GuzzleHttp\Psr7\MessageFactory;

class MessageFactoryTest extends \PHPUnit_Framework_TestCase
{
public function testItIsAMessageFactory()
{
$this->assertInstanceOf('Http\\Message\\MessageFactory', new MessageFactory());
}

public function testReturnsRequest()
{
$mf = new MessageFactory();

$request = $mf->createRequest('GET', '/', ['Content-Type' => 'text/html'], 'body', '1.0');

$this->assertInstanceOf('Psr\\Http\\Message\\RequestInterface', $request);
$this->assertInstanceOf('GuzzleHttp\\Psr7\\Request', $request);

$this->assertEquals('GET', $request->getMethod());
$this->assertEquals('/', $request->getRequestTarget());
$this->assertEquals(['Content-Type' => ['text/html']], $request->getHeaders());
$this->assertEquals('body', (string) $request->getBody());
$this->assertEquals('1.0', $request->getProtocolVersion());
}

public function testReturnsResponse()
{
$mf = new MessageFactory();

$response = $mf->createResponse(200, null, ['Content-Type' => 'text/html'], 'response', '1.0');

$this->assertInstanceOf('Psr\\Http\\Message\\ResponseInterface', $response);
$this->assertInstanceOf('GuzzleHttp\\Psr7\\Response', $response);

$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('OK', $response->getReasonPhrase());
$this->assertEquals(['Content-Type' => ['text/html']], $response->getHeaders());
$this->assertEquals('response', (string) $response->getBody());
$this->assertEquals('1.0', $response->getProtocolVersion());
}
}
48 changes: 48 additions & 0 deletions tests/StreamFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace GuzzleHttp\Tests\Psr7;

use GuzzleHttp\Psr7\StreamFactory;

class StreamFactoryTest extends \PHPUnit_Framework_TestCase
{
public function testItIsAStreamFactory()
{
$this->assertInstanceOf('Http\\Message\\StreamFactory', new StreamFactory());
}

public function testCreatesStreamFromString()
{
$sf = new StreamFactory();

$stream = $sf->createStream('body');

$this->assertInstanceOf('Psr\\Http\\Message\\StreamInterface', $stream);
$this->assertInstanceOf('GuzzleHttp\\Psr7\\Stream', $stream);

$this->assertEquals('body', (string) $stream);
}

public function testCreatesEmptyStream()
{
$sf = new StreamFactory();

$stream = $sf->createStream();

$this->assertInstanceOf('Psr\\Http\\Message\\StreamInterface', $stream);
$this->assertInstanceOf('GuzzleHttp\\Psr7\\Stream', $stream);

$this->assertEquals('', (string) $stream);
}

public function testReturnsSameStream()
{
$sf = new StreamFactory();

$originalStream = \GuzzleHttp\Psr7\stream_for('');

$stream = $sf->createStream($originalStream);

$this->assertSame($originalStream, $stream);
}
}
36 changes: 36 additions & 0 deletions tests/UriFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace GuzzleHttp\Tests\Psr7;

use GuzzleHttp\Psr7\UriFactory;

class UriFactoryTest extends \PHPUnit_Framework_TestCase
{
public function testItIsAUriFactory()
{
$this->assertInstanceOf('Http\\Message\\UriFactory', new UriFactory());
}

public function testReturnsUri()
{
$uf = new UriFactory();

$uri = $uf->createUri('/');

$this->assertInstanceOf('Psr\\Http\\Message\\UriInterface', $uri);
$this->assertInstanceOf('GuzzleHttp\\Psr7\\Uri', $uri);

$this->assertEquals('/', (string) $uri);
}

public function testReturnsSameUri()
{
$uf = new UriFactory();

$originalUri = \GuzzleHttp\Psr7\uri_for('');

$uri = $uf->createUri($originalUri);

$this->assertSame($originalUri, $uri);
}
}