1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the KleijnWeb\SwaggerBundle package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace KleijnWeb\SwaggerBundle\Tests\Request\ContentDecoder; |
10
|
|
|
|
11
|
|
|
use JMS\Serializer\Serializer; |
12
|
|
|
use KleijnWeb\SwaggerBundle\Document\OperationObject; |
13
|
|
|
use KleijnWeb\SwaggerBundle\Request\ContentDecoder; |
14
|
|
|
use KleijnWeb\SwaggerBundle\Serializer\JmsSerializerFactory; |
15
|
|
|
use KleijnWeb\SwaggerBundle\Serializer\SerializationTypeResolver; |
16
|
|
|
use KleijnWeb\SwaggerBundle\Serializer\SerializerAdapter; |
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author John Kleijn <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class ContentDecoderJmsSerializerCompatibilityTest extends \PHPUnit_Framework_TestCase |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var ContentDecoder |
26
|
|
|
*/ |
27
|
|
|
private $contentDecoder; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Serializer |
31
|
|
|
*/ |
32
|
|
|
private $serializer; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Create serializer |
36
|
|
|
*/ |
37
|
|
|
protected function setUp() |
38
|
|
|
{ |
39
|
|
|
$this->serializer = new SerializerAdapter(JmsSerializerFactory::factory()); |
|
|
|
|
40
|
|
|
$this->contentDecoder = new ContentDecoder( |
41
|
|
|
$this->serializer, |
42
|
|
|
new SerializationTypeResolver('KleijnWeb\SwaggerBundle\Tests\Request\ContentDecoder') |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @test |
48
|
|
|
*/ |
49
|
|
View Code Duplication |
public function canDeserializeIntoObject() |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
$content = [ |
52
|
|
|
'foo' => 'bar' |
53
|
|
|
]; |
54
|
|
|
$request = new Request([], [], [], [], [], [], json_encode($content)); |
55
|
|
|
$request->headers->set('Content-Type', 'application/json'); |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
$operationDefinition = (object)[ |
59
|
|
|
'parameters' => [ |
60
|
|
|
(object)[ |
61
|
|
|
"in" => "body", |
62
|
|
|
"name" => "body", |
63
|
|
|
"schema" => (object)[ |
64
|
|
|
'$ref' => "#/definitions/JmsAnnotatedResourceStub" |
65
|
|
|
] |
66
|
|
|
] |
67
|
|
|
] |
68
|
|
|
]; |
69
|
|
|
|
70
|
|
|
$operationObject = OperationObject::createFromOperationDefinition((object)$operationDefinition); |
71
|
|
|
|
72
|
|
|
$actual = $this->contentDecoder->decodeContent($request, $operationObject); |
73
|
|
|
|
74
|
|
|
$className = 'KleijnWeb\SwaggerBundle\Tests\Request\ContentDecoder\JmsAnnotatedResourceStub'; |
75
|
|
|
$expected = (new $className)->setFoo('bar'); |
76
|
|
|
|
77
|
|
|
$this->assertEquals($expected, $actual); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @test |
82
|
|
|
* |
83
|
|
|
* @expectedException \KleijnWeb\SwaggerBundle\Exception\MalformedContentException |
84
|
|
|
*/ |
85
|
|
View Code Duplication |
public function willThrowMalformedContentExceptionWhenDecodingFails() |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
$content = 'lkjhlkj'; |
88
|
|
|
$request = new Request([], [], [], [], [], [], $content); |
89
|
|
|
$request->headers->set('Content-Type', 'application/json'); |
90
|
|
|
|
91
|
|
|
$operationObject = OperationObject::createFromOperationDefinition((object)[]); |
92
|
|
|
$this->contentDecoder->decodeContent($request, $operationObject); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @test |
97
|
|
|
* @dataProvider contentTypeProvider |
98
|
|
|
* |
99
|
|
|
* @param string $contentType |
100
|
|
|
*/ |
101
|
|
View Code Duplication |
public function willAlwaysDecodeJson($contentType) |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
$content = '{ "foo": "bar" }'; |
104
|
|
|
$request = new Request([], [], [], [], [], [], $content); |
105
|
|
|
$request->headers->set('Content-Type', $contentType); |
106
|
|
|
|
107
|
|
|
$operationDefinition = (object)[ |
108
|
|
|
'parameters' => [ |
109
|
|
|
(object)[ |
110
|
|
|
"in" => "body", |
111
|
|
|
"name" => "body", |
112
|
|
|
"schema" => (object)[ |
113
|
|
|
'$ref' => "#/definitions/JmsAnnotatedResourceStub" |
114
|
|
|
] |
115
|
|
|
] |
116
|
|
|
] |
117
|
|
|
]; |
118
|
|
|
|
119
|
|
|
$operationObject = OperationObject::createFromOperationDefinition((object)$operationDefinition); |
120
|
|
|
|
121
|
|
|
$actual = $this->contentDecoder->decodeContent($request, $operationObject); |
122
|
|
|
$className = 'KleijnWeb\SwaggerBundle\Tests\Request\ContentDecoder\JmsAnnotatedResourceStub'; |
123
|
|
|
$expected = (new $className)->setFoo('bar'); |
124
|
|
|
$this->assertEquals($expected, $actual); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return array |
129
|
|
|
*/ |
130
|
|
|
public static function contentTypeProvider() |
131
|
|
|
{ |
132
|
|
|
return [ |
133
|
|
|
['application/json'], |
134
|
|
|
['application/vnd.api+json'] |
135
|
|
|
]; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..