From d84a881630d94dd1d2d85abe270e552b0dd09c48 Mon Sep 17 00:00:00 2001 From: Felipe Barbosa Date: Thu, 29 Sep 2022 03:23:20 -0300 Subject: [PATCH] Allowing when creating the id isn't required (#83) * Allowing when creating the id isn't required Signed-off-by: Felipe Barbosa * Added unit tests Signed-off-by: Felipe Barbosa * Added create integration test Signed-off-by: Felipe Barbosa * Added class PHP Doc in CreateIntegrationTest Signed-off-by: Felipe Barbosa * Removing _type key assert from test Signed-off-by: Felipe Barbosa Signed-off-by: Felipe Barbosa --- USER_GUIDE.md | 11 +++ src/OpenSearch/Endpoints/Create.php | 15 ++- tests/Endpoints/CreateIntegrationTest.php | 83 ++++++++++++++++ tests/Endpoints/CreateTest.php | 109 ++++++++++++++++++++++ 4 files changed, 210 insertions(+), 8 deletions(-) create mode 100644 tests/Endpoints/CreateIntegrationTest.php create mode 100644 tests/Endpoints/CreateTest.php diff --git a/USER_GUIDE.md b/USER_GUIDE.md index 552daa4e..8bca1091 100644 --- a/USER_GUIDE.md +++ b/USER_GUIDE.md @@ -37,6 +37,7 @@ $client->indices()->create([ ] ]); +// Create a document passing the id $client->create([ 'index' => $indexName, 'id' => 1, @@ -47,6 +48,16 @@ $client->create([ ] ]); +// Create a document without passing the id (will be generated automatically) +$client->create([ + 'index' => $indexName, + 'body' => [ + 'title' => 'Remember the Titans', + 'director' => 'Boaz Yakin', + 'year' => 2000 + ] +]); + // Search for it var_dump( $client->search([ diff --git a/src/OpenSearch/Endpoints/Create.php b/src/OpenSearch/Endpoints/Create.php index 87a98213..e1954a52 100644 --- a/src/OpenSearch/Endpoints/Create.php +++ b/src/OpenSearch/Endpoints/Create.php @@ -28,19 +28,18 @@ class Create extends AbstractEndpoint { public function getURI(): string { - if (isset($this->id) !== true) { - throw new RuntimeException( - 'id is required for create' - ); - } - $id = $this->id; if (isset($this->index) !== true) { throw new RuntimeException( 'index is required for create' ); } $index = $this->index; - return "/$index/_create/$id"; + + if (isset($this->id) !== true) { + return "/$index/_doc"; + } + + return "/$index/_create/$this->id"; } public function getParamWhitelist(): array @@ -58,7 +57,7 @@ public function getParamWhitelist(): array public function getMethod(): string { - return 'PUT'; + return isset($this->id) ? 'PUT' : 'POST'; } public function setBody($body): Create diff --git a/tests/Endpoints/CreateIntegrationTest.php b/tests/Endpoints/CreateIntegrationTest.php new file mode 100644 index 00000000..a6acb012 --- /dev/null +++ b/tests/Endpoints/CreateIntegrationTest.php @@ -0,0 +1,83 @@ +create([ + 'index' => $expectedIndex, + 'id' => $expectedId, + 'body' => [ + 'title' => 'Remember the Titans', + 'director' => 'Boaz Yakin', + 'year' => 2000 + ] + ]); + + // Assert + $this->assertEquals($expectedIndex, $result['_index']); + $this->assertEquals($expectedResult, $result['result']); + $this->assertEquals($expectedId, $result['_id']); + } + + public function testCreateWithoutPassId() + { + // Arrange + $expectedIndex = 'movies'; + $expectedResult = 'created'; + + $client = Utility::getClient(); + + // Act + $result = $client->create([ + 'index' => $expectedIndex, + 'body' => [ + 'title' => 'Remember the Titans', + 'director' => 'Boaz Yakin', + 'year' => 2000 + ] + ]); + + // Assert + $this->assertEquals($expectedIndex, $result['_index']); + $this->assertEquals($expectedResult, $result['result']); + $this->assertNotEmpty($result['_id']); + } +} diff --git a/tests/Endpoints/CreateTest.php b/tests/Endpoints/CreateTest.php new file mode 100644 index 00000000..61d2e985 --- /dev/null +++ b/tests/Endpoints/CreateTest.php @@ -0,0 +1,109 @@ +instance = new Create(); + } + + public function testGetURIWhenIndexAndIdAreDefined(): void + { + // Arrange + $expected = '/index/_create/10'; + + $this->instance->setIndex('index'); + $this->instance->setId(10); + + // Act + $result = $this->instance->getURI(); + + // Assert + $this->assertEquals($expected, $result); + } + + public function testGetURIWhenIndexIsDefinedAndIdIsNotDefined(): void + { + // Arrange + $expected = '/index/_doc'; + + $this->instance->setIndex('index'); + + // Act + $result = $this->instance->getURI(); + + // Assert + $this->assertEquals($expected, $result); + } + + public function testGetURIWhenIndexIsNotDefined(): void + { + // Arrange + $expected = RuntimeException::class; + $expectedMessage = 'index is required for create'; + + // Assert + $this->expectException($expected); + $this->expectExceptionMessage($expectedMessage); + + // Act + $this->instance->getURI(); + } + + public function testGetMethodWhenIdIsDefined(): void + { + // Arrange + $expected = 'PUT'; + + $this->instance->setId(10); + + // Act + $result = $this->instance->getMethod(); + + // Assert + $this->assertEquals($expected, $result); + } + + public function testGetMethodWhenIdIsNotDefined(): void + { + // Arrange + $expected = 'POST'; + + // Act + $result = $this->instance->getMethod(); + + // Assert + $this->assertEquals($expected, $result); + } +}