22
33namespace Http \Client \Tests ;
44
5+ use Http \Client \Exception ;
56use Http \Client \HttpAsyncClient ;
7+ use Http \Promise \Promise ;
68use PHPUnit \Framework \Attributes \DataProvider ;
79use PHPUnit \Framework \Attributes \Group ;
810
911abstract class HttpAsyncClientTest extends HttpBaseTest
1012{
11- /**
12- * @var HttpAsyncClient
13- */
14- protected $ httpAsyncClient ;
13+ protected HttpAsyncClient $ httpAsyncClient ;
1514
16- /**
17- * {@inheritdoc}
18- */
1915 protected function setUp (): void
2016 {
2117 $ this ->httpAsyncClient = $ this ->createHttpAsyncClient ();
2218 }
2319
24- /**
25- * {@inheritdoc}
26- */
2720 protected function tearDown (): void
2821 {
2922 unset($ this ->httpAdapter );
3023 }
3124
3225 abstract protected function createHttpAsyncClient (): HttpAsyncClient ;
3326
34- public function testSuccessiveCallMustUseResponseInterface ()
27+ public function testSuccessiveCallMustUseResponseInterface (): void
3528 {
36- $ request = self ::$ messageFactory ->createRequest (
37- ' GET ' ,
38- $ this -> getUri (),
39- $ this -> defaultHeaders
40- );
29+ $ request = self ::$ requestFactory ->createRequest (' GET ' , self :: getUri ());
30+ foreach ( self :: $ defaultHeaders as $ name => $ value ) {
31+ $ request = $ request -> withHeader ( $ name , $ value );
32+ }
33+
4134
4235 $ promise = $ this ->httpAsyncClient ->sendAsyncRequest ($ request );
43- $ this ->assertInstanceOf (' Http\ Promise\Promise ' , $ promise );
36+ $ this ->assertInstanceOf (Promise::class , $ promise );
4437
4538 $ response = null ;
4639 $ promise ->then ()->then ()->then (function ($ r ) use (&$ response ) {
4740 $ response = $ r ;
4841
49- return $ response ;
42+ return $ r ;
5043 });
5144
5245 $ promise ->wait (false );
@@ -58,23 +51,22 @@ public function testSuccessiveCallMustUseResponseInterface()
5851 );
5952 }
6053
61- public function testSuccessiveInvalidCallMustUseException ()
54+ public function testSuccessiveInvalidCallMustUseException (): void
6255 {
63- $ request = self ::$ messageFactory ->createRequest (
64- 'GET ' ,
65- $ this ->getInvalidUri (),
66- $ this ->defaultHeaders
67- );
56+ $ request = self ::$ requestFactory ->createRequest ('GET ' , $ this ->getInvalidUri ());
57+ foreach (self ::$ defaultHeaders as $ name => $ value ) {
58+ $ request = $ request ->withHeader ($ name , $ value );
59+ }
6860
6961 $ promise = $ this ->httpAsyncClient ->sendAsyncRequest ($ request );
70- $ this ->assertInstanceOf (' Http\ Promise\Promise ' , $ promise );
62+ $ this ->assertInstanceOf (Promise::class , $ promise );
7163
7264 $ exception = null ;
7365 $ response = null ;
7466 $ promise ->then ()->then ()->then (function ($ r ) use (&$ response ) {
7567 $ response = $ r ;
7668
77- return $ response ;
69+ return $ r ;
7870 }, function ($ e ) use (&$ exception ) {
7971 $ exception = $ e ;
8072
@@ -85,7 +77,7 @@ public function testSuccessiveInvalidCallMustUseException()
8577
8678 $ this ->assertNull ($ response );
8779 $ this ->assertNotNull ($ exception );
88- $ this ->assertInstanceOf (' \Http\Client\ Exception' , $ exception );
80+ $ this ->assertInstanceOf (Exception::class , $ exception );
8981 }
9082
9183 /**
@@ -94,21 +86,22 @@ public function testSuccessiveInvalidCallMustUseException()
9486 */
9587 #[DataProvider('requestProvider ' )]
9688 #[Group('integration ' )]
97- public function testAsyncSendRequest ($ method , $ uri , array $ headers , $ body )
89+ public function testAsyncSendRequest (string $ method , string $ uri , array $ headers , ? string $ body ): void
9890 {
9991 if (null != $ body ) {
10092 $ headers ['Content-Length ' ] = (string ) strlen ($ body );
10193 }
10294
103- $ request = self ::$ messageFactory ->createRequest (
104- $ method ,
105- $ uri ,
106- $ headers ,
107- $ body
108- );
95+ $ request = self ::$ requestFactory ->createRequest ($ method , $ uri );
96+ foreach ($ headers as $ name => $ value ) {
97+ $ request = $ request ->withHeader ($ name , $ value );
98+ }
99+ if (null !== $ body ) {
100+ $ request = $ request ->withBody (self ::$ streamFactory ->createStream ($ body ));
101+ }
109102
110103 $ promise = $ this ->httpAsyncClient ->sendAsyncRequest ($ request );
111- $ this ->assertInstanceOf (' Http\ Promise\Promise ' , $ promise );
104+ $ this ->assertInstanceOf (Promise::class , $ promise );
112105
113106 $ response = null ;
114107 $ promise ->then (function ($ r ) use (&$ response ) {
@@ -131,18 +124,17 @@ public function testAsyncSendRequest($method, $uri, array $headers, $body)
131124 * @group integration
132125 */
133126 #[Group('integration ' )]
134- public function testSendAsyncWithInvalidUri ()
127+ public function testSendAsyncWithInvalidUri (): void
135128 {
136- $ request = self ::$ messageFactory ->createRequest (
137- 'GET ' ,
138- $ this ->getInvalidUri (),
139- $ this ->defaultHeaders
140- );
129+ $ request = self ::$ requestFactory ->createRequest ('GET ' , $ this ->getInvalidUri ());
130+ foreach (self ::$ defaultHeaders as $ name => $ value ) {
131+ $ request = $ request ->withHeader ($ name , $ value );
132+ }
141133
142134 $ exception = null ;
143135 $ response = null ;
144136 $ promise = $ this ->httpAsyncClient ->sendAsyncRequest ($ request );
145- $ this ->assertInstanceOf (' Http\ Promise\Promise ' , $ promise );
137+ $ this ->assertInstanceOf (Promise::class , $ promise );
146138
147139 $ promise ->then (function ($ r ) use (&$ response ) {
148140 $ response = $ r ;
@@ -157,7 +149,7 @@ public function testSendAsyncWithInvalidUri()
157149
158150 $ this ->assertNull ($ response );
159151 $ this ->assertNotNull ($ exception );
160- $ this ->assertInstanceOf (' \Http\Client\ Exception' , $ exception );
152+ $ this ->assertInstanceOf (Exception::class , $ exception );
161153 }
162154
163155 /**
@@ -166,7 +158,7 @@ public function testSendAsyncWithInvalidUri()
166158 */
167159 #[Group('integration ' )]
168160 #[DataProvider('requestWithOutcomeProvider ' )]
169- public function testSendAsyncRequestWithOutcome ($ uriAndOutcome , $ protocolVersion , array $ headers , $ body )
161+ public function testSendAsyncRequestWithOutcome (array $ uriAndOutcome , string $ protocolVersion , array $ headers , ? string $ body ): void
170162 {
171163 if ('1.0 ' === $ protocolVersion ) {
172164 $ body = null ;
@@ -176,13 +168,14 @@ public function testSendAsyncRequestWithOutcome($uriAndOutcome, $protocolVersion
176168 $ headers ['Content-Length ' ] = (string ) strlen ($ body );
177169 }
178170
179- $ request = self ::$ messageFactory ->createRequest (
180- $ method = 'GET ' ,
181- $ uriAndOutcome [0 ],
182- $ headers ,
183- $ body ,
184- $ protocolVersion
185- );
171+ $ request = self ::$ requestFactory ->createRequest ($ method = 'GET ' , $ uriAndOutcome [0 ]);
172+ foreach ($ headers as $ name => $ value ) {
173+ $ request = $ request ->withHeader ($ name , $ value );
174+ }
175+ if (null !== $ body ) {
176+ $ request = $ request ->withBody (self ::$ streamFactory ->createStream ($ body ));
177+ }
178+ $ request = $ request ->withProtocolVersion ($ protocolVersion );
186179
187180 $ outcome = $ uriAndOutcome [1 ];
188181 $ outcome ['protocolVersion ' ] = $ protocolVersion ;
@@ -195,7 +188,7 @@ public function testSendAsyncRequestWithOutcome($uriAndOutcome, $protocolVersion
195188 return $ response ;
196189 });
197190
198- $ this ->assertInstanceOf (' Http\ Promise\Promise ' , $ promise );
191+ $ this ->assertInstanceOf (Promise::class , $ promise );
199192 $ promise ->wait ();
200193 $ this ->assertResponse (
201194 $ response ,
0 commit comments