Skip to content

Commit a446ee4

Browse files
authored
Fix #1058 using object instead of array in onFailure event (#1066)
1 parent 7346315 commit a446ee4

File tree

2 files changed

+89
-3
lines changed

2 files changed

+89
-3
lines changed

src/Elasticsearch/Transport.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function getConnection(): ConnectionInterface
9393
*
9494
* @throws Common\Exceptions\NoNodesAvailableException|\Exception
9595
*/
96-
public function performRequest(string $method, string $uri, array $params = null, $body = null, array $options = []): FutureArrayInterface
96+
public function performRequest(string $method, string $uri, array $params = [], $body = null, array $options = []): FutureArrayInterface
9797
{
9898
try {
9999
$connection = $this->getConnection();
@@ -114,7 +114,7 @@ public function performRequest(string $method, string $uri, array $params = null
114114
$options,
115115
$this
116116
);
117-
117+
118118
$future->promise()->then(
119119
//onSuccess
120120
function ($response) {
@@ -123,8 +123,9 @@ function ($response) {
123123
},
124124
//onFailure
125125
function ($response) {
126+
$code = $response->getCode();
126127
// Ignore 400 level errors, as that means the server responded just fine
127-
if (!(isset($response['code']) && $response['code'] >=400 && $response['code'] < 500)) {
128+
if ($code < 400 || $code >= 500) {
128129
// Otherwise schedule a check
129130
$this->connectionPool->scheduleCheck();
130131
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
/**
3+
* Elasticsearch PHP client
4+
*
5+
* @link https://github.com/elastic/elasticsearch-php/
6+
* @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
7+
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
8+
* @license https://www.gnu.org/licenses/lgpl-2.1.html GNU Lesser General Public License, Version 2.1
9+
*
10+
* Licensed to Elasticsearch B.V under one or more agreements.
11+
* Elasticsearch B.V licenses this file to you under the Apache 2.0 License or
12+
* the GNU Lesser General Public License, Version 2.1, at your option.
13+
* See the LICENSE file in the project root for more information.
14+
*/
15+
16+
17+
declare(strict_types = 1);
18+
19+
namespace Elasticsearch\Tests;
20+
21+
use Elasticsearch\Common\Exceptions\ServerErrorResponseException;
22+
use Elasticsearch\ConnectionPool\AbstractConnectionPool;
23+
use Elasticsearch\Connections\Connection;
24+
use Elasticsearch\Serializers\SerializerInterface;
25+
use Elasticsearch\Transport;
26+
use GuzzleHttp\Ring\Future\FutureArray;
27+
use GuzzleHttp\Ring\Future\FutureArrayInterface;
28+
use PHPUnit\Framework\TestCase;
29+
use Psr\Log\LoggerInterface;
30+
use React\Promise\Deferred;
31+
use React\Promise\Promise;
32+
33+
class TransportTest extends TestCase
34+
{
35+
public function setUp(): void
36+
{
37+
$this->logger = $this->createMock(LoggerInterface::class);
38+
$this->trace = $this->createMock(LoggerInterface::class);
39+
$this->serializer = $this->createMock(SerializerInterface::class);
40+
$this->connectionPool = $this->createMock(AbstractConnectionPool::class);
41+
$this->connection = $this->createMock(Connection::class);
42+
}
43+
44+
public function testPerformRequestWithServerErrorResponseException404Result()
45+
{
46+
$deferred = new Deferred();
47+
$deferred->reject(new ServerErrorResponseException('foo', 404));
48+
$future = new FutureArray($deferred->promise());
49+
50+
$this->connection->method('performRequest')
51+
->willReturn($future);
52+
53+
$this->connectionPool->method('nextConnection')
54+
->willReturn($this->connection);
55+
56+
$this->connectionPool->expects($this->never())
57+
->method('scheduleCheck');
58+
59+
$transport = new Transport(1, $this->connectionPool, $this->logger);
60+
61+
$result = $transport->performRequest('GET', '/');
62+
$this->assertInstanceOf(FutureArrayInterface::class, $result);
63+
}
64+
65+
public function testPerformRequestWithServerErrorResponseException500Result()
66+
{
67+
$deferred = new Deferred();
68+
$deferred->reject(new ServerErrorResponseException('foo', 500));
69+
$future = new FutureArray($deferred->promise());
70+
71+
$this->connection->method('performRequest')
72+
->willReturn($future);
73+
74+
$this->connectionPool->method('nextConnection')
75+
->willReturn($this->connection);
76+
77+
$this->connectionPool->expects($this->once())
78+
->method('scheduleCheck');
79+
80+
$transport = new Transport(1, $this->connectionPool, $this->logger);
81+
82+
$result = $transport->performRequest('GET', '/');
83+
$this->assertInstanceOf(FutureArrayInterface::class, $result);
84+
}
85+
}

0 commit comments

Comments
 (0)