Skip to content

Commit

Permalink
Allowing when creating the id isn't required (#83)
Browse files Browse the repository at this point in the history
* Allowing when creating the id isn't required

Signed-off-by: Felipe Barbosa <[email protected]>

* Added unit tests

Signed-off-by: Felipe Barbosa <[email protected]>

* Added create integration test

Signed-off-by: Felipe Barbosa <[email protected]>

* Added class PHP Doc in CreateIntegrationTest

Signed-off-by: Felipe Barbosa <[email protected]>

* Removing _type key assert from test

Signed-off-by: Felipe Barbosa <[email protected]>

Signed-off-by: Felipe Barbosa <[email protected]>
  • Loading branch information
feeh27 authored Sep 29, 2022
1 parent 3d80473 commit d84a881
Show file tree
Hide file tree
Showing 4 changed files with 210 additions and 8 deletions.
11 changes: 11 additions & 0 deletions USER_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ $client->indices()->create([
]
]);

// Create a document passing the id
$client->create([
'index' => $indexName,
'id' => 1,
Expand All @@ -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([
Expand Down
15 changes: 7 additions & 8 deletions src/OpenSearch/Endpoints/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
83 changes: 83 additions & 0 deletions tests/Endpoints/CreateIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

declare(strict_types=1);

/**
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* Elasticsearch PHP client
*
* @link https://github.com/elastic/elasticsearch-php/
* @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @license https://www.gnu.org/licenses/lgpl-2.1.html GNU Lesser General Public License, Version 2.1
*
* Licensed to Elasticsearch B.V under one or more agreements.
* Elasticsearch B.V licenses this file to you under the Apache 2.0 License or
* the GNU Lesser General Public License, Version 2.1, at your option.
* See the LICENSE file in the project root for more information.
*/

namespace OpenSearch\Tests\Endpoints;

use OpenSearch\Tests\Utility;

/**
* Class CreateIntegrationTest
*
* @subpackage Tests\Endpoints
* @group Integration
*/
class CreateIntegrationTest extends \PHPUnit\Framework\TestCase
{
public function testCreatePassingId()
{
// Arrange
$expectedIndex = 'movies';
$expectedResult = 'created';
$expectedId = 100;

$client = Utility::getClient();

// Act
$result = $client->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']);
}
}
109 changes: 109 additions & 0 deletions tests/Endpoints/CreateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

declare(strict_types=1);

/**
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* Elasticsearch PHP client
*
* @link https://github.com/elastic/elasticsearch-php/
* @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @license https://www.gnu.org/licenses/lgpl-2.1.html GNU Lesser General Public License, Version 2.1
*
* Licensed to Elasticsearch B.V under one or more agreements.
* Elasticsearch B.V licenses this file to you under the Apache 2.0 License or
* the GNU Lesser General Public License, Version 2.1, at your option.
* See the LICENSE file in the project root for more information.
*/

namespace OpenSearch\Tests\Endpoints;

use OpenSearch\Common\Exceptions\RuntimeException;
use OpenSearch\Endpoints\Create;

class CreateTest extends \PHPUnit\Framework\TestCase
{
/** @var Create */
private $instance;

/**
* @inheritDoc
*/
protected function setUp(): void
{
// Instance
$this->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);
}
}

0 comments on commit d84a881

Please sign in to comment.