diff --git a/CHANGELOG.md b/CHANGELOG.md index dfa32e0..a6519a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/LICENSE b/LICENSE index 8e2c4a0..4558d6f 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2015 PHP HTTP Team +Copyright (c) 2015-2016 PHP HTTP Team Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/composer.json b/composer.json index 1a46159..bd57dfb 100644 --- a/composer.json +++ b/composer.json @@ -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", @@ -34,9 +35,7 @@ }, "extra": { "branch-alias": { - "dev-master": "0.1-dev" + "dev-master": "0.4-dev" } - }, - "prefer-stable": true, - "minimum-stability": "beta" + } } diff --git a/spec/ClientSpec.php b/spec/ClientSpec.php new file mode 100644 index 0000000..a6ad71d --- /dev/null +++ b/spec/ClientSpec.php @@ -0,0 +1,55 @@ +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); + } +} diff --git a/src/Client.php b/src/Client.php index 6909f43..090e2bc 100644 --- a/src/Client.php +++ b/src/Client.php @@ -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; @@ -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} */ @@ -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 */ @@ -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 */ @@ -66,7 +92,7 @@ public function addResponse(ResponseInterface $response) } /** - * Get requests that were sent. + * Returns requests that were sent. * * @return RequestInterface[] */