From 26246cb23caa3d8f4541922b6f148f064090c62f Mon Sep 17 00:00:00 2001 From: Oleg Zhulnev Date: Sun, 24 Mar 2024 18:58:18 +0300 Subject: [PATCH] Fix tests --- tests/Aggregation/DateRangeTest.php | 4 ++-- tests/Cluster/SettingsTest.php | 2 +- tests/Index/SettingsTest.php | 10 +++------- tests/IndexTemplateTest.php | 7 +++---- tests/PipelineTest.php | 7 +++---- tests/Query/CombinedFieldsQueryTest.php | 10 +++++----- tests/Query/MultiMatchTest.php | 10 +++++----- tests/Query/QueryStringTest.php | 5 ++--- tests/SearchTest.php | 5 ++--- 9 files changed, 26 insertions(+), 34 deletions(-) diff --git a/tests/Aggregation/DateRangeTest.php b/tests/Aggregation/DateRangeTest.php index 9e0dea661..e653373c2 100644 --- a/tests/Aggregation/DateRangeTest.php +++ b/tests/Aggregation/DateRangeTest.php @@ -136,9 +136,9 @@ 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->assertSame('search_phase_execution_exception', $error['root_cause'][0]['type']); $this->assertStringStartsWith('failed to parse date field', $error['root_cause'][0]['reason']); } } diff --git a/tests/Cluster/SettingsTest.php b/tests/Cluster/SettingsTest.php index f0ffb4ab9..c61e6d7cf 100644 --- a/tests/Cluster/SettingsTest.php +++ b/tests/Cluster/SettingsTest.php @@ -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']); diff --git a/tests/Index/SettingsTest.php b/tests/Index/SettingsTest.php index e3475d4a3..820d9ebe2 100644 --- a/tests/Index/SettingsTest.php +++ b/tests/Index/SettingsTest.php @@ -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; /** @@ -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']); @@ -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']); @@ -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']); } diff --git a/tests/IndexTemplateTest.php b/tests/IndexTemplateTest.php index bd66de787..bf5148812 100644 --- a/tests/IndexTemplateTest.php +++ b/tests/IndexTemplateTest.php @@ -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()); } } } diff --git a/tests/PipelineTest.php b/tests/PipelineTest.php index c9ec96a9f..bdde1cabb 100644 --- a/tests/PipelineTest.php +++ b/tests/PipelineTest.php @@ -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']); } } } diff --git a/tests/Query/CombinedFieldsQueryTest.php b/tests/Query/CombinedFieldsQueryTest.php index de775dac9..f10d96340 100644 --- a/tests/Query/CombinedFieldsQueryTest.php +++ b/tests/Query/CombinedFieldsQueryTest.php @@ -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'], ]; /** @@ -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 diff --git a/tests/Query/MultiMatchTest.php b/tests/Query/MultiMatchTest.php index a4b392e9d..17fd671b5 100644 --- a/tests/Query/MultiMatchTest.php +++ b/tests/Query/MultiMatchTest.php @@ -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'], ]; /** @@ -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 diff --git a/tests/Query/QueryStringTest.php b/tests/Query/QueryStringTest.php index 9324f0a3f..3774f38de 100644 --- a/tests/Query/QueryStringTest.php +++ b/tests/Query/QueryStringTest.php @@ -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()); } } diff --git a/tests/SearchTest.php b/tests/SearchTest.php index e21147ff0..9c6c36de6 100644 --- a/tests/SearchTest.php +++ b/tests/SearchTest.php @@ -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()); @@ -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']);