Skip to content

Update dependencies, remove puli requirement, add unit tests #10

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
Feb 26, 2016
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
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
# Change Log


## 0.3.0 - 2016-02-26

### Added

- Support for custom MessageFactory

### Changed

- Updated dependencies


## 0.2.0 - 2016-02-01

### Changed
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2015 PHP HTTP Team <[email protected]>
Copyright (c) 2015-2016 PHP HTTP Team <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
9 changes: 4 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"php": ">=5.4",
"php-http/httplug": "^1.0",
"php-http/client-common": "^1.0",
"php-http/discovery": "^0.7"
"php-http/discovery": "^0.8",
"php-http/message-factory": "^1.0"
},
"require-dev": {
"phpspec/phpspec": "^2.4",
Expand All @@ -34,9 +35,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "0.1-dev"
"dev-master": "0.4-dev"
}
},
"prefer-stable": true,
"minimum-stability": "beta"
}
}
55 changes: 55 additions & 0 deletions spec/ClientSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace spec\Http\Mock;

use Http\Message\ResponseFactory;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use PhpSpec\ObjectBehavior;

class ClientSpec extends ObjectBehavior
{
function let(ResponseFactory $responseFactory)
{
$this->beConstructedWith($responseFactory);
}

function it_is_initializable()
{
$this->shouldHaveType('Http\Mock\Client');
}

function it_is_an_http_client()
{
$this->shouldImplement('Http\Client\HttpClient');
}

function it_is_an_async_http_client()
{
$this->shouldImplement('Http\Client\HttpAsyncClient');
}

function it_returns_a_response_for_a_request(RequestInterface $request, ResponseInterface $response)
{
$this->addResponse($response);

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

function it_throws_an_exception_for_a_request(RequestInterface $request)
{
$this->addException(new \Exception());

$this->shouldThrow('Exception')->duringSendRequest($request);
}

function it_creates_an_empty_response_when_none_is_added(
RequestInterface $request,
ResponseFactory $responseFactory,
ResponseInterface $response
) {
$responseFactory->createResponse()->willReturn($response);

$this->sendRequest($request)->shouldReturn($response);
}
}
34 changes: 30 additions & 4 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
namespace Http\Mock;

use Http\Client\Common\HttpAsyncClientEmulator;
use Http\Client\Exception;
use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Message\ResponseFactory;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

Expand All @@ -22,10 +24,34 @@ class Client implements HttpClient, HttpAsyncClient
{
use HttpAsyncClientEmulator;

/**
* @var ResponseFactory
*/
private $responseFactory;

/**
* @var RequestInterface[]
*/
private $requests = [];

/**
* @var ResponseInterface[]
*/
private $responses = [];

/**
* @var Exception[]
*/
private $exceptions = [];

/**
* @param ResponseFactory|null $responseFactory
*/
public function __construct(ResponseFactory $responseFactory = null)
{
$this->responseFactory = $responseFactory ?: MessageFactoryDiscovery::find();
}

/**
* {@inheritdoc}
*/
Expand All @@ -42,11 +68,11 @@ public function sendRequest(RequestInterface $request)
}

// Return success response by default
return MessageFactoryDiscovery::find()->createResponse();
return $this->responseFactory->createResponse();
}

/**
* Add exception that will be thrown.
* Adds an exception that will be thrown.
*
* @param \Exception $exception
*/
Expand All @@ -56,7 +82,7 @@ public function addException(\Exception $exception)
}

/**
* Add response that will be returned.
* Adds a response that will be returned.
*
* @param ResponseInterface $response
*/
Expand All @@ -66,7 +92,7 @@ public function addResponse(ResponseInterface $response)
}

/**
* Get requests that were sent.
* Returns requests that were sent.
*
* @return RequestInterface[]
*/
Expand Down