|
| 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