Skip to content

Commit

Permalink
Fix async support (#300)
Browse files Browse the repository at this point in the history
Signed-off-by: Kim Pepper <[email protected]>
Co-authored-by: Romain Ruaud <[email protected]>
  • Loading branch information
kimpepper and romainruaud authored Feb 19, 2025
1 parent 14181fa commit ec2b48a
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/OpenSearch/HttpTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function sendRequest(
array $params = [],
mixed $body = null,
array $headers = [],
): array|string|null {
): iterable|string|null {
$request = $this->createRequest($method, $uri, $params, $body, $headers);
$response = $this->client->sendRequest($request);
$statusCode = $response->getStatusCode();
Expand Down
14 changes: 6 additions & 8 deletions src/OpenSearch/LegacyTransportWrapper.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace OpenSearch;
declare(strict_types=1);

use GuzzleHttp\Ring\Future\FutureArrayInterface;
namespace OpenSearch;

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

}
2 changes: 1 addition & 1 deletion src/OpenSearch/TransportInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ public function sendRequest(
array $params = [],
string|array|null $body = null,
array $headers = [],
): array|string|null;
): iterable|string|null;

}
62 changes: 62 additions & 0 deletions tests/LegacyTransportWrapperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace OpenSearch\Tests;

use GuzzleHttp\Ring\Future\FutureArray;
use OpenSearch\ConnectionPool\AbstractConnectionPool;
use OpenSearch\Connections\Connection;
use OpenSearch\LegacyTransportWrapper;
use OpenSearch\Transport;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use React\Promise\Deferred;

/**
* Tests for the LegacyTransportWrapper class.
*
* @coversDefaultClass \OpenSearch\LegacyTransportWrapper
* @group legacy
* @deprecated in 2.4.2 and will be removed in 3.0.0.
*/
class LegacyTransportWrapperTest extends TestCase
{
private Connection&MockObject $connection;

private AbstractConnectionPool&MockObject $connectionPool;

private MockObject&LoggerInterface $logger;

public function setUp(): void
{
$this->logger = $this->createMock(LoggerInterface::class);
$this->connectionPool = $this->createMock(AbstractConnectionPool::class);
$this->connection = $this->createMock(Connection::class);
}

public function testSuccess(): void
{
$deferred = new Deferred();
$deferred->resolve(['foo' => 'bar']);
$future = new FutureArray($deferred->promise());

$this->connection->method('performRequest')
->willReturn($future);

$this->connectionPool->method('nextConnection')
->willReturn($this->connection);

$transport = new Transport(1, $this->connectionPool, $this->logger);

$wrapper = new LegacyTransportWrapper($transport);

$response = $wrapper->sendRequest('GET', 'http://localhost:9200', [], null, []);

$this->assertIsIterable($response);

$this->assertEquals(['foo' => 'bar'], $response);
}

}

0 comments on commit ec2b48a

Please sign in to comment.