Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(metadata): check if elasticsearch is set to false by user through ApiResource (#5115) #5177

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use ApiPlatform\Elasticsearch\State\CollectionProvider;
use ApiPlatform\Elasticsearch\State\ItemProvider;
use ApiPlatform\Metadata\CollectionOperationInterface;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
use ApiPlatform\Util\Inflector;
Expand All @@ -41,7 +42,7 @@ public function create(string $resourceClass): ResourceMetadataCollection

if ($operations) {
foreach ($resourceMetadata->getOperations() as $operationName => $operation) {
if ($this->hasIndices($operation->getShortName())) {
if ($this->hasIndices($operation)) {
$operation = $operation->withElasticsearch(true);
}

Expand All @@ -59,7 +60,7 @@ public function create(string $resourceClass): ResourceMetadataCollection

if ($graphQlOperations) {
foreach ($graphQlOperations as $operationName => $graphQlOperation) {
if ($this->hasIndices($graphQlOperation->getShortName())) {
if ($this->hasIndices($graphQlOperation)) {
$graphQlOperation = $graphQlOperation->withElasticsearch(true);
}

Expand All @@ -79,8 +80,13 @@ public function create(string $resourceClass): ResourceMetadataCollection
return $resourceMetadataCollection;
}

private function hasIndices(string $shortName): bool
private function hasIndices(Operation $operation): bool
{
if (false === $operation->getElasticsearch()) {
return false;
}

$shortName = $operation->getShortName();
$index = Inflector::tableize($shortName);

try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Tests\Elasticsearch\Metadata\Resource\Factory;

use ApiPlatform\Elasticsearch\Metadata\Resource\Factory\ElasticsearchProviderResourceMetadataCollectionFactory;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Operations;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Foo;
use ApiPlatform\Tests\Fixtures\TestBundle\Metadata\Get;
use Elasticsearch\Client;
use Elasticsearch\Common\Exceptions\Missing404Exception;
use Elasticsearch\Namespaces\CatNamespace;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;

class ElasticsearchProviderResourceMetadataCollectionFactoryTest extends TestCase
{
use ProphecyTrait;

public function testConstruct(): void
{
self::assertInstanceOf(
ResourceMetadataCollectionFactoryInterface::class,
new ElasticsearchProviderResourceMetadataCollectionFactory(
$this->prophesize(Client::class)->reveal(),
$this->prophesize(ResourceMetadataCollectionFactoryInterface::class)->reveal()
)
);
}

/** @dataProvider elasticsearchProvider */
public function testCreate(?bool $elasticsearchFlag, int $expectedCatCallCount, ?bool $expectedResult): void
{
$get = (new Get())->withShortName('Foo')->withElasticsearch($elasticsearchFlag);
$resource = (new ApiResource())->withOperations(new Operations(['foo_get' => $get]));
$metadata = new ResourceMetadataCollection(Foo::class, [$resource]);

$decorated = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
$decorated->create(Foo::class)->willReturn($metadata)->shouldBeCalled();

$catNamespace = $this->prophesize(CatNamespace::class);
if ($elasticsearchFlag) {
$catNamespace->indices(['index' => 'foo'])->willReturn([[
'health' => 'yellow',
'status' => 'open',
'index' => 'foo',
'uuid' => '123456789abcdefghijklmn',
'pri' => '5',
'rep' => '1',
'docs.count' => '42',
'docs.deleted' => '0',
'store.size' => '42kb',
'pri.store.size' => '42kb',
]]);
} else {
$catNamespace->indices(['index' => 'foo'])->willThrow(new Missing404Exception());
}

$client = $this->prophesize(Client::class);
$client->cat()->willReturn($catNamespace)->shouldBeCalledTimes($expectedCatCallCount);

$resourceMetadataFactory = new ElasticsearchProviderResourceMetadataCollectionFactory($client->reveal(), $decorated->reveal());
$elasticsearchResult = $resourceMetadataFactory->create(Foo::class)->getOperation('foo_get')->getElasticsearch();
self::assertEquals($expectedResult, $elasticsearchResult);
}

public function elasticsearchProvider(): array
{
return [
'elasticsearch: false' => [false, 0, false],
'elasticsearch: null' => [null, 1, false],
'elasticsearch: true' => [true, 1, true],
];
}
}