Skip to content

Commit

Permalink
Default body for RequestFactory and ResponseFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
fenric committed Dec 10, 2018
1 parent c4a2222 commit 83f1cca
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
12 changes: 9 additions & 3 deletions src/RequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\UriInterface;
use Sunrise\Uri\Uri;
use Sunrise\Stream\StreamFactory;
use Sunrise\Uri\UriFactory;

/**
* RequestFactory
Expand All @@ -34,11 +35,16 @@ public function createRequest(string $method, $uri) : RequestInterface
{
if (! ($uri instanceof UriInterface))
{
$uri = new Uri($uri);
$uri = (new UriFactory)
->createUri($uri);
}

$body = (new StreamFactory)
->createStream();

return (new Request)
->withMethod($method)
->withUri($uri);
->withUri($uri)
->withBody($body);
}
}
8 changes: 7 additions & 1 deletion src/ResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Sunrise\Stream\StreamFactory;

/**
* ResponseFactory
Expand All @@ -30,6 +31,11 @@ class ResponseFactory implements ResponseFactoryInterface
*/
public function createResponse(int $code = 200, string $reasonPhrase = '') : ResponseInterface
{
return (new Response)->withStatus($code, $reasonPhrase);
$body = (new StreamFactory)
->createStream();

return (new Response)
->withStatus($code, $reasonPhrase)
->withBody($body);
}
}
8 changes: 8 additions & 0 deletions tests/RequestFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UriInterface;
use Sunrise\Http\Message\RequestFactory;
use Sunrise\Uri\Uri;
Expand All @@ -29,6 +30,13 @@ public function testCreateRequest()
$this->assertInstanceOf(RequestInterface::class, $request);
$this->assertEquals($method, $request->getMethod());
$this->assertEquals($uri, $request->getUri());

// default body of the request...
$this->assertInstanceOf(StreamInterface::class, $request->getBody());
$this->assertTrue($request->getBody()->isSeekable());
$this->assertTrue($request->getBody()->isWritable());
$this->assertTrue($request->getBody()->isReadable());
$this->assertEquals('php://temp', $request->getBody()->getMetadata('uri'));
}

public function testCreateRequestWithUriAsString()
Expand Down
8 changes: 8 additions & 0 deletions tests/ResponseFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use Sunrise\Http\Message\ResponseFactory;

class ResponseFactoryTest extends TestCase
Expand All @@ -27,5 +28,12 @@ public function testCreateResponse()
$this->assertInstanceOf(ResponseInterface::class, $response);
$this->assertEquals($statusCode, $response->getStatusCode());
$this->assertEquals($reasonPhrase, $response->getReasonPhrase());

// default body of the response...
$this->assertInstanceOf(StreamInterface::class, $response->getBody());
$this->assertTrue($response->getBody()->isSeekable());
$this->assertTrue($response->getBody()->isWritable());
$this->assertTrue($response->getBody()->isReadable());
$this->assertEquals('php://temp', $response->getBody()->getMetadata('uri'));
}
}

0 comments on commit 83f1cca

Please sign in to comment.