|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <dunglas@gmail.com> |
| 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\Metadata\Tests\Resource\Factory; |
| 15 | + |
| 16 | +use ApiPlatform\Metadata\ApiResource; |
| 17 | +use ApiPlatform\Metadata\HttpOperation; |
| 18 | +use ApiPlatform\Metadata\Mutator\OperationMutatorCollection; |
| 19 | +use ApiPlatform\Metadata\Mutator\ResourceMutatorCollection; |
| 20 | +use ApiPlatform\Metadata\Operation; |
| 21 | +use ApiPlatform\Metadata\OperationMutatorInterface; |
| 22 | +use ApiPlatform\Metadata\Operations; |
| 23 | +use ApiPlatform\Metadata\Resource\Factory\MutatorResourceMetadataCollectionFactory; |
| 24 | +use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; |
| 25 | +use ApiPlatform\Metadata\Resource\ResourceMetadataCollection; |
| 26 | +use ApiPlatform\Metadata\ResourceMutatorInterface; |
| 27 | +use PHPUnit\Framework\TestCase; |
| 28 | + |
| 29 | +final class MutatorResourceMetadataCollectionFactoryTest extends TestCase |
| 30 | +{ |
| 31 | + public function testMutateResource(): void |
| 32 | + { |
| 33 | + $decorated = $this->createMock(ResourceMetadataCollectionFactoryInterface::class); |
| 34 | + $resourceClass = \stdClass::class; |
| 35 | + $resourceMetadataCollection = new ResourceMetadataCollection($resourceClass); |
| 36 | + $resourceMetadataCollection[] = (new ApiResource())->withClass($resourceClass); |
| 37 | + |
| 38 | + $resourceMutatorCollection = new ResourceMutatorCollection(); |
| 39 | + $resourceMutatorCollection->addMutator($resourceClass, new DummyResourceMutator()); |
| 40 | + |
| 41 | + $customResourceMetadataCollectionFactory = new MutatorResourceMetadataCollectionFactory($resourceMutatorCollection, new OperationMutatorCollection(), $decorated); |
| 42 | + |
| 43 | + $decorated->expects($this->once())->method('create')->with($resourceClass)->willReturn( |
| 44 | + $resourceMetadataCollection, |
| 45 | + ); |
| 46 | + |
| 47 | + $resourceMetadataCollection = $customResourceMetadataCollectionFactory->create($resourceClass); |
| 48 | + |
| 49 | + $resource = $resourceMetadataCollection->getIterator()->current(); |
| 50 | + $this->assertInstanceOf(ApiResource::class, $resource); |
| 51 | + $this->assertSame('dummy', $resource->getShortName()); |
| 52 | + } |
| 53 | + |
| 54 | + public function testMutateOperation(): void |
| 55 | + { |
| 56 | + $decorated = $this->createMock(ResourceMetadataCollectionFactoryInterface::class); |
| 57 | + $resourceClass = \stdClass::class; |
| 58 | + |
| 59 | + $operations = new Operations(); |
| 60 | + $operations->add('_api_Dummy_get', new HttpOperation()); |
| 61 | + |
| 62 | + $resourceMetadataCollection = new ResourceMetadataCollection($resourceClass); |
| 63 | + $resourceMetadataCollection[] = (new ApiResource())->withClass($resourceClass)->withOperations($operations); |
| 64 | + |
| 65 | + $operationMutatorCollection = new OperationMutatorCollection(); |
| 66 | + $operationMutatorCollection->addMutator('_api_Dummy_get', new DummyOperationMutator()); |
| 67 | + |
| 68 | + $customResourceMetadataCollectionFactory = new MutatorResourceMetadataCollectionFactory(new ResourceMutatorCollection(), $operationMutatorCollection, $decorated); |
| 69 | + |
| 70 | + $decorated->expects($this->once())->method('create')->with($resourceClass)->willReturn( |
| 71 | + $resourceMetadataCollection, |
| 72 | + ); |
| 73 | + |
| 74 | + $resourceMetadataCollection = $customResourceMetadataCollectionFactory->create($resourceClass); |
| 75 | + |
| 76 | + $resource = $resourceMetadataCollection->getIterator()->current(); |
| 77 | + $this->assertInstanceOf(ApiResource::class, $resource); |
| 78 | + $this->assertEquals('custom_dummy', $resourceMetadataCollection->getOperation('_api_Dummy_get')->getShortName()); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +final class DummyResourceMutator implements ResourceMutatorInterface |
| 83 | +{ |
| 84 | + public function __invoke(ApiResource $resource): ApiResource |
| 85 | + { |
| 86 | + return $resource->withShortName('dummy'); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +final class DummyOperationMutator implements OperationMutatorInterface |
| 91 | +{ |
| 92 | + public function __invoke(Operation $operation): Operation |
| 93 | + { |
| 94 | + return $operation->withShortName('custom_dummy'); |
| 95 | + } |
| 96 | +} |
0 commit comments