Skip to content

Commit dbf4447

Browse files
authored
fix(metadata): check if elasticsearch is set to false by user through ApiResource (#5115) (#5177)
1 parent 27f98e2 commit dbf4447

File tree

2 files changed

+97
-3
lines changed

2 files changed

+97
-3
lines changed

src/Elasticsearch/Metadata/Resource/Factory/ElasticsearchProviderResourceMetadataCollectionFactory.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use ApiPlatform\Elasticsearch\State\CollectionProvider;
1717
use ApiPlatform\Elasticsearch\State\ItemProvider;
1818
use ApiPlatform\Metadata\CollectionOperationInterface;
19+
use ApiPlatform\Metadata\Operation;
1920
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
2021
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
2122
use ApiPlatform\Util\Inflector;
@@ -41,7 +42,7 @@ public function create(string $resourceClass): ResourceMetadataCollection
4142

4243
if ($operations) {
4344
foreach ($resourceMetadata->getOperations() as $operationName => $operation) {
44-
if ($this->hasIndices($operation->getShortName())) {
45+
if ($this->hasIndices($operation)) {
4546
$operation = $operation->withElasticsearch(true);
4647
}
4748

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

6061
if ($graphQlOperations) {
6162
foreach ($graphQlOperations as $operationName => $graphQlOperation) {
62-
if ($this->hasIndices($graphQlOperation->getShortName())) {
63+
if ($this->hasIndices($graphQlOperation)) {
6364
$graphQlOperation = $graphQlOperation->withElasticsearch(true);
6465
}
6566

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

82-
private function hasIndices(string $shortName): bool
83+
private function hasIndices(Operation $operation): bool
8384
{
85+
if (false === $operation->getElasticsearch()) {
86+
return false;
87+
}
88+
89+
$shortName = $operation->getShortName();
8490
$index = Inflector::tableize($shortName);
8591

8692
try {
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Elasticsearch\Metadata\Resource\Factory;
15+
16+
use ApiPlatform\Elasticsearch\Metadata\Resource\Factory\ElasticsearchProviderResourceMetadataCollectionFactory;
17+
use ApiPlatform\Metadata\ApiResource;
18+
use ApiPlatform\Metadata\Operations;
19+
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
20+
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
21+
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Foo;
22+
use ApiPlatform\Tests\Fixtures\TestBundle\Metadata\Get;
23+
use Elasticsearch\Client;
24+
use Elasticsearch\Common\Exceptions\Missing404Exception;
25+
use Elasticsearch\Namespaces\CatNamespace;
26+
use PHPUnit\Framework\TestCase;
27+
use Prophecy\PhpUnit\ProphecyTrait;
28+
29+
class ElasticsearchProviderResourceMetadataCollectionFactoryTest extends TestCase
30+
{
31+
use ProphecyTrait;
32+
33+
public function testConstruct(): void
34+
{
35+
self::assertInstanceOf(
36+
ResourceMetadataCollectionFactoryInterface::class,
37+
new ElasticsearchProviderResourceMetadataCollectionFactory(
38+
$this->prophesize(Client::class)->reveal(),
39+
$this->prophesize(ResourceMetadataCollectionFactoryInterface::class)->reveal()
40+
)
41+
);
42+
}
43+
44+
/** @dataProvider elasticsearchProvider */
45+
public function testCreate(?bool $elasticsearchFlag, int $expectedCatCallCount, ?bool $expectedResult): void
46+
{
47+
$get = (new Get())->withShortName('Foo')->withElasticsearch($elasticsearchFlag);
48+
$resource = (new ApiResource())->withOperations(new Operations(['foo_get' => $get]));
49+
$metadata = new ResourceMetadataCollection(Foo::class, [$resource]);
50+
51+
$decorated = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
52+
$decorated->create(Foo::class)->willReturn($metadata)->shouldBeCalled();
53+
54+
$catNamespace = $this->prophesize(CatNamespace::class);
55+
if ($elasticsearchFlag) {
56+
$catNamespace->indices(['index' => 'foo'])->willReturn([[
57+
'health' => 'yellow',
58+
'status' => 'open',
59+
'index' => 'foo',
60+
'uuid' => '123456789abcdefghijklmn',
61+
'pri' => '5',
62+
'rep' => '1',
63+
'docs.count' => '42',
64+
'docs.deleted' => '0',
65+
'store.size' => '42kb',
66+
'pri.store.size' => '42kb',
67+
]]);
68+
} else {
69+
$catNamespace->indices(['index' => 'foo'])->willThrow(new Missing404Exception());
70+
}
71+
72+
$client = $this->prophesize(Client::class);
73+
$client->cat()->willReturn($catNamespace)->shouldBeCalledTimes($expectedCatCallCount);
74+
75+
$resourceMetadataFactory = new ElasticsearchProviderResourceMetadataCollectionFactory($client->reveal(), $decorated->reveal());
76+
$elasticsearchResult = $resourceMetadataFactory->create(Foo::class)->getOperation('foo_get')->getElasticsearch();
77+
self::assertEquals($expectedResult, $elasticsearchResult);
78+
}
79+
80+
public function elasticsearchProvider(): array
81+
{
82+
return [
83+
'elasticsearch: false' => [false, 0, false],
84+
'elasticsearch: null' => [null, 1, false],
85+
'elasticsearch: true' => [true, 1, true],
86+
];
87+
}
88+
}

0 commit comments

Comments
 (0)