forked from FriendsOfSymfony/FOSRestBundle
/
ViewHandlerTest.php
324 lines (276 loc) · 11.7 KB
/
ViewHandlerTest.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<?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\View;
use FOS\RestBundle\View\View,
FOS\RestBundle\View\ViewHandler,
Symfony\Bundle\FrameworkBundle\Templating\TemplateReference,
FOS\Rest\Util\Codes,
Symfony\Component\HttpFoundation\Request,
Symfony\Component\HttpFoundation\Response,
Symfony\Component\Form\FormView;
/**
* View test
*
* @author Victor Berchet <victor@suumit.com>
*/
class ViewHandlerTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider supportsFormatDataProvider
*/
public function testSupportsFormat($expected, $formats, $customFormatName)
{
$viewHandler = new ViewHandler($formats);
$viewHandler->registerHandler($customFormatName, function(){});
$this->assertEquals($expected, $viewHandler->supports('html'));
}
public static function supportsFormatDataProvider()
{
return array(
'not supported' => array(false, array('json' => false), 'xml'),
'html default' => array(true, array('html' => true), 'xml'),
'html custom' => array(true, array('json' => false), 'html'),
'html both' => array(true, array('html' => true), 'html'),
);
}
public function testRegsiterHandle()
{
$viewHandler = new ViewHandler();
$viewHandler->registerHandler('html', ($callback = function(){}));
$this->assertAttributeEquals(array('html' => $callback), 'customHandlers', $viewHandler);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testRegisterHandleExpectsException()
{
$viewHandler = new ViewHandler();
$viewHandler->registerHandler('json', new \stdClass());
}
/**
* @dataProvider getStatusCodeDataProvider
*/
public function testGetStatusCode($expected, $data, $isBound, $isValid, $isBoundCalled, $isValidCalled)
{
$reflectionMethod = new \ReflectionMethod('\FOS\RestBundle\View\ViewHandler', 'getStatusCode');
$reflectionMethod->setAccessible(true);
$form = $this->getMock('\Symfony\Component\Form\Form', array('isBound', 'isValid'), array(), '', false);
$form
->expects($this->exactly($isBoundCalled))
->method('isBound')
->will($this->returnValue($isBound));
$form
->expects($this->exactly($isValidCalled))
->method('isValid')
->will($this->returnValue($isValid));
if ($data) {
$data = array('form' => $form);
}
$view = new View($data);
$viewHandler = new ViewHandler(array(), $expected);
$this->assertEquals($expected, $reflectionMethod->invoke($viewHandler, $view));
}
public static function getStatusCodeDataProvider()
{
return array(
'no data' => array(Codes::HTTP_OK, false, false, false, 0, 0),
'form key form not bound' => array(Codes::HTTP_OK, true, false, true, 1, 0),
'form key form is bound and invalid' => array(403, true, true, false, 1, 1),
'form key form bound and valid' => array(Codes::HTTP_OK, true, true, true, 1, 1),
'form key null form bound and valid' => array(Codes::HTTP_OK, true, true, true, 1, 1),
);
}
/**
* @dataProvider createResponseWithLocationDataProvider
*/
public function testCreateResponseWithLocation($expected, $format, $forceRedirects)
{
$viewHandler = new ViewHandler(array('html' => true, 'json' => false, 'xml' => false), Codes::HTTP_BAD_REQUEST, $forceRedirects);
$view = new View();
$view->setLocation('foo');
$returnedResponse = $viewHandler->createResponse($view, new Request(), $format);
$this->assertEquals($expected, $returnedResponse->getStatusCode());
$this->assertEquals('foo', $returnedResponse->headers->get('location'));
}
public static function createResponseWithLocationDataProvider()
{
return array(
'empty force redirects' => array(200, 'xml', array('json' => 403)),
'force redirects response is redirect' => array(200, 'json', array()),
'force redirects response not redirect' => array(403, 'json', array('json' => 403)),
'html and redirect' => array(301, 'html', array('html' => 301)),
);
}
/**
* @dataProvider createResponseWithoutLocationDataProvider
*/
public function testCreateResponseWithoutLocation($format, $expected, $createViewCalls = 0, $formIsValid = false, $form = false)
{
$viewHandler = new ViewHandler(array('html' => true, 'json' => false));
$container = $this->getMock('\Symfony\Component\DependencyInjection\Container', array('get', 'getParameter'));
if ('html' === $format) {
$templating = $this->getMockBuilder('\Symfony\Bundle\FrameworkBundle\Templating\PhpEngine')
->setMethods(array('render'))
->disableOriginalConstructor()
->getMock();
$templating
->expects($this->once())
->method('render')
->will($this->returnValue(var_export($expected, true)));
$container
->expects($this->once())
->method('get')
->with('fos_rest.templating')
->will($this->returnValue($templating));
} else {
$serializer = $this->getMock('\stdClass', array('serialize', 'setVersion'));
$serializer
->expects($this->once())
->method('serialize')
->will($this->returnValue(var_export($expected, true)));
$container
->expects($this->once())
->method('get')
->with('fos_rest.serializer')
->will($this->returnValue($serializer));
$container
->expects($this->once())
->method('getParameter')
->with('fos_rest.objects_version')
->will($this->returnValue('1.0'));
}
$viewHandler->setContainer($container);
if ($form) {
$data = $this->getMock('\Symfony\Component\Form\Form', array('createView', 'isValid', 'getChildren'), array(), '', false);
$data
->expects($this->exactly($createViewCalls))
->method('createView')
->will($this->returnValue(array('bla' => 'toto')));
$data
->expects($this->any())
->method('isValid')
->will($this->returnValue($formIsValid));
} else {
$data = array('foo' => 'bar');
}
$view = new View($data);
$response = $viewHandler->createResponse($view, new Request, $format);
$this->assertEquals(var_export($expected, true), $response->getContent());
}
public static function createResponseWithoutLocationDataProvider()
{
return array(
'not templating aware no form' => array('json', array('foo' => 'bar')),
'templating aware no form' => array('html', array('foo' => 'bar')),
'templating aware and form' => array('html', array('data' => array('bla' => 'toto')), 1, true, true),
'not templating aware and invalid form' => array('json', array('data' => array(0 => 'error', 1 => 'error')), 0, false, true),
);
}
/**
* @dataProvider createResponseDataProvider
*/
public function testCreateResponse($expected, $format, $formats)
{
$viewHandler = new ViewHandler($formats);
$viewHandler->registerHandler('html', function($handler, $view, $request){return $view;});
$response = $viewHandler->handle(new View(null, $expected), new Request(), $format);
$this->assertEquals($expected, $response->getStatusCode());
}
public static function createResponseDataProvider()
{
return array(
'no handler' => array(Codes::HTTP_UNSUPPORTED_MEDIA_TYPE, 'xml', array()),
'custom handler' => array(200, 'html', array()),
'transform called' => array(200, 'json', array('json' => false)),
);
}
public function testHandle()
{
$viewHandler = new ViewHandler(array('html' => true));
$templating = $this->getMockBuilder('\Symfony\Bundle\FrameworkBundle\Templating\PhpEngine')
->setMethods(array('render'))
->disableOriginalConstructor()
->getMock();
$templating
->expects($this->once())
->method('render')
->will($this->returnValue(''));
$container = $this->getMock('\Symfony\Component\DependencyInjection\Container', array('get'));
$container
->expects($this->exactly(2))
->method('get')
->will($this->onConsecutiveCalls(new Request(), $templating));
$viewHandler->setContainer($container);
$data = array('foo' => 'bar');
$view = new View($data);
$this->assertInstanceOf('\Symfony\Component\HttpFoundation\Response', $viewHandler->handle($view));
}
public function testHandleCustom()
{
$viewHandler = new ViewHandler(array());
$viewHandler->registerHandler('html', ($callback = function(){ return 'foo'; }));
$container = $this->getMock('\Symfony\Component\DependencyInjection\Container', array('get'));
$container
->expects($this->once())
->method('get')
->with('request')
->will($this->returnValue(new Request()));
$viewHandler->setContainer($container);
$data = array('foo' => 'bar');
$view = new View($data);
$this->assertEquals('foo', $viewHandler->handle($view));
}
/**
* @expectedException \Symfony\Component\HttpKernel\Exception\HttpException
*/
public function testHandleNotSupported()
{
$viewHandler = new ViewHandler(array());
$container = $this->getMock('\Symfony\Component\DependencyInjection\Container', array('get'));
$container
->expects($this->once())
->method('get')
->with('request')
->will($this->returnValue(new Request()));
$viewHandler->setContainer($container);
$data = array('foo' => 'bar');
$view = new View($data);
$viewHandler->handle($view);
}
/**
* @dataProvider prepareTemplateParametersDataProvider
*/
public function testPrepareTemplateParametersWithProvider($viewData, $expected)
{
$handler = new ViewHandler();
$view = new View();
$view->setData($viewData);
$this->assertEquals($expected, $handler->prepareTemplateParameters($view));
}
public function prepareTemplateParametersDataProvider()
{
$object = new \stdClass();
$formView = new FormView();
$form = $this->getMockBuilder('\Symfony\Component\Form\Form')
->setMethods(array('createView'))
->disableOriginalConstructor()
->getMock();
$form
->expects($this->once())
->method('createView')
->will($this->returnValue($formView));
return array(
'assoc array does not change' => array(array('foo' => 'bar'), array('foo' => 'bar')),
'ordered array is wrapped as data key' => array(array('foo', 'bar'), array('data' => array('foo', 'bar'))),
'object is wrapped as data key' => array($object, array('data' => $object)),
'form is wrapped as form key' => array($form, array('data' => $formView))
);
}
}