4
4
5
5
namespace tests \Http \Client \Common ;
6
6
7
+ use Http \Client \Common \Plugin ;
8
+ use Http \Client \Common \Plugin \HeaderAppendPlugin ;
9
+ use Http \Client \Common \Plugin \RedirectPlugin ;
10
+ use Http \Client \Common \PluginClient ;
11
+ use Http \Client \HttpAsyncClient ;
12
+ use Http \Client \Promise \HttpFulfilledPromise ;
13
+ use Http \Promise \Promise ;
14
+ use Nyholm \Psr7 \Request ;
15
+ use Nyholm \Psr7 \Response ;
7
16
use PHPUnit \Framework \TestCase ;
17
+ use Psr \Http \Client \ClientInterface ;
18
+ use Psr \Http \Message \RequestInterface ;
19
+ use Psr \Http \Message \ResponseInterface ;
8
20
9
21
class PluginClientTest extends TestCase
10
22
{
11
- private $ syncClient ;
12
- private $ asyncClient ;
23
+ /**
24
+ * @dataProvider clientAndMethodProvider
25
+ */
26
+ public function testRestartChain (PluginClient $ client , string $ method , string $ returnType )
27
+ {
28
+ $ request = new Request ('GET ' , 'https://example.com ' );
29
+ $ result = call_user_func ([$ client , $ method ], $ request );
30
+
31
+ $ this ->assertInstanceOf ($ returnType , $ result );
32
+ }
13
33
14
- protected function setUp ()
34
+ public function clientAndMethodProvider ()
15
35
{
36
+ $ syncClient = new class implements ClientInterface {
37
+ public function sendRequest (RequestInterface $ request ): ResponseInterface
38
+ {
39
+ return new Response ();
40
+ }
41
+ };
42
+
43
+ $ asyncClient = new class implements HttpAsyncClient {
44
+ public function sendAsyncRequest (RequestInterface $ request )
45
+ {
46
+ return new HttpFulfilledPromise (new Response ());
47
+ }
48
+ };
49
+
50
+ $ headerAppendPlugin = new HeaderAppendPlugin (['Content-Type ' =>'text/html ' ]);
51
+ $ redirectPlugin = new RedirectPlugin ();
52
+ $ restartOncePlugin = new class implements Plugin {
53
+ private $ firstRun = true ;
54
+
55
+ public function handleRequest (RequestInterface $ request , callable $ next , callable $ first ): Promise
56
+ {
57
+ if ($ this ->firstRun ) {
58
+ $ this ->firstRun = false ;
59
+ return $ first ($ request )->wait ();
60
+ }
61
+ return $ next ($ request );
62
+ }
63
+ };
64
+
65
+ $ plugins = [$ headerAppendPlugin , $ restartOncePlugin , $ redirectPlugin ];
66
+
67
+ $ pluginClient = new PluginClient ($ syncClient , $ plugins );
68
+ yield [$ pluginClient , 'sendRequest ' , ResponseInterface::class];
69
+ yield [$ pluginClient , 'sendAsyncRequest ' , Promise::class];
16
70
71
+ // Async
72
+ $ pluginClient = new PluginClient ($ asyncClient , $ plugins );
73
+ yield [$ pluginClient , 'sendRequest ' , ResponseInterface::class];
74
+ yield [$ pluginClient , 'sendAsyncRequest ' , Promise::class];
17
75
}
18
76
}
0 commit comments