Skip to content

Commit 3e60bd3

Browse files
author
Miha Vrhovnik
committed
Fix the bug introdiced in PR #866, where the $format was never set.
1 parent a517372 commit 3e60bd3

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

EventListener/FormatListener.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ public function onKernelRequest(GetResponseEvent $event)
4949
try {
5050
$request = $event->getRequest();
5151

52-
$format = null;
53-
if (null === $request->getRequestFormat(null)) {
52+
$format = $request->getRequestFormat(null);
53+
if (null === $format) {
5454
if ($this->formatNegotiator instanceof MediaTypeNegotiatorInterface) {
5555
$mediaType = $this->formatNegotiator->getBestMediaType($request);
5656
if ($mediaType) {

Tests/EventListener/FormatListenerTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,41 @@ public function useSpecifiedFormatDataProvider()
143143
array('json', 'json'),
144144
);
145145
}
146+
147+
/**
148+
* Generates a request like a symfony fragment listener does.
149+
* Set request type to master
150+
*/
151+
public function testSfFragmentFormat()
152+
{
153+
$event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent')
154+
->disableOriginalConstructor()
155+
->getMock();
156+
157+
$request = new Request();
158+
$attributes = array ( '_locale' => 'en', '_format' => 'json', '_controller' => 'FooBundle:Index:featured', );
159+
$request->attributes->add($attributes);
160+
$request->attributes->set('_route_params', array_replace($request->attributes->get('_route_params', array()), $attributes));
161+
162+
$event->expects($this->once())
163+
->method('getRequest')
164+
->will($this->returnValue($request));
165+
166+
$event->expects($this->any())
167+
->method('getRequestType')
168+
->will($this->returnValue(HttpKernelInterface::MASTER_REQUEST));
169+
170+
$formatNegotiator = $this->getMockBuilder('FOS\RestBundle\Util\FormatNegotiator')
171+
->disableOriginalConstructor()
172+
->getMock();
173+
$formatNegotiator->expects($this->any())
174+
->method('getBestMediaType')
175+
->will($this->returnValue('application/json'));
176+
177+
$listener = new FormatListener($formatNegotiator);
178+
179+
$listener->onKernelRequest($event);
180+
181+
$this->assertEquals($request->getRequestFormat(), 'json');
182+
}
146183
}

0 commit comments

Comments
 (0)