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 7b1a90b
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 22 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($e->getResponse()->getBody()->getContents(), true)['error']['root_cause'][0] ?? 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($e->getResponse()->getBody()->getContents(), 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($e->getResponse()->getBody()->getContents(), 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($e->getResponse()->getBody()->getContents(), 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($e->getResponse()->getBody()->getContents(), 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($e->getResponse()->getBody()->getContents(), 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($e->getResponse()->getBody()->getContents(), 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']);
}
}
}
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($e->getResponse()->getBody()->getContents(), true)['error']['root_cause'][0] ?? 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
3 changes: 1 addition & 2 deletions tests/SearchTest.php
Original file line number Diff line number Diff line change
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($e->getResponse()->getBody()->getContents(), 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 7b1a90b

Please sign in to comment.