Skip to content

Commit ec2b48a

Browse files
Fix async support (#300)
Signed-off-by: Kim Pepper <[email protected]> Co-authored-by: Romain Ruaud <[email protected]>
1 parent 14181fa commit ec2b48a

File tree

4 files changed

+70
-10
lines changed

4 files changed

+70
-10
lines changed

src/OpenSearch/HttpTransport.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function sendRequest(
3636
array $params = [],
3737
mixed $body = null,
3838
array $headers = [],
39-
): array|string|null {
39+
): iterable|string|null {
4040
$request = $this->createRequest($method, $uri, $params, $body, $headers);
4141
$response = $this->client->sendRequest($request);
4242
$statusCode = $response->getStatusCode();

src/OpenSearch/LegacyTransportWrapper.php

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
namespace OpenSearch;
3+
declare(strict_types=1);
44

5-
use GuzzleHttp\Ring\Future\FutureArrayInterface;
5+
namespace OpenSearch;
66

77
// @phpstan-ignore classConstant.deprecatedClass
88
@trigger_error(LegacyTransportWrapper::class . ' is deprecated in 2.4.0 and will be removed in 3.0.0.', E_USER_DEPRECATED);
@@ -28,13 +28,11 @@ public function sendRequest(
2828
array $params = [],
2929
mixed $body = null,
3030
array $headers = [],
31-
): array|string|null {
31+
): iterable|string|null {
3232
$promise = $this->transport->performRequest($method, $uri, $params, $body);
33-
$futureArray = $this->transport->resultOrFuture($promise);
34-
if ($futureArray instanceof FutureArrayInterface) {
35-
return $futureArray->wait();
36-
}
37-
return $futureArray;
33+
// Provide legacy support for options.
34+
$options = $headers;
35+
return $this->transport->resultOrFuture($promise, $options);
3836
}
3937

4038
}

src/OpenSearch/TransportInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ public function sendRequest(
2323
array $params = [],
2424
string|array|null $body = null,
2525
array $headers = [],
26-
): array|string|null;
26+
): iterable|string|null;
2727

2828
}

tests/LegacyTransportWrapperTest.php

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenSearch\Tests;
6+
7+
use GuzzleHttp\Ring\Future\FutureArray;
8+
use OpenSearch\ConnectionPool\AbstractConnectionPool;
9+
use OpenSearch\Connections\Connection;
10+
use OpenSearch\LegacyTransportWrapper;
11+
use OpenSearch\Transport;
12+
use PHPUnit\Framework\MockObject\MockObject;
13+
use PHPUnit\Framework\TestCase;
14+
use Psr\Log\LoggerInterface;
15+
use React\Promise\Deferred;
16+
17+
/**
18+
* Tests for the LegacyTransportWrapper class.
19+
*
20+
* @coversDefaultClass \OpenSearch\LegacyTransportWrapper
21+
* @group legacy
22+
* @deprecated in 2.4.2 and will be removed in 3.0.0.
23+
*/
24+
class LegacyTransportWrapperTest extends TestCase
25+
{
26+
private Connection&MockObject $connection;
27+
28+
private AbstractConnectionPool&MockObject $connectionPool;
29+
30+
private MockObject&LoggerInterface $logger;
31+
32+
public function setUp(): void
33+
{
34+
$this->logger = $this->createMock(LoggerInterface::class);
35+
$this->connectionPool = $this->createMock(AbstractConnectionPool::class);
36+
$this->connection = $this->createMock(Connection::class);
37+
}
38+
39+
public function testSuccess(): void
40+
{
41+
$deferred = new Deferred();
42+
$deferred->resolve(['foo' => 'bar']);
43+
$future = new FutureArray($deferred->promise());
44+
45+
$this->connection->method('performRequest')
46+
->willReturn($future);
47+
48+
$this->connectionPool->method('nextConnection')
49+
->willReturn($this->connection);
50+
51+
$transport = new Transport(1, $this->connectionPool, $this->logger);
52+
53+
$wrapper = new LegacyTransportWrapper($transport);
54+
55+
$response = $wrapper->sendRequest('GET', 'http://localhost:9200', [], null, []);
56+
57+
$this->assertIsIterable($response);
58+
59+
$this->assertEquals(['foo' => 'bar'], $response);
60+
}
61+
62+
}

0 commit comments

Comments
 (0)