|
25 | 25 | use ApiPlatform\Core\Metadata\Property\PropertyNameCollection;
|
26 | 26 | use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
|
27 | 27 | use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\RelatedDummy;
|
28 |
| -use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\RelatedOwnedDummy; |
29 | 28 | use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\RelatedOwningDummy;
|
30 | 29 | use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\ThirdLevel;
|
31 | 30 | use Doctrine\Common\Persistence\ManagerRegistry;
|
@@ -269,64 +268,62 @@ public function testGetSubSubresourceItem()
|
269 | 268 | $this->assertEquals($result, $dataProvider->getSubresource(ThirdLevel::class, ['id' => ['id' => 1], 'relatedDummies' => ['id' => 1]], $context));
|
270 | 269 | }
|
271 | 270 |
|
272 |
| - public function testGetSubresourceOneToOneRelation() |
| 271 | + public function testGetSubresourceOneToOneOwningRelation() |
273 | 272 | {
|
274 |
| - // RelatedOwningDummy OneToOne RelatedOwnedDummy |
275 |
| - $managerRegistryProphecy = $this->prophesize(ManagerRegistry::class); |
| 273 | + // RelatedOwningDummy OneToOne Dummy |
| 274 | + $dql = 'SELECT ownedDummy_a2 FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy id_a1 INNER JOIN id_a1.ownedDummy ownedDummy_a2 WHERE id_a1.id = :id_p1'; |
| 275 | + |
| 276 | + $queryProphecy = $this->prophesize(AbstractQuery::class); |
| 277 | + $queryProphecy->getOneOrNullResult()->shouldBeCalled()->willReturn([]); |
| 278 | + |
276 | 279 | $identifiers = ['id'];
|
| 280 | + $queryBuilder = $this->prophesize(QueryBuilder::class); |
| 281 | + $queryBuilder->setParameter('id_p1', 1, DBALType::INTEGER)->shouldBeCalled()->willReturn($queryBuilder); |
| 282 | + $funcProphecy = $this->prophesize(Func::class); |
| 283 | + $func = $funcProphecy->reveal(); |
277 | 284 |
|
278 |
| - // First manager (RelatedOwningDummy) |
279 |
| - // Generated QueryBuilder |
280 |
| - $qb = $this->prophesize(QueryBuilder::class); |
281 |
| - $qb->select('IDENTITY(owningDummy_a1.owningDummy)')->shouldBeCalled()->willReturn($qb); |
282 |
| - $qb->from(RelatedOwningDummy::class, 'owningDummy_a1')->shouldBeCalled()->willReturn($qb); |
283 |
| - $qb->andWhere('owningDummy_a1.id = :id_p1')->shouldBeCalled()->willReturn($qb); |
| 285 | + $queryBuilder->andWhere($func)->shouldBeCalled()->willReturn($queryBuilder); |
| 286 | + |
| 287 | + $queryBuilder->getQuery()->shouldBeCalled()->willReturn($queryProphecy->reveal()); |
| 288 | + |
| 289 | + $repositoryProphecy = $this->prophesize(EntityRepository::class); |
| 290 | + $repositoryProphecy->createQueryBuilder('o')->shouldBeCalled()->willReturn($queryBuilder->reveal()); |
| 291 | + |
| 292 | + $managerProphecy = $this->prophesize(EntityManager::class); |
| 293 | + $managerProphecy->getRepository(RelatedOwningDummy::class)->shouldBeCalled()->willReturn($repositoryProphecy->reveal()); |
| 294 | + $this->assertIdentifierManagerMethodCalls($managerProphecy); |
284 | 295 |
|
285 |
| - // RelatedOwningDummy classMetaData |
286 | 296 | $classMetadataProphecy = $this->prophesize(ClassMetadata::class);
|
287 | 297 | $classMetadataProphecy->hasAssociation('ownedDummy')->willReturn(true)->shouldBeCalled();
|
288 | 298 | $classMetadataProphecy->getAssociationMapping('ownedDummy')->shouldBeCalled()->willReturn(['type' => ClassMetadata::ONE_TO_ONE]);
|
289 | 299 | $classMetadataProphecy->getTypeOfField('id')->willReturn(DBALType::INTEGER)->shouldBeCalled();
|
290 | 300 |
|
291 |
| - // RelatedOwningDummy Manager |
292 |
| - $relatedOwningDummyManagerProphecy = $this->prophesize(EntityManager::class); |
293 |
| - $relatedOwningDummyManagerProphecy->createQueryBuilder()->shouldBeCalled()->willReturn($qb->reveal()); |
294 |
| - $relatedOwningDummyManagerProphecy->getClassMetadata(RelatedOwningDummy::class)->shouldBeCalled()->willReturn($classMetadataProphecy->reveal()); |
295 |
| - |
296 |
| - $managerRegistryProphecy->getManagerForClass(RelatedOwningDummy::class)->shouldBeCalled()->willReturn($relatedOwningDummyManagerProphecy->reveal()); |
297 |
| - |
298 |
| - // Result Query |
299 |
| - $result = new \stdClass(); |
300 |
| - $queryProphecy = $this->prophesize(AbstractQuery::class); |
301 |
| - $queryProphecy->getOneOrNullResult()->shouldBeCalled()->willReturn($result); |
| 301 | + $managerProphecy->getClassMetadata(Dummy::class)->shouldBeCalled()->willReturn($classMetadataProphecy->reveal()); |
302 | 302 |
|
303 |
| - // Second manager (RelatedOwnedDummy) |
304 |
| - // RelatedOwnedDummy classMetaData |
305 |
| - $classMetadataOwnedProphecy = $this->prophesize(ClassMetadata::class); |
306 |
| - $classMetadataOwnedProphecy->hasAssociation('owningDummy')->willReturn(true)->shouldBeCalled(); |
307 |
| - $classMetadataOwnedProphecy->getAssociationMapping('owningDummy')->shouldBeCalled()->willReturn(['type' => ClassMetadata::ONE_TO_ONE]); |
308 |
| - $classMetadataOwnedProphecy->getTypeOfField('id')->willReturn(DBALType::INTEGER)->shouldBeCalled(); |
| 303 | + $qb = $this->prophesize(QueryBuilder::class); |
| 304 | + $qb->select('IDENTITY(id_a1.ownedDummy)')->shouldBeCalled()->willReturn($qb); |
| 305 | + $qb->from(Dummy::class, 'id_a1')->shouldBeCalled()->willReturn($qb); |
| 306 | + $qb->andWhere('id_a1.id = :id_p1')->shouldBeCalled()->willReturn($qb); |
| 307 | + $qb->getDQL()->shouldBeCalled()->willReturn($dql); |
309 | 308 |
|
310 |
| - $queryBuilder = $this->prophesize(QueryBuilder::class); |
311 |
| - $queryBuilder->getQuery()->shouldBeCalled()->willReturn($queryProphecy->reveal()); |
312 |
| - $queryBuilder->setParameter('id_p1', 1, DBALType::INTEGER)->shouldBeCalled()->willReturn($queryBuilder); |
| 309 | + $exprProphecy = $this->prophesize(Expr::class); |
| 310 | + $exprProphecy->in('o', $dql)->willReturn($func)->shouldBeCalled(); |
313 | 311 |
|
314 |
| - $repositoryProphecy = $this->prophesize(EntityRepository::class); |
315 |
| - $repositoryProphecy->createQueryBuilder('o')->shouldBeCalled()->willReturn($queryBuilder->reveal()); |
| 312 | + $qb->expr()->shouldBeCalled()->willReturn($exprProphecy->reveal()); |
316 | 313 |
|
317 |
| - $ownedDummyManagerProphecy = $this->prophesize(EntityManager::class); |
318 |
| - $ownedDummyManagerProphecy->getClassMetaData(RelatedOwnedDummy::class)->willReturn($classMetadataOwnedProphecy->reveal()); |
319 |
| - $ownedDummyManagerProphecy->getRepository(RelatedOwnedDummy::class)->willReturn($repositoryProphecy->reveal()); |
| 314 | + $managerProphecy->createQueryBuilder()->shouldBeCalled()->willReturn($qb->reveal()); |
320 | 315 |
|
321 |
| - $managerRegistryProphecy->getManagerForClass(RelatedOwnedDummy::class)->shouldBeCalled()->willReturn($ownedDummyManagerProphecy->reveal()); |
| 316 | + $managerRegistryProphecy = $this->prophesize(ManagerRegistry::class); |
| 317 | + $managerRegistryProphecy->getManagerForClass(RelatedOwningDummy::class)->shouldBeCalled()->willReturn($managerProphecy->reveal()); |
| 318 | + $managerRegistryProphecy->getManagerForClass(Dummy::class)->shouldBeCalled()->willReturn($managerProphecy->reveal()); |
322 | 319 |
|
323 |
| - list($propertyNameCollectionFactory, $propertyMetadataFactory) = $this->getMetadataProphecies([RelatedOwnedDummy::class => $identifiers, RelatedOwningDummy::class => $identifiers]); |
| 320 | + list($propertyNameCollectionFactory, $propertyMetadataFactory) = $this->getMetadataProphecies([Dummy::class => $identifiers]); |
324 | 321 |
|
325 | 322 | $dataProvider = new SubresourceDataProvider($managerRegistryProphecy->reveal(), $propertyNameCollectionFactory, $propertyMetadataFactory);
|
326 | 323 |
|
327 |
| - $context = ['property' => 'ownedDummy', 'identifiers' => [['id', RelatedOwningDummy::class], ['ownedDummy', RelatedOwnedDummy::class]], 'collection' => false, IdentifierConverterInterface::HAS_IDENTIFIER_CONVERTER => true]; |
| 324 | + $context = ['property' => 'ownedDummy', 'identifiers' => [['id', Dummy::class]], 'collection' => false, IdentifierConverterInterface::HAS_IDENTIFIER_CONVERTER => true]; |
328 | 325 |
|
329 |
| - $this->assertEquals($result, $dataProvider->getSubresource(RelatedOwnedDummy::class, ['id' => ['id' => 1], 'ownedDummy' => ['id' => 1]], $context)); |
| 326 | + $this->assertEquals([], $dataProvider->getSubresource(RelatedOwningDummy::class, ['id' => ['id' => 1]], $context)); |
330 | 327 | }
|
331 | 328 |
|
332 | 329 | public function testQueryResultExtension()
|
|
0 commit comments