From 798308fc564904e9a085f93ce7cd49c2d7be04be Mon Sep 17 00:00:00 2001 From: imdhemy Date: Sat, 2 Mar 2024 21:00:21 +0100 Subject: [PATCH] chore(client): improve raw request tests --- tests/ClientIntegrationTest.php | 51 ++++------------------------- tests/ClientTest.php | 57 +++++++++++++++++++++++++++++++-- 2 files changed, 61 insertions(+), 47 deletions(-) diff --git a/tests/ClientIntegrationTest.php b/tests/ClientIntegrationTest.php index 32d710f8..f3b480b5 100644 --- a/tests/ClientIntegrationTest.php +++ b/tests/ClientIntegrationTest.php @@ -151,7 +151,7 @@ public function testIndexCannotBeArrayOfNullsForDelete() } /** @test */ - public function sendRawGetRequest(): void + public function sendRawRequest(): void { $client = $this->getClient(); @@ -165,56 +165,19 @@ public function sendRawGetRequest(): void } /** @test */ - public function sendRawPostRequest(): void + public function insertDocumentUsingRawRequest(): void { $client = $this->getClient(); - $index = 'test_index_' . time(); - $client->indices()->create(['index' => $index]); + $randomIndex = 'test_index_' .time(); - $response = $client->request('POST', "/$index/_doc", ['body' => ['foo' => 'bar']]); + $response = $client->request('POST', "/$randomIndex/_doc", ['body' => ['field' => 'value']]); $this->assertIsArray($response); $this->assertArrayHasKey('_index', $response); - $this->assertEquals($index, $response['_index']); + $this->assertSame($randomIndex, $response['_index']); + $this->assertArrayHasKey('_id', $response); $this->assertArrayHasKey('result', $response); - $this->assertEquals('created', $response['result']); - - $client->indices()->delete(['index' => $index]); - } - - /** @test */ - public function sendRawPutRequest(): void - { - $client = $this->getClient(); - $index = 'test_index_' . time(); - $client->indices()->create(['index' => $index]); - - $response = $client->request('PUT', "/$index/_settings", ['body' => ['index' => ['number_of_replicas' => 2]]]); - - $this->assertIsArray($response); - $this->assertArrayHasKey('acknowledged', $response); - $this->assertTrue($response['acknowledged']); - - $client->indices()->delete(['index' => $index]); - } - - /** @test */ - public function sendRawDeleteRequest(): void - { - $client = $this->getClient(); - $index = 'test_index_' . time(); - $client->indices()->create(['index' => $index]); - $client->index(['index' => $index, 'id' => 1, 'body' => ['foo' => 'bar']]); - - $response = $client->request('DELETE', "/$index/_doc/1"); - - $this->assertIsArray($response); - $this->assertArrayHasKey('_index', $response); - $this->assertEquals($index, $response['_index']); - $this->assertArrayHasKey('result', $response); - $this->assertEquals('deleted', $response['result']); - - $client->indices()->delete(['index' => $index]); + $this->assertSame('created', $response['result']); } private function getLevelOutput(string $level, array $output): string diff --git a/tests/ClientTest.php b/tests/ClientTest.php index bc1d092c..4a405c10 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -21,13 +21,13 @@ namespace OpenSearch\Tests; +use GuzzleHttp\Ring\Client\MockHandler; +use GuzzleHttp\Ring\Future\FutureArray; +use Mockery as m; use OpenSearch; use OpenSearch\Client; use OpenSearch\ClientBuilder; use OpenSearch\Common\Exceptions\MaxRetriesException; -use GuzzleHttp\Ring\Client\MockHandler; -use GuzzleHttp\Ring\Future\FutureArray; -use Mockery as m; /** * Class ClientTest @@ -409,4 +409,55 @@ public function testExtractArgumentIterable() $this->assertCount(0, $params); $this->assertInstanceOf(\IteratorIterator::class, $argument); } + + /** @test */ + public function sendRawRequest(): void + { + $callable = function () {}; + $transport = $this->createMock(OpenSearch\Transport::class); + $client = new OpenSearch\Client($transport, $callable, []); + + $transport->expects($this->once())->method('performRequest')->with('GET', '/', [], null, []); + + $client->request('GET', '/'); + } + + /** @test */ + public function sendRawRequestWithBody(): void + { + $callable = function () {}; + $transport = $this->createMock(OpenSearch\Transport::class); + $client = new OpenSearch\Client($transport, $callable, []); + $body = ['query' => ['match' => ['text_entry' => 'long live king']]]; + + $transport->expects($this->once())->method('performRequest')->with('GET', '/shakespeare/_search', [], $body, []); + + $client->request('GET', '/shakespeare/_search', compact('body')); + } + + /** @test */ + public function sendRawRequestWithParams(): void + { + $callable = function () {}; + $transport = $this->createMock(OpenSearch\Transport::class); + $client = new OpenSearch\Client($transport, $callable, []); + $params = ['foo' => 'bar']; + + $transport->expects($this->once())->method('performRequest')->with('GET', '/_search', $params, null, []); + + $client->request('GET', '/_search', compact('params')); + } + + /** @test */ + public function sendRawRequestWithOptions(): void + { + $callable = function () {}; + $transport = $this->createMock(OpenSearch\Transport::class); + $client = new OpenSearch\Client($transport, $callable, []); + $options = ['client' => ['future' => 'lazy']]; + + $transport->expects($this->once())->method('performRequest')->with('GET', '/', [], null, $options); + + $client->request('GET', '/', compact('options')); + } }