|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace spec\Http\Client\Promise; |
| 4 | + |
| 5 | +use Http\Client\Exception; |
| 6 | +use Http\Client\Exception\TransferException; |
| 7 | +use Http\Promise\Promise; |
| 8 | +use PhpSpec\ObjectBehavior; |
| 9 | +use Prophecy\Argument; |
| 10 | +use Psr\Http\Message\ResponseInterface; |
| 11 | + |
| 12 | +class HttpRejectedPromiseSpec extends ObjectBehavior |
| 13 | +{ |
| 14 | + function let() |
| 15 | + { |
| 16 | + $this->beConstructedWith(new TransferException()); |
| 17 | + } |
| 18 | + |
| 19 | + function it_is_initializable() |
| 20 | + { |
| 21 | + $this->shouldHaveType('Http\Client\Promise\HttpRejectedPromise'); |
| 22 | + } |
| 23 | + |
| 24 | + function it_is_a_promise() |
| 25 | + { |
| 26 | + $this->shouldImplement('Http\Promise\Promise'); |
| 27 | + } |
| 28 | + |
| 29 | + function it_returns_a_fulfilled_promise(ResponseInterface $response) |
| 30 | + { |
| 31 | + $exception = new TransferException(); |
| 32 | + $this->beConstructedWith($exception); |
| 33 | + |
| 34 | + $promise = $this->then(null, function (Exception $exceptionReceived) use($exception, $response) { |
| 35 | + return $response->getWrappedObject(); |
| 36 | + }); |
| 37 | + |
| 38 | + $promise->shouldHaveType('Http\Promise\Promise'); |
| 39 | + $promise->shouldHaveType('Http\Client\Promise\HttpFulfilledPromise'); |
| 40 | + $promise->getState()->shouldReturn(Promise::FULFILLED); |
| 41 | + $promise->wait()->shouldReturnAnInstanceOf('Psr\Http\Message\ResponseInterface'); |
| 42 | + } |
| 43 | + |
| 44 | + function it_returns_a_rejected_promise() |
| 45 | + { |
| 46 | + $exception = new TransferException(); |
| 47 | + $this->beConstructedWith($exception); |
| 48 | + |
| 49 | + $promise = $this->then(null, function (Exception $exceptionReceived) use($exception) { |
| 50 | + if (Argument::is($exceptionReceived)->scoreArgument($exception)) { |
| 51 | + throw $exception; |
| 52 | + } |
| 53 | + }); |
| 54 | + |
| 55 | + $promise->shouldHaveType('Http\Promise\Promise'); |
| 56 | + $promise->shouldHaveType('Http\Client\Promise\HttpRejectedPromise'); |
| 57 | + $promise->getState()->shouldReturn(Promise::REJECTED); |
| 58 | + $promise->shouldThrow($exception)->duringWait(); |
| 59 | + } |
| 60 | + |
| 61 | + function it_returns_itself_when_no_on_rejected_callback_is_passed() |
| 62 | + { |
| 63 | + $this->then()->shouldReturn($this); |
| 64 | + } |
| 65 | + |
| 66 | + function it_is_in_rejected_state() |
| 67 | + { |
| 68 | + $this->getState()->shouldReturn(Promise::REJECTED); |
| 69 | + } |
| 70 | + |
| 71 | + function it_returns_an_exception() |
| 72 | + { |
| 73 | + $exception = new TransferException(); |
| 74 | + |
| 75 | + $this->beConstructedWith($exception); |
| 76 | + $this->shouldThrow($exception)->duringWait(); |
| 77 | + } |
| 78 | + |
| 79 | + function it_does_not_unwrap_a_value() |
| 80 | + { |
| 81 | + $this->shouldNotThrow('Http\Client\Exception')->duringWait(false); |
| 82 | + } |
| 83 | + |
| 84 | + function it_throws_not_http_exception() |
| 85 | + { |
| 86 | + $this->shouldThrow('Exception')->duringThen(null, function () { |
| 87 | + throw new \Exception(); |
| 88 | + }); |
| 89 | + } |
| 90 | +} |
0 commit comments