7
7
use Http \Client \Common \FlexibleHttpClient ;
8
8
use Http \Client \HttpAsyncClient ;
9
9
use Http \Client \HttpClient ;
10
+ use Http \Promise \Promise ;
11
+ use Psr \Http \Message \RequestInterface ;
12
+ use Psr \Http \Message \ResponseInterface ;
10
13
use PhpSpec \ObjectBehavior ;
11
14
use Prophecy \Prophet ;
12
15
13
16
class FlexibleHttpClientSpec extends ObjectBehavior
14
17
{
15
18
function let (HttpClient $ httpClient )
16
19
{
17
- $ this ->beAnInstanceOf (
18
- 'spec\Http\Client\Common\FlexibleHttpClientStub ' , [
19
- $ httpClient
20
- ]
21
- );
20
+ $ this ->beConstructedWith ($ httpClient );
22
21
}
23
22
24
23
function it_is_initializable ()
@@ -38,55 +37,58 @@ function it_is_an_async_http_client()
38
37
39
38
function it_throw_exception_if_invalid_client ()
40
39
{
41
- $ httpClient = null ;
42
- $ this ->beConstructedWith ($ httpClient );
40
+ $ this ->beConstructedWith (null );
43
41
44
42
$ this ->shouldThrow ('\LogicException ' )->duringInstantiation ();
45
43
}
46
44
47
- function it_emulates_an_async_client (HttpClient $ httpClient )
48
- {
45
+ function it_emulates_an_async_client (
46
+ HttpClient $ httpClient ,
47
+ RequestInterface $ syncRequest ,
48
+ ResponseInterface $ syncResponse ,
49
+ RequestInterface $ asyncRequest ,
50
+ ResponseInterface $ asyncResponse
51
+ ) {
49
52
$ this ->beConstructedWith ($ httpClient );
50
53
51
- $ this ->getClient ()->shouldImplement ('Http\Client\HttpClient ' );
52
- $ this ->getAsyncClient ()->shouldImplement ('Http\Client\HttpAsyncClient ' );
53
- $ this ->getClient ()->shouldNotImplement ('Http\Client\Common\EmulatedHttpClient ' );
54
- $ this ->getAsyncClient ()->shouldImplement ('Http\Client\Common\EmulatedHttpAsyncClient ' );
55
- }
54
+ $ httpClient ->sendRequest ($ syncRequest )->willReturn ($ syncResponse );
55
+ $ httpClient ->sendRequest ($ asyncRequest )->willReturn ($ asyncResponse );
56
56
57
- function it_emulates_a_client (HttpAsyncClient $ httpAsyncClient )
58
- {
59
- $ this ->beConstructedWith ($ httpAsyncClient );
57
+ $ this ->sendRequest ($ syncRequest )->shouldReturn ($ syncResponse );
58
+ $ promise = $ this ->sendAsyncRequest ($ asyncRequest );
60
59
61
- $ this ->getClient ()->shouldImplement ('Http\Client\HttpClient ' );
62
- $ this ->getAsyncClient ()->shouldImplement ('Http\Client\HttpAsyncClient ' );
63
- $ this ->getClient ()->shouldImplement ('Http\Client\Common\EmulatedHttpClient ' );
64
- $ this ->getAsyncClient ()->shouldNotImplement ('Http\Client\EmulatedHttpAsyncClient ' );
60
+ $ promise ->shouldHaveType ('Http\Promise\Promise ' );
61
+ $ promise ->wait ()->shouldReturn ($ asyncResponse );
65
62
}
66
63
67
- function it_does_not_emulates_a_client ()
68
- {
69
- $ prophet = new Prophet ();
70
- $ httpClient = $ prophet ->prophesize ();
71
- $ httpClient ->willImplement ('Http\Client\HttpClient ' );
72
- $ httpClient ->willImplement ('Http\Client\HttpAsyncClient ' );
64
+ function it_emulates_a_client (
65
+ HttpAsyncClient $ httpAsyncClient ,
66
+ RequestInterface $ asyncRequest ,
67
+ Promise $ promise ,
68
+ RequestInterface $ syncRequest ,
69
+ Promise $ syncPromise ,
70
+ ResponseInterface $ syncResponse
71
+ ) {
72
+ $ this ->beConstructedWith ($ httpAsyncClient );
73
73
74
- $ this ->beConstructedWith ($ httpClient );
74
+ $ httpAsyncClient ->sendAsyncRequest ($ asyncRequest )->willReturn ($ promise );
75
+ $ httpAsyncClient ->sendAsyncRequest ($ syncRequest )->willReturn ($ syncPromise );
76
+ $ syncPromise ->wait ()->willReturn ($ syncResponse );
75
77
76
- $ this ->getClient ( )->shouldNotImplement ( ' Http\Client\Common\EmulatedHttpClient ' );
77
- $ this ->getAsyncClient ( )->shouldNotImplement ( ' Http\Client\Common\EmulatedHttpAsyncClient ' );
78
+ $ this ->sendAsyncRequest ( $ asyncRequest )->shouldReturn ( $ promise );
79
+ $ this ->sendRequest ( $ syncRequest )->shouldReturn ( $ syncResponse );
78
80
}
79
- }
80
81
81
- class FlexibleHttpClientStub extends FlexibleHttpClient
82
- {
83
- public function getClient ()
82
+ function it_does_not_emulate_a_client (FlexibleHttpClient $ client , RequestInterface $ syncRequest , RequestInterface $ asyncRequest )
84
83
{
85
- return $ this ->httpClient ;
86
- }
84
+ $ client ->sendRequest ($ syncRequest )->shouldBeCalled ();
85
+ $ client ->sendRequest ($ asyncRequest )->shouldNotBeCalled ();
86
+ $ client ->sendAsyncRequest ($ asyncRequest )->shouldBeCalled ();
87
+ $ client ->sendAsyncRequest ($ syncRequest )->shouldNotBeCalled ();
87
88
88
- public function getAsyncClient ()
89
- {
90
- return $ this ->httpAsyncClient ;
89
+ $ this ->beConstructedWith ($ client );
90
+
91
+ $ this ->sendRequest ($ syncRequest );
92
+ $ this ->sendAsyncRequest ($ asyncRequest );
91
93
}
92
94
}
0 commit comments