|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace spec\Http\Client\Common; |
| 4 | + |
| 5 | +use Http\Client\Exception\TransferException; |
| 6 | +use Http\Client\HttpClient; |
| 7 | +use Http\Client\HttpAsyncClient; |
| 8 | +use Http\Client\Common\HttpClientEmulator; |
| 9 | +use Http\Client\Common\HttpAsyncClientDecorator; |
| 10 | +use Http\Promise\Promise; |
| 11 | +use Psr\Http\Message\RequestInterface; |
| 12 | +use Psr\Http\Message\ResponseInterface; |
| 13 | +use PhpSpec\ObjectBehavior; |
| 14 | + |
| 15 | +class HttpClientEmulatorSpec extends ObjectBehavior |
| 16 | +{ |
| 17 | + function let(HttpAsyncClient $httpAsyncClient) |
| 18 | + { |
| 19 | + $this->beAnInstanceOf('spec\Http\Client\Common\HttpClientEmulatorStub', [$httpAsyncClient]); |
| 20 | + } |
| 21 | + |
| 22 | + function it_is_initializable() |
| 23 | + { |
| 24 | + $this->shouldHaveType('spec\Http\Client\Common\HttpClientEmulatorStub'); |
| 25 | + } |
| 26 | + |
| 27 | + function it_emulates_a_successful_request(HttpAsyncClient $httpAsyncClient, RequestInterface $request, Promise $promise, ResponseInterface $response) |
| 28 | + { |
| 29 | + $promise->wait()->shouldBeCalled(); |
| 30 | + $promise->getState()->willReturn(Promise::FULFILLED); |
| 31 | + $promise->wait()->willReturn($response); |
| 32 | + |
| 33 | + $httpAsyncClient->sendAsyncRequest($request)->willReturn($promise); |
| 34 | + |
| 35 | + $this->sendRequest($request)->shouldReturn($response); |
| 36 | + } |
| 37 | + |
| 38 | + function it_emulates_a_failed_request(HttpAsyncClient $httpAsyncClient, RequestInterface $request, Promise $promise) |
| 39 | + { |
| 40 | + $promise->wait()->shouldBeCalled(); |
| 41 | + $promise->getState()->willReturn(Promise::REJECTED); |
| 42 | + $promise->wait()->willThrow(new TransferException()); |
| 43 | + |
| 44 | + $httpAsyncClient->sendAsyncRequest($request)->willReturn($promise); |
| 45 | + |
| 46 | + $this->shouldThrow('Http\Client\Exception')->duringSendRequest($request); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +class HttpClientEmulatorStub implements HttpAsyncClient, HttpClient |
| 51 | +{ |
| 52 | + use HttpAsyncClientDecorator; |
| 53 | + use HttpClientEmulator; |
| 54 | + |
| 55 | + /** |
| 56 | + * @param HttpAsyncClient $httpAsyncClient |
| 57 | + */ |
| 58 | + public function __construct(HttpAsyncClient $httpAsyncClient) |
| 59 | + { |
| 60 | + $this->httpAsyncClient = $httpAsyncClient; |
| 61 | + } |
| 62 | +} |
0 commit comments