|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Tests\Api\SnapshotTests\Extension; |
| 4 | + |
| 5 | +use ApiPlatform\Doctrine\Orm\Util\QueryNameGenerator; |
| 6 | +use ApiPlatform\Metadata\GetCollection; |
| 7 | +use ApiPlatform\Metadata\Operation; |
| 8 | +use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; |
| 9 | +use App\Doctrine\Orm\Extension\FilterEagerLoadingsExtension; |
| 10 | +use App\Entity\CampCollaboration; |
| 11 | +use App\Repository\CampCollaborationRepository; |
| 12 | +use App\Tests\Api\ECampApiTestCase; |
| 13 | +use Doctrine\ORM\QueryBuilder; |
| 14 | + |
| 15 | +use function PHPUnit\Framework\assertThat; |
| 16 | +use function PHPUnit\Framework\equalTo; |
| 17 | + |
| 18 | +/** |
| 19 | + * @internal |
| 20 | + */ |
| 21 | +class FilterEagerLoadingExtensionIntegrationTest extends ECampApiTestCase { |
| 22 | + private CampCollaborationRepository $repository; |
| 23 | + private QueryNameGenerator $queryNameGenerator; |
| 24 | + private Operation $operation; |
| 25 | + private array $context = []; |
| 26 | + private FilterEagerLoadingsExtension $filterEagerLoadingExtension; |
| 27 | + |
| 28 | + public function setUp(): void { |
| 29 | + parent::setUp(); |
| 30 | + $container = static::getContainer(); |
| 31 | + |
| 32 | + $this->repository = $container->get(CampCollaborationRepository::class); |
| 33 | + $this->resourceMetadataCollectionFactory = $container->get(ResourceMetadataCollectionFactoryInterface::class); |
| 34 | + $this->queryNameGenerator = new QueryNameGenerator(); |
| 35 | + $this->entityClass = CampCollaboration::class; |
| 36 | + $this->operation = new GetCollection(); |
| 37 | + $this->filterEagerLoadingExtension = $container->get(FilterEagerLoadingsExtension::class); |
| 38 | + } |
| 39 | + |
| 40 | + public function testLetQueryAsIsIfNoCondition() { |
| 41 | + /** @var QueryBuilder $queryBuilder */ |
| 42 | + $queryBuilder = $this->repository->createQueryBuilder('o'); |
| 43 | + $sqlBefore = $this->toSql($queryBuilder); |
| 44 | + |
| 45 | + $this->applyExtension($queryBuilder); |
| 46 | + |
| 47 | + assertThat($this->toSql($queryBuilder), equalTo($sqlBefore)); |
| 48 | + } |
| 49 | + |
| 50 | + public function testOptimizeQueryWhenFilterHitsToManyJoin() { |
| 51 | + $activity = static::getFixture('activity1'); |
| 52 | + $queryBuilder = $this->repository->createQueryBuilder('o'); |
| 53 | + $queryBuilder = $queryBuilder |
| 54 | + ->innerJoin('o.activityResponsibles', 'activityResponsibles_a1') |
| 55 | + ->innerJoin('activityResponsibles_a1.activity', 'a2') |
| 56 | + ->where('a2 = :activity_p1') |
| 57 | + ->orWhere( |
| 58 | + $queryBuilder->expr()->andX( |
| 59 | + $queryBuilder->expr()->eq('a2.title', ':text'), |
| 60 | + $queryBuilder->expr()->lte('a2.title', ':text'), |
| 61 | + $queryBuilder->expr()->orX( |
| 62 | + $queryBuilder->expr()->in('a2.title', [':text']), |
| 63 | + $queryBuilder->expr()->eq($queryBuilder->expr()->lower('a2.title'), ':text') |
| 64 | + ) |
| 65 | + ) |
| 66 | + ) |
| 67 | + ->setParameter('activity_p1', $activity) |
| 68 | + ->setParameter('text', 'text') |
| 69 | + ; |
| 70 | + |
| 71 | + $this->applyExtension($queryBuilder); |
| 72 | + |
| 73 | + $this->assertMatchesSnapshot($this->toSql($queryBuilder)); |
| 74 | + } |
| 75 | + |
| 76 | + private function applyExtension($queryBuilder): void { |
| 77 | + $this->filterEagerLoadingExtension->applyToCollection( |
| 78 | + queryBuilder: $queryBuilder, |
| 79 | + queryNameGenerator: $this->queryNameGenerator, |
| 80 | + resourceClass: $this->entityClass, |
| 81 | + operation: $this->operation, |
| 82 | + context: $this->context |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + private function toSql(QueryBuilder $queryBuilder): array|string { |
| 87 | + return $queryBuilder->getQuery()->getSQL(); |
| 88 | + } |
| 89 | +} |
0 commit comments