|
| 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\State\Tests\Processor; |
| 15 | + |
| 16 | +use ApiPlatform\Hal\Tests\Fixtures\Dummy; |
| 17 | +use ApiPlatform\Metadata\ApiResource; |
| 18 | +use ApiPlatform\Metadata\Delete; |
| 19 | +use ApiPlatform\Metadata\Error; |
| 20 | +use ApiPlatform\Metadata\Get; |
| 21 | +use ApiPlatform\Metadata\GetCollection; |
| 22 | +use ApiPlatform\Metadata\Post; |
| 23 | +use ApiPlatform\Metadata\Put; |
| 24 | +use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; |
| 25 | +use ApiPlatform\Metadata\Resource\ResourceMetadataCollection; |
| 26 | +use ApiPlatform\Metadata\ResourceClassResolverInterface; |
| 27 | +use ApiPlatform\State\Processor\LinkedDataPlatformProcessor; |
| 28 | +use ApiPlatform\State\ProcessorInterface; |
| 29 | +use PHPUnit\Framework\MockObject\MockObject; |
| 30 | +use PHPUnit\Framework\TestCase; |
| 31 | +use Symfony\Component\HttpFoundation\Request; |
| 32 | +use Symfony\Component\HttpFoundation\Response; |
| 33 | + |
| 34 | +class LinkedDataPlatformProcessorTest extends TestCase |
| 35 | +{ |
| 36 | + private ResourceMetadataCollectionFactoryInterface&MockObject $resourceMetadataCollectionFactory; |
| 37 | + |
| 38 | + private ResourceClassResolverInterface&MockObject $resourceClassResolver; |
| 39 | + |
| 40 | + private ProcessorInterface&MockObject $decorated; |
| 41 | + |
| 42 | + protected function setUp(): void |
| 43 | + { |
| 44 | + $this->resourceClassResolver = $this->createMock(ResourceClassResolverInterface::class); |
| 45 | + $this->resourceClassResolver |
| 46 | + ->method('isResourceClass') |
| 47 | + ->willReturn(true); |
| 48 | + |
| 49 | + $this->resourceMetadataCollectionFactory = $this->createMock(ResourceMetadataCollectionFactoryInterface::class); |
| 50 | + $this->resourceMetadataCollectionFactory |
| 51 | + ->method('create') |
| 52 | + ->willReturn( |
| 53 | + new ResourceMetadataCollection($this->getResourceClassName(), [ |
| 54 | + new ApiResource(operations: [ |
| 55 | + new Get(uriTemplate: '/dummy/{dummyResourceId}{._format}', class: $this->getResourceClassName(), name: 'get'), |
| 56 | + new GetCollection(uriTemplate: '/dummy{._format}', class: $this->getResourceClassName(), name: 'get_collections'), |
| 57 | + new Post(uriTemplate: '/dummy{._format}', outputFormats: ['jsonld' => ['application/ld+json'], 'text/turtle' => ['text/turtle']], class: $this->getResourceClassName(), name: 'post'), |
| 58 | + new Delete(uriTemplate: '/dummy/{dummyResourceId}{._format}', class: $this->getResourceClassName(), name: 'delete'), |
| 59 | + new Put(uriTemplate: '/dummy/{dummyResourceId}{._format}', class: $this->getResourceClassName(), name: 'put'), |
| 60 | + ]), |
| 61 | + ]) |
| 62 | + ); |
| 63 | + |
| 64 | + $this->decorated = $this->createMock(ProcessorInterface::class); |
| 65 | + $this->decorated->method('process')->willReturn(new Response()); |
| 66 | + } |
| 67 | + |
| 68 | + public function testHeadersAcceptPostIsReturnWhenPostAllowed(): void |
| 69 | + { |
| 70 | + /** @var class-string $dummy */ |
| 71 | + $dummy = 'dummy'; |
| 72 | + $operation = new Get('/dummy{._format}', class: $this->getResourceClassName()); |
| 73 | + |
| 74 | + $context = $this->getContext(); |
| 75 | + |
| 76 | + $processor = new LinkedDataPlatformProcessor( |
| 77 | + $this->decorated, |
| 78 | + $this->resourceClassResolver, |
| 79 | + $this->resourceMetadataCollectionFactory |
| 80 | + ); |
| 81 | + /** @var Response $response */ |
| 82 | + $response = $processor->process(null, $operation, [], $context); |
| 83 | + |
| 84 | + $this->assertSame('application/ld+json, text/turtle', $response->headers->get('Accept-Post')); |
| 85 | + } |
| 86 | + |
| 87 | + public function testHeadersAcceptPostIsNotSetWhenPostIsNotAllowed(): void |
| 88 | + { |
| 89 | + $operation = new Get('/dummy/{dummyResourceId}{._format}', class: $this->getResourceClassName()); |
| 90 | + $context = $this->getContext(); |
| 91 | + |
| 92 | + $processor = new LinkedDataPlatformProcessor( |
| 93 | + $this->decorated, |
| 94 | + $this->resourceClassResolver, |
| 95 | + $this->resourceMetadataCollectionFactory |
| 96 | + ); |
| 97 | + /** @var Response $response */ |
| 98 | + $response = $processor->process(null, $operation, [], $context); |
| 99 | + |
| 100 | + $this->assertNull($response->headers->get('Accept-Post')); |
| 101 | + } |
| 102 | + |
| 103 | + public function testHeaderAllowReflectsResourceAllowedMethods(): void |
| 104 | + { |
| 105 | + $operation = new Get('/dummy{._format}', class: $this->getResourceClassName()); |
| 106 | + $context = $this->getContext(); |
| 107 | + |
| 108 | + $processor = new LinkedDataPlatformProcessor( |
| 109 | + $this->decorated, |
| 110 | + $this->resourceClassResolver, |
| 111 | + $this->resourceMetadataCollectionFactory |
| 112 | + ); |
| 113 | + /** @var Response $response */ |
| 114 | + $response = $processor->process(null, $operation, [], $context); |
| 115 | + $allowHeader = $response->headers->get('Allow'); |
| 116 | + $this->assertStringContainsString('OPTIONS', $allowHeader); |
| 117 | + $this->assertStringContainsString('HEAD', $allowHeader); |
| 118 | + $this->assertStringContainsString('GET', $allowHeader); |
| 119 | + $this->assertStringContainsString('POST', $allowHeader); |
| 120 | + $operation = new Get('/dummy/{dummyResourceId}{._format}', class: $this->getResourceClassName()); |
| 121 | + |
| 122 | + /** @var Response $response */ |
| 123 | + $processor = new LinkedDataPlatformProcessor( |
| 124 | + $this->decorated, |
| 125 | + $this->resourceClassResolver, |
| 126 | + $this->resourceMetadataCollectionFactory |
| 127 | + ); |
| 128 | + /** @var Response $response */ |
| 129 | + $response = $processor->process('data', $operation, [], $this->getContext()); |
| 130 | + $allowHeader = $response->headers->get('Allow'); |
| 131 | + $this->assertStringContainsString('OPTIONS', $allowHeader); |
| 132 | + $this->assertStringContainsString('HEAD', $allowHeader); |
| 133 | + $this->assertStringContainsString('GET', $allowHeader); |
| 134 | + $this->assertStringContainsString('PUT', $allowHeader); |
| 135 | + $this->assertStringContainsString('DELETE', $allowHeader); |
| 136 | + } |
| 137 | + |
| 138 | + public function testProcessorWithoutRequiredConditionReturnOriginalResponse(): void |
| 139 | + { |
| 140 | + // Operation is an Error |
| 141 | + $processor = new LinkedDataPlatformProcessor($this->decorated, $this->resourceClassResolver, $this->resourceMetadataCollectionFactory); |
| 142 | + $response = $processor->process(null, new Error(), $this->getContext()); |
| 143 | + $this->assertNull($response->headers->get('Allow')); |
| 144 | + } |
| 145 | + |
| 146 | + private function createGetRequest(): Request |
| 147 | + { |
| 148 | + $request = new Request(); |
| 149 | + $request->setMethod('GET'); |
| 150 | + $request->setRequestFormat('json'); |
| 151 | + $request->headers->set('Accept', 'application/ld+json'); |
| 152 | + |
| 153 | + return $request; |
| 154 | + } |
| 155 | + |
| 156 | + private function getContext(): array |
| 157 | + { |
| 158 | + return [ |
| 159 | + 'resource_class' => $this->getResourceClassName(), |
| 160 | + 'request' => $this->createGetRequest(), |
| 161 | + ]; |
| 162 | + } |
| 163 | + |
| 164 | + private function getResourceClassName(): string |
| 165 | + { |
| 166 | + return Dummy::class; |
| 167 | + } |
| 168 | +} |
0 commit comments