|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tests\Unit\Support; |
| 6 | + |
| 7 | +use GuzzleHttp\Client; |
| 8 | +use GuzzleHttp\HandlerStack; |
| 9 | +use Hyperf\Context\ApplicationContext; |
| 10 | +use Hyperf\Contract\ConfigInterface; |
| 11 | +use Hyperf\Guzzle\ClientFactory; |
| 12 | +use Hyperf\Guzzle\CoroutineHandler; |
| 13 | +use Hyperf\Guzzle\PoolHandler; |
| 14 | +use Hyperf\OpenTelemetry\Support\HyperfGuzzle; |
| 15 | +use Mockery as m; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | +use Psr\Container\ContainerInterface; |
| 18 | +use ReflectionAttribute; |
| 19 | +use ReflectionProperty; |
| 20 | + |
| 21 | +/** |
| 22 | + * @internal |
| 23 | + */ |
| 24 | +class HyperfGuzzleTest extends TestCase |
| 25 | +{ |
| 26 | + private ?ContainerInterface $container = null; |
| 27 | + |
| 28 | + protected function setUp(): void |
| 29 | + { |
| 30 | + parent::setUp(); |
| 31 | + |
| 32 | + if (ApplicationContext::hasContainer()) { |
| 33 | + $this->container = ApplicationContext::getContainer(); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + protected function tearDown(): void |
| 38 | + { |
| 39 | + m::close(); |
| 40 | + |
| 41 | + if ($this->container !== null) { |
| 42 | + ApplicationContext::setContainer($this->container); |
| 43 | + } |
| 44 | + parent::tearDown(); |
| 45 | + } |
| 46 | + |
| 47 | + public function testAvailableReturnsTrueWhenCoroutineHandlerExistsAndContainerAvailable() |
| 48 | + { |
| 49 | + $container = m::mock(ContainerInterface::class); |
| 50 | + ApplicationContext::setContainer($container); |
| 51 | + |
| 52 | + $hyperfGuzzle = new HyperfGuzzle(); |
| 53 | + $this->assertTrue($hyperfGuzzle->available()); |
| 54 | + } |
| 55 | + |
| 56 | + public function testCreateReturnsClientWithCoroutineHandlerWhenPoolConfigIsEmpty() |
| 57 | + { |
| 58 | + $container = m::mock(ContainerInterface::class); |
| 59 | + $config = m::mock(ConfigInterface::class); |
| 60 | + |
| 61 | + $config->shouldReceive('get') |
| 62 | + ->with('open-telemetry.otlp_http.pool', []) |
| 63 | + ->andReturn([]); |
| 64 | + |
| 65 | + $container->shouldReceive('get') |
| 66 | + ->with(ConfigInterface::class) |
| 67 | + ->andReturn($config); |
| 68 | + |
| 69 | + ApplicationContext::setContainer($container); |
| 70 | + |
| 71 | + $hyperfGuzzle = new HyperfGuzzle(); |
| 72 | + $options = ['timeout' => 30]; |
| 73 | + $client = $hyperfGuzzle->create($options); |
| 74 | + |
| 75 | + $this->assertInstanceOf(Client::class, $client); |
| 76 | + |
| 77 | + /** @var Client $client */ |
| 78 | + $clientConfig = $client->getConfig(); |
| 79 | + |
| 80 | + $this->assertIsArray($clientConfig); |
| 81 | + $this->assertInstanceOf(HandlerStack::class, $clientConfig['handler']); |
| 82 | + |
| 83 | + $property = new ReflectionProperty(HandlerStack::class, 'handler'); |
| 84 | + |
| 85 | + $this->assertInstanceOf(CoroutineHandler::class, $property->getValue($clientConfig['handler'])); |
| 86 | + } |
| 87 | + |
| 88 | + public function testCreateReturnsClientWithPoolHandlerWhenPoolConfigIsProvided() |
| 89 | + { |
| 90 | + $container = m::mock(ContainerInterface::class); |
| 91 | + $config = m::mock(ConfigInterface::class); |
| 92 | + $poolHandler = m::mock(PoolHandler::class); |
| 93 | + |
| 94 | + $poolConfig = [ |
| 95 | + 'min_connections' => 1, |
| 96 | + 'max_connections' => 10, |
| 97 | + ]; |
| 98 | + |
| 99 | + $config->shouldReceive('get') |
| 100 | + ->with('open-telemetry.otlp_http.pool', []) |
| 101 | + ->andReturn($poolConfig); |
| 102 | + |
| 103 | + $container->shouldReceive('get') |
| 104 | + ->with(ConfigInterface::class) |
| 105 | + ->andReturn($config); |
| 106 | + |
| 107 | + $container->shouldReceive('make') |
| 108 | + ->with(PoolHandler::class, ['option' => $poolConfig]) |
| 109 | + ->andReturn($poolHandler); |
| 110 | + |
| 111 | + ApplicationContext::setContainer($container); |
| 112 | + |
| 113 | + $hyperfGuzzle = new HyperfGuzzle(); |
| 114 | + $options = ['timeout' => 30]; |
| 115 | + $client = $hyperfGuzzle->create($options); |
| 116 | + |
| 117 | + $this->assertInstanceOf(Client::class, $client); |
| 118 | + |
| 119 | + /** @var Client $client */ |
| 120 | + $clientConfig = $client->getConfig(); |
| 121 | + |
| 122 | + $this->assertIsArray($clientConfig); |
| 123 | + $this->assertInstanceOf(HandlerStack::class, $clientConfig['handler']); |
| 124 | + |
| 125 | + $property = new ReflectionProperty(HandlerStack::class, 'handler'); |
| 126 | + |
| 127 | + $this->assertInstanceOf(PoolHandler::class, $property->getValue($clientConfig['handler'])); |
| 128 | + } |
| 129 | + |
| 130 | + public function testCreateMergesOptionsWithHandler() |
| 131 | + { |
| 132 | + $container = m::mock(ContainerInterface::class); |
| 133 | + $config = m::mock(ConfigInterface::class); |
| 134 | + |
| 135 | + $config->shouldReceive('get') |
| 136 | + ->with('open-telemetry.otlp_http.pool', []) |
| 137 | + ->andReturn([]); |
| 138 | + |
| 139 | + $container->shouldReceive('get') |
| 140 | + ->with(ConfigInterface::class) |
| 141 | + ->andReturn($config); |
| 142 | + |
| 143 | + ApplicationContext::setContainer($container); |
| 144 | + |
| 145 | + $hyperfGuzzle = new HyperfGuzzle(); |
| 146 | + $options = [ |
| 147 | + 'timeout' => 30, |
| 148 | + 'base_uri' => 'http://localhost', |
| 149 | + ]; |
| 150 | + |
| 151 | + /** @var Client $client */ |
| 152 | + $client = $hyperfGuzzle->create($options); |
| 153 | + |
| 154 | + $this->assertInstanceOf(Client::class, $client); |
| 155 | + |
| 156 | + $config = $client->getConfig(); |
| 157 | + $this->assertEquals(30, $config['timeout']); |
| 158 | + $this->assertEquals('http://localhost', $config['base_uri']); |
| 159 | + $this->assertInstanceOf(HandlerStack::class, $config['handler']); |
| 160 | + } |
| 161 | + |
| 162 | + public function testCreateWithEmptyOptions() |
| 163 | + { |
| 164 | + $container = m::mock(ContainerInterface::class); |
| 165 | + $config = m::mock(ConfigInterface::class); |
| 166 | + |
| 167 | + $config->shouldReceive('get') |
| 168 | + ->with('open-telemetry.otlp_http.pool', []) |
| 169 | + ->andReturn([]); |
| 170 | + |
| 171 | + $container->shouldReceive('get') |
| 172 | + ->with(ConfigInterface::class) |
| 173 | + ->andReturn($config); |
| 174 | + |
| 175 | + ApplicationContext::setContainer($container); |
| 176 | + |
| 177 | + $hyperfGuzzle = new HyperfGuzzle(); |
| 178 | + |
| 179 | + /** @var Client $client */ |
| 180 | + $client = $hyperfGuzzle->create(null); |
| 181 | + |
| 182 | + $this->assertInstanceOf(Client::class, $client); |
| 183 | + |
| 184 | + $clientConfig = $client->getConfig(); |
| 185 | + |
| 186 | + $this->assertIsArray($clientConfig); |
| 187 | + $this->assertInstanceOf(HandlerStack::class, $clientConfig['handler']); |
| 188 | + |
| 189 | + $property = new ReflectionProperty(HandlerStack::class, 'handler'); |
| 190 | + |
| 191 | + $this->assertInstanceOf(CoroutineHandler::class, $property->getValue($clientConfig['handler'])); |
| 192 | + } |
| 193 | +} |
0 commit comments