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