|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace spec\Http\Client\Common\HttpClientPool; |
| 4 | + |
| 5 | +use Http\Client\Common\HttpClientPoolItem; |
| 6 | +use Http\Client\HttpAsyncClient; |
| 7 | +use Http\Client\HttpClient; |
| 8 | +use Http\Promise\Promise; |
| 9 | +use PhpSpec\Exception\Example\SkippingException; |
| 10 | +use PhpSpec\ObjectBehavior; |
| 11 | +use Prophecy\Argument; |
| 12 | +use Psr\Http\Message\RequestInterface; |
| 13 | +use Psr\Http\Message\ResponseInterface; |
| 14 | + |
| 15 | +class LeastUsedClientPoolSpec extends ObjectBehavior |
| 16 | +{ |
| 17 | + public function it_is_initializable() |
| 18 | + { |
| 19 | + $this->shouldHaveType('Http\Client\Common\HttpClientPool\LeastUsedClientPool'); |
| 20 | + } |
| 21 | + |
| 22 | + public function it_is_an_http_client() |
| 23 | + { |
| 24 | + $this->shouldImplement('Http\Client\HttpClient'); |
| 25 | + } |
| 26 | + |
| 27 | + public function it_is_an_async_http_client() |
| 28 | + { |
| 29 | + $this->shouldImplement('Http\Client\HttpAsyncClient'); |
| 30 | + } |
| 31 | + |
| 32 | + public function it_throw_exception_with_no_client(RequestInterface $request) |
| 33 | + { |
| 34 | + $this->shouldThrow('Http\Client\Common\Exception\HttpClientNotFoundException')->duringSendRequest($request); |
| 35 | + $this->shouldThrow('Http\Client\Common\Exception\HttpClientNotFoundException')->duringSendAsyncRequest($request); |
| 36 | + } |
| 37 | + |
| 38 | + public function it_sends_request(HttpClient $httpClient, RequestInterface $request, ResponseInterface $response) |
| 39 | + { |
| 40 | + $this->addHttpClient($httpClient); |
| 41 | + $httpClient->sendRequest($request)->willReturn($response); |
| 42 | + |
| 43 | + $this->sendRequest($request)->shouldReturn($response); |
| 44 | + } |
| 45 | + |
| 46 | + public function it_sends_async_request(HttpAsyncClient $httpAsyncClient, RequestInterface $request, Promise $promise) |
| 47 | + { |
| 48 | + $this->addHttpClient($httpAsyncClient); |
| 49 | + $httpAsyncClient->sendAsyncRequest($request)->willReturn($promise); |
| 50 | + $promise->then(Argument::type('callable'), Argument::type('callable'))->willReturn($promise); |
| 51 | + |
| 52 | + $this->sendAsyncRequest($request)->shouldReturn($promise); |
| 53 | + } |
| 54 | + |
| 55 | + public function it_throw_exception_if_no_more_enable_client(HttpClient $client, RequestInterface $request) |
| 56 | + { |
| 57 | + $this->addHttpClient($client); |
| 58 | + $client->sendRequest($request)->willThrow('Http\Client\Exception\HttpException'); |
| 59 | + |
| 60 | + $this->shouldThrow('Http\Client\Exception\HttpException')->duringSendRequest($request); |
| 61 | + $this->shouldThrow('Http\Client\Common\Exception\HttpClientNotFoundException')->duringSendRequest($request); |
| 62 | + } |
| 63 | + |
| 64 | + public function it_reenable_client(HttpClient $client, RequestInterface $request) |
| 65 | + { |
| 66 | + $this->addHttpClient(new HttpClientPoolItem($client->getWrappedObject(), 0)); |
| 67 | + $client->sendRequest($request)->willThrow('Http\Client\Exception\HttpException'); |
| 68 | + |
| 69 | + $this->shouldThrow('Http\Client\Exception\HttpException')->duringSendRequest($request); |
| 70 | + $this->shouldThrow('Http\Client\Exception\HttpException')->duringSendRequest($request); |
| 71 | + } |
| 72 | + |
| 73 | + public function it_uses_the_lowest_request_client(HttpClientPoolItem $client1, HttpClientPoolItem $client2, RequestInterface $request, ResponseInterface $response) |
| 74 | + { |
| 75 | + if (extension_loaded('xdebug')) { |
| 76 | + throw new SkippingException('This test fail when xdebug is enable on PHP < 7'); |
| 77 | + } |
| 78 | + |
| 79 | + $this->addHttpClient($client1); |
| 80 | + $this->addHttpClient($client2); |
| 81 | + |
| 82 | + $client1->getSendingRequestCount()->willReturn(10); |
| 83 | + $client2->getSendingRequestCount()->willReturn(2); |
| 84 | + |
| 85 | + $client1->isDisabled()->willReturn(false); |
| 86 | + $client2->isDisabled()->willReturn(false); |
| 87 | + |
| 88 | + $client1->sendRequest($request)->shouldNotBeCalled(); |
| 89 | + $client2->sendRequest($request)->willReturn($response); |
| 90 | + |
| 91 | + $this->sendRequest($request)->shouldReturn($response); |
| 92 | + } |
| 93 | +} |
0 commit comments