Skip to content

Add message decorators #1

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

Merged
merged 1 commit into from
Dec 21, 2015
Merged
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
142 changes: 142 additions & 0 deletions spec/Decorator/MessageDecoratorSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php

namespace spec\Http\Message\Decorator;

use Http\Message\Decorator\MessageDecorator;
use Psr\Http\Message\MessageInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UriInterface;
use PhpSpec\ObjectBehavior;

class MessageDecoratorSpec extends ObjectBehavior
{
function let(MessageInterface $message)
{
$this->beAnInstanceOf('spec\Http\Message\Decorator\MessageDecoratorStub', [$message]);
}

function it_is_initializable()
{
$this->shouldHaveType('spec\Http\Message\Decorator\MessageDecoratorStub');
}

function it_is_a_message()
{
$this->shouldImplement('Psr\Http\Message\MessageInterface');
}

function it_is_a_message_decorator()
{
$this->shouldUseTrait('Http\Message\Decorator\MessageDecorator');
}

function it_has_a_message()
{
$this->getMessage()->shouldImplement('Psr\Http\Message\MessageInterface');
}

function it_has_a_protocol_version(MessageInterface $message)
{
$message->getProtocolVersion()->willReturn('1.1');

$this->getProtocolVersion()->shouldReturn('1.1');
}

function it_accepts_a_protocol_version(MessageInterface $message, MessageInterface $newMessage)
{
$message->withProtocolVersion('1.1')->willReturn($newMessage);

$new = $this->withProtocolVersion('1.1');
$new->getMessage()->shouldReturn($newMessage);
}

function it_has_headers(MessageInterface $message)
{
$headers = [
'Content-Type' => 'application/xml'
];

$message->getHeaders()->willReturn($headers);

$this->getHeaders()->shouldReturn($headers);
}

function it_can_check_a_header(MessageInterface $message)
{
$message->hasHeader('Content-Type')->willReturn(true);

$this->hasHeader('Content-Type')->shouldReturn(true);
}

function it_has_a_header(MessageInterface $message)
{
$message->getHeader('Content-Type')->willReturn('application/xml');

$this->getHeader('Content-Type')->shouldReturn('application/xml');
}

function it_has_a_header_line(MessageInterface $message)
{
$message->getHeaderLine('Accept-Encoding')->willReturn('gzip, deflate');

$this->getHeaderLine('Accept-Encoding')->shouldReturn('gzip, deflate');
}

function it_accepts_a_header(MessageInterface $message, MessageInterface $newMessage)
{
$message->withHeader('Content-Type', 'application/xml')->willReturn($newMessage);

$new = $this->withHeader('Content-Type', 'application/xml');
$new->getMessage()->shouldReturn($newMessage);
}

function it_accepts_added_headers(MessageInterface $message, MessageInterface $newMessage)
{
$message->withAddedHeader('Content-Type', 'application/xml')->willReturn($newMessage);

$new = $this->withAddedHeader('Content-Type', 'application/xml');
$new->getMessage()->shouldReturn($newMessage);
}

function it_removes_a_header(MessageInterface $message, MessageInterface $newMessage)
{
$message->withoutHeader('Content-Type')->willReturn($newMessage);

$new = $this->withoutHeader('Content-Type');
$new->getMessage()->shouldReturn($newMessage);
}

function it_has_a_body(MessageInterface $message, StreamInterface $body)
{
$message->getBody()->willReturn($body);

$this->getBody()->shouldReturn($body);
}

function it_accepts_a_body(MessageInterface $message, MessageInterface $newMessage, StreamInterface $body)
{
$message->withBody($body)->willReturn($newMessage);

$new = $this->withBody($body);
$new->getMessage()->shouldReturn($newMessage);
}

function getMatchers()
{
return [
'useTrait' => function ($subject, $trait) {
return class_uses($subject, $trait);
}
];
}
}

class MessageDecoratorStub implements MessageInterface
{
use MessageDecorator;

public function __construct(MessageInterface $message)
{
$this->message = $message;
}
}
108 changes: 108 additions & 0 deletions spec/Decorator/RequestDecoratorSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

namespace spec\Http\Message\Decorator;

use Http\Message\Decorator\RequestDecorator;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UriInterface;
use PhpSpec\ObjectBehavior;

class RequestDecoratorSpec extends ObjectBehavior
{
function let(RequestInterface $request)
{
$this->beAnInstanceOf('spec\Http\Message\Decorator\RequestDecoratorStub', [$request]);
}

function it_is_initializable()
{
$this->shouldHaveType('spec\Http\Message\Decorator\RequestDecoratorStub');
}

function it_is_a_request()
{
$this->shouldImplement('Psr\Http\Message\RequestInterface');
}

function it_is_a_request_decorator()
{
$this->shouldUseTrait('Http\Message\Decorator\RequestDecorator');
}

function it_has_a_request()
{
$this->getRequest()->shouldImplement('Psr\Http\Message\RequestInterface');
}

function it_accepts_a_request(RequestInterface $request)
{
$new = $this->withRequest($request);

$new->getRequest()->shouldReturn($request);
}

function it_has_a_request_target(RequestInterface $request)
{
$request->getRequestTarget()->willReturn('/');

$this->getRequestTarget()->shouldReturn('/');
}

function it_accepts_a_request_target(RequestInterface $request, RequestInterface $newRequest)
{
$request->withRequestTarget('/')->willReturn($newRequest);

$new = $this->withRequestTarget('/');
$new->getMessage()->shouldReturn($newRequest);
}

function it_has_a_method(RequestInterface $request)
{
$request->getMethod()->willReturn('GET');

$this->getMethod()->shouldReturn('GET');
}

function it_accepts_a_method(RequestInterface $request, RequestInterface $newRequest)
{
$request->withMethod('GET')->willReturn($newRequest);

$new = $this->withMethod('GET');
$new->getMessage()->shouldReturn($newRequest);
}

function it_has_an_uri(RequestInterface $request, UriInterface $uri)
{
$request->getUri()->willReturn($uri);

$this->getUri()->shouldReturn($uri);
}

function it_accepts_an_uri(RequestInterface $request, RequestInterface $newRequest, UriInterface $uri)
{
$request->withUri($uri, false)->willReturn($newRequest);

$new = $this->withUri($uri);
$new->getMessage()->shouldReturn($newRequest);
}

function getMatchers()
{
return [
'useTrait' => function ($subject, $trait) {
return class_uses($subject, $trait);
}
];
}
}

class RequestDecoratorStub implements RequestInterface
{
use RequestDecorator;

public function __construct(RequestInterface $request)
{
$this->message = $request;
}
}
84 changes: 84 additions & 0 deletions spec/Decorator/ResponseDecoratorSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace spec\Http\Message\Decorator;

use Http\Message\Decorator\ResponseDecorator;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use PhpSpec\ObjectBehavior;

class ResponseDecoratorSpec extends ObjectBehavior
{
function let(ResponseInterface $response)
{
$this->beAnInstanceOf('spec\Http\Message\Decorator\ResponseDecoratorStub', [$response]);
}

function it_is_initializable()
{
$this->shouldHaveType('spec\Http\Message\Decorator\ResponseDecoratorStub');
}

function it_is_a_response()
{
$this->shouldImplement('Psr\Http\Message\ResponseInterface');
}

function it_is_a_response_decorator()
{
$this->shouldUseTrait('Http\Message\Decorator\ResponseDecorator');
}

function it_has_a_response()
{
$this->getResponse()->shouldImplement('Psr\Http\Message\ResponseInterface');
}

function it_accepts_a_response(ResponseInterface $response)
{
$new = $this->withResponse($response);

$new->getResponse()->shouldReturn($response);
}

function it_has_a_status_code(ResponseInterface $response)
{
$response->getStatusCode()->willReturn(200);

$this->getStatusCode()->shouldReturn(200);
}

function it_accepts_a_status(ResponseInterface $response, ResponseInterface $newResponse)
{
$response->withStatus(200, 'OK')->willReturn($newResponse);

$new = $this->withStatus(200, 'OK');
$new->getMessage()->shouldReturn($newResponse);
}

function it_has_a_reason_phrase(ResponseInterface $response)
{
$response->getReasonPhrase()->willReturn('OK');

$this->getReasonPhrase()->shouldReturn('OK');
}

function getMatchers()
{
return [
'useTrait' => function ($subject, $trait) {
return class_uses($subject, $trait);
}
];
}
}

class ResponseDecoratorStub implements ResponseInterface
{
use ResponseDecorator;

public function __construct(ResponseInterface $response)
{
$this->message = $response;
}
}
Loading