|  | 
|  | 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(Dummy::class, [ | 
|  | 54 | +                    new ApiResource(operations: [ | 
|  | 55 | +                        new Get(uriTemplate: '/dummy/{dummyResourceId}{._format}', class: Dummy::class, name: 'get'), | 
|  | 56 | +                        new GetCollection(uriTemplate: '/dummy{._format}', class: Dummy::class, name: 'get_collections'), | 
|  | 57 | +                        new Post(uriTemplate: '/dummy{._format}', outputFormats: ['jsonld' => ['application/ld+json'], 'text/turtle' => ['text/turtle']], class: Dummy::class, name: 'post'), | 
|  | 58 | +                        new Delete(uriTemplate: '/dummy/{dummyResourceId}{._format}', class: Dummy::class, name: 'delete'), | 
|  | 59 | +                        new Put(uriTemplate: '/dummy/{dummyResourceId}{._format}', class: Dummy::class, 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 | +        $operation = new Get('/dummy{._format}', class: Dummy::class); | 
|  | 71 | + | 
|  | 72 | +        $context = $this->getContext(); | 
|  | 73 | + | 
|  | 74 | +        $processor = new LinkedDataPlatformProcessor( | 
|  | 75 | +            $this->decorated, | 
|  | 76 | +            $this->resourceClassResolver, | 
|  | 77 | +            $this->resourceMetadataCollectionFactory | 
|  | 78 | +        ); | 
|  | 79 | +        /** @var Response $response */ | 
|  | 80 | +        $response = $processor->process(null, $operation, [], $context); | 
|  | 81 | + | 
|  | 82 | +        $this->assertSame('application/ld+json, text/turtle', $response->headers->get('Accept-Post')); | 
|  | 83 | +    } | 
|  | 84 | + | 
|  | 85 | +    public function testHeadersAcceptPostIsNotSetWhenPostIsNotAllowed(): void | 
|  | 86 | +    { | 
|  | 87 | +        $operation = new Get('/dummy/{dummyResourceId}{._format}', class: Dummy::class); | 
|  | 88 | +        $context = $this->getContext(); | 
|  | 89 | + | 
|  | 90 | +        $processor = new LinkedDataPlatformProcessor( | 
|  | 91 | +            $this->decorated, | 
|  | 92 | +            $this->resourceClassResolver, | 
|  | 93 | +            $this->resourceMetadataCollectionFactory | 
|  | 94 | +        ); | 
|  | 95 | +        /** @var Response $response */ | 
|  | 96 | +        $response = $processor->process(null, $operation, [], $context); | 
|  | 97 | + | 
|  | 98 | +        $this->assertNull($response->headers->get('Accept-Post')); | 
|  | 99 | +    } | 
|  | 100 | + | 
|  | 101 | +    public function testHeaderAllowReflectsResourceAllowedMethods(): void | 
|  | 102 | +    { | 
|  | 103 | +        $operation = new Get('/dummy{._format}', class: Dummy::class); | 
|  | 104 | +        $context = $this->getContext(); | 
|  | 105 | + | 
|  | 106 | +        $processor = new LinkedDataPlatformProcessor( | 
|  | 107 | +            $this->decorated, | 
|  | 108 | +            $this->resourceClassResolver, | 
|  | 109 | +            $this->resourceMetadataCollectionFactory | 
|  | 110 | +        ); | 
|  | 111 | +        /** @var Response $response */ | 
|  | 112 | +        $response = $processor->process(null, $operation, [], $context); | 
|  | 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 | +        $operation = new Get('/dummy/{dummyResourceId}{._format}', class: Dummy::class); | 
|  | 119 | + | 
|  | 120 | +        /** @var Response $response */ | 
|  | 121 | +        $processor = new LinkedDataPlatformProcessor( | 
|  | 122 | +            $this->decorated, | 
|  | 123 | +            $this->resourceClassResolver, | 
|  | 124 | +            $this->resourceMetadataCollectionFactory | 
|  | 125 | +        ); | 
|  | 126 | +        /** @var Response $response */ | 
|  | 127 | +        $response = $processor->process('data', $operation, [], $this->getContext()); | 
|  | 128 | +        $allowHeader = $response->headers->get('Allow'); | 
|  | 129 | +        $this->assertStringContainsString('OPTIONS', $allowHeader); | 
|  | 130 | +        $this->assertStringContainsString('HEAD', $allowHeader); | 
|  | 131 | +        $this->assertStringContainsString('GET', $allowHeader); | 
|  | 132 | +        $this->assertStringContainsString('PUT', $allowHeader); | 
|  | 133 | +        $this->assertStringContainsString('DELETE', $allowHeader); | 
|  | 134 | +    } | 
|  | 135 | + | 
|  | 136 | +    public function testProcessorWithoutRequiredConditionReturnOriginalResponse(): void | 
|  | 137 | +    { | 
|  | 138 | +        // Operation is an Error | 
|  | 139 | +        $processor = new LinkedDataPlatformProcessor($this->decorated, $this->resourceClassResolver, $this->resourceMetadataCollectionFactory); | 
|  | 140 | +        $response = $processor->process(null, new Error(), $this->getContext()); | 
|  | 141 | +        $this->assertNull($response->headers->get('Allow')); | 
|  | 142 | +    } | 
|  | 143 | + | 
|  | 144 | +    private function createGetRequest(): Request | 
|  | 145 | +    { | 
|  | 146 | +        $request = new Request(); | 
|  | 147 | +        $request->setMethod('GET'); | 
|  | 148 | +        $request->setRequestFormat('json'); | 
|  | 149 | +        $request->headers->set('Accept', 'application/ld+json'); | 
|  | 150 | + | 
|  | 151 | +        return $request; | 
|  | 152 | +    } | 
|  | 153 | + | 
|  | 154 | +    private function getContext(): array | 
|  | 155 | +    { | 
|  | 156 | +        return [ | 
|  | 157 | +            'resource_class' => Dummy::class, | 
|  | 158 | +            'request' => $this->createGetRequest(), | 
|  | 159 | +        ]; | 
|  | 160 | +    } | 
|  | 161 | +} | 
0 commit comments