|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace tests\Http\Client\Common; |
| 6 | + |
| 7 | +use Closure; |
| 8 | +use Http\Client\Common\Plugin; |
| 9 | +use Http\Client\Common\PluginClient; |
| 10 | +use Http\Client\Common\PluginClientBuilder; |
| 11 | +use Http\Client\HttpAsyncClient; |
| 12 | +use Http\Client\HttpClient; |
| 13 | +use PHPUnit\Framework\TestCase; |
| 14 | + |
| 15 | +class PluginClientBuilderTest extends TestCase |
| 16 | +{ |
| 17 | + /** @dataProvider clientProvider */ |
| 18 | + public function testPriority(string $client): void |
| 19 | + { |
| 20 | + $builder = new PluginClientBuilder(); |
| 21 | + |
| 22 | + $plugins = [ |
| 23 | + 10 => $this->prophesize(Plugin::class)->reveal(), |
| 24 | + -10 => $this->prophesize(Plugin::class)->reveal(), |
| 25 | + 0 => $this->prophesize(Plugin::class)->reveal(), |
| 26 | + ]; |
| 27 | + |
| 28 | + foreach ($plugins as $priority => $plugin) { |
| 29 | + $builder->addPlugin($plugin, $priority); |
| 30 | + } |
| 31 | + |
| 32 | + $client = $this->prophesize($client)->reveal(); |
| 33 | + $client = $builder->createClient($client); |
| 34 | + |
| 35 | + $closure = Closure::bind( |
| 36 | + function (): array { |
| 37 | + return $this->plugins; |
| 38 | + }, |
| 39 | + $client, |
| 40 | + PluginClient::class |
| 41 | + ); |
| 42 | + |
| 43 | + $plugged = $closure(); |
| 44 | + |
| 45 | + $expected = $plugins; |
| 46 | + krsort($expected); |
| 47 | + $expected = array_values($expected); |
| 48 | + |
| 49 | + $this->assertSame($expected, $plugged); |
| 50 | + } |
| 51 | + |
| 52 | + public function clientProvider(): iterable |
| 53 | + { |
| 54 | + yield 'sync\'d http client' => [HttpClient::class]; |
| 55 | + yield 'async\'d http client' => [HttpAsyncClient::class]; |
| 56 | + } |
| 57 | +} |
0 commit comments