Skip to content
Open
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 @@ -221,7 +221,7 @@
$services->alias('api_platform.state.item_provider', 'ApiPlatform\Doctrine\Odm\State\ItemProvider');

$services->set('api_platform.doctrine.odm.metadata.resource.metadata_collection_factory', DoctrineMongoDbOdmResourceCollectionMetadataFactory::class)
->decorate('api_platform.metadata.resource.metadata_collection_factory', null, -50)
->decorate('api_platform.metadata.resource.metadata_collection_factory', null, -5)
->args([
service('doctrine_mongodb'),
service('api_platform.doctrine.odm.metadata.resource.metadata_collection_factory.inner'),
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/Resources/config/doctrine_orm.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@
->args([[]]);

$services->set('api_platform.doctrine.orm.metadata.resource.metadata_collection_factory', DoctrineOrmResourceCollectionMetadataFactory::class)
->decorate('api_platform.metadata.resource.metadata_collection_factory', null, -50)
->decorate('api_platform.metadata.resource.metadata_collection_factory', null, -5)
->args([
service('doctrine'),
service('api_platform.doctrine.orm.metadata.resource.metadata_collection_factory.inner'),
Expand Down
68 changes: 68 additions & 0 deletions tests/Functional/DoctrineMetadataFactoryCacheTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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\Functional;

use ApiPlatform\Doctrine\Odm\Metadata\Resource\DoctrineMongoDbOdmResourceCollectionMetadataFactory;
use ApiPlatform\Doctrine\Orm\Metadata\Resource\DoctrineOrmResourceCollectionMetadataFactory;
use ApiPlatform\Metadata\Resource\Factory\CachedResourceMetadataCollectionFactory;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use ApiPlatform\Tests\SetupClassResourcesTrait;

/**
* The Doctrine resource metadata collection factory must be decorated by the
* metadata cache, otherwise it runs again on every ResourceMetadataCollectionFactoryInterface::create() call.
*/
final class DoctrineMetadataFactoryCacheTest extends ApiTestCase
{
use SetupClassResourcesTrait;

protected static ?bool $alwaysBootKernel = false;

/**
* @return class-string[]
*/
public static function getResources(): array
{
return [];
}

public function testDoctrineFactoryIsInsideTheMetadataCache(): void
{
self::bootKernel();

$factory = self::getContainer()->get('api_platform.metadata.resource.metadata_collection_factory');
$this->assertInstanceOf(CachedResourceMetadataCollectionFactory::class, $factory, 'The metadata cache must be the outermost factory.');

$doctrineFactories = [DoctrineOrmResourceCollectionMetadataFactory::class, DoctrineMongoDbOdmResourceCollectionMetadataFactory::class];
$decorated = [];
while (null !== ($factory = $this->getDecorated($factory))) {
$decorated[] = $factory::class;
}

$this->assertNotEmpty(array_intersect($doctrineFactories, $decorated), 'The Doctrine metadata factory must be decorated by the metadata cache.');
}

private function getDecorated(ResourceMetadataCollectionFactoryInterface $factory): ?ResourceMetadataCollectionFactoryInterface
{
foreach ((new \ReflectionObject($factory))->getProperties() as $property) {
$value = $property->getValue($factory);
if ($value instanceof ResourceMetadataCollectionFactoryInterface) {
return $value;
}
}

return null;
}
}
Loading