Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(state): add headers to comply with LDP specification #6917

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
17 changes: 15 additions & 2 deletions src/Laravel/ApiPlatformProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@
use ApiPlatform\State\Pagination\PaginationOptions;
use ApiPlatform\State\ParameterProviderInterface;
use ApiPlatform\State\Processor\AddLinkHeaderProcessor;
use ApiPlatform\State\Processor\LinkedDataPlatformProcessor;
use ApiPlatform\State\Processor\RespondProcessor;
use ApiPlatform\State\Processor\SerializeProcessor;
use ApiPlatform\State\Processor\WriteProcessor;
Expand Down Expand Up @@ -555,11 +556,23 @@
});

$this->app->singleton(RespondProcessor::class, function () {
return new AddLinkHeaderProcessor(new RespondProcessor(), new HttpHeaderSerializer());
return new RespondProcessor();
});

$this->app->singleton(AddLinkHeaderProcessor::class, function (Application $app) {

Check warning on line 562 in src/Laravel/ApiPlatformProvider.php

View check run for this annotation

Codecov / codecov/patch

src/Laravel/ApiPlatformProvider.php#L560-L562

Added lines #L560 - L562 were not covered by tests
return new AddLinkHeaderProcessor($app->make(RespondProcessor::class), new HttpHeaderSerializer());
});

$this->app->singleton(LinkedDataPlatformProcessor::class, function (Application $app) {

Check warning on line 566 in src/Laravel/ApiPlatformProvider.php

View check run for this annotation

Codecov / codecov/patch

src/Laravel/ApiPlatformProvider.php#L564-L566

Added lines #L564 - L566 were not covered by tests
return new LinkedDataPlatformProcessor(
$app->make(AddLinkHeaderProcessor::class), // Original service

Check warning on line 568 in src/Laravel/ApiPlatformProvider.php

View check run for this annotation

Codecov / codecov/patch

src/Laravel/ApiPlatformProvider.php#L568

Added line #L568 was not covered by tests
$app->make(ResourceClassResolverInterface::class),
$app->make(ResourceMetadataCollectionFactoryInterface::class)

Check warning on line 570 in src/Laravel/ApiPlatformProvider.php

View check run for this annotation

Codecov / codecov/patch

src/Laravel/ApiPlatformProvider.php#L570

Added line #L570 was not covered by tests
);
});

$this->app->singleton(SerializeProcessor::class, function (Application $app) {
return new SerializeProcessor($app->make(RespondProcessor::class), $app->make(Serializer::class), $app->make(SerializerContextBuilderInterface::class));
return new SerializeProcessor($app->make(LinkedDataPlatformProcessor::class), $app->make(Serializer::class), $app->make(SerializerContextBuilderInterface::class));

Check warning on line 575 in src/Laravel/ApiPlatformProvider.php

View check run for this annotation

Codecov / codecov/patch

src/Laravel/ApiPlatformProvider.php#L575

Added line #L575 was not covered by tests
});

$this->app->singleton(WriteProcessor::class, function (Application $app) {
Expand Down
68 changes: 68 additions & 0 deletions src/Laravel/Tests/LinkedDataPlatformTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

use ApiPlatform\Laravel\Test\ApiTestAssertionsTrait;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Orchestra\Testbench\Concerns\WithWorkbench;
use Orchestra\Testbench\TestCase;
use Workbench\App\Models\Book;
use Workbench\Database\Factories\BookFactory;

class LinkedDataPlatformTest extends TestCase
{
use ApiTestAssertionsTrait;
use RefreshDatabase;
use WithWorkbench;

/**
* @param Application $app
*/
protected function defineEnvironment($app): void

Check warning on line 32 in src/Laravel/Tests/LinkedDataPlatformTest.php

View check run for this annotation

Codecov / codecov/patch

src/Laravel/Tests/LinkedDataPlatformTest.php#L32

Added line #L32 was not covered by tests
{
tap($app['config'], function (Repository $config): void {
$config->set('api-platform.formats', ['jsonld' => ['application/ld+json'], 'turtle' => ['text/turtle']]);
$config->set('api-platform.resources', [app_path('Models'), app_path('ApiResource')]);
$config->set('app.debug', true);
});

Check warning on line 38 in src/Laravel/Tests/LinkedDataPlatformTest.php

View check run for this annotation

Codecov / codecov/patch

src/Laravel/Tests/LinkedDataPlatformTest.php#L34-L38

Added lines #L34 - L38 were not covered by tests
}

public function testHeadersAcceptPostIsReturnWhenPostAllowed(): void

Check warning on line 41 in src/Laravel/Tests/LinkedDataPlatformTest.php

View check run for this annotation

Codecov / codecov/patch

src/Laravel/Tests/LinkedDataPlatformTest.php#L41

Added line #L41 was not covered by tests
{
$response = $this->get('/api/books', ['accept' => ['application/ld+json']]);
$response->assertStatus(200);
$response->assertHeader('accept-post', 'application/ld+json, text/turtle, text/html');

Check warning on line 45 in src/Laravel/Tests/LinkedDataPlatformTest.php

View check run for this annotation

Codecov / codecov/patch

src/Laravel/Tests/LinkedDataPlatformTest.php#L43-L45

Added lines #L43 - L45 were not covered by tests
}

public function testHeadersAcceptPostIsNotSetWhenPostIsNotAllowed(): void

Check warning on line 48 in src/Laravel/Tests/LinkedDataPlatformTest.php

View check run for this annotation

Codecov / codecov/patch

src/Laravel/Tests/LinkedDataPlatformTest.php#L48

Added line #L48 was not covered by tests
{
BookFactory::new()->createOne();
$book = Book::first();
$response = $this->get($this->getIriFromResource($book), ['accept' => ['application/ld+json']]);
$response->assertStatus(200);
$response->assertHeaderMissing('accept-post');

Check warning on line 54 in src/Laravel/Tests/LinkedDataPlatformTest.php

View check run for this annotation

Codecov / codecov/patch

src/Laravel/Tests/LinkedDataPlatformTest.php#L50-L54

Added lines #L50 - L54 were not covered by tests
}

public function testHeaderAllowReflectsResourceAllowedMethods(): void

Check warning on line 57 in src/Laravel/Tests/LinkedDataPlatformTest.php

View check run for this annotation

Codecov / codecov/patch

src/Laravel/Tests/LinkedDataPlatformTest.php#L57

Added line #L57 was not covered by tests
{
$response = $this->get('/api/books', ['accept' => ['application/ld+json']]);
$response->assertHeader('allow', 'OPTIONS, HEAD, POST, GET');

Check warning on line 60 in src/Laravel/Tests/LinkedDataPlatformTest.php

View check run for this annotation

Codecov / codecov/patch

src/Laravel/Tests/LinkedDataPlatformTest.php#L59-L60

Added lines #L59 - L60 were not covered by tests

BookFactory::new()->createOne();
$book = Book::first();
$response = $this->get($this->getIriFromResource($book), ['accept' => ['application/ld+json']]);
$response->assertStatus(200);
$response->assertHeader('allow', 'OPTIONS, HEAD, PUT, PATCH, GET, DELETE');

Check warning on line 66 in src/Laravel/Tests/LinkedDataPlatformTest.php

View check run for this annotation

Codecov / codecov/patch

src/Laravel/Tests/LinkedDataPlatformTest.php#L62-L66

Added lines #L62 - L66 were not covered by tests
}
}
76 changes: 76 additions & 0 deletions src/State/Processor/LinkedDataPlatformProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\State\Processor;

use ApiPlatform\Metadata\Error;
use ApiPlatform\Metadata\HttpOperation;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\Metadata\ResourceClassResolverInterface;
use ApiPlatform\State\ProcessorInterface;
use Symfony\Component\HttpFoundation\Response;

/**
* @template T1
* @template T2
*
* @implements ProcessorInterface<T1, T2>
*/
final class LinkedDataPlatformProcessor implements ProcessorInterface
{
private const DEFAULT_ALLOWED_METHODS = ['OPTIONS', 'HEAD'];

/**
* @param ProcessorInterface<T1, T2> $decorated
*/
public function __construct(
private readonly ProcessorInterface $decorated,
private readonly ?ResourceClassResolverInterface $resourceClassResolver = null,
private readonly ?ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null,
) {
}

public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): mixed
{
$response = $this->decorated->process($data, $operation, $uriVariables, $context);
if (
!$response instanceof Response
|| !$operation instanceof HttpOperation
|| $operation instanceof Error
|| !$this->resourceMetadataCollectionFactory
|| !($context['resource_class'] ?? null)
|| !$operation->getUriTemplate()
|| !$this->resourceClassResolver?->isResourceClass($context['resource_class'])
) {
return $response;
}

$allowedMethods = self::DEFAULT_ALLOWED_METHODS;
$resourceCollection = $this->resourceMetadataCollectionFactory->create($context['resource_class']);
foreach ($resourceCollection as $resource) {
foreach ($resource->getOperations() as $op) {
if ($op->getUriTemplate() === $operation->getUriTemplate()) {
$allowedMethods[] = $method = $op->getMethod();
if ('POST' === $method && \is_array($outputFormats = $op->getOutputFormats())) {
$response->headers->set('Accept-Post', implode(', ', array_merge(...array_values($outputFormats))));
}
}
}
}

$response->headers->set('Allow', implode(', ', $allowedMethods));

return $response;
}
}
183 changes: 183 additions & 0 deletions src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\State\Tests\Processor;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Error;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\HttpOperation;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
use ApiPlatform\Metadata\ResourceClassResolverInterface;
use ApiPlatform\State\Processor\LinkedDataPlatformProcessor;
use ApiPlatform\State\ProcessorInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class LinkedDataPlatformProcessorTest extends TestCase
{
private ResourceMetadataCollectionFactoryInterface&MockObject $resourceMetadataCollectionFactory;
private ResourceClassResolverInterface&MockObject $resourceClassResolver;

private ProcessorInterface&MockObject $decorated;

protected function setUp(): void

Check warning on line 41 in src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php

View check run for this annotation

Codecov / codecov/patch

src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php#L41

Added line #L41 was not covered by tests
{
$this->resourceClassResolver = $this->createMock(ResourceClassResolverInterface::class);
$this->resourceClassResolver
->method('isResourceClass')
->willReturn(true);

Check warning on line 46 in src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php

View check run for this annotation

Codecov / codecov/patch

src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php#L43-L46

Added lines #L43 - L46 were not covered by tests

$this->resourceMetadataCollectionFactory = $this->createMock(ResourceMetadataCollectionFactoryInterface::class);
$this->resourceMetadataCollectionFactory
->method('create')
->willReturn(
new ResourceMetadataCollection('DummyResource', [ // todo mock $dummy_resource
new ApiResource(operations: [
new Get(uriTemplate: '/dummy_resources/{dummyResourceId}{._format}', name: 'get'),
new GetCollection(uriTemplate: '/dummy_resources{._format}', name: 'get_collections'),
new Post(uriTemplate: '/dummy_resources{._format}', outputFormats: ['jsonld' => ['application/ld+json'], 'text/turtle' => ['text/turtle']], name: 'post'),
new Delete(uriTemplate: '/dummy_resources/{dummyResourceId}{._format}', name: 'delete'),
new Put(uriTemplate: '/dummy_resources/{dummyResourceId}{._format}', name: 'put'),
]),
])
);

Check warning on line 61 in src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php

View check run for this annotation

Codecov / codecov/patch

src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php#L48-L61

Added lines #L48 - L61 were not covered by tests

$this->decorated = $this->createMock(ProcessorInterface::class);
$this->decorated->method('process')->willReturn(new Response());

Check warning on line 64 in src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php

View check run for this annotation

Codecov / codecov/patch

src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php#L63-L64

Added lines #L63 - L64 were not covered by tests
}

public function testHeadersAcceptPostIsReturnWhenPostAllowed(): void

Check warning on line 67 in src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php

View check run for this annotation

Codecov / codecov/patch

src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php#L67

Added line #L67 was not covered by tests
{
$operation = (new HttpOperation('GET', '/dummy_resources{._format}'));

Check warning on line 69 in src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php

View check run for this annotation

Codecov / codecov/patch

src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php#L69

Added line #L69 was not covered by tests

$context = $this->getContext();

Check warning on line 71 in src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php

View check run for this annotation

Codecov / codecov/patch

src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php#L71

Added line #L71 was not covered by tests

$processor = new LinkedDataPlatformProcessor(
$this->decorated,
$this->resourceClassResolver,
$this->resourceMetadataCollectionFactory
);

Check warning on line 77 in src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php

View check run for this annotation

Codecov / codecov/patch

src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php#L73-L77

Added lines #L73 - L77 were not covered by tests
/** @var Response $response */
$response = $processor->process(null, $operation, [], $context);

Check warning on line 79 in src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php

View check run for this annotation

Codecov / codecov/patch

src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php#L79

Added line #L79 was not covered by tests

$this->assertSame('application/ld+json, text/turtle', $response->headers->get('Accept-Post'));

Check warning on line 81 in src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php

View check run for this annotation

Codecov / codecov/patch

src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php#L81

Added line #L81 was not covered by tests
}

public function testHeadersAcceptPostIsNotSetWhenPostIsNotAllowed(): void

Check warning on line 84 in src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php

View check run for this annotation

Codecov / codecov/patch

src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php#L84

Added line #L84 was not covered by tests
{
$operation = (new HttpOperation('GET', '/dummy_resources/{dummyResourceId}{._format}'));
$context = $this->getContext();

Check warning on line 87 in src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php

View check run for this annotation

Codecov / codecov/patch

src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php#L86-L87

Added lines #L86 - L87 were not covered by tests

$processor = new LinkedDataPlatformProcessor(
$this->decorated,
$this->resourceClassResolver,
$this->resourceMetadataCollectionFactory
);

Check warning on line 93 in src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php

View check run for this annotation

Codecov / codecov/patch

src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php#L89-L93

Added lines #L89 - L93 were not covered by tests
/** @var Response $response */
$response = $processor->process(null, $operation, [], $context);

Check warning on line 95 in src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php

View check run for this annotation

Codecov / codecov/patch

src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php#L95

Added line #L95 was not covered by tests

$this->assertNull($response->headers->get('Accept-Post'));

Check warning on line 97 in src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php

View check run for this annotation

Codecov / codecov/patch

src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php#L97

Added line #L97 was not covered by tests
}

public function testHeaderAllowReflectsResourceAllowedMethods(): void

Check warning on line 100 in src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php

View check run for this annotation

Codecov / codecov/patch

src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php#L100

Added line #L100 was not covered by tests
{
$operation = (new HttpOperation('GET', '/dummy_resources{._format}'));
$context = $this->getContext();

Check warning on line 103 in src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php

View check run for this annotation

Codecov / codecov/patch

src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php#L102-L103

Added lines #L102 - L103 were not covered by tests

$processor = new LinkedDataPlatformProcessor(
$this->decorated,
$this->resourceClassResolver,
$this->resourceMetadataCollectionFactory
);

Check warning on line 109 in src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php

View check run for this annotation

Codecov / codecov/patch

src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php#L105-L109

Added lines #L105 - L109 were not covered by tests
/** @var Response $response */
$response = $processor->process(null, $operation, [], $context);
$allowHeader = $response->headers->get('Allow');
$this->assertStringContainsString('OPTIONS', $allowHeader);
$this->assertStringContainsString('HEAD', $allowHeader);
$this->assertStringContainsString('GET', $allowHeader);
$this->assertStringContainsString('POST', $allowHeader);

Check warning on line 116 in src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php

View check run for this annotation

Codecov / codecov/patch

src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php#L111-L116

Added lines #L111 - L116 were not covered by tests

$operation = (new HttpOperation('GET', '/dummy_resources/{dummyResourceId}{._format}'));

Check warning on line 118 in src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php

View check run for this annotation

Codecov / codecov/patch

src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php#L118

Added line #L118 was not covered by tests

/** @var Response $response */
$processor = new LinkedDataPlatformProcessor(
$this->decorated,
$this->resourceClassResolver,
$this->resourceMetadataCollectionFactory
);

Check warning on line 125 in src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php

View check run for this annotation

Codecov / codecov/patch

src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php#L121-L125

Added lines #L121 - L125 were not covered by tests
/** @var Response $response */
$response = $processor->process('data', $operation, [], $this->getContext());
$allowHeader = $response->headers->get('Allow');
$this->assertStringContainsString('OPTIONS', $allowHeader);
$this->assertStringContainsString('HEAD', $allowHeader);
$this->assertStringContainsString('GET', $allowHeader);
$this->assertStringContainsString('PUT', $allowHeader);
$this->assertStringContainsString('DELETE', $allowHeader);

Check warning on line 133 in src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php

View check run for this annotation

Codecov / codecov/patch

src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php#L127-L133

Added lines #L127 - L133 were not covered by tests
}

public function testProcessorWithoutRequiredConditionReturnOriginalResponse(): void

Check warning on line 136 in src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php

View check run for this annotation

Codecov / codecov/patch

src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php#L136

Added line #L136 was not covered by tests
{
$operation = (new HttpOperation('GET', '/dummy_resources/{dummyResourceId}{._format}'));

Check warning on line 138 in src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php

View check run for this annotation

Codecov / codecov/patch

src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php#L138

Added line #L138 was not covered by tests

// No collection factory
$processor = new LinkedDataPlatformProcessor($this->decorated, $this->resourceClassResolver, null);

Check warning on line 141 in src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php

View check run for this annotation

Codecov / codecov/patch

src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php#L141

Added line #L141 was not covered by tests
/** @var Response $response */
$response = $processor->process(null, $operation, [], $this->getContext());

Check warning on line 143 in src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php

View check run for this annotation

Codecov / codecov/patch

src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php#L143

Added line #L143 was not covered by tests

$this->assertNull($response->headers->get('Allow'));

Check warning on line 145 in src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php

View check run for this annotation

Codecov / codecov/patch

src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php#L145

Added line #L145 was not covered by tests

// No uri variable in context
$processor = new LinkedDataPlatformProcessor($this->decorated, null, $this->resourceMetadataCollectionFactory);
$response = $processor->process(null, $operation, [], $this->getContext());
$this->assertNull($response->headers->get('Allow'));

Check warning on line 150 in src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php

View check run for this annotation

Codecov / codecov/patch

src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php#L148-L150

Added lines #L148 - L150 were not covered by tests

// Operation is an Error
$processor = new LinkedDataPlatformProcessor($this->decorated, $this->resourceClassResolver, $this->resourceMetadataCollectionFactory);
$response = $processor->process(null, new Error(), $this->getContext());
$this->assertNull($response->headers->get('Allow'));

Check warning on line 155 in src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php

View check run for this annotation

Codecov / codecov/patch

src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php#L153-L155

Added lines #L153 - L155 were not covered by tests

// Not a resource class
$this->resourceClassResolver
->method('isResourceClass')
->willReturn(false);
$processor = new LinkedDataPlatformProcessor($this->decorated, $this->resourceClassResolver, $this->resourceMetadataCollectionFactory);
$response = $processor->process(null, $operation, []);
$this->assertNull($response->headers->get('Allow'));

Check warning on line 163 in src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php

View check run for this annotation

Codecov / codecov/patch

src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php#L158-L163

Added lines #L158 - L163 were not covered by tests
}

private function createGetRequest(): Request

Check warning on line 166 in src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php

View check run for this annotation

Codecov / codecov/patch

src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php#L166

Added line #L166 was not covered by tests
{
$request = new Request();
$request->setMethod('GET');
$request->setRequestFormat('json');
$request->headers->set('Accept', 'application/ld+json');

Check warning on line 171 in src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php

View check run for this annotation

Codecov / codecov/patch

src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php#L168-L171

Added lines #L168 - L171 were not covered by tests

return $request;

Check warning on line 173 in src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php

View check run for this annotation

Codecov / codecov/patch

src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php#L173

Added line #L173 was not covered by tests
}

private function getContext(): array

Check warning on line 176 in src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php

View check run for this annotation

Codecov / codecov/patch

src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php#L176

Added line #L176 was not covered by tests
{
return [
'resource_class' => 'DummyResource',
'request' => $this->createGetRequest(),
];

Check warning on line 181 in src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php

View check run for this annotation

Codecov / codecov/patch

src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php#L178-L181

Added lines #L178 - L181 were not covered by tests
}
}
6 changes: 6 additions & 0 deletions src/Symfony/Bundle/Resources/config/state/processor.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,11 @@
<service id="api_platform.state_processor.add_link_header" class="ApiPlatform\State\Processor\AddLinkHeaderProcessor" decorates="api_platform.state_processor.respond">
<argument type="service" id="api_platform.state_processor.add_link_header.inner" />
</service>

<service id="api_platform.state_processor.linked_data_platform" class="ApiPlatform\State\Processor\LinkedDataPlatformProcessor" decorates="api_platform.state_processor.respond">
<argument type="service" id="api_platform.state_processor.linked_data_platform.inner" />
<argument type="service" id="api_platform.resource_class_resolver" />
<argument type="service" id="api_platform.metadata.resource.metadata_collection_factory" />
</service>
</services>
</container>
6 changes: 6 additions & 0 deletions src/Symfony/Bundle/Resources/config/symfony/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@
<argument type="service" id="api_platform.state_processor.add_link_header.inner" />
</service>

<service id="api_platform.state_processor.linked_data_platform" class="ApiPlatform\State\Processor\LinkedDataPlatformProcessor" decorates="api_platform.state_processor.respond">
<argument type="service" id="api_platform.state_processor.linked_data_platform.inner" />
<argument type="service" id="api_platform.resource_class_resolver" />
<argument type="service" id="api_platform.metadata.resource.metadata_collection_factory" />
</service>

<service id="api_platform.listener.view.write" class="ApiPlatform\Symfony\EventListener\WriteListener">
<argument type="service" id="api_platform.state_processor.write" />
<argument type="service" id="api_platform.metadata.resource.metadata_collection_factory" />
Expand Down
Loading
Loading