1616use ApiPlatform \Metadata \ApiResource ;
1717use ApiPlatform \Metadata \Delete ;
1818use ApiPlatform \Metadata \Get ;
19+ use ApiPlatform \Metadata \GetCollection ;
1920use ApiPlatform \Metadata \HttpOperation ;
20- use ApiPlatform \Metadata \Patch ;
2121use ApiPlatform \Metadata \Post ;
2222use ApiPlatform \Metadata \Put ;
2323use ApiPlatform \Metadata \Resource \Factory \ResourceMetadataCollectionFactoryInterface ;
@@ -42,6 +42,19 @@ protected function setUp(): void
4242 ->willReturn (true );
4343
4444 $ this ->resourceMetadataCollectionFactory = $ this ->createMock (ResourceMetadataCollectionFactoryInterface::class);
45+ $ this ->resourceMetadataCollectionFactory
46+ ->method ('create ' )
47+ ->willReturn (
48+ new ResourceMetadataCollection ('DummyResource ' , [
49+ new ApiResource (operations: [
50+ new Get (uriTemplate: '/dummy_resources/{dummyResourceId}{._format} ' , name: 'get ' ),
51+ new GetCollection (uriTemplate: '/dummy_resources{._format} ' , name: 'get_collections ' ),
52+ new Post (uriTemplate: '/dummy_resources{._format} ' , name: 'post ' ),
53+ new Delete (uriTemplate: '/dummy_resources/{dummyResourceId}{._format} ' , name: 'delete ' ),
54+ new Put (uriTemplate: '/dummy_resources/{dummyResourceId}{._format} ' , name: 'put ' ),
55+ ]),
56+ ])
57+ );
4558
4659 $ this ->processor = new RespondProcessor (
4760 null ,
@@ -51,57 +64,36 @@ protected function setUp(): void
5164 );
5265 }
5366
54- public function testHeadersAcceptPostIsSetCorrectly (): void
67+ public function testHeadersAcceptPostIsReturnWhenPostAllowed (): void
5568 {
56- $ this ->resourceMetadataCollectionFactory
57- ->method ('create ' )
58- ->willReturn (new ResourceMetadataCollection ('DummyResourceClass ' ));
59-
60- $ operation = new HttpOperation ('GET ' );
69+ $ operation = (new HttpOperation ('GET ' , '/dummy_resources{._format} ' , outputFormats: ['jsonld ' => ['application/ld+json ' ], 'json ' => ['application/json ' ]]));
6170 $ context = [
62- 'resource_class ' => 'SomeResourceClass ' ,
71+ 'resource_class ' => 'DummyResource ' ,
6372 'request ' => $ this ->createGetRequest (),
6473 ];
6574
6675 /** @var Response $response */
6776 $ response = $ this ->processor ->process (null , $ operation , [], $ context );
68-
69- $ this ->assertSame ('text/turtle, application/ld+json ' , $ response ->headers ->get ('Accept-Post ' ));
77+ $ this ->assertSame ('application/ld+json, application/json ' , $ response ->headers ->get ('Accept-Post ' ));
7078 }
7179
72- public function testHeaderAllowHasHeadOptionsByDefault (): void
80+ public function testHeadersAcceptPostIsNotSetWhenPostIsNotAllowed (): void
7381 {
74- $ this ->resourceMetadataCollectionFactory
75- ->method ('create ' )
76- ->willReturn (new ResourceMetadataCollection ('DummyResourceClass ' ));
77-
78- $ operation = new HttpOperation ('GET ' );
82+ $ operation = (new HttpOperation ('GET ' , '/dummy_resources/{dummyResourceId}{._format} ' ));
7983 $ context = [
80- 'resource_class ' => 'SomeResourceClass ' ,
84+ 'resource_class ' => 'DummyResource ' ,
8185 'request ' => $ this ->createGetRequest (),
8286 ];
8387
8488 /** @var Response $response */
8589 $ response = $ this ->processor ->process (null , $ operation , [], $ context );
8690
87- $ this ->assertSame ( ' OPTIONS, HEAD ' , $ response ->headers ->get ('Allow ' ));
91+ $ this ->assertNull ( $ response ->headers ->get ('Accept-Post ' ));
8892 }
8993
9094 public function testHeaderAllowReflectsResourceAllowedMethods (): void
9195 {
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 ' );
96+ $ operation = (new HttpOperation ('GET ' , '/dummy_resources{._format} ' ));
10597 $ context = [
10698 'resource_class ' => 'SomeResourceClass ' ,
10799 'request ' => $ this ->createGetRequest (),
@@ -115,30 +107,13 @@ public function testHeaderAllowReflectsResourceAllowedMethods(): void
115107 $ this ->assertStringContainsString ('HEAD ' , $ allowHeader );
116108 $ this ->assertStringContainsString ('GET ' , $ allowHeader );
117109 $ 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- );
110+ $ this ->assertStringNotContainsString ('DELETE ' , $ allowHeader );
136111
137- $ operation = new HttpOperation ('GET ' );
138112 $ context = [
139113 'resource_class ' => 'SomeResourceClass ' ,
140114 'request ' => $ this ->createGetRequest (),
141115 ];
116+ $ operation = (new HttpOperation ('GET ' , '/dummy_resources/{dummyResourceId}{._format} ' ));
142117
143118 /** @var Response $response */
144119 $ response = $ this ->processor ->process (null , $ operation , [], $ context );
@@ -147,10 +122,9 @@ public function testHeaderAllowReflectsAllowedResourcesGetPutPatch(): void
147122 $ this ->assertStringContainsString ('OPTIONS ' , $ allowHeader );
148123 $ this ->assertStringContainsString ('HEAD ' , $ allowHeader );
149124 $ this ->assertStringContainsString ('GET ' , $ allowHeader );
150- $ this ->assertStringContainsString ('PATCH ' , $ allowHeader );
151125 $ this ->assertStringContainsString ('PUT ' , $ allowHeader );
126+ $ this ->assertStringContainsString ('DELETE ' , $ allowHeader );
152127 $ this ->assertStringNotContainsString ('POST ' , $ allowHeader );
153- $ this ->assertStringNotContainsString ('DELETE ' , $ allowHeader );
154128 }
155129
156130 private function createGetRequest (): Request
0 commit comments