|
1
|
|
|
<?php declare(strict_types = 1); |
|
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\DocumentRepository; |
|
13
|
|
|
use KleijnWeb\SwaggerBundle\Document\Specification; |
|
14
|
|
|
use KleijnWeb\SwaggerBundle\Document\Specification\Operation; |
|
15
|
|
|
use KleijnWeb\SwaggerBundle\Request\ContentDecoder; |
|
16
|
|
|
use KleijnWeb\SwaggerBundle\Serialize\SerializationTypeResolver; |
|
17
|
|
|
use KleijnWeb\SwaggerBundle\Serialize\Serializer\Factory\JmsSerializerFactory; |
|
18
|
|
|
use KleijnWeb\SwaggerBundle\Serialize\Serializer\JmsSerializerAdapter; |
|
19
|
|
|
use KleijnWeb\SwaggerBundle\Tests\Request\TestRequestFactory; |
|
20
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @author John Kleijn <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class ContentDecoderJmsSerializerCompatibilityTest extends \PHPUnit_Framework_TestCase |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var ContentDecoder |
|
29
|
|
|
*/ |
|
30
|
|
|
private $contentDecoder; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var Serializer |
|
34
|
|
|
*/ |
|
35
|
|
|
private $serializer; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Create serializer |
|
39
|
|
|
*/ |
|
40
|
|
|
protected function setUp() |
|
41
|
|
|
{ |
|
42
|
|
|
$this->serializer = new JmsSerializerAdapter(JmsSerializerFactory::factory()); |
|
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
$typeResolver = $this |
|
45
|
|
|
->getMockBuilder(SerializationTypeResolver::class) |
|
46
|
|
|
->disableOriginalConstructor() |
|
47
|
|
|
->getMock(); |
|
48
|
|
|
|
|
49
|
|
|
$typeResolver |
|
50
|
|
|
->expects($this->any()) |
|
51
|
|
|
->method('resolveOperationBodyType') |
|
52
|
|
|
->willReturn(JmsAnnotatedResourceStub::class); |
|
53
|
|
|
|
|
54
|
|
|
$documentRepository = $this |
|
55
|
|
|
->getMockBuilder(DocumentRepository::class) |
|
56
|
|
|
->disableOriginalConstructor() |
|
57
|
|
|
->getMock(); |
|
58
|
|
|
|
|
59
|
|
|
$documentRepository |
|
60
|
|
|
->expects($this->any()) |
|
61
|
|
|
->method('get') |
|
62
|
|
|
->willReturn(new Specification(new \stdClass)); |
|
63
|
|
|
|
|
64
|
|
|
/** @noinspection PhpParamsInspection */ |
|
65
|
|
|
$this->contentDecoder = new ContentDecoder($this->serializer, $documentRepository, $typeResolver); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @test |
|
70
|
|
|
*/ |
|
71
|
|
View Code Duplication |
public function canDeserializeIntoObject() |
|
|
|
|
|
|
72
|
|
|
{ |
|
73
|
|
|
$content = [ |
|
74
|
|
|
'foo' => 'bar' |
|
75
|
|
|
]; |
|
76
|
|
|
$request = TestRequestFactory::create(json_encode($content), [], 'faux'); |
|
77
|
|
|
$request->headers->set('Content-Type', 'application/json'); |
|
78
|
|
|
|
|
79
|
|
|
$operationDefinition = (object)[ |
|
80
|
|
|
'parameters' => [ |
|
81
|
|
|
(object)[ |
|
82
|
|
|
"in" => "body", |
|
83
|
|
|
"name" => "body", |
|
84
|
|
|
"schema" => (object)[ |
|
85
|
|
|
'$ref' => "#/definitions/JmsAnnotatedResourceStub" |
|
86
|
|
|
] |
|
87
|
|
|
] |
|
88
|
|
|
] |
|
89
|
|
|
]; |
|
90
|
|
|
|
|
91
|
|
|
$operationObject = Operation::createFromOperationDefinition((object)$operationDefinition); |
|
92
|
|
|
|
|
93
|
|
|
$actual = $this->contentDecoder->decodeContent($request, $operationObject); |
|
94
|
|
|
|
|
95
|
|
|
$expected = (new JmsAnnotatedResourceStub)->setFoo('bar'); |
|
96
|
|
|
|
|
97
|
|
|
$this->assertEquals($expected, $actual); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @test |
|
102
|
|
|
* |
|
103
|
|
|
* @expectedException \KleijnWeb\SwaggerBundle\Exception\MalformedContentException |
|
104
|
|
|
*/ |
|
105
|
|
View Code Duplication |
public function willThrowMalformedContentExceptionWhenDecodingFails() |
|
|
|
|
|
|
106
|
|
|
{ |
|
107
|
|
|
$content = 'lkjhlkj'; |
|
108
|
|
|
$request = TestRequestFactory::create($content, [], 'faux'); |
|
109
|
|
|
$request->headers->set('Content-Type', 'application/json'); |
|
110
|
|
|
|
|
111
|
|
|
$operationObject = Operation::createFromOperationDefinition((object)[]); |
|
112
|
|
|
$this->contentDecoder->decodeContent($request, $operationObject); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @test |
|
117
|
|
|
* @dataProvider contentTypeProvider |
|
118
|
|
|
* |
|
119
|
|
|
* @param string $contentType |
|
120
|
|
|
*/ |
|
121
|
|
View Code Duplication |
public function willAlwaysDecodeJson($contentType) |
|
|
|
|
|
|
122
|
|
|
{ |
|
123
|
|
|
$content = '{ "foo": "bar" }'; |
|
124
|
|
|
$request = TestRequestFactory::create($content, [], 'faux'); |
|
125
|
|
|
$request->headers->set('Content-Type', $contentType); |
|
126
|
|
|
|
|
127
|
|
|
$operationDefinition = (object)[ |
|
128
|
|
|
'parameters' => [ |
|
129
|
|
|
(object)[ |
|
130
|
|
|
"in" => "body", |
|
131
|
|
|
"name" => "body", |
|
132
|
|
|
"schema" => (object)[ |
|
133
|
|
|
'$ref' => "#/definitions/JmsAnnotatedResourceStub" |
|
134
|
|
|
] |
|
135
|
|
|
] |
|
136
|
|
|
] |
|
137
|
|
|
]; |
|
138
|
|
|
|
|
139
|
|
|
$operationObject = Operation::createFromOperationDefinition((object)$operationDefinition); |
|
140
|
|
|
|
|
141
|
|
|
$actual = $this->contentDecoder->decodeContent($request, $operationObject); |
|
142
|
|
|
$expected = (new JmsAnnotatedResourceStub)->setFoo('bar'); |
|
143
|
|
|
$this->assertEquals($expected, $actual); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @return array |
|
148
|
|
|
*/ |
|
149
|
|
|
public static function contentTypeProvider() |
|
150
|
|
|
{ |
|
151
|
|
|
return [ |
|
152
|
|
|
['application/json'], |
|
153
|
|
|
['application/vnd.api+json'] |
|
154
|
|
|
]; |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
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..