-
Notifications
You must be signed in to change notification settings - Fork 689
Expand file tree
/
Copy pathRequestBodyParamConverterTest.php
More file actions
299 lines (255 loc) · 10.5 KB
/
RequestBodyParamConverterTest.php
File metadata and controls
299 lines (255 loc) · 10.5 KB
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
<?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\Request;
use FOS\RestBundle\Context\Context;
use FOS\RestBundle\Request\RequestBodyParamConverter;
use FOS\RestBundle\Serializer\Serializer;
use FOS\RestBundle\Tests\Functional\Bundle\TestBundle\Controller\Post;
use JMS\Serializer\Exception\InvalidArgumentException as JmsInvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;
use Symfony\Component\Serializer\Exception\InvalidArgumentException as SymfonyInvalidArgumentException;
use Symfony\Component\Validator\ConstraintViolationListInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
/**
* @author Tyler Stroud <tyler@tylerstroud.com>
*/
class RequestBodyParamConverterTest extends TestCase
{
protected $serializer;
protected $converterBuilder;
protected function setUp(): void
{
// skip the test if the installed version of SensioFrameworkExtraBundle
// is not compatible with the RequestBodyParamConverter class
$parameter = new \ReflectionParameter(
[
ParamConverterInterface::class,
'supports',
],
'configuration'
);
if (ParamConverter::class != $parameter->getType()->getName()) {
$this->markTestSkipped(
'skipping RequestBodyParamConverterTest due to an incompatible version of the SensioFrameworkExtraBundle'
);
}
$this->serializer = $this->getMockBuilder(Serializer::class)->getMock();
$this->converter = new RequestBodyParamConverter($this->serializer);
}
public function testInterface()
{
$converter = new RequestBodyParamConverter($this->serializer);
$this->assertInstanceOf(ParamConverterInterface::class, $converter);
}
public function testContextMergeDuringExecution()
{
$options = [
'deserializationContext' => [
'groups' => ['foo', 'bar'],
'foobar' => 'foo',
],
];
$configuration = $this->createConfiguration('FooClass', null, $options);
$converter = new RequestBodyParamConverter($this->serializer, ['foogroup'], 'fooversion');
$request = $this->createRequest('body', 'application/json');
$expectedContext = new Context();
$expectedContext->setGroups(['foo', 'bar']);
$expectedContext->setVersion('fooversion');
$expectedContext->setAttribute('foobar', 'foo');
$this->serializer
->expects($this->once())
->method('deserialize')
->with(
'body',
'FooClass',
'json',
$expectedContext
);
return $converter->apply($request, $configuration);
}
public function testExecutionInterceptsUnsupportedFormatException()
{
$this->expectException(UnsupportedMediaTypeHttpException::class);
$converter = new RequestBodyParamConverter($this->serializer);
$this->serializer
->expects($this->once())
->method('deserialize')
->will($this->throwException(new UnsupportedMediaTypeHttpException()));
$this->launchExecution($converter);
}
public function testExecutionInterceptsJMSException()
{
$this->expectException(BadRequestHttpException::class);
$converter = new RequestBodyParamConverter($this->serializer);
$this->serializer
->expects($this->once())
->method('deserialize')
->will($this->throwException(new JmsInvalidArgumentException()));
$this->launchExecution($converter);
}
public function testExecutionInterceptsSymfonySerializerException()
{
$this->expectException(BadRequestHttpException::class);
$converter = new RequestBodyParamConverter($this->serializer);
$this->serializer
->expects($this->once())
->method('deserialize')
->will($this->throwException(new SymfonyInvalidArgumentException()));
$this->launchExecution($converter);
}
public function testRequestAttribute()
{
$converter = new RequestBodyParamConverter($this->serializer);
$this->serializer
->expects($this->once())
->method('deserialize')
->willReturn('Object');
$request = $this->createRequest(null, 'application/json');
$this->launchExecution($converter, $request);
$this->assertEquals('Object', $request->attributes->get('foo'));
}
public function testValidatorParameters()
{
$this->serializer
->expects($this->once())
->method('deserialize')
->willReturn('Object');
$errors = $this->getMockBuilder(ConstraintViolationListInterface::class)->getMock();
$validator = $this->getMockBuilder(ValidatorInterface::class)->getMock();
$validator
->expects($this->once())
->method('validate')
->with('Object', null, ['foo'])
->willReturn($errors);
$converter = new RequestBodyParamConverter($this->serializer, null, null, $validator, 'errors');
$request = $this->createRequest(null, 'application/json');
$configuration = $this->createConfiguration('FooClass', null, ['validator' => ['groups' => ['foo']]]);
$this->launchExecution($converter, $request, $configuration);
$this->assertEquals($errors, $request->attributes->get('errors'));
}
public function testValidatorSkipping()
{
$this->serializer
->expects($this->once())
->method('deserialize')
->willReturn('Object');
$validator = $this->getMockBuilder(ValidatorInterface::class)->getMock();
$validator
->expects($this->never())
->method('validate');
$converter = new RequestBodyParamConverter($this->serializer, null, null, $validator, 'errors');
$request = $this->createRequest(null, 'application/json');
$configuration = $this->createConfiguration('FooClass', null, ['validate' => false]);
$this->launchExecution($converter, $request, $configuration);
$this->assertNull($request->attributes->get('errors'));
}
public function testReturn()
{
$converter = new RequestBodyParamConverter($this->serializer);
$this->assertTrue($this->launchExecution($converter));
}
public function testContextConfiguration()
{
$converter = new RequestBodyParamConverter($this->serializer);
$options = [
'groups' => ['foo', 'bar'],
'version' => 'v1.2',
'enableMaxDepth' => true,
'serializeNull' => false,
'foo' => 'bar',
];
$contextConfigurationMethod = new \ReflectionMethod($converter, 'configureContext');
$contextConfigurationMethod->setAccessible(true);
$contextConfigurationMethod->invoke($converter, $context = new Context(), $options);
$expectedContext = new Context();
$expectedContext
->addGroups($options['groups'])
->setVersion($options['version'])
->enableMaxDepth($options['enableMaxDepth'])
->setSerializeNull($options['serializeNull'])
->setAttribute('foo', 'bar');
$this->assertEquals($expectedContext, $context);
}
public function testValidatorOptionsGetter()
{
$converter = new RequestBodyParamConverter($this->serializer);
$options1 = [
'validator' => [
'groups' => ['foo'],
'traverse' => true,
],
];
$options2 = [
'validator' => [
'deep' => true,
],
];
$validatorMethod = new \ReflectionMethod($converter, 'getValidatorOptions');
$validatorMethod->setAccessible(true);
$this->assertEquals(['groups' => ['foo'], 'traverse' => true, 'deep' => false], $validatorMethod->invoke($converter, $options1));
$this->assertEquals(['groups' => false, 'traverse' => false, 'deep' => true], $validatorMethod->invoke($converter, $options2));
}
public function testSupports()
{
$converter = new RequestBodyParamConverter($this->serializer);
$config = $this->createConfiguration(Post::class, 'post');
$this->assertTrue($converter->supports($config));
}
public function testSupportsWithNoClass()
{
$converter = new RequestBodyParamConverter($this->serializer);
$this->assertFalse($converter->supports($this->createConfiguration(null, 'post')));
}
public function testNoContentTypeCausesUnsupportedMediaTypeException()
{
$converter = new RequestBodyParamConverter($this->serializer);
$request = $this->createRequest();
$this->expectException(UnsupportedMediaTypeHttpException::class);
$this->launchExecution($converter, $request);
}
protected function launchExecution($converter, $request = null, $configuration = null)
{
if (null === $request) {
$request = $this->createRequest('body', 'application/json');
}
if (null === $configuration) {
$configuration = $this->createConfiguration('FooClass', 'foo');
}
return $converter->apply($request, $configuration);
}
protected function createConfiguration($class, $name = null, array $options = []): \Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter
{
return new ParamConverter([
'name' => (string) $name,
'class' => $class,
'options' => $options,
'converter' => 'fos_rest.request_body',
]);
}
protected function createRequest($body = null, $contentType = null): \Symfony\Component\HttpFoundation\Request
{
$request = new Request(
[],
[],
[],
[],
[],
[],
$body
);
$request->headers->set('CONTENT_TYPE', $contentType);
return $request;
}
}