Skip to content

Commit

Permalink
fix(metadata): check if elasticsearch is set to false by user through…
Browse files Browse the repository at this point in the history
… ApiResource (#5115)
  • Loading branch information
jannes-io committed Nov 9, 2022
1 parent 66d3097 commit 70acc9e
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 3 deletions.
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,78 @@
<?php
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],
];
}
}

0 comments on commit 70acc9e

Please sign in to comment.