|
| 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\Laravel\Tests\Unit\Routing; |
| 15 | + |
| 16 | +use ApiPlatform\Laravel\Routing\IriConverter; |
| 17 | +use ApiPlatform\Metadata\ApiResource; |
| 18 | +use ApiPlatform\Metadata\Get; |
| 19 | +use ApiPlatform\Metadata\GetCollection; |
| 20 | +use ApiPlatform\Metadata\IdentifiersExtractorInterface; |
| 21 | +use ApiPlatform\Metadata\Operation\Factory\OperationMetadataFactoryInterface; |
| 22 | +use ApiPlatform\Metadata\Operations; |
| 23 | +use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; |
| 24 | +use ApiPlatform\Metadata\Resource\ResourceMetadataCollection; |
| 25 | +use ApiPlatform\Metadata\ResourceClassResolverInterface; |
| 26 | +use ApiPlatform\Metadata\UrlGeneratorInterface; |
| 27 | +use ApiPlatform\State\ProviderInterface; |
| 28 | +use PHPUnit\Framework\TestCase; |
| 29 | +use Symfony\Component\Routing\RouterInterface; |
| 30 | +use Workbench\App\Models\Book; |
| 31 | + |
| 32 | +class IriConverterTest extends TestCase |
| 33 | +{ |
| 34 | + public function testLocalCacheKeyDistinguishesItemAndCollectionForStringResource(): void |
| 35 | + { |
| 36 | + $collectionOpName = 'collection_op'; |
| 37 | + $itemOpName = 'item_op'; |
| 38 | + |
| 39 | + $collectionOp = (new GetCollection())->withName($collectionOpName)->withClass(Book::class); |
| 40 | + $itemOp = (new Get())->withName($itemOpName)->withClass(Book::class); |
| 41 | + |
| 42 | + $router = $this->createMock(RouterInterface::class); |
| 43 | + $router->expects($this->exactly(2)) |
| 44 | + ->method('generate') |
| 45 | + ->willReturnCallback(function (string $routeName, array $params) use ($collectionOpName, $itemOpName): string { |
| 46 | + if ($collectionOpName === $routeName) { |
| 47 | + return '/api/books'; |
| 48 | + } |
| 49 | + if ($itemOpName === $routeName) { |
| 50 | + return '/api/books/'.$params['id']; |
| 51 | + } |
| 52 | + $this->fail(\sprintf('Unexpected route name "%s".', $routeName)); |
| 53 | + }); |
| 54 | + |
| 55 | + $metadataCollection = new ResourceMetadataCollection(Book::class, [ |
| 56 | + (new ApiResource())->withOperations(new Operations([ |
| 57 | + $collectionOpName => $collectionOp, |
| 58 | + $itemOpName => $itemOp, |
| 59 | + ])), |
| 60 | + ]); |
| 61 | + |
| 62 | + $resourceMetadataFactory = $this->createStub(ResourceMetadataCollectionFactoryInterface::class); |
| 63 | + $resourceMetadataFactory->method('create')->willReturn($metadataCollection); |
| 64 | + |
| 65 | + $resourceClassResolver = $this->createStub(ResourceClassResolverInterface::class); |
| 66 | + $resourceClassResolver->method('isResourceClass')->willReturn(true); |
| 67 | + |
| 68 | + $iriConverter = new IriConverter( |
| 69 | + $this->createStub(ProviderInterface::class), |
| 70 | + $this->createStub(OperationMetadataFactoryInterface::class), |
| 71 | + $router, |
| 72 | + $this->createStub(IdentifiersExtractorInterface::class), |
| 73 | + $resourceClassResolver, |
| 74 | + $resourceMetadataFactory, |
| 75 | + ); |
| 76 | + |
| 77 | + $this->assertSame('/api/books', $iriConverter->getIriFromResource( |
| 78 | + Book::class, |
| 79 | + UrlGeneratorInterface::ABS_PATH, |
| 80 | + new GetCollection(), |
| 81 | + )); |
| 82 | + |
| 83 | + $this->assertSame('/api/books/1', $iriConverter->getIriFromResource( |
| 84 | + Book::class, |
| 85 | + UrlGeneratorInterface::ABS_PATH, |
| 86 | + new Get(), |
| 87 | + ['uri_variables' => ['id' => 1]], |
| 88 | + )); |
| 89 | + } |
| 90 | +} |
0 commit comments