forked from FriendsOfSymfony/FOSRestBundle
/
ViewResponseListenerTest.php
191 lines (145 loc) · 7.1 KB
/
ViewResponseListenerTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
/*
* This file is part of the FOSRestBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\RestBundle\Tests\EventListener;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use FOS\RestBundle\EventListener\ViewResponseListener;
/**
* View response listener test
*
* @author Lukas Kahwe Smith <smith@pooteeweet.org>
*/
class ViewResponseListenerTest extends \PHPUnit_Framework_TestCase
{
public function testOnKernelController()
{
$request = new Request();
$request->attributes->set('_view', 'foo');
$event = $this->getMockBuilder('\Symfony\Component\HttpKernel\Event\FilterControllerEvent')->disableOriginalConstructor()->getMock();
$event->expects($this->once())
->method('getRequest')
->will($this->returnValue($request));
$container = $this->getMockBuilder('\Symfony\Component\DependencyInjection\Container')->disableOriginalConstructor()->getMock();
$listener = new ViewResponseListener($container);
$listener->onKernelController($event);
$this->assertEquals('foo', $request->attributes->get('_template'));
}
public function testOnKernelControllerNoView()
{
$request = new Request();
$event = $this->getMockBuilder('\Symfony\Component\HttpKernel\Event\FilterControllerEvent')->disableOriginalConstructor()->getMock();
$event->expects($this->once())
->method('getRequest')
->will($this->returnValue($request));
$container = $this->getMockBuilder('\Symfony\Component\DependencyInjection\Container')->disableOriginalConstructor()->getMock();
$listener = new ViewResponseListener($container);
$listener->onKernelController($event);
$this->assertNull($request->attributes->get('_template'));
}
public function testOnKernelView()
{
$template = $this->getMockBuilder('\Symfony\Bundle\FrameworkBundle\Templating\TemplateReference')->disableOriginalConstructor()->getMock();
$template->expects($this->once())
->method('set')
->with('format', null);
$request = new Request();
$request->attributes->set('_template_default_vars', array('foo', 'halli'));
$request->attributes->set('foo', 'baz');
$request->attributes->set('halli', 'galli');
$request->attributes->set('_template', $template);
$response = new Response();
$view = $this->getMockBuilder('\FOS\RestBundle\View\View')->disableOriginalConstructor()->getMock();
$view->expects($this->exactly(2))
->method('getFormat')
->will($this->onConsecutiveCalls(null, 'html'));
$viewHandler = $this->getMock('\FOS\RestBundle\View\ViewHandlerInterface');
$viewHandler->expects($this->once())
->method('handle')
->with($this->isInstanceOf('\FOS\RestBundle\View\View'), $this->equalTo($request))
->will($this->returnValue($response));
$viewHandler->expects($this->once())
->method('isFormatTemplating')
->with('html')
->will($this->returnValue(true));
$container = $this->getMockBuilder('\Symfony\Component\DependencyInjection\Container')->disableOriginalConstructor()->getMock();
$container->expects($this->once())
->method('get')
->with($this->equalTo('fos_rest.view_handler'))
->will($this->returnValue($viewHandler));
$listener = new ViewResponseListener($container);
$event = $this->getMockBuilder('\Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent')->disableOriginalConstructor()->getMock();
$event->expects($this->once())
->method('getRequest')
->will($this->returnValue($request));
$event->expects($this->once())
->method('getControllerResult')
->will($this->returnValue($view));
$event->expects($this->once())
->method('setResponse');
$listener->onKernelView($event);
}
public function testOnKernelViewWhenControllerResultIsNotViewObject()
{
$request = new Request();
$container = $this->getMockBuilder('\Symfony\Component\DependencyInjection\Container')->disableOriginalConstructor()->getMock();
$listener = new ViewResponseListener($container);
$event = $this->getMockBuilder('\Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent')->disableOriginalConstructor()->getMock();
$event->expects($this->exactly(2))
->method('getRequest')
->will($this->returnValue($request));
$event->expects($this->exactly(2))
->method('getControllerResult')
->will($this->returnValue(array()));
$event->expects($this->never())
->method('setResponse');
$listener->onKernelView($event);
}
/**
* onKernelView falls back to FrameworkExtraBundles' onKernelView
* when fos_rest.view_response_listener.force_view is false
*/
public function testOnKernelViewFallsBackToFrameworkExtraBundle()
{
$template = $this->getMockBuilder('\Symfony\Bundle\FrameworkBundle\Templating\TemplateReference')->disableOriginalConstructor()->getMock();
$request = new Request();
$request->attributes->set('_template', $template);
$templating = $this->getMock('\Symfony\Bundle\FrameworkBundle\Templating\EngineInterface');
$templating->expects($this->any())
->method('renderResponse')
->with($template, array())
->will($this->returnValue(new Response('output')));
$templating->expects($this->any())
->method('render')
->with($template, array())
->will($this->returnValue('output'));
$container = $this->getMockBuilder('\Symfony\Component\DependencyInjection\Container')->disableOriginalConstructor()->getMock();
$container->expects($this->once())
->method('get')
->with('templating')
->will($this->returnValue($templating));
$listener = new ViewResponseListener($container);
$event = $this->getMockBuilder('\Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent')->disableOriginalConstructor()->getMock();
$event->expects($this->any())
->method('getRequest')
->will($this->returnValue($request));
$event->expects($this->any())
->method('getControllerResult')
->will($this->returnValue(array()));
$response = null;
$event->expects($this->once())
->method('setResponse')
->will($this->returnCallback(function ($r) use (&$response) {
$response = $r;
}));
$listener->onKernelView($event);
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
$this->assertSame('output', $response->getContent());
}
}