Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sidz committed Mar 24, 2024
1 parent 98334b8 commit a09bd5a
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 34 deletions.
2 changes: 1 addition & 1 deletion tests/Aggregation/DateRangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function testDateRangeSetFormatAccordingToFormatTargetField(): void
$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 (ClientResponseException $e) {
$error = \json_decode($e->getResponse()->getBody(), true)['error'] ?? null;
$error = json_decode((string) $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
2 changes: 1 addition & 1 deletion tests/Cluster/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function testSetReadOnly(): void
$index->addDocument($doc2);
$this->fail('should throw read only exception');
} catch (ClientResponseException $e) {
$error = \json_decode($e->getResponse()->getBody(), true)['error'] ?? null;
$error = json_decode((string) $e->getResponse()->getBody(), true)['error']['root_cause'][0] ?? null;

$this->assertSame('cluster_block_exception', $error['type']);
$this->assertStringContainsString('cluster read-only', $error['reason']);
Expand Down
10 changes: 3 additions & 7 deletions tests/Index/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Elastic\Elasticsearch\Exception\ClientResponseException;
use Elastica\Document;
use Elastica\Index\Settings as IndexSettings;
use Elastica\ResponseConverter;
use Elastica\Test\Base as BaseTest;

/**
Expand Down Expand Up @@ -138,8 +137,7 @@ public function testDeleteAliasWithException(): void
$indexAlias->delete();
$this->fail('Should throw exception because you should delete the concrete index and not the alias');
} catch (ClientResponseException $e) {
$response = ResponseConverter::toElastica($e->getResponse());
$error = $response->getFullError();
$error = json_decode((string) $e->getResponse()->getBody(), true)['error']['root_cause'][0] ?? null;

$this->assertSame('illegal_argument_exception', $error['type']);
$this->assertStringContainsString('specify the corresponding concrete indices instead.', $error['reason']);
Expand Down Expand Up @@ -351,8 +349,7 @@ public function testSetReadOnly(): void
$index->addDocument($doc2);
$this->fail('Should throw exception because of read only');
} catch (ClientResponseException $e) {
$response = ResponseConverter::toElastica($e->getResponse());
$error = $response->getFullError();
$error = json_decode((string) $e->getResponse()->getBody(), true)['error']['root_cause'][0] ?? null;

$this->assertSame('cluster_block_exception', $error['type']);
$this->assertStringContainsString('read-only', $error['reason']);
Expand Down Expand Up @@ -453,8 +450,7 @@ public function testNotFoundIndex(): void
$index->getSettings()->get();
$this->fail('Should throw exception because of index not found');
} catch (ClientResponseException $e) {
$response = ResponseConverter::toElastica($e->getResponse());
$error = $response->getFullError();
$error = json_decode((string) $e->getResponse()->getBody(), true)['error']['root_cause'][0] ?? null;

$this->assertSame('index_not_found_exception', $error['type']);
}
Expand Down
7 changes: 3 additions & 4 deletions tests/IndexTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,12 @@ public function testCreateAlreadyExistsTemplateException(): void
$indexTemplate->create($template);
try {
$indexTemplate->create($template);
} catch (ClientResponseException $ex) {
$response = ResponseConverter::toElastica($ex->getResponse());
$error = $response->getFullError();
} catch (ClientResponseException $e) {
$error = json_decode((string) $e->getResponse()->getBody(), true)['error']['root_cause'][0] ?? null;

$this->assertNotEquals('index_template_already_exists_exception', $error['type']);
$this->assertEquals('resource_already_exists_exception', $error['type']);
$this->assertEquals(400, $ex->getResponse()->getStatusCode());
$this->assertEquals(400, $e->getResponse()->getStatusCode());
}
}
}
7 changes: 3 additions & 4 deletions tests/PipelineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,10 @@ public function testDeletePipeline(): void
$pipeline->deletePipeline('non_existent_pipeline');
$this->fail('an exception should be raised!');
} catch (ClientResponseException $e) {
$response = ResponseConverter::toElastica($e->getResponse());
$result = $response->getFullError();
$error = json_decode((string) $e->getResponse()->getBody(), true)['error']['root_cause'][0] ?? null;

$this->assertEquals('resource_not_found_exception', $result['type']);
$this->assertEquals('pipeline [non_existent_pipeline] is missing', $result['reason']);
$this->assertEquals('resource_not_found_exception', $error['type']);
$this->assertEquals('pipeline [non_existent_pipeline] is missing', $error['reason']);
}
}
}
10 changes: 5 additions & 5 deletions tests/Query/CombinedFieldsQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
class CombinedFieldsQueryTest extends BaseTest
{
private static $data = [
['id' => 1, 'title' => 'Rodolfo', 'body' => 'Moraes', 'abstract' => 'Lorem'],
['id' => 2, 'title' => 'Tristan', 'body' => 'Maindron', 'abstract' => 'Dolor'],
['id' => 3, 'title' => 'Monique', 'body' => 'Maindron', 'abstract' => 'Ipsum'],
['id' => 4, 'title' => 'John', 'body' => 'not Doe', 'abstract' => 'Consectetur'],
['id' => '1', 'title' => 'Rodolfo', 'body' => 'Moraes', 'abstract' => 'Lorem'],
['id' => '2', 'title' => 'Tristan', 'body' => 'Maindron', 'abstract' => 'Dolor'],
['id' => '3', 'title' => 'Monique', 'body' => 'Maindron', 'abstract' => 'Ipsum'],
['id' => '4', 'title' => 'John', 'body' => 'not Doe', 'abstract' => 'Consectetur'],
];

/**
Expand Down Expand Up @@ -145,7 +145,7 @@ private function _generateIndex(): Index
);

foreach (self::$data as $key => $docData) {
$index->addDocument(new Document($key, $docData));
$index->addDocument(new Document((string) $key, $docData));
}

// Refresh index
Expand Down
10 changes: 5 additions & 5 deletions tests/Query/MultiMatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
class MultiMatchTest extends BaseTest
{
private static $data = [
['id' => 1, 'name' => 'Rodolfo', 'last_name' => 'Moraes', 'full_name' => 'Rodolfo Moraes'],
['id' => 2, 'name' => 'Tristan', 'last_name' => 'Maindron', 'full_name' => 'Tristan Maindron'],
['id' => 3, 'name' => 'Monique', 'last_name' => 'Maindron', 'full_name' => 'Monique Maindron'],
['id' => 4, 'name' => 'John', 'last_name' => 'not Doe', 'full_name' => 'John not Doe'],
['id' => '1', 'name' => 'Rodolfo', 'last_name' => 'Moraes', 'full_name' => 'Rodolfo Moraes'],
['id' => '2', 'name' => 'Tristan', 'last_name' => 'Maindron', 'full_name' => 'Tristan Maindron'],
['id' => '3', 'name' => 'Monique', 'last_name' => 'Maindron', 'full_name' => 'Monique Maindron'],
['id' => '4', 'name' => 'John', 'last_name' => 'not Doe', 'full_name' => 'John not Doe'],
];

/**
Expand Down Expand Up @@ -218,7 +218,7 @@ private function _generateIndex(): Index
);

foreach (self::$data as $key => $docData) {
$index->addDocument(new Document($key, $docData));
$index->addDocument(new Document((string) $key, $docData));
}

// Refresh index
Expand Down
5 changes: 2 additions & 3 deletions tests/Query/QueryStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,13 @@ public function testSearchFieldsValidationException(): void
try {
$index->search($query);
} catch (ClientResponseException $e) {
$response = ResponseConverter::toElastica($e->getResponse());
$error = $response->getFullError();
$error = json_decode((string) $e->getResponse()->getBody(), true)['error'] ?? null;

$this->assertSame('query_shard_exception', $error['root_cause'][0]['type']);
$this->assertStringContainsString('failed to create query', $error['root_cause'][0]['reason']);
$this->assertStringContainsString('[fields] parameter in conjunction with [default_field]', $error['failed_shards'][0]['reason']['caused_by']['reason']);

$this->assertEquals(400, $response->getStatus());
$this->assertEquals(400, $e->getResponse()->getStatusCode());
}
}

Expand Down
7 changes: 3 additions & 4 deletions tests/SearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public function testSearchScrollRequest(): void
]);

\parse_str($search->getClient()->getLastRequest()->getUri()->getQuery(), $lastRequestQuery);
$lastRequestData = \json_decode($search->getClient()->getLastRequest()->getBody(), true);
$lastRequestData = \json_decode((string) $search->getClient()->getLastRequest()->getBody(), true);

$this->assertFalse($result->getResponse()->hasError());
$this->assertCount(5, $result->getResults());
Expand All @@ -271,7 +271,7 @@ public function testSearchScrollRequest(): void
]);

\parse_str($search->getClient()->getLastRequest()->getUri()->getQuery(), $lastRequestQuery);
$lastRequestData = \json_decode($search->getClient()->getLastRequest()->getBody(), true);
$lastRequestData = \json_decode((string) $search->getClient()->getLastRequest()->getBody(), true);

$this->assertFalse($result->getResponse()->hasError());
$this->assertCount(0, $result->getResults());
Expand Down Expand Up @@ -688,8 +688,7 @@ public function testIgnoreUnavailableOption(): void
$search->search($query);
$this->fail('Should raise an Index not found exception');
} catch (ClientResponseException $e) {
$response = ResponseConverter::toElastica($e->getResponse());
$error = $response->getFullError();
$error = json_decode((string) $e->getResponse()->getBody(), true)['error']['root_cause'][0] ?? null;

$this->assertEquals('index_not_found_exception', $error['type']);
$this->assertEquals('no such index [elastica_7086b4c2ee585bbb6740ece5ed7ece01]', $error['reason']);
Expand Down

0 comments on commit a09bd5a

Please sign in to comment.