|
| 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\Tests\Metadata\Resource\Factory; |
| 15 | + |
| 16 | +use ApiPlatform\Metadata\ApiResource; |
| 17 | +use ApiPlatform\Metadata\Get; |
| 18 | +use ApiPlatform\Metadata\Operation; |
| 19 | +use ApiPlatform\Metadata\Resource\Factory\OperationNameResourceMetadataCollectionFactory; |
| 20 | +use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; |
| 21 | +use ApiPlatform\Metadata\Resource\ResourceMetadataCollection; |
| 22 | +use PHPUnit\Framework\TestCase; |
| 23 | +use Prophecy\PhpUnit\ProphecyTrait; |
| 24 | + |
| 25 | +class OperationNameResourceMetadataFactoryTest extends TestCase |
| 26 | +{ |
| 27 | + use ProphecyTrait; |
| 28 | + |
| 29 | + /** |
| 30 | + * @dataProvider operationProvider |
| 31 | + */ |
| 32 | + public function testGeneratesName(Operation $operation, string $expectedOperationName): void |
| 33 | + { |
| 34 | + $decorated = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class); |
| 35 | + $decorated->create('a')->willReturn(new ResourceMetadataCollection('a', [ |
| 36 | + new ApiResource(operations: [$operation]), |
| 37 | + ])); |
| 38 | + |
| 39 | + $operationNameResourceMetadataFactory = new OperationNameResourceMetadataCollectionFactory($decorated->reveal()); |
| 40 | + $result = $operationNameResourceMetadataFactory->create('a'); |
| 41 | + |
| 42 | + $this->assertEquals($operation->withName($expectedOperationName), $result->getOperation($expectedOperationName)); |
| 43 | + } |
| 44 | + |
| 45 | +public function operationProvider(): array |
| 46 | +{ |
| 47 | + return [ |
| 48 | + [new Get(), '_api_a_get'], |
| 49 | + [new Get(shortName: 'Foo'), '_api_Foo_get'], |
| 50 | + [new Get(name: 'test'), 'test'], |
| 51 | + [new Get(routePrefix: 'foo'), '_api_foo_get'], |
| 52 | + [new Get(uriTemplate: '/foo'), '_api_/foo_get'], |
| 53 | + [new Get(routePrefix: '/admin', uriTemplate: '/foo'), '_api_/admin/foo_get'], |
| 54 | + ]; |
| 55 | +} |
| 56 | +} |
0 commit comments