|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <[email protected]> |
| 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\Doctrine\Orm\Metadata\Resource; |
| 15 | + |
| 16 | +use ApiPlatform\Api\ResourceClassResolverInterface; |
| 17 | +use ApiPlatform\Doctrine\Orm\Metadata\Resource\DoctrineOrmLinkFactory; |
| 18 | +use ApiPlatform\Metadata\ApiResource; |
| 19 | +use ApiPlatform\Metadata\Get; |
| 20 | +use ApiPlatform\Metadata\Link; |
| 21 | +use ApiPlatform\Metadata\Operation; |
| 22 | +use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface; |
| 23 | +use ApiPlatform\Metadata\Property\PropertyNameCollection; |
| 24 | +use ApiPlatform\Metadata\Resource\Factory\LinkFactoryInterface; |
| 25 | +use ApiPlatform\Metadata\Resource\Factory\PropertyLinkFactoryInterface; |
| 26 | +use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Dummy; |
| 27 | +use ApiPlatform\Tests\Fixtures\TestBundle\Entity\RelatedDummy; |
| 28 | +use ApiPlatform\Tests\Fixtures\TestBundle\Model\Car; |
| 29 | +use Doctrine\ORM\EntityManagerInterface; |
| 30 | +use Doctrine\Persistence\ManagerRegistry; |
| 31 | +use Doctrine\Persistence\Mapping\ClassMetadata; |
| 32 | +use PHPUnit\Framework\TestCase; |
| 33 | +use Prophecy\PhpUnit\ProphecyTrait; |
| 34 | + |
| 35 | +final class DoctrineOrmLinkFactoryTest extends TestCase |
| 36 | +{ |
| 37 | + use ProphecyTrait; |
| 38 | + |
| 39 | + public function testCreateLinksFromRelations(): void |
| 40 | + { |
| 41 | + $class = Dummy::class; |
| 42 | + $operation = (new Get())->withClass($class); |
| 43 | + |
| 44 | + $classMetadataProphecy = $this->prophesize(ClassMetadata::class); |
| 45 | + $classMetadataProphecy->hasAssociation('name')->willReturn(false); |
| 46 | + $classMetadataProphecy->hasAssociation('relatedNonResource')->willReturn(true); |
| 47 | + $classMetadataProphecy->hasAssociation('relatedDummy')->willReturn(true); |
| 48 | + $classMetadataProphecy->hasAssociation('relatedDummies')->willReturn(true); |
| 49 | + $classMetadataProphecy->getAssociationTargetClass('relatedNonResource')->willReturn(Car::class); |
| 50 | + $classMetadataProphecy->getAssociationTargetClass('relatedDummy')->willReturn(RelatedDummy::class); |
| 51 | + $classMetadataProphecy->getAssociationTargetClass('relatedDummies')->willReturn(RelatedDummy::class); |
| 52 | + $classMetadataProphecy->getAssociationMappedByTargetField('relatedNonResource')->willReturn('dummies'); |
| 53 | + $classMetadataProphecy->getAssociationMappedByTargetField('relatedDummy')->willReturn(null); |
| 54 | + $classMetadataProphecy->getAssociationMappedByTargetField('relatedDummies')->willReturn('dummies'); |
| 55 | + $entityManagerProphecy = $this->prophesize(EntityManagerInterface::class); |
| 56 | + $entityManagerProphecy->getClassMetadata($class)->willReturn($classMetadataProphecy->reveal()); |
| 57 | + $managerRegistryProphecy = $this->prophesize(ManagerRegistry::class); |
| 58 | + $managerRegistryProphecy->getManagerForClass($class)->willReturn($entityManagerProphecy->reveal()); |
| 59 | + $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class); |
| 60 | + $propertyNameCollectionFactoryProphecy->create($class)->willReturn(new PropertyNameCollection(['name', 'relatedNonResource', 'relatedDummy', 'relatedDummies'])); |
| 61 | + $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class); |
| 62 | + $resourceClassResolverProphecy->isResourceClass(Car::class)->willReturn(false); |
| 63 | + $resourceClassResolverProphecy->isResourceClass(RelatedDummy::class)->willReturn(true); |
| 64 | + |
| 65 | + $doctrineOrmLinkFactory = new DoctrineOrmLinkFactory($managerRegistryProphecy->reveal(), $propertyNameCollectionFactoryProphecy->reveal(), $resourceClassResolverProphecy->reveal(), new LinkFactoryStub()); |
| 66 | + |
| 67 | + self::assertEquals([ |
| 68 | + new Link( |
| 69 | + fromProperty: 'relatedDummies', |
| 70 | + toProperty: 'dummies', |
| 71 | + fromClass: Dummy::class, |
| 72 | + toClass: RelatedDummy::class, |
| 73 | + ), |
| 74 | + ], $doctrineOrmLinkFactory->createLinksFromRelations($operation)); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +class LinkFactoryStub implements LinkFactoryInterface, PropertyLinkFactoryInterface |
| 79 | +{ |
| 80 | + public function createLinkFromProperty(ApiResource|Operation $operation, string $property): Link |
| 81 | + { |
| 82 | + return new Link(); |
| 83 | + } |
| 84 | + |
| 85 | + public function createLinksFromIdentifiers(ApiResource|Operation $operation): array |
| 86 | + { |
| 87 | + return []; |
| 88 | + } |
| 89 | + |
| 90 | + public function createLinksFromRelations(ApiResource|Operation $operation): array |
| 91 | + { |
| 92 | + return []; |
| 93 | + } |
| 94 | + |
| 95 | + public function createLinksFromAttributes(ApiResource|Operation $operation): array |
| 96 | + { |
| 97 | + return []; |
| 98 | + } |
| 99 | + |
| 100 | + public function completeLink(Link $link): Link |
| 101 | + { |
| 102 | + return $link; |
| 103 | + } |
| 104 | +} |
0 commit comments