|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Silverback API Components Bundle Project |
| 5 | + * |
| 6 | + * (c) Daniel West <daniel@silverback.is> |
| 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 | +namespace Silverback\ApiComponentsBundle\Tests\Metadata\Factory; |
| 13 | + |
| 14 | +use ApiPlatform\Metadata\ApiResource; |
| 15 | +use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; |
| 16 | +use ApiPlatform\Metadata\Resource\ResourceMetadataCollection; |
| 17 | +use Doctrine\ORM\EntityManagerInterface; |
| 18 | +use Doctrine\ORM\Mapping\ClassMetadata; |
| 19 | +use Doctrine\Persistence\ManagerRegistry; |
| 20 | +use PHPUnit\Framework\TestCase; |
| 21 | +use Silverback\ApiComponentsBundle\Metadata\Factory\PageDataMetadataFactory; |
| 22 | +use Silverback\ApiComponentsBundle\Metadata\PageDataMetadata; |
| 23 | +use Silverback\ApiComponentsBundle\Metadata\PageDataPropertyMetadata; |
| 24 | +use Silverback\ApiComponentsBundle\Tests\Functional\TestBundle\Entity\DummyComponent; |
| 25 | +use Silverback\ApiComponentsBundle\Tests\Functional\TestBundle\Entity\DummyPageDataSupplier; |
| 26 | +use Silverback\ApiComponentsBundle\Tests\Functional\TestBundle\Entity\DummyPageDataVariant; |
| 27 | +use Silverback\ApiComponentsBundle\Tests\Functional\TestBundle\Entity\PageDataWithVariants; |
| 28 | + |
| 29 | +final class PageDataMetadataFactoryTest extends TestCase |
| 30 | +{ |
| 31 | + public function test_a_to_many_association_is_left_out_of_the_metadata(): void |
| 32 | + { |
| 33 | + $classMetadata = $this->createStub(ClassMetadata::class); |
| 34 | + $classMetadata->method('getAssociationNames')->willReturn(['page', 'component', 'variants']); |
| 35 | + $classMetadata->method('isCollectionValuedAssociation')->willReturnMap([ |
| 36 | + ['page', false], |
| 37 | + ['component', false], |
| 38 | + ['variants', true], |
| 39 | + ]); |
| 40 | + $classMetadata->method('getAssociationTargetClass')->willReturnMap([ |
| 41 | + ['component', DummyComponent::class], |
| 42 | + ['variants', DummyPageDataVariant::class], |
| 43 | + ]); |
| 44 | + $resourceMetadataFactory = $this->createStub(ResourceMetadataCollectionFactoryInterface::class); |
| 45 | + $resourceMetadataFactory->method('create')->willReturnCallback( |
| 46 | + static fn (string $class) => new ResourceMetadataCollection($class, [new ApiResource(shortName: (new \ReflectionClass($class))->getShortName())]), |
| 47 | + ); |
| 48 | + |
| 49 | + $metadata = (new PageDataMetadataFactory(self::registryFor($classMetadata), $resourceMetadataFactory))->create(PageDataWithVariants::class); |
| 50 | + |
| 51 | + self::assertSame(PageDataWithVariants::class, $metadata->getResourceClass()); |
| 52 | + self::assertSame([['component', DummyComponent::class, 'DummyComponent']], self::propertiesOf($metadata)); |
| 53 | + } |
| 54 | + |
| 55 | + public function test_an_association_to_a_class_that_is_not_an_api_resource_is_left_out_of_the_metadata(): void |
| 56 | + { |
| 57 | + $classMetadata = $this->createStub(ClassMetadata::class); |
| 58 | + $classMetadata->method('getAssociationNames')->willReturn(['supplier', 'component']); |
| 59 | + $classMetadata->method('isCollectionValuedAssociation')->willReturn(false); |
| 60 | + $classMetadata->method('getAssociationTargetClass')->willReturnMap([ |
| 61 | + ['supplier', DummyPageDataSupplier::class], |
| 62 | + ['component', DummyComponent::class], |
| 63 | + ]); |
| 64 | + $resourceMetadataFactory = $this->createStub(ResourceMetadataCollectionFactoryInterface::class); |
| 65 | + $resourceMetadataFactory->method('create')->willReturnCallback( |
| 66 | + static fn (string $class) => DummyComponent::class === $class |
| 67 | + ? new ResourceMetadataCollection($class, [new ApiResource(shortName: 'DummyComponent')]) |
| 68 | + : new ResourceMetadataCollection($class), |
| 69 | + ); |
| 70 | + |
| 71 | + $metadata = (new PageDataMetadataFactory(self::registryFor($classMetadata), $resourceMetadataFactory))->create(PageDataWithVariants::class); |
| 72 | + |
| 73 | + self::assertSame([['component', DummyComponent::class, 'DummyComponent']], self::propertiesOf($metadata)); |
| 74 | + } |
| 75 | + |
| 76 | + private static function registryFor(ClassMetadata $classMetadata): ManagerRegistry |
| 77 | + { |
| 78 | + $manager = self::createStub(EntityManagerInterface::class); |
| 79 | + $manager->method('getClassMetadata')->willReturn($classMetadata); |
| 80 | + $registry = self::createStub(ManagerRegistry::class); |
| 81 | + $registry->method('getManagerForClass')->willReturn($manager); |
| 82 | + |
| 83 | + return $registry; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @return list<array{string, string, string}> |
| 88 | + */ |
| 89 | + private static function propertiesOf(PageDataMetadata $metadata): array |
| 90 | + { |
| 91 | + return array_values(array_map( |
| 92 | + static fn (PageDataPropertyMetadata $property) => [$property->getProperty(), $property->getComponentClass(), $property->getComponentShortName()], |
| 93 | + $metadata->getProperties()->toArray(), |
| 94 | + )); |
| 95 | + } |
| 96 | +} |
0 commit comments