|
| 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\GraphQl\Tests\Metadata; |
| 15 | + |
| 16 | +use ApiPlatform\GraphQl\Metadata\RuntimeOperationMetadataFactory; |
| 17 | +use ApiPlatform\Metadata\ApiResource; |
| 18 | +use ApiPlatform\Metadata\GraphQl\Query; |
| 19 | +use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; |
| 20 | +use ApiPlatform\Metadata\Resource\ResourceMetadataCollection; |
| 21 | +use PHPUnit\Framework\TestCase; |
| 22 | +use Symfony\Component\Routing\Exception\ResourceNotFoundException; |
| 23 | +use Symfony\Component\Routing\RouterInterface; |
| 24 | + |
| 25 | +class RuntimeOperationMetadataFactoryTest extends TestCase |
| 26 | +{ |
| 27 | + public function testCreate(): void |
| 28 | + { |
| 29 | + $resourceClass = 'Dummy'; |
| 30 | + $operationName = 'item_query'; |
| 31 | + |
| 32 | + $operation = (new Query())->withName($operationName); |
| 33 | + $resourceMetadata = (new ApiResource())->withGraphQlOperations([$operationName => $operation]); |
| 34 | + $resourceMetadataCollection = new ResourceMetadataCollection($resourceClass, [$resourceMetadata]); |
| 35 | + |
| 36 | + $resourceMetadataCollectionFactory = $this->createMock(ResourceMetadataCollectionFactoryInterface::class); |
| 37 | + $resourceMetadataCollectionFactory->expects($this->once()) |
| 38 | + ->method('create') |
| 39 | + ->with($resourceClass) |
| 40 | + ->willReturn($resourceMetadataCollection); |
| 41 | + |
| 42 | + $router = $this->createMock(RouterInterface::class); |
| 43 | + $router->expects($this->once()) |
| 44 | + ->method('match') |
| 45 | + ->with('/dummies/1') |
| 46 | + ->willReturn([ |
| 47 | + '_api_resource_class' => $resourceClass, |
| 48 | + '_api_operation_name' => $operationName, |
| 49 | + ]); |
| 50 | + |
| 51 | + $factory = new RuntimeOperationMetadataFactory($resourceMetadataCollectionFactory, $router); |
| 52 | + $this->assertEquals($operation, $factory->create('/dummies/1')); |
| 53 | + } |
| 54 | + |
| 55 | + public function testCreateThrowsExceptionWhenRouteNotFound(): void |
| 56 | + { |
| 57 | + $this->expectException(\ApiPlatform\Metadata\Exception\InvalidArgumentException::class); |
| 58 | + $this->expectExceptionMessage('No route matches "/unknown".'); |
| 59 | + |
| 60 | + $router = $this->createMock(RouterInterface::class); |
| 61 | + $router->expects($this->once()) |
| 62 | + ->method('match') |
| 63 | + ->with('/unknown') |
| 64 | + ->willThrowException(new ResourceNotFoundException()); |
| 65 | + |
| 66 | + $resourceMetadataCollectionFactory = $this->createMock(ResourceMetadataCollectionFactoryInterface::class); |
| 67 | + |
| 68 | + $factory = new RuntimeOperationMetadataFactory($resourceMetadataCollectionFactory, $router); |
| 69 | + $factory->create('/unknown'); |
| 70 | + } |
| 71 | + |
| 72 | + public function testCreateThrowsExceptionWhenResourceClassMissing(): void |
| 73 | + { |
| 74 | + $this->expectException(\ApiPlatform\Metadata\Exception\InvalidArgumentException::class); |
| 75 | + $this->expectExceptionMessage('The route "/dummies/1" is not an API route, it has no resource class in the defaults.'); |
| 76 | + |
| 77 | + $router = $this->createMock(RouterInterface::class); |
| 78 | + $router->expects($this->once()) |
| 79 | + ->method('match') |
| 80 | + ->with('/dummies/1') |
| 81 | + ->willReturn([]); |
| 82 | + |
| 83 | + $resourceMetadataCollectionFactory = $this->createMock(ResourceMetadataCollectionFactoryInterface::class); |
| 84 | + |
| 85 | + $factory = new RuntimeOperationMetadataFactory($resourceMetadataCollectionFactory, $router); |
| 86 | + $factory->create('/dummies/1'); |
| 87 | + } |
| 88 | + |
| 89 | + public function testCreateThrowsExceptionWhenOperationNotFound(): void |
| 90 | + { |
| 91 | + $this->expectException(\ApiPlatform\Metadata\Exception\InvalidArgumentException::class); |
| 92 | + $this->expectExceptionMessage('No operation found for id "/dummies/1".'); |
| 93 | + |
| 94 | + $resourceClass = 'Dummy'; |
| 95 | + |
| 96 | + $resourceMetadataCollectionFactory = $this->createMock(ResourceMetadataCollectionFactoryInterface::class); |
| 97 | + $resourceMetadataCollectionFactory->expects($this->once()) |
| 98 | + ->method('create') |
| 99 | + ->with($resourceClass) |
| 100 | + ->willReturn(new ResourceMetadataCollection($resourceClass, [new ApiResource()])); |
| 101 | + |
| 102 | + $router = $this->createMock(RouterInterface::class); |
| 103 | + $router->expects($this->once()) |
| 104 | + ->method('match') |
| 105 | + ->with('/dummies/1') |
| 106 | + ->willReturn([ |
| 107 | + '_api_resource_class' => $resourceClass, |
| 108 | + ]); |
| 109 | + |
| 110 | + $factory = new RuntimeOperationMetadataFactory($resourceMetadataCollectionFactory, $router); |
| 111 | + $factory->create('/dummies/1'); |
| 112 | + } |
| 113 | + |
| 114 | + public function testCreateIgnoresOperationsWithResolvers(): void |
| 115 | + { |
| 116 | + $this->expectException(\ApiPlatform\Metadata\Exception\InvalidArgumentException::class); |
| 117 | + $this->expectExceptionMessage('No operation found for id "/dummies/1".'); |
| 118 | + |
| 119 | + $resourceClass = 'Dummy'; |
| 120 | + $operationName = 'item_query'; |
| 121 | + |
| 122 | + $operation = (new Query())->withResolver('t')->withName($operationName); |
| 123 | + $resourceMetadata = (new ApiResource())->withGraphQlOperations([$operationName => $operation]); |
| 124 | + $resourceMetadataCollection = new ResourceMetadataCollection($resourceClass, [$resourceMetadata]); |
| 125 | + |
| 126 | + $resourceMetadataCollectionFactory = $this->createMock(ResourceMetadataCollectionFactoryInterface::class); |
| 127 | + $resourceMetadataCollectionFactory->expects($this->once()) |
| 128 | + ->method('create') |
| 129 | + ->with($resourceClass) |
| 130 | + ->willReturn($resourceMetadataCollection); |
| 131 | + |
| 132 | + $router = $this->createMock(RouterInterface::class); |
| 133 | + $router->expects($this->once()) |
| 134 | + ->method('match') |
| 135 | + ->with('/dummies/1') |
| 136 | + ->willReturn([ |
| 137 | + '_api_resource_class' => $resourceClass, |
| 138 | + '_api_operation_name' => $operationName, |
| 139 | + ]); |
| 140 | + |
| 141 | + $factory = new RuntimeOperationMetadataFactory($resourceMetadataCollectionFactory, $router); |
| 142 | + $factory->create('/dummies/1'); |
| 143 | + } |
| 144 | +} |
0 commit comments