|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <[email protected]> |
| 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\Metadata\ApiResource; |
| 17 | +use ApiPlatform\Metadata\Delete; |
| 18 | +use ApiPlatform\Metadata\Get; |
| 19 | +use ApiPlatform\Metadata\HttpOperation; |
| 20 | +use ApiPlatform\Metadata\Patch; |
| 21 | +use ApiPlatform\Metadata\Post; |
| 22 | +use ApiPlatform\Metadata\Put; |
| 23 | +use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; |
| 24 | +use ApiPlatform\Metadata\Resource\ResourceMetadataCollection; |
| 25 | +use ApiPlatform\Metadata\ResourceClassResolverInterface; |
| 26 | +use ApiPlatform\State\Processor\RespondProcessor; |
| 27 | +use PHPUnit\Framework\MockObject\MockObject; |
| 28 | +use PHPUnit\Framework\TestCase; |
| 29 | +use Symfony\Component\HttpFoundation\Request; |
| 30 | +use Symfony\Component\HttpFoundation\Response; |
| 31 | + |
| 32 | +class RespondProcessorTest extends TestCase |
| 33 | +{ |
| 34 | + private ResourceMetadataCollectionFactoryInterface&MockObject $resourceMetadataCollectionFactory; |
| 35 | + private RespondProcessor $processor; |
| 36 | + |
| 37 | + protected function setUp(): void |
| 38 | + { |
| 39 | + $resourceClassResolver = $this->createMock(ResourceClassResolverInterface::class); |
| 40 | + $resourceClassResolver |
| 41 | + ->method('isResourceClass') |
| 42 | + ->willReturn(true); |
| 43 | + |
| 44 | + $this->resourceMetadataCollectionFactory = $this->createMock(ResourceMetadataCollectionFactoryInterface::class); |
| 45 | + |
| 46 | + $this->processor = new RespondProcessor( |
| 47 | + null, |
| 48 | + $resourceClassResolver, |
| 49 | + null, |
| 50 | + $this->resourceMetadataCollectionFactory |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + public function testHeadersAcceptPostIsSetCorrectly(): void |
| 55 | + { |
| 56 | + $this->resourceMetadataCollectionFactory |
| 57 | + ->method('create') |
| 58 | + ->willReturn(new ResourceMetadataCollection('DummyResourceClass')); |
| 59 | + |
| 60 | + $operation = new HttpOperation('GET'); |
| 61 | + $context = [ |
| 62 | + 'resource_class' => 'SomeResourceClass', |
| 63 | + 'request' => $this->createGetRequest(), |
| 64 | + ]; |
| 65 | + |
| 66 | + /** @var Response $response */ |
| 67 | + $response = $this->processor->process(null, $operation, [], $context); |
| 68 | + |
| 69 | + $this->assertSame('text/turtle, application/ld+json', $response->headers->get('Accept-Post')); |
| 70 | + } |
| 71 | + |
| 72 | + public function testHeaderAllowHasHeadOptionsByDefault(): void |
| 73 | + { |
| 74 | + $this->resourceMetadataCollectionFactory |
| 75 | + ->method('create') |
| 76 | + ->willReturn(new ResourceMetadataCollection('DummyResourceClass')); |
| 77 | + |
| 78 | + $operation = new HttpOperation('GET'); |
| 79 | + $context = [ |
| 80 | + 'resource_class' => 'SomeResourceClass', |
| 81 | + 'request' => $this->createGetRequest(), |
| 82 | + ]; |
| 83 | + |
| 84 | + /** @var Response $response */ |
| 85 | + $response = $this->processor->process(null, $operation, [], $context); |
| 86 | + |
| 87 | + $this->assertSame('OPTIONS, HEAD', $response->headers->get('Allow')); |
| 88 | + } |
| 89 | + |
| 90 | + public function testHeaderAllowReflectsResourceAllowedMethods(): void |
| 91 | + { |
| 92 | + $this->resourceMetadataCollectionFactory |
| 93 | + ->method('create') |
| 94 | + ->willReturn( |
| 95 | + new ResourceMetadataCollection('DummyResource', [ |
| 96 | + new ApiResource(operations: [ |
| 97 | + 'get' => new Get(name: 'get'), |
| 98 | + 'post' => new Post(name: 'post'), |
| 99 | + 'delete' => new Delete(name: 'delete'), |
| 100 | + ]), |
| 101 | + ]) |
| 102 | + ); |
| 103 | + |
| 104 | + $operation = new HttpOperation('GET'); |
| 105 | + $context = [ |
| 106 | + 'resource_class' => 'SomeResourceClass', |
| 107 | + 'request' => $this->createGetRequest(), |
| 108 | + ]; |
| 109 | + |
| 110 | + /** @var Response $response */ |
| 111 | + $response = $this->processor->process(null, $operation, [], $context); |
| 112 | + |
| 113 | + $allowHeader = $response->headers->get('Allow'); |
| 114 | + $this->assertStringContainsString('OPTIONS', $allowHeader); |
| 115 | + $this->assertStringContainsString('HEAD', $allowHeader); |
| 116 | + $this->assertStringContainsString('GET', $allowHeader); |
| 117 | + $this->assertStringContainsString('POST', $allowHeader); |
| 118 | + $this->assertStringContainsString('DELETE', $allowHeader); |
| 119 | + $this->assertStringNotContainsString('PATCH', $allowHeader); |
| 120 | + $this->assertStringNotContainsString('PUT', $allowHeader); |
| 121 | + } |
| 122 | + |
| 123 | + public function testHeaderAllowReflectsAllowedResourcesGetPutPatch(): void |
| 124 | + { |
| 125 | + $this->resourceMetadataCollectionFactory |
| 126 | + ->method('create') |
| 127 | + ->willReturn( |
| 128 | + new ResourceMetadataCollection('DummyResource', [ |
| 129 | + new ApiResource(operations: [ |
| 130 | + 'get' => new Get(name: 'get'), |
| 131 | + 'patch' => new Patch(name: 'patch'), |
| 132 | + 'put' => new Put(name: 'put'), |
| 133 | + ]), |
| 134 | + ]) |
| 135 | + ); |
| 136 | + |
| 137 | + $operation = new HttpOperation('GET'); |
| 138 | + $context = [ |
| 139 | + 'resource_class' => 'SomeResourceClass', |
| 140 | + 'request' => $this->createGetRequest(), |
| 141 | + ]; |
| 142 | + |
| 143 | + /** @var Response $response */ |
| 144 | + $response = $this->processor->process(null, $operation, [], $context); |
| 145 | + |
| 146 | + $allowHeader = $response->headers->get('Allow'); |
| 147 | + $this->assertStringContainsString('OPTIONS', $allowHeader); |
| 148 | + $this->assertStringContainsString('HEAD', $allowHeader); |
| 149 | + $this->assertStringContainsString('GET', $allowHeader); |
| 150 | + $this->assertStringContainsString('PATCH', $allowHeader); |
| 151 | + $this->assertStringContainsString('PUT', $allowHeader); |
| 152 | + $this->assertStringNotContainsString('POST', $allowHeader); |
| 153 | + $this->assertStringNotContainsString('DELETE', $allowHeader); |
| 154 | + } |
| 155 | + |
| 156 | + private function createGetRequest(): Request |
| 157 | + { |
| 158 | + $request = new Request(); |
| 159 | + $request->setMethod('GET'); |
| 160 | + $request->setRequestFormat('json'); |
| 161 | + $request->headers->set('Accept', 'application/ld+json'); |
| 162 | + |
| 163 | + return $request; |
| 164 | + } |
| 165 | +} |
0 commit comments