Skip to content

Commit 1e65565

Browse files
committed
fix(client): handle legacy psr-18 client compatibility
- modify client instance checks in `Configuration.php` to handle psr-18 clients correctly - update `testWorksWithPsr18Client` to wrap client with required factories - add new test `testWorksWithLegacyPsr18Client` for backward compatibility - simplify array key check using `isset` instead of `array_key_exists`
1 parent adc2386 commit 1e65565

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/Lib/Configuration.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ public function __construct(array $config)
104104
$this->logger = new Logger('typesense');
105105
$this->logger->pushHandler(new StreamHandler('php://stdout', $this->logLevel));
106106

107-
if (true === \array_key_exists('client', $config)) {
108-
if ($config['client'] instanceof HttpMethodsClient) {
107+
if (isset($config['client'])) {
108+
if ($config['client'] instanceof HttpMethodsClient || $config['client'] instanceof ClientInterface) {
109109
$this->client = $config['client'];
110-
} elseif ($config['client'] instanceof HttpClient || $config['client'] instanceof ClientInterface) {
110+
} elseif ($config['client'] instanceof HttpClient) {
111111
$this->client = new HttpMethodsClient(
112112
$config['client'],
113113
Psr17FactoryDiscovery::findRequestFactory(),

tests/Feature/HttpClientsTest.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,13 @@ public function testWorksWithDefaultClient(): void
3838
public function testWorksWithPsr18Client(): void
3939
{
4040
$httpClient = new Psr18Client();
41-
$config = array_merge($this->baseConfig, ['client' => $httpClient]);
41+
$wrappedClient = new HttpMethodsClient(
42+
$httpClient,
43+
Psr17FactoryDiscovery::findRequestFactory(),
44+
Psr17FactoryDiscovery::findStreamFactory()
45+
);
4246

47+
$config = array_merge($this->baseConfig, ['client' => $wrappedClient]);
4348
$client = new Client($config);
4449
$response = $client->health->retrieve();
4550
$this->assertIsBool($response['ok']);
@@ -60,6 +65,14 @@ public function testWorksWithHttpMethodsClient(): void
6065
$this->assertIsBool($response['ok']);
6166
}
6267

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+
6376
public function testRejectsInvalidClient(): void
6477
{
6578
$this->expectException(ConfigError::class);

0 commit comments

Comments
 (0)