Skip to content

Commit 67ce613

Browse files
committed
Add specific http promise
1 parent 5183cf8 commit 67ce613

File tree

5 files changed

+282
-0
lines changed

5 files changed

+282
-0
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Change Log
22

3+
## 1.1.0 - Unreleased
4+
5+
- Added HttpFulfilledPromise and HttpRejectedPromise which respect the HttpAsyncClient interface
36

47
## 1.0.0 - 2016-01-26
58

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace spec\Http\Client\Promise;
4+
5+
use Http\Client\Exception\TransferException;
6+
use Http\Promise\Promise;
7+
use PhpSpec\ObjectBehavior;
8+
use Psr\Http\Message\ResponseInterface;
9+
10+
class HttpFulfilledPromiseSpec extends ObjectBehavior
11+
{
12+
function let(ResponseInterface $response)
13+
{
14+
$this->beConstructedWith($response);
15+
}
16+
17+
function it_is_initializable()
18+
{
19+
$this->shouldHaveType('Http\Client\Promise\HttpFulfilledPromise');
20+
}
21+
22+
function it_is_a_promise()
23+
{
24+
$this->shouldImplement('Http\Promise\Promise');
25+
}
26+
27+
function it_returns_a_http_fulfilled_promise()
28+
{
29+
$promise = $this->then(function ($result) {
30+
return $result;
31+
});
32+
$promise->shouldHaveType('Http\Promise\Promise');
33+
$promise->shouldHaveType('Http\Client\Promise\HttpFulfilledPromise');
34+
$promise->getState()->shouldReturn(Promise::FULFILLED);
35+
$promise->wait()->shouldReturnAnInstanceOf('Psr\Http\Message\ResponseInterface');
36+
}
37+
38+
function it_returns_a_http_rejected_promise()
39+
{
40+
$exception = new TransferException();
41+
$promise = $this->then(function () use ($exception) {
42+
throw $exception;
43+
});
44+
$promise->shouldHaveType('Http\Promise\Promise');
45+
$promise->shouldHaveType('Http\Client\Promise\HttpRejectedPromise');
46+
$promise->getState()->shouldReturn(Promise::REJECTED);
47+
$promise->shouldThrow($exception)->duringWait();
48+
}
49+
50+
function it_returns_itself_when_no_on_fulfilled_callback_is_passed()
51+
{
52+
$this->then()->shouldReturn($this);
53+
}
54+
55+
function it_is_in_fulfilled_state()
56+
{
57+
$this->getState()->shouldReturn(Promise::FULFILLED);
58+
}
59+
60+
function it_has_a_response()
61+
{
62+
$this->wait()->shouldReturnAnInstanceOf('Psr\Http\Message\ResponseInterface');
63+
}
64+
65+
function it_does_not_unwrap_a_response()
66+
{
67+
$this->wait(false)->shouldNotReturnAnInstanceOf('Psr\Http\Message\ResponseInterface');
68+
}
69+
70+
function it_throws_not_http_exception()
71+
{
72+
$this->shouldThrow('Exception')->duringThen(function () {
73+
throw new \Exception();
74+
}, null);
75+
}
76+
}
+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
}

src/Promise/HttpFulfilledPromise.php

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Http\Client\Promise;
4+
5+
use Http\Client\Exception;
6+
use Http\Promise\Promise;
7+
use Psr\Http\Message\ResponseInterface;
8+
9+
final class HttpFulfilledPromise implements Promise
10+
{
11+
/**
12+
* @var ResponseInterface
13+
*/
14+
private $response;
15+
16+
/**
17+
* @param ResponseInterface $response
18+
*/
19+
public function __construct(ResponseInterface $response)
20+
{
21+
$this->response = $response;
22+
}
23+
24+
/**
25+
* {@inheritdoc}
26+
*/
27+
public function then(callable $onFulfilled = null, callable $onRejected = null)
28+
{
29+
if (null === $onFulfilled) {
30+
return $this;
31+
}
32+
33+
try {
34+
return new self($onFulfilled($this->response));
35+
} catch (Exception $e) {
36+
return new HttpRejectedPromise($e);
37+
}
38+
}
39+
40+
/**
41+
* {@inheritdoc}
42+
*/
43+
public function getState()
44+
{
45+
return Promise::FULFILLED;
46+
}
47+
48+
/**
49+
* {@inheritdoc}
50+
*/
51+
public function wait($unwrap = true)
52+
{
53+
if ($unwrap) {
54+
return $this->response;
55+
}
56+
}
57+
}

src/Promise/HttpRejectedPromise.php

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace Http\Client\Promise;
4+
5+
use Http\Client\Exception;
6+
use Http\Promise\Promise;
7+
8+
final class HttpRejectedPromise implements Promise
9+
{
10+
/**
11+
* @var Exception
12+
*/
13+
private $exception;
14+
15+
/**
16+
* @param Exception $exception
17+
*/
18+
public function __construct(Exception $exception)
19+
{
20+
$this->exception = $exception;
21+
}
22+
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
public function then(callable $onFulfilled = null, callable $onRejected = null)
27+
{
28+
if (null === $onRejected) {
29+
return $this;
30+
}
31+
32+
try {
33+
return new HttpFulfilledPromise($onRejected($this->exception));
34+
} catch (Exception $e) {
35+
return new self($e);
36+
}
37+
}
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
public function getState()
43+
{
44+
return Promise::REJECTED;
45+
}
46+
47+
/**
48+
* {@inheritdoc}
49+
*/
50+
public function wait($unwrap = true)
51+
{
52+
if ($unwrap) {
53+
throw $this->exception;
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)