Skip to content

Commit 5896ecc

Browse files
committed
feat(state): fixes
1 parent a0cf1a9 commit 5896ecc

3 files changed

Lines changed: 76 additions & 14 deletions

File tree

src/State/Processor/LinkedDataPlatformProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function process(mixed $data, Operation $operation, array $uriVariables =
6262
if ($op->getUriTemplate() === $operation->getUriTemplate()) {
6363
$allowedMethods[] = $method = $op->getMethod();
6464
if ('POST' === $method && $outputFormats = $op->getOutputFormats()) {
65-
$acceptPost = $outputFormats;
65+
$acceptPost = array_map('strval', $outputFormats);
6666
}
6767
}
6868
}

src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace ApiPlatform\State\Tests\Processor;
1515

16+
use ApiPlatform\Hal\Tests\Fixtures\Dummy;
1617
use ApiPlatform\Metadata\ApiResource;
1718
use ApiPlatform\Metadata\Delete;
1819
use ApiPlatform\Metadata\Error;
@@ -51,11 +52,11 @@ protected function setUp(): void
5152
->willReturn(
5253
new ResourceMetadataCollection('DummyResource', [
5354
new ApiResource(operations: [
54-
new Get(uriTemplate: '/dummy_resources/{dummyResourceId}{._format}', class: 'DummyResource', name: 'get'),
55-
new GetCollection(uriTemplate: '/dummy_resources{._format}', class: 'DummyResource', name: 'get_collections'),
56-
new Post(uriTemplate: '/dummy_resources{._format}', outputFormats: ['jsonld' => ['application/ld+json'], 'text/turtle' => ['text/turtle']], class: 'DummyResource', name: 'post'),
57-
new Delete(uriTemplate: '/dummy_resources/{dummyResourceId}{._format}', class: 'DummyResource', name: 'delete'),
58-
new Put(uriTemplate: '/dummy_resources/{dummyResourceId}{._format}', class: 'DummyResource', name: 'put'),
55+
new Get(uriTemplate: '/dummy/{dummyResourceId}{._format}', class: 'DummyResource', name: 'get'),
56+
new GetCollection(uriTemplate: '/dummy{._format}', class: 'DummyResource', name: 'get_collections'),
57+
new Post(uriTemplate: '/dummy{._format}', outputFormats: ['jsonld' => ['application/ld+json'], 'text/turtle' => ['text/turtle']], class: 'DummyResource', name: 'post'),
58+
new Delete(uriTemplate: '/dummy/{dummyResourceId}{._format}', class: 'DummyResource', name: 'delete'),
59+
new Put(uriTemplate: '/dummy/{dummyResourceId}{._format}', class: 'DummyResource', name: 'put'),
5960
]),
6061
])
6162
);
@@ -66,8 +67,8 @@ protected function setUp(): void
6667

6768
public function testHeadersAcceptPostIsReturnWhenPostAllowed(): void
6869
{
69-
// $operation = (new HttpOperation('GET', '/dummy_resources{._format}', class: 'DummyResource'));
70-
$operation = new Get('/dummy_resources{._format}');
70+
// $operation = (new HttpOperation('GET', '/dummy{._format}', class: 'DummyResource'));
71+
$operation = new Get('/dummy{._format}');
7172

7273
$context = $this->getContext();
7374

@@ -84,8 +85,7 @@ public function testHeadersAcceptPostIsReturnWhenPostAllowed(): void
8485

8586
public function testHeadersAcceptPostIsNotSetWhenPostIsNotAllowed(): void
8687
{
87-
// $operation = (new HttpOperation('GET', '/dummy_resources/{dummyResourceId}{._format}', class: 'DummyResource'));
88-
$operation = new Get('/dummy_resources/{dummyResourceId}{._format}');
88+
$operation = new Get('/dummy/{dummyResourceId}{._format}', class:Dummy::class);
8989
$context = $this->getContext();
9090

9191
$processor = new LinkedDataPlatformProcessor(
@@ -101,7 +101,7 @@ public function testHeadersAcceptPostIsNotSetWhenPostIsNotAllowed(): void
101101

102102
public function testHeaderAllowReflectsResourceAllowedMethods(): void
103103
{
104-
$operation = new Get('/dummy_resources{._format}');
104+
$operation = new Get('/dummy{._format}', class: Dummy::class);
105105
$context = $this->getContext();
106106

107107
$processor = new LinkedDataPlatformProcessor(
@@ -117,8 +117,7 @@ public function testHeaderAllowReflectsResourceAllowedMethods(): void
117117
$this->assertStringContainsString('GET', $allowHeader);
118118
$this->assertStringContainsString('POST', $allowHeader);
119119

120-
// $operation = (new HttpOperation('GET', '/dummy_resources/{dummyResourceId}{._format}', class: 'DummyResource'));
121-
$operation = new Get('/dummy_resources/{dummyResourceId}{._format}');
120+
$operation = new Get('/dummy/{dummyResourceId}{._format}');
122121

123122

124123
/** @var Response $response */
@@ -158,7 +157,7 @@ private function createGetRequest(): Request
158157
private function getContext(): array
159158
{
160159
return [
161-
'resource_class' => 'DummyResource',
160+
'resource_class' => 'Dummy',
162161
'request' => $this->createGetRequest(),
163162
];
164163
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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\Tests\Fixtures\TestBundle\ApiResource;
15+
16+
use ApiPlatform\Metadata\ApiResource;
17+
use ApiPlatform\Metadata\Delete;
18+
use ApiPlatform\Metadata\Get;
19+
use ApiPlatform\Metadata\GetCollection;
20+
use ApiPlatform\Metadata\Operation;
21+
use ApiPlatform\Metadata\Post;
22+
23+
#[ApiResource(operations: [
24+
new Get(
25+
uriTemplate: '/dummy_get_post_delete_operations/{id}',
26+
provider: [self::class, 'provideItem'],
27+
),
28+
new GetCollection(
29+
uriTemplate: '/dummy_get_post_delete_operations',
30+
provider: [self::class, 'provide'], ),
31+
new Post(
32+
uriTemplate: '/dummy_get_post_delete_operations',
33+
provider: [self::class, 'provide'], ),
34+
new Delete(
35+
uriTemplate: '/dummy_get_post_delete_operations/{id}',
36+
provider: [self::class, 'provideItem'], ),
37+
])]
38+
class DummyGetPostDeleteOperation
39+
{
40+
public ?int $id;
41+
42+
public ?string $name = null;
43+
44+
public static function provide(Operation $operation, array $uriVariables = [], array $context = []): array
45+
{
46+
$dummyResource = new self();
47+
$dummyResource->id = 1;
48+
$dummyResource->name = 'Dummy name';
49+
50+
return [
51+
$dummyResource,
52+
];
53+
}
54+
55+
public static function provideItem(Operation $operation, array $uriVariables = [], array $context = []): self
56+
{
57+
$dummyResource = new self();
58+
$dummyResource->id = $uriVariables['id'];
59+
$dummyResource->name = 'Dummy name';
60+
61+
return $dummyResource;
62+
}
63+
}

0 commit comments

Comments
 (0)