Skip to content

Commit 2e487c5

Browse files
committed
Add test for unsupported Accept header which happens to match a Symfony built-in format
1 parent 78cd201 commit 2e487c5

File tree

2 files changed

+42
-22
lines changed

2 files changed

+42
-22
lines changed

src/EventListener/AddFormatListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2424

2525
/**
26-
* Chooses the format to user according to the Accept header and supported formats.
26+
* Chooses the format to use according to the Accept header and supported formats.
2727
*
2828
* @author Kévin Dunglas <[email protected]>
2929
*/

tests/EventListener/AddFormatListenerTest.php

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function testNoResourceClass()
3232
$request = new Request();
3333

3434
$eventProphecy = $this->prophesize(GetResponseEvent::class);
35-
$eventProphecy->getRequest()->willReturn($request)->shouldBeCalled();
35+
$eventProphecy->getRequest()->willReturn($request);
3636
$event = $eventProphecy->reveal();
3737

3838
$formatsProviderProphecy = $this->prophesize(FormatsProviderInterface::class);
@@ -50,11 +50,11 @@ public function testSupportedRequestFormat()
5050
$request->setRequestFormat('xml');
5151

5252
$eventProphecy = $this->prophesize(GetResponseEvent::class);
53-
$eventProphecy->getRequest()->willReturn($request)->shouldBeCalled();
53+
$eventProphecy->getRequest()->willReturn($request);
5454
$event = $eventProphecy->reveal();
5555

5656
$formatsProviderProphecy = $this->prophesize(FormatsProviderInterface::class);
57-
$formatsProviderProphecy->getFormatsFromAttributes([])->willReturn(['xml' => ['text/xml']])->shouldBeCalled();
57+
$formatsProviderProphecy->getFormatsFromAttributes([])->willReturn(['xml' => ['text/xml']]);
5858

5959
$listener = new AddFormatListener(new Negotiator(), $formatsProviderProphecy->reveal());
6060
$listener->onKernelRequest($event);
@@ -69,11 +69,11 @@ public function testRespondFlag()
6969
$request->setRequestFormat('xml');
7070

7171
$eventProphecy = $this->prophesize(GetResponseEvent::class);
72-
$eventProphecy->getRequest()->willReturn($request)->shouldBeCalled();
72+
$eventProphecy->getRequest()->willReturn($request);
7373
$event = $eventProphecy->reveal();
7474

7575
$formatsProviderProphecy = $this->prophesize(FormatsProviderInterface::class);
76-
$formatsProviderProphecy->getFormatsFromAttributes([])->willReturn(['xml' => ['text/xml']])->shouldBeCalled();
76+
$formatsProviderProphecy->getFormatsFromAttributes([])->willReturn(['xml' => ['text/xml']]);
7777

7878
$listener = new AddFormatListener(new Negotiator(), $formatsProviderProphecy->reveal());
7979
$listener->onKernelRequest($event);
@@ -91,10 +91,11 @@ public function testUnsupportedRequestFormat()
9191
$request->setRequestFormat('xml');
9292

9393
$eventProphecy = $this->prophesize(GetResponseEvent::class);
94-
$eventProphecy->getRequest()->willReturn($request)->shouldBeCalled();
94+
$eventProphecy->getRequest()->willReturn($request);
9595
$event = $eventProphecy->reveal();
96+
9697
$formatsProviderProphecy = $this->prophesize(FormatsProviderInterface::class);
97-
$formatsProviderProphecy->getFormatsFromAttributes([])->willReturn(['json' => ['application/json']])->shouldBeCalled();
98+
$formatsProviderProphecy->getFormatsFromAttributes([])->willReturn(['json' => ['application/json']]);
9899

99100
$listener = new AddFormatListener(new Negotiator(), $formatsProviderProphecy->reveal());
100101
$listener->onKernelRequest($event);
@@ -108,11 +109,11 @@ public function testSupportedAcceptHeader()
108109
$request->headers->set('Accept', 'text/html, application/xhtml+xml, application/xml, application/json;q=0.9, */*;q=0.8');
109110

110111
$eventProphecy = $this->prophesize(GetResponseEvent::class);
111-
$eventProphecy->getRequest()->willReturn($request)->shouldBeCalled();
112+
$eventProphecy->getRequest()->willReturn($request);
112113
$event = $eventProphecy->reveal();
113114

114115
$formatsProviderProphecy = $this->prophesize(FormatsProviderInterface::class);
115-
$formatsProviderProphecy->getFormatsFromAttributes([])->willReturn(['binary' => ['application/octet-stream'], 'json' => ['application/json']])->shouldBeCalled();
116+
$formatsProviderProphecy->getFormatsFromAttributes([])->willReturn(['binary' => ['application/octet-stream'], 'json' => ['application/json']]);
116117

117118
$listener = new AddFormatListener(new Negotiator(), $formatsProviderProphecy->reveal());
118119
$listener->onKernelRequest($event);
@@ -126,11 +127,11 @@ public function testAcceptAllHeader()
126127
$request->headers->set('Accept', 'text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8');
127128

128129
$eventProphecy = $this->prophesize(GetResponseEvent::class);
129-
$eventProphecy->getRequest()->willReturn($request)->shouldBeCalled();
130+
$eventProphecy->getRequest()->willReturn($request);
130131
$event = $eventProphecy->reveal();
131132

132133
$formatsProviderProphecy = $this->prophesize(FormatsProviderInterface::class);
133-
$formatsProviderProphecy->getFormatsFromAttributes([])->willReturn(['binary' => ['application/octet-stream'], 'json' => ['application/json']])->shouldBeCalled();
134+
$formatsProviderProphecy->getFormatsFromAttributes([])->willReturn(['binary' => ['application/octet-stream'], 'json' => ['application/json']]);
134135

135136
$listener = new AddFormatListener(new Negotiator(), $formatsProviderProphecy->reveal());
136137
$listener->onKernelRequest($event);
@@ -148,11 +149,30 @@ public function testUnsupportedAcceptHeader()
148149
$request->headers->set('Accept', 'text/html, application/xhtml+xml, application/xml;q=0.9');
149150

150151
$eventProphecy = $this->prophesize(GetResponseEvent::class);
151-
$eventProphecy->getRequest()->willReturn($request)->shouldBeCalled();
152+
$eventProphecy->getRequest()->willReturn($request);
153+
$event = $eventProphecy->reveal();
154+
155+
$formatsProviderProphecy = $this->prophesize(FormatsProviderInterface::class);
156+
$formatsProviderProphecy->getFormatsFromAttributes([])->willReturn(['binary' => ['application/octet-stream'], 'json' => ['application/json']]);
157+
158+
$listener = new AddFormatListener(new Negotiator(), $formatsProviderProphecy->reveal());
159+
$listener->onKernelRequest($event);
160+
}
161+
162+
public function testUnsupportedAcceptHeaderSymfonyBuiltInFormat()
163+
{
164+
$this->expectException(NotAcceptableHttpException::class);
165+
$this->expectExceptionMessage('Requested format "text/xml" is not supported. Supported MIME types are "application/json".');
166+
167+
$request = new Request([], [], ['_api_resource_class' => 'Foo']);
168+
$request->headers->set('Accept', 'text/xml');
169+
170+
$eventProphecy = $this->prophesize(GetResponseEvent::class);
171+
$eventProphecy->getRequest()->willReturn($request);
152172
$event = $eventProphecy->reveal();
153173

154174
$formatsProviderProphecy = $this->prophesize(FormatsProviderInterface::class);
155-
$formatsProviderProphecy->getFormatsFromAttributes([])->willReturn(['binary' => ['application/octet-stream'], 'json' => ['application/json']])->shouldBeCalled();
175+
$formatsProviderProphecy->getFormatsFromAttributes([])->willReturn(['json' => ['application/json']]);
156176

157177
$listener = new AddFormatListener(new Negotiator(), $formatsProviderProphecy->reveal());
158178
$listener->onKernelRequest($event);
@@ -167,11 +187,11 @@ public function testInvalidAcceptHeader()
167187
$request->headers->set('Accept', 'invalid');
168188

169189
$eventProphecy = $this->prophesize(GetResponseEvent::class);
170-
$eventProphecy->getRequest()->willReturn($request)->shouldBeCalled();
190+
$eventProphecy->getRequest()->willReturn($request);
171191
$event = $eventProphecy->reveal();
172192

173193
$formatsProviderProphecy = $this->prophesize(FormatsProviderInterface::class);
174-
$formatsProviderProphecy->getFormatsFromAttributes([])->willReturn(['json' => ['application/json']])->shouldBeCalled();
194+
$formatsProviderProphecy->getFormatsFromAttributes([])->willReturn(['json' => ['application/json']]);
175195

176196
$listener = new AddFormatListener(new Negotiator(), $formatsProviderProphecy->reveal());
177197
$listener->onKernelRequest($event);
@@ -184,11 +204,11 @@ public function testAcceptHeaderTakePrecedenceOverRequestFormat()
184204
$request->setRequestFormat('xml');
185205

186206
$eventProphecy = $this->prophesize(GetResponseEvent::class);
187-
$eventProphecy->getRequest()->willReturn($request)->shouldBeCalled();
207+
$eventProphecy->getRequest()->willReturn($request);
188208
$event = $eventProphecy->reveal();
189209

190210
$formatsProviderProphecy = $this->prophesize(FormatsProviderInterface::class);
191-
$formatsProviderProphecy->getFormatsFromAttributes([])->willReturn(['xml' => ['application/xml'], 'json' => ['application/json']])->shouldBeCalled();
211+
$formatsProviderProphecy->getFormatsFromAttributes([])->willReturn(['xml' => ['application/xml'], 'json' => ['application/json']]);
192212

193213
$listener = new AddFormatListener(new Negotiator(), $formatsProviderProphecy->reveal());
194214
$listener->onKernelRequest($event);
@@ -204,11 +224,11 @@ public function testInvalidRouteFormat()
204224
$request = new Request([], [], ['_api_resource_class' => 'Foo', '_format' => 'invalid']);
205225

206226
$eventProphecy = $this->prophesize(GetResponseEvent::class);
207-
$eventProphecy->getRequest()->willReturn($request)->shouldBeCalled();
227+
$eventProphecy->getRequest()->willReturn($request);
208228
$event = $eventProphecy->reveal();
209229

210230
$formatsProviderProphecy = $this->prophesize(FormatsProviderInterface::class);
211-
$formatsProviderProphecy->getFormatsFromAttributes([])->willReturn(['json' => ['application/json']])->shouldBeCalled();
231+
$formatsProviderProphecy->getFormatsFromAttributes([])->willReturn(['json' => ['application/json']]);
212232

213233
$listener = new AddFormatListener(new Negotiator(), $formatsProviderProphecy->reveal());
214234
$listener->onKernelRequest($event);
@@ -220,11 +240,11 @@ public function testResourceClassSupportedRequestFormat()
220240
$request->setRequestFormat('csv');
221241

222242
$eventProphecy = $this->prophesize(GetResponseEvent::class);
223-
$eventProphecy->getRequest()->willReturn($request)->shouldBeCalled();
243+
$eventProphecy->getRequest()->willReturn($request);
224244
$event = $eventProphecy->reveal();
225245

226246
$formatsProviderProphecy = $this->prophesize(FormatsProviderInterface::class);
227-
$formatsProviderProphecy->getFormatsFromAttributes(['resource_class' => 'Foo', 'collection_operation_name' => 'get', 'receive' => true])->willReturn(['csv' => ['text/csv']])->shouldBeCalled();
247+
$formatsProviderProphecy->getFormatsFromAttributes(['resource_class' => 'Foo', 'collection_operation_name' => 'get', 'receive' => true])->willReturn(['csv' => ['text/csv']]);
228248

229249
$listener = new AddFormatListener(new Negotiator(), $formatsProviderProphecy->reveal());
230250
$listener->onKernelRequest($event);

0 commit comments

Comments
 (0)