Skip to content

[WIP] Enhancement: Implement StrictClient #42

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

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
43 changes: 43 additions & 0 deletions spec/Exception/RequestMismatchExceptionSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace spec\Http\Mock\Exception;

use Http\Mock\Exception\RequestMismatchException;
use Http\Mock\Exception\UnexpectedRequestException;
use Http\Mock\StrictClient;
use PhpSpec\ObjectBehavior;
use Psr\Http\Message\RequestInterface;

class RequestMismatchExceptionSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType(RequestMismatchException::class);
}

function it_is_runtime_exception()
{
$this->shouldHaveType(\RuntimeException::class);
}

function it_can_be_created_from_matcher_exception()
{
$exception = new \Exception('Hmm');

$this->beConstructedThrough('fromMatcherException', [
$exception,
]);

$this->shouldHaveType(RequestMismatchException::class);
$this->getMessage()->shouldReturn('Expected a different request to be sent.');
$this->getPrevious()->shouldReturn($exception);
}

function it_can_be_created_with_create()
{
$this->beConstructedThrough('create');

$this->shouldHaveType(RequestMismatchException::class);
$this->getMessage()->shouldReturn('Expected a different request to be sent.');
}
}
31 changes: 31 additions & 0 deletions spec/Exception/UnexpectedRequestExceptionSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace spec\Http\Mock\Exception;

use Http\Mock\Exception\UnexpectedRequestException;
use PhpSpec\ObjectBehavior;
use Psr\Http\Message\RequestInterface;

class UnexpectedRequestExceptionSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType(UnexpectedRequestException::class);
}

function it_is_runtime_exception()
{
$this->shouldHaveType(\RuntimeException::class);
}

function it_can_be_created_from_request(RequestInterface $request)
{
$this->beConstructedThrough('fromRequest', [
$request,
]);

$this->shouldHaveType(UnexpectedRequestException::class);
$this->getMessage()->shouldReturn('Did not expect request to be sent');
$this->getRequest()->shouldReturn($request->getWrappedObject());
}
}
167 changes: 167 additions & 0 deletions spec/StrictClientSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
<?php

namespace spec\Http\Mock;

use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;
use Http\Message\RequestMatcher;
use Http\Message\ResponseFactory;
use Http\Mock\Client;
use Http\Mock\Exception\RequestMismatchException;
use Http\Mock\Exception\UnexpectedRequestException;
use Http\Mock\StrictClient;
use PhpSpec\Matcher\MatcherInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use PhpSpec\ObjectBehavior;

/**
* @mixin StrictClient
*/
class StrictClientSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType(StrictClient::class);
}

function it_is_an_http_client()
{
$this->shouldImplement(HttpClient::class);
}

function it_is_an_async_http_client()
{
$this->shouldImplement(HttpAsyncClient::class);
}

function it_throws_when_no_matchers_have_been_configured(RequestInterface $request)
{
$this
->shouldThrow(UnexpectedRequestException::fromRequest($request->getWrappedObject()))
->duringSendRequest($request);
}

function it_throws_when_next_request_matcher_throws_exception(
RequestMatcher $matcher,
RequestInterface $request,
ResponseInterface $response
) {
$exception = new \Exception('Failed asserting that request method is "POST", got "GET" instead.');

$matcher->matches($request)->willThrow($exception);

$this->on($matcher, $response);

$this
->shouldThrow(RequestMismatchException::fromMatcherException($exception))
->duringSendRequest($request);
}

function it_throws_when_next_request_matcher_does_not_match_request(
RequestMatcher $matcher,
RequestInterface $request,
ResponseInterface $response
) {
$matcher->matches($request)->willReturn(false);

$this->on($matcher, $response);

$this
->shouldThrow(RequestMismatchException::create())
->duringSendRequest($request);
}

function it_throws_configured_exception_when_next_request_matcher_matches_request(
RequestMatcher $matcher,
RequestInterface $request,
ResponseInterface $response
) {
$exception = new \Exception('Sending the request failed because of a network error');

$matcher->matches($request)->willReturn(true);

$this->on($matcher, $exception);

$this
->shouldThrow($exception)
->duringSendRequest($request);
}

function it_returns_configured_request_when_next_request_matcher_matches_request(
RequestMatcher $matcher,
RequestInterface $request,
ResponseInterface $response
) {
$matcher->matches($request)->willReturn(true);

$this->on($matcher, $response);

$this->sendRequest($request)->shouldReturn($response);
}

function it_calls_callable_with_request_as_argument_when_matcher_returns_true(
RequestMatcher $matcher,
RequestInterface $request,
ResponseInterface $response
) {
$matcher->matches($request)->willReturn(true);

$this->on(
$matcher,
function(RequestInterface $request) use ($response) {
return $response->getWrappedObject();
}
);

$this->sendRequest($request)->shouldReturn($response);
}

function it_has_completed_sequence_when_no_matches_have_been_configured()
{
$this->hasCompletedSequence()->shouldReturn(true);
}

function it_has_not_completed_sequence_when_not_all_expected_requests_have_been_sent(
RequestMatcher $firstMatcher,
RequestInterface $firstRequest,
ResponseInterface $firstResponse,
RequestMatcher $secondMatcher,
RequestInterface $secondRequest,
ResponseInterface $secondResponse
) {
$firstMatcher->matches($firstRequest)->willReturn(true);

$this->on($firstMatcher, $firstResponse);

$secondMatcher->matches($secondRequest)->willReturn(true);

$this->on($secondMatcher, $secondResponse);

$this->sendRequest($firstRequest);

$this->hasCompletedSequence()->shouldReturn(false);
}

function it_has_completed_sequence_when_all_expected_requests_have_been_sent(
RequestMatcher $firstMatcher,
RequestInterface $firstRequest,
ResponseInterface $firstResponse,
RequestMatcher $secondMatcher,
RequestInterface $secondRequest,
ResponseInterface $secondResponse
) {
$firstMatcher->matches($firstRequest)->willReturn(true);

$this->on($firstMatcher, $firstResponse);

$secondMatcher->matches($secondRequest)->willReturn(true);

$this->on($secondMatcher, $secondResponse);

$this->sendRequest($firstRequest);
$this->sendRequest($secondRequest);

$this->hasCompletedSequence()->shouldReturn(true);
}
}
28 changes: 28 additions & 0 deletions src/Exception/RequestMismatchException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Http\Mock\Exception;

class RequestMismatchException extends \RuntimeException
{
/**
* @param \Exception $exception
*
* @return self
*/
public static function fromMatcherException(\Exception $exception)
{
return new self(
'Expected a different request to be sent.',
0,
$exception
);
}

/**
* @return self
*/
public static function create()
{
return new self('Expected a different request to be sent.');
}
}
35 changes: 35 additions & 0 deletions src/Exception/UnexpectedRequestException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Http\Mock\Exception;

use Psr\Http\Message\RequestInterface;

class UnexpectedRequestException extends \RuntimeException
{
/**
* @var RequestInterface|null
*/
private $request;

/**
* @param RequestInterface $request
*
* @return self
*/
public static function fromRequest(RequestInterface $request)
{
$instance = new self('Did not expect request to be sent');

$instance->request = $request;

return $instance;
}

/**
* @return RequestInterface|null
*/
public function getRequest()
{
return $this->request;
}
}
Loading