|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Feature; |
| 4 | + |
| 5 | +use Tests\TestCase; |
| 6 | +use Http\Client\Common\HttpMethodsClient; |
| 7 | +use Http\Discovery\Psr17FactoryDiscovery; |
| 8 | +use Http\Discovery\Psr18ClientDiscovery; |
| 9 | +use Typesense\Exceptions\ConfigError; |
| 10 | +use Symfony\Component\HttpClient\Psr18Client; |
| 11 | +use Typesense\Client; |
| 12 | +use \stdClass; |
| 13 | + |
| 14 | +class HttpClientsTest extends TestCase |
| 15 | +{ |
| 16 | + private array $baseConfig; |
| 17 | + |
| 18 | + protected function setUp(): void |
| 19 | + { |
| 20 | + parent::setUp(); |
| 21 | + $this->baseConfig = [ |
| 22 | + 'api_key' => $_ENV['TYPESENSE_API_KEY'], |
| 23 | + 'nodes' => [[ |
| 24 | + 'host' => $_ENV['TYPESENSE_NODE_HOST'], |
| 25 | + 'port' => $_ENV['TYPESENSE_NODE_PORT'], |
| 26 | + 'protocol' => $_ENV['TYPESENSE_NODE_PROTOCOL'] |
| 27 | + ]] |
| 28 | + ]; |
| 29 | + } |
| 30 | + |
| 31 | + public function testWorksWithDefaultClient(): void |
| 32 | + { |
| 33 | + $client = new Client($this->baseConfig); |
| 34 | + $response = $client->health->retrieve(); |
| 35 | + $this->assertIsBool($response['ok']); |
| 36 | + } |
| 37 | + |
| 38 | + public function testWorksWithPsr18Client(): void |
| 39 | + { |
| 40 | + $httpClient = new Psr18Client(); |
| 41 | + $wrappedClient = new HttpMethodsClient( |
| 42 | + $httpClient, |
| 43 | + Psr17FactoryDiscovery::findRequestFactory(), |
| 44 | + Psr17FactoryDiscovery::findStreamFactory() |
| 45 | + ); |
| 46 | + |
| 47 | + $config = array_merge($this->baseConfig, ['client' => $wrappedClient]); |
| 48 | + $client = new Client($config); |
| 49 | + $response = $client->health->retrieve(); |
| 50 | + $this->assertIsBool($response['ok']); |
| 51 | + } |
| 52 | + |
| 53 | + public function testWorksWithHttpMethodsClient(): void |
| 54 | + { |
| 55 | + $httpClient = new HttpMethodsClient( |
| 56 | + Psr18ClientDiscovery::find(), |
| 57 | + Psr17FactoryDiscovery::findRequestFactory(), |
| 58 | + Psr17FactoryDiscovery::findStreamFactory() |
| 59 | + ); |
| 60 | + |
| 61 | + $config = array_merge($this->baseConfig, ['client' => $httpClient]); |
| 62 | + |
| 63 | + $client = new Client($config); |
| 64 | + $response = $client->health->retrieve(); |
| 65 | + $this->assertIsBool($response['ok']); |
| 66 | + } |
| 67 | + |
| 68 | + public function testWorksWithLegacyPsr18Client(): void |
| 69 | + { |
| 70 | + $httpClient = $this->createMock(\Psr\Http\Client\ClientInterface::class); |
| 71 | + $config = array_merge($this->baseConfig, ['client' => $httpClient]); |
| 72 | + $client = new Client($config); |
| 73 | + $this->assertInstanceOf(Client::class, $client); |
| 74 | + } |
| 75 | + |
| 76 | + public function testRejectsInvalidClient(): void |
| 77 | + { |
| 78 | + $this->expectException(ConfigError::class); |
| 79 | + $this->expectExceptionMessage('Client must implement PSR-18 ClientInterface or Http\Client\HttpClient'); |
| 80 | + |
| 81 | + $config = array_merge($this->baseConfig, ['client' => new stdClass()]); |
| 82 | + new Client($config); |
| 83 | + } |
| 84 | +} |
0 commit comments