|
| 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 | + |
| 32 | +class TransportTest extends TestCase |
| 33 | +{ |
| 34 | + public function setUp(): void |
| 35 | + { |
| 36 | + $this->logger = $this->createMock(LoggerInterface::class); |
| 37 | + $this->trace = $this->createMock(LoggerInterface::class); |
| 38 | + $this->serializer = $this->createMock(SerializerInterface::class); |
| 39 | + $this->connectionPool = $this->createMock(AbstractConnectionPool::class); |
| 40 | + $this->connection = $this->createMock(Connection::class); |
| 41 | + } |
| 42 | + |
| 43 | + public function testPerformRequestWithServerErrorResponseException404Result() |
| 44 | + { |
| 45 | + $deferred = new Deferred(); |
| 46 | + $deferred->reject(new ServerErrorResponseException('foo', 404)); |
| 47 | + $future = new FutureArray($deferred->promise()); |
| 48 | + |
| 49 | + $this->connection->method('performRequest') |
| 50 | + ->willReturn($future); |
| 51 | + |
| 52 | + $this->connectionPool->method('nextConnection') |
| 53 | + ->willReturn($this->connection); |
| 54 | + |
| 55 | + $this->connectionPool->expects($this->never()) |
| 56 | + ->method('scheduleCheck'); |
| 57 | + |
| 58 | + $transport = new Transport(1, $this->connectionPool, $this->logger); |
| 59 | + |
| 60 | + $result = $transport->performRequest('GET', '/'); |
| 61 | + $this->assertInstanceOf(FutureArrayInterface::class, $result); |
| 62 | + } |
| 63 | + |
| 64 | + public function testPerformRequestWithServerErrorResponseException500Result() |
| 65 | + { |
| 66 | + $deferred = new Deferred(); |
| 67 | + $deferred->reject(new ServerErrorResponseException('foo', 500)); |
| 68 | + $future = new FutureArray($deferred->promise()); |
| 69 | + |
| 70 | + $this->connection->method('performRequest') |
| 71 | + ->willReturn($future); |
| 72 | + |
| 73 | + $this->connectionPool->method('nextConnection') |
| 74 | + ->willReturn($this->connection); |
| 75 | + |
| 76 | + $this->connectionPool->expects($this->once()) |
| 77 | + ->method('scheduleCheck'); |
| 78 | + |
| 79 | + $transport = new Transport(1, $this->connectionPool, $this->logger); |
| 80 | + |
| 81 | + $result = $transport->performRequest('GET', '/'); |
| 82 | + $this->assertInstanceOf(FutureArrayInterface::class, $result); |
| 83 | + } |
| 84 | +} |
0 commit comments