|
13 | 13 |
|
14 | 14 | namespace ApiPlatform\Core\Tests\Bridge\Doctrine\Orm\Extension;
|
15 | 15 |
|
| 16 | +use ApiPlatform\Core\Api\ResourceClassResolver; |
16 | 17 | use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\FilterEagerLoadingExtension;
|
17 | 18 | use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
|
18 | 19 | use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
|
| 20 | +use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface; |
19 | 21 | use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
|
| 22 | +use ApiPlatform\Core\Metadata\Resource\ResourceNameCollection; |
20 | 23 | use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeItem;
|
21 | 24 | use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeLabel;
|
22 | 25 | use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeRelation;
|
23 | 26 | use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyCar;
|
| 27 | +use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyTravel; |
24 | 28 | use Doctrine\ORM\EntityManager;
|
25 | 29 | use Doctrine\ORM\Mapping\ClassMetadataInfo;
|
26 | 30 | use Doctrine\ORM\Query\Expr;
|
@@ -159,6 +163,55 @@ public function testApplyCollection()
|
159 | 163 | $this->assertEquals('SELECT o FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyCar o LEFT JOIN o.colors colors WHERE o IN(SELECT o_2 FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyCar o_2 LEFT JOIN o_2.colors colors_2 WHERE o_2.colors = :foo)', $qb->getDQL());
|
160 | 164 | }
|
161 | 165 |
|
| 166 | + public function testApplyCollectionWithManualJoin() |
| 167 | + { |
| 168 | + $resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class); |
| 169 | + $resourceMetadataFactoryProphecy->create(DummyCar::class)->willReturn(new ResourceMetadata(DummyCar::class)); |
| 170 | + |
| 171 | + $resourceNameCollectionFactoryProphecy = $this->prophesize(ResourceNameCollectionFactoryInterface::class); |
| 172 | + $resourceNameCollectionFactoryProphecy->create()->willReturn(new ResourceNameCollection([DummyTravel::class])); |
| 173 | + |
| 174 | + $em = $this->prophesize(EntityManager::class); |
| 175 | + $em->getExpressionBuilder()->shouldBeCalled()->willReturn(new Expr()); |
| 176 | + $em->getClassMetadata(DummyCar::class)->shouldBeCalled()->willReturn(new ClassMetadataInfo(DummyCar::class)); |
| 177 | + |
| 178 | + $qb = new QueryBuilder($em->reveal()); |
| 179 | + |
| 180 | + $qb->select('o') |
| 181 | + ->from(DummyCar::class, 'o') |
| 182 | + ->leftJoin('o.colors', 'colors') |
| 183 | + ->join(DummyTravel::class, 't_a3', Expr\Join::WITH, 'o.id = t_a3.car AND t_a3.passenger = :user') |
| 184 | + ->where('o.colors = :foo') |
| 185 | + ->andwhere('t_a3.confirmed = :confirmation') |
| 186 | + ->setParameter('foo', 1) |
| 187 | + ->setParameter('user', 2) |
| 188 | + ->setParameter('confirmation', true) |
| 189 | + ; |
| 190 | + |
| 191 | + $queryNameGenerator = $this->prophesize(QueryNameGeneratorInterface::class); |
| 192 | + $queryNameGenerator->generateJoinAlias('colors')->shouldBeCalled()->willReturn('colors_2'); |
| 193 | + $queryNameGenerator->generateJoinAlias('o')->shouldBeCalled()->willReturn('o_2'); |
| 194 | + $queryNameGenerator->generateJoinAlias('t_a3')->shouldBeCalled()->willReturn('t_a3_a20'); |
| 195 | + |
| 196 | + $filterEagerLoadingExtension = new FilterEagerLoadingExtension($resourceMetadataFactoryProphecy->reveal(), true, new ResourceClassResolver($resourceNameCollectionFactoryProphecy->reveal())); |
| 197 | + $filterEagerLoadingExtension->applyToCollection($qb, $queryNameGenerator->reveal(), DummyCar::class, 'get'); |
| 198 | + |
| 199 | + $expected = <<<'SQL' |
| 200 | +SELECT o |
| 201 | +FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyCar o |
| 202 | +LEFT JOIN o.colors colors |
| 203 | +INNER JOIN ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyTravel t_a3 WITH o.id = t_a3.car AND t_a3.passenger = :user |
| 204 | +WHERE o IN( |
| 205 | + SELECT o_2 FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyCar o_2 |
| 206 | + LEFT JOIN o_2.colors colors_2 |
| 207 | + INNER JOIN ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyTravel t_a3_a20 WITH o_2.id = t_a3_a20.car AND t_a3_a20.passenger = :user |
| 208 | + WHERE o_2.colors = :foo AND t_a3_a20.confirmed = :confirmation |
| 209 | +) |
| 210 | +SQL; |
| 211 | + |
| 212 | + $this->assertEquals($this->toDQLString($expected), $qb->getDQL()); |
| 213 | + } |
| 214 | + |
162 | 215 | /**
|
163 | 216 | * https://github.com/api-platform/core/issues/1021.
|
164 | 217 | */
|
|
0 commit comments