-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathPluginChainTest.php
94 lines (72 loc) · 2.67 KB
/
PluginChainTest.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
declare(strict_types=1);
namespace Tests\Http\Client\Common;
use Http\Client\Common\Exception\LoopException;
use Http\Client\Common\Plugin;
use Http\Client\Common\PluginChain;
use Http\Promise\Promise;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Psr\Http\Message\RequestInterface;
class PluginChainTest extends TestCase
{
use ProphecyTrait;
private function createPlugin(callable $func): Plugin
{
return new class($func) implements Plugin {
public $func;
public function __construct(callable $func)
{
$this->func = $func;
}
public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise
{
($this->func)($request, $next, $first);
return $next($request);
}
};
}
public function testChainShouldInvokePluginsInReversedOrder(): void
{
$pluginOrderCalls = [];
$plugin1 = $this->createPlugin(static function () use (&$pluginOrderCalls) {
$pluginOrderCalls[] = 'plugin1';
});
$plugin2 = $this->createPlugin(static function () use (&$pluginOrderCalls) {
$pluginOrderCalls[] = 'plugin2';
});
$request = $this->prophesize(RequestInterface::class);
$responsePromise = $this->prophesize(Promise::class);
$clientCallable = static function () use ($responsePromise) {
return $responsePromise->reveal();
};
$pluginOrderCalls = [];
$plugins = [
$plugin1,
$plugin2,
];
$pluginChain = new PluginChain($plugins, $clientCallable);
$result = $pluginChain($request->reveal());
$this->assertSame($responsePromise->reveal(), $result);
$this->assertSame(['plugin1', 'plugin2'], $pluginOrderCalls);
}
public function testShouldThrowLoopExceptionOnMaxRestarts(): void
{
$this->expectException(LoopException::class);
$request = $this->prophesize(RequestInterface::class);
$responsePromise = $this->prophesize(Promise::class);
$calls = 0;
$clientCallable = static function () use ($responsePromise, &$calls) {
++$calls;
return $responsePromise->reveal();
};
$pluginChain = new PluginChain([], $clientCallable, ['max_restarts' => 2]);
$pluginChain($request->reveal());
$this->assertSame(1, $calls);
$pluginChain($request->reveal());
$this->assertSame(2, $calls);
$pluginChain($request->reveal());
$this->assertSame(3, $calls);
$pluginChain($request->reveal());
}
}