Skip to content

Commit 72c99d2

Browse files
committed
Tidy ViewResponseListener and tests
1 parent b8fac8f commit 72c99d2

File tree

2 files changed

+131
-113
lines changed

2 files changed

+131
-113
lines changed

EventListener/ViewResponseListener.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,11 @@ public function onKernelController(FilterControllerEvent $event)
7272
public function onKernelView(GetResponseForControllerResultEvent $event)
7373
{
7474
$request = $event->getRequest();
75+
/** @var \FOS\RestBundle\Controller\Annotations\View $configuration */
7576
$configuration = $request->attributes->get('_view');
7677

7778
$view = $event->getControllerResult();
78-
if (!$view instanceOf View) {
79+
if (!$view instanceof View) {
7980
if (!$configuration && !$this->container->getParameter('fos_rest.view_response_listener.force_view')) {
8081
return parent::onKernelView($event);
8182
}
@@ -133,4 +134,4 @@ public function onKernelView(GetResponseForControllerResultEvent $event)
133134

134135
$event->setResponse($response);
135136
}
136-
}
137+
}

Tests/EventListener/ViewResponseListenerTest.php

Lines changed: 128 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -11,57 +11,105 @@
1111

1212
namespace FOS\RestBundle\Tests\EventListener;
1313

14+
use FOS\RestBundle\Controller\Annotations\View as ViewAnnotation;
15+
use FOS\RestBundle\EventListener\ViewResponseListener;
16+
use FOS\RestBundle\View\View;
17+
use FOS\RestBundle\View\ViewHandler;
1418
use Symfony\Component\HttpFoundation\Request;
1519
use Symfony\Component\HttpFoundation\Response;
1620

17-
use FOS\RestBundle\EventListener\ViewResponseListener;
18-
use FOS\Rest\Util\Codes;
19-
2021
/**
2122
* View response listener test
2223
*
2324
* @author Lukas Kahwe Smith <[email protected]>
2425
*/
2526
class ViewResponseListenerTest extends \PHPUnit_Framework_TestCase
2627
{
27-
public function testOnKernelController()
28+
/**
29+
* @var \FOS\RestBundle\EventListener\ViewResponseListener
30+
*/
31+
private $listener;
32+
33+
/**
34+
* @var \Symfony\Component\DependencyInjection\Container|\PHPUnit_Framework_MockObject_MockObject
35+
*/
36+
private $container;
37+
38+
/**
39+
* @var \FOS\RestBundle\View\ViewHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
40+
*/
41+
private $viewHandler;
42+
43+
/**
44+
* @var \Symfony\Bundle\FrameworkBundle\Templating\EngineInterface|\PHPUnit_Framework_MockObject_MockObject
45+
*/
46+
private $templating;
47+
48+
/**
49+
* @param Request $request
50+
* @return \Symfony\Component\HttpKernel\Event\FilterControllerEvent|\PHPUnit_Framework_MockObject_MockObject
51+
*/
52+
protected function getFilterEvent(Request $request)
2853
{
29-
$request = new Request();
30-
$request->attributes->set('_view', 'foo');
54+
$event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\FilterControllerEvent')
55+
->disableOriginalConstructor()
56+
->getMock();
3157

32-
$event = $this->getMockBuilder('\Symfony\Component\HttpKernel\Event\FilterControllerEvent')->disableOriginalConstructor()->getMock();
3358
$event->expects($this->once())
3459
->method('getRequest')
3560
->will($this->returnValue($request));
3661

37-
$container = $this->getMockBuilder('\Symfony\Component\DependencyInjection\Container')->disableOriginalConstructor()->getMock();
38-
$listener = new ViewResponseListener($container);
62+
return $event;
63+
}
3964

40-
$listener->onKernelController($event);
65+
/**
66+
* @param Request $request
67+
* @param mixed $result
68+
*
69+
* @return \Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent|\PHPUnit_Framework_MockObject_MockObject
70+
*/
71+
protected function getResponseEvent(Request $request, $result)
72+
{
73+
$event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent')
74+
->disableOriginalConstructor()
75+
->getMock();
4176

42-
$this->assertEquals('foo', $request->attributes->get('_template'));
77+
$event->expects($this->atLeastOnce())
78+
->method('getRequest')
79+
->will($this->returnValue($request));
80+
$event->expects($this->any())
81+
->method('getControllerResult')
82+
->will($this->returnValue($result));
83+
84+
return $event;
4385
}
4486

45-
public function testOnKernelControllerNoView()
87+
public function testOnKernelController()
4688
{
4789
$request = new Request();
90+
$request->attributes->set('_view', 'foo');
91+
$event = $this->getFilterEvent($request);
4892

49-
$event = $this->getMockBuilder('\Symfony\Component\HttpKernel\Event\FilterControllerEvent')->disableOriginalConstructor()->getMock();
50-
$event->expects($this->once())
51-
->method('getRequest')
52-
->will($this->returnValue($request));
93+
$this->listener->onKernelController($event);
94+
95+
$this->assertEquals('foo', $request->attributes->get('_template'));
96+
}
5397

54-
$container = $this->getMockBuilder('\Symfony\Component\DependencyInjection\Container')->disableOriginalConstructor()->getMock();
55-
$listener = new ViewResponseListener($container);
98+
public function testOnKernelControllerNoView()
99+
{
100+
$request = new Request();
101+
$event = $this->getFilterEvent($request);
56102

57-
$listener->onKernelController($event);
103+
$this->listener->onKernelController($event);
58104

59105
$this->assertNull($request->attributes->get('_template'));
60106
}
61107

62108
public function testOnKernelView()
63109
{
64-
$template = $this->getMockBuilder('\Symfony\Bundle\FrameworkBundle\Templating\TemplateReference')->disableOriginalConstructor()->getMock();
110+
$template = $this->getMockBuilder('Symfony\Bundle\FrameworkBundle\Templating\TemplateReference')
111+
->disableOriginalConstructor()
112+
->getMock();
65113
$template->expects($this->once())
66114
->method('set')
67115
->with('format', null);
@@ -71,70 +119,50 @@ public function testOnKernelView()
71119
$request->attributes->set('foo', 'baz');
72120
$request->attributes->set('halli', 'galli');
73121
$request->attributes->set('_template', $template);
74-
75122
$response = new Response();
76123

77-
$view = $this->getMockBuilder('\FOS\RestBundle\View\View')->disableOriginalConstructor()->getMock();
78-
124+
$view = $this->getMockBuilder('FOS\RestBundle\View\View')
125+
->disableOriginalConstructor()
126+
->getMock();
79127
$view->expects($this->exactly(2))
80-
->method('getFormat')
81-
->will($this->onConsecutiveCalls(null, 'html'));
128+
->method('getFormat')
129+
->will($this->onConsecutiveCalls(null, 'html'));
82130

83-
$viewHandler = $this->getMock('\FOS\RestBundle\View\ViewHandlerInterface');
84-
$viewHandler->expects($this->once())
131+
$this->viewHandler->expects($this->once())
85132
->method('handle')
86-
->with($this->isInstanceOf('\FOS\RestBundle\View\View'), $this->equalTo($request))
133+
->with($this->isInstanceOf('FOS\RestBundle\View\View'), $this->equalTo($request))
87134
->will($this->returnValue($response));
88-
$viewHandler->expects($this->once())
135+
$this->viewHandler->expects($this->once())
89136
->method('isFormatTemplating')
90137
->with('html')
91138
->will($this->returnValue(true));
92139

93-
$container = $this->getMockBuilder('\Symfony\Component\DependencyInjection\Container')->disableOriginalConstructor()->getMock();
94-
$container->expects($this->once())
95-
->method('get')
96-
->with($this->equalTo('fos_rest.view_handler'))
97-
->will($this->returnValue($viewHandler));
98-
99-
$listener = new ViewResponseListener($container);
100-
101-
$event = $this->getMockBuilder('\Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent')->disableOriginalConstructor()->getMock();
102-
140+
$event = $this->getResponseEvent($request, $view);
103141
$event->expects($this->once())
104-
->method('getRequest')
105-
->will($this->returnValue($request));
106-
107-
$event->expects($this->once())
108-
->method('getControllerResult')
109-
->will($this->returnValue($view));
142+
->method('setResponse');
110143

111-
$event->expects($this->once())
112-
->method('setResponse');
144+
$this->container->expects($this->once())
145+
->method('get')
146+
->with($this->equalTo('fos_rest.view_handler'))
147+
->will($this->returnValue($this->viewHandler));
113148

114-
$listener->onKernelView($event);
149+
$this->listener->onKernelView($event);
115150
}
116151

117152
public function testOnKernelViewWhenControllerResultIsNotViewObject()
118153
{
119154
$request = new Request();
120155

121-
$container = $this->getMockBuilder('\Symfony\Component\DependencyInjection\Container')->disableOriginalConstructor()->getMock();
122-
$listener = new ViewResponseListener($container);
123-
124-
$event = $this->getMockBuilder('\Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent')->disableOriginalConstructor()->getMock();
125-
126-
$event->expects($this->exactly(2))
127-
->method('getRequest')
128-
->will($this->returnValue($request));
129-
130-
$event->expects($this->exactly(2))
131-
->method('getControllerResult')
132-
->will($this->returnValue(array()));
133-
156+
$event = $this->getResponseEvent($request, array());
134157
$event->expects($this->never())
135158
->method('setResponse');
136159

137-
$listener->onKernelView($event);
160+
$this->container->expects($this->once())
161+
->method('get')
162+
->with($this->equalTo('templating'))
163+
->will($this->returnValue($this->templating));
164+
165+
$this->listener->onKernelView($event);
138166
}
139167

140168
/**
@@ -143,39 +171,23 @@ public function testOnKernelViewWhenControllerResultIsNotViewObject()
143171
*/
144172
public function testOnKernelViewFallsBackToFrameworkExtraBundle()
145173
{
146-
$template = $this->getMockBuilder('\Symfony\Bundle\FrameworkBundle\Templating\TemplateReference')->disableOriginalConstructor()->getMock();
174+
$template = $this->getMockBuilder('Symfony\Bundle\FrameworkBundle\Templating\TemplateReference')
175+
->disableOriginalConstructor()
176+
->getMock();
147177

148178
$request = new Request();
149179
$request->attributes->set('_template', $template);
150180

151-
$templating = $this->getMock('\Symfony\Bundle\FrameworkBundle\Templating\EngineInterface');
152-
$templating->expects($this->any())
181+
$this->templating->expects($this->any())
153182
->method('renderResponse')
154183
->with($template, array())
155184
->will($this->returnValue(new Response('output')));
156-
$templating->expects($this->any())
185+
$this->templating->expects($this->any())
157186
->method('render')
158187
->with($template, array())
159188
->will($this->returnValue('output'));
160189

161-
$container = $this->getMockBuilder('\Symfony\Component\DependencyInjection\Container')->disableOriginalConstructor()->getMock();
162-
$container->expects($this->once())
163-
->method('get')
164-
->with('templating')
165-
->will($this->returnValue($templating));
166-
167-
$listener = new ViewResponseListener($container);
168-
169-
$event = $this->getMockBuilder('\Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent')->disableOriginalConstructor()->getMock();
170-
171-
$event->expects($this->any())
172-
->method('getRequest')
173-
->will($this->returnValue($request));
174-
175-
$event->expects($this->any())
176-
->method('getControllerResult')
177-
->will($this->returnValue(array()));
178-
190+
$event = $this->getResponseEvent($request, array());
179191
$response = null;
180192

181193
$event->expects($this->once())
@@ -184,7 +196,12 @@ public function testOnKernelViewFallsBackToFrameworkExtraBundle()
184196
$response = $r;
185197
}));
186198

187-
$listener->onKernelView($event);
199+
$this->container->expects($this->once())
200+
->method('get')
201+
->with($this->equalTo('templating'))
202+
->will($this->returnValue($this->templating));
203+
204+
$this->listener->onKernelView($event);
188205

189206
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
190207
$this->assertSame('output', $response->getContent());
@@ -204,43 +221,35 @@ public static function statusCodeProvider()
204221
*/
205222
public function testStatusCode($annotationCode, $viewCode, $expectedCode)
206223
{
207-
$viewAnnotation = new \FOS\RestBundle\Controller\Annotations\View(array());
224+
$viewAnnotation = new ViewAnnotation(array());
208225
$viewAnnotation->setStatusCode($annotationCode);
209226

210227
$request = new Request();
211228
$request->setRequestFormat('json');
212229
$request->attributes->set('_view', $viewAnnotation);
213230

214-
$viewHandler = new \FOS\RestBundle\View\ViewHandler(array('json' => true));
215-
$container = $this->getMock('\Symfony\Component\DependencyInjection\Container');
216-
$viewHandler->setContainer($container);
217-
$container->expects($this->at(0))
218-
->method('get')
219-
->with($this->equalTo('fos_rest.view_handler'))
220-
->will($this->returnValue($viewHandler));
231+
$this->viewHandler = new ViewHandler(array('json' => true));
232+
$this->viewHandler->setContainer($this->container);
221233

222-
$templating = $this->getMock('\Symfony\Bundle\FrameworkBundle\Templating\EngineInterface');
223-
$container->expects($this->at(1))
234+
// This is why we avoid container dependencies!
235+
$this->container->expects($this->exactly(2))
224236
->method('get')
225-
->with($this->equalTo('fos_rest.templating'))
226-
->will($this->returnValue($templating));
227-
$templating->expects($this->any())
237+
->with($this->logicalOr('fos_rest.view_handler', 'fos_rest.templating'))
238+
->will($this->returnCallback(function ($service) {
239+
return $service === 'fos_rest.view_handler' ?
240+
$this->viewHandler :
241+
$this->templating;
242+
}));
243+
244+
$this->templating->expects($this->any())
228245
->method('render')
229246
->will($this->returnValue('foo'));
230247

231-
$listener = new ViewResponseListener($container);
232-
$event = $this->getMockBuilder('\Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent')->disableOriginalConstructor()->getMock();
233-
$event->expects($this->any())
234-
->method('getRequest')
235-
->will($this->returnValue($request));
236-
237-
$view = new \FOS\RestBundle\View\View();
238-
$view->setStatusCode($viewCode);
239-
$view->setData('foo');
248+
$view = new View();
249+
$view->setStatusCode($viewCode);
250+
$view->setData('foo');
240251

241-
$event->expects($this->any())
242-
->method('getControllerResult')
243-
->will($this->returnValue($view));
252+
$event = $this->getResponseEvent($request, $view);
244253

245254
$response = new Response();
246255
$event->expects($this->any())
@@ -249,9 +258,17 @@ public function testStatusCode($annotationCode, $viewCode, $expectedCode)
249258
$response = $r;
250259
}));
251260

252-
$listener->onKernelView($event);
261+
$this->listener->onKernelView($event);
253262

254263
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
255264
$this->assertSame($expectedCode, $response->getStatusCode());
256265
}
266+
267+
protected function setUp()
268+
{
269+
$this->viewHandler = $this->getMock('FOS\RestBundle\View\ViewHandlerInterface');
270+
$this->templating = $this->getMock('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface');
271+
$this->container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
272+
$this->listener = new ViewResponseListener($this->container);
273+
}
257274
}

0 commit comments

Comments
 (0)