Skip to content

Commit

Permalink
[Tests] Fix all
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelkeska committed Nov 28, 2023
1 parent a29169c commit bf71075
Show file tree
Hide file tree
Showing 51 changed files with 586 additions and 1,856 deletions.
6 changes: 3 additions & 3 deletions tests/Aggregation/DateRangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Elastica\Test\Aggregation;

use Elastic\Elasticsearch\Exception\ClientResponseException;
use Elastica\Aggregation\DateRange;
use Elastica\Document;
use Elastica\Exception\ResponseException;
use Elastica\Index;
use Elastica\Mapping;
use Elastica\Query;
Expand Down Expand Up @@ -133,8 +133,8 @@ public function testDateRangeSetFormatAccordingToFormatTargetField(): void
try {
$this->_getIndexForTest()->search($query)->getAggregation('date');
$this->fail('Should throw exception to and from parameters in date_range aggregation are interpreted according of the target field');
} catch (ResponseException $e) {
$error = $e->getResponse()->getFullError();
} catch (ClientResponseException $e) {
$error = \json_decode($e->getResponse()->getBody(), true)['error'] ?? null;

$this->assertSame('search_phase_execution_exception', $error['type']);
$this->assertStringStartsWith('failed to parse date field', $error['root_cause'][0]['reason']);
Expand Down
4 changes: 2 additions & 2 deletions tests/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,14 @@ protected function _checkPlugin($plugin): void

protected function _getVersion()
{
$data = $this->_getClient()->request('/')->getData();
$data = $this->_getClient()->info()->asArray();

return \substr($data['version']['number'], 0, 1);
}

protected function _checkVersion($version): void
{
$data = $this->_getClient()->request('/')->getData();
$data = $this->_getClient()->info()->asArray();
$installedVersion = $data['version']['number'];

if (\version_compare($installedVersion, $version) < 0) {
Expand Down
26 changes: 13 additions & 13 deletions tests/Bulk/ResponseSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

namespace Elastica\Test\Bulk;

use Elastic\Elasticsearch\Response\Elasticsearch;
use Elastica\Bulk;
use Elastica\Bulk\Action;
use Elastica\Bulk\ResponseSet;
use Elastica\Client;
use Elastica\Exception\Bulk\Response\ActionException;
use Elastica\Exception\Bulk\ResponseException;
use Elastica\Response;
use Elastica\Test\Base as BaseTest;
use GuzzleHttp\Psr7\Response;

/**
* @internal
Expand All @@ -26,11 +27,6 @@ public function testConstructor(): void
$responseSet = $this->_createResponseSet($responseData, $actions);

$this->assertEquals(200, $responseSet->getStatus());
$this->assertEquals(12.3, $responseSet->getQueryTime());
$this->assertEquals([
'url' => 'http://127.0.0.1:9200/_bulk',
'http_code' => 200,
], $responseSet->getTransferInfo());
}

/**
Expand Down Expand Up @@ -146,15 +142,19 @@ protected function _createResponseSet(array $responseData, array $actions): Resp
{
$client = $this->createMock(Client::class);

$response = new Response($responseData, 200);
$response->setQueryTime(12.3);
$response->setTransferInfo([
'url' => 'http://127.0.0.1:9200/_bulk',
'http_code' => 200,
]);
$response = new Elasticsearch();
$response->setResponse(new Response(
200,
[
Elasticsearch::HEADER_CHECK => Elasticsearch::PRODUCT_NAME,
'Accept' => 'application/json',
'Content-Type' => 'application/json',
],
\json_encode($responseData)
));

$client->expects($this->once())
->method('request')
->method('baseBulk')
->withAnyParameters()
->willReturn($response)
;
Expand Down
16 changes: 13 additions & 3 deletions tests/BulkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Elastica\Test;

use Elastic\Elasticsearch\Response\Elasticsearch;
use Elastica\Bulk;
use Elastica\Bulk\Action;
use Elastica\Bulk\Action\AbstractDocument;
Expand All @@ -15,9 +16,9 @@
use Elastica\Exception\InvalidException;
use Elastica\Exception\NotFoundException;
use Elastica\Exception\RequestEntityTooLargeException;
use Elastica\Response as ElasticaResponse;
use Elastica\Script\Script;
use Elastica\Test\Base as BaseTest;
use GuzzleHttp\Psr7\Response as Psr7Response;
use PHPUnit\Framework\MockObject\MockObject;

/**
Expand Down Expand Up @@ -776,12 +777,21 @@ public function testHasIndex(): void
*/
public function testSendRequestEntityTooLargeExceptionIf413Response(): void
{
$response = new ElasticaResponse('', 413);
$response = new Elasticsearch();
$response->setResponse(new Psr7Response(
413,
[
Elasticsearch::HEADER_CHECK => Elasticsearch::PRODUCT_NAME,
'Accept' => 'application/json',
'Content-Type' => 'application/json',
],
\json_encode([])
), false);

/** @var Client|MockObject $clientMock */
$clientMock = $this->createMock(Client::class);
$clientMock
->method('request')
->method('baseBulk')
->willReturn($response)
;

Expand Down
49 changes: 9 additions & 40 deletions tests/ClientConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,42 +38,32 @@ public function testFromSimpleDsn(): void
'port' => 9201,
'path' => null,
'url' => null,
'proxy' => null,
'transport' => null,
'persistent' => true,
'timeout' => null,
'connections' => [],
'roundRobin' => false,
'retryOnConflict' => 0,
'bigintConversion' => false,
'username' => null,
'password' => null,
'auth_type' => null,
'transport_config' => [],
];

$this->assertEquals($expected, $configuration->getAll());
}

public function testFromDsnWithParameters(): void
{
$configuration = ClientConfiguration::fromDsn('https://user:p4ss@foo.com:9201/my-path?proxy=https://proxy.com&persistent=false&timeout=45&roundRobin=true&retryOnConflict=2&bigintConversion=true&extra=abc');
$configuration = ClientConfiguration::fromDsn('https://user:p4ss@foo.com:9201/my-path?roundRobin=true&retryOnConflict=2&extra=abc');
$expected = [
'host' => 'foo.com',
'port' => 9201,
'path' => '/my-path',
'url' => null,
'proxy' => 'https://proxy.com',
'transport' => 'https',
'persistent' => false,
'timeout' => 45,
'connections' => [],
'roundRobin' => true,
'retryOnConflict' => 2,
'bigintConversion' => true,
'username' => 'user',
'password' => 'p4ss',
'auth_type' => 'basic',
'extra' => 'abc',
'transport_config' => [],
];

$this->assertEquals($expected, $configuration->getAll());
Expand All @@ -87,21 +77,16 @@ public function testFromDsnWithPool(): void
'port' => null,
'path' => null,
'url' => null,
'proxy' => null,
'transport' => null,
'persistent' => true,
'timeout' => null,
'connections' => [
['host' => '127.0.0.1', 'transport' => 'http', 'username' => 'nicolas'],
['host' => '127.0.0.2', 'path' => '/bar', 'transport' => 'http', 'timeout' => 4],
['host' => '127.0.0.1', 'username' => 'nicolas'],
['host' => '127.0.0.2', 'path' => '/bar', 'timeout' => 4],
],
'roundRobin' => false,
'retryOnConflict' => 0,
'bigintConversion' => false,
'username' => 'tobias',
'password' => null,
'auth_type' => null,
'extra' => 'abc',
'transport_config' => [],
];

$this->assertEquals($expected, $configuration->getAll());
Expand All @@ -116,17 +101,12 @@ public function testFromEmptyArray(): void
'port' => null,
'path' => null,
'url' => null,
'proxy' => null,
'transport' => null,
'persistent' => true,
'timeout' => null,
'connections' => [], // host, port, path, timeout, transport, compression, persistent, timeout, username, password, config -> (curl, headers, url)
'roundRobin' => false,
'retryOnConflict' => 0,
'bigintConversion' => false,
'username' => null,
'password' => null,
'auth_type' => null,
'transport_config' => [],
];

$this->assertEquals($expected, $configuration->getAll());
Expand All @@ -144,17 +124,12 @@ public function testFromArray(): void
'port' => null,
'path' => null,
'url' => null,
'proxy' => null,
'transport' => null,
'persistent' => true,
'timeout' => null,
'connections' => [], // host, port, path, timeout, transport, compression, persistent, timeout, username, password, config -> (curl, headers, url)
'roundRobin' => false,
'retryOnConflict' => 0,
'bigintConversion' => false,
'username' => 'Jdoe',
'password' => null,
'auth_type' => null,
'transport_config' => [],
'extra' => 'abc',
];

Expand All @@ -171,24 +146,18 @@ public function testHas(): void
public function testGet(): void
{
$configuration = new ClientConfiguration();
$this->assertTrue($configuration->get('persistent'));

$expected = [
'host' => null,
'port' => null,
'path' => null,
'url' => null,
'proxy' => null,
'transport' => null,
'persistent' => true,
'timeout' => null,
'connections' => [],
'roundRobin' => false,
'retryOnConflict' => 0,
'bigintConversion' => false,
'username' => null,
'password' => null,
'auth_type' => null,
'transport_config' => [],
];

$this->assertEquals($expected, $configuration->get(''));
Expand Down
Loading

0 comments on commit bf71075

Please sign in to comment.