|
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\Serialize\Serializer; |
|
10
|
|
|
|
|
11
|
|
|
use KleijnWeb\SwaggerBundle\Document\Specification; |
|
12
|
|
|
use KleijnWeb\SwaggerBundle\Tests\Serialize\Serializer\Stubs\Bar; |
|
13
|
|
|
use KleijnWeb\SwaggerBundle\Tests\Serialize\Serializer\Stubs\Foo; |
|
14
|
|
|
use KleijnWeb\SwaggerBundle\Tests\Serialize\Serializer\Stubs\Meh; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @author John Kleijn <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class ObjectDeserializationTest extends ObjectSerializerTest |
|
20
|
|
|
{ |
|
21
|
|
|
protected function setUp() |
|
22
|
|
|
{ |
|
23
|
|
|
parent::setUp(); |
|
24
|
|
|
|
|
25
|
|
|
$this->typeResolverMock->expects($this->once())->method('reverseLookup')->willReturn('Foo'); |
|
26
|
|
|
$this->typeResolverMock |
|
27
|
|
|
->expects($this->atLeast(1)) |
|
28
|
|
|
->method('resolveUsingSchema') |
|
29
|
|
|
->willReturnCallback(function (\stdClass $schema) { |
|
30
|
|
|
return "KleijnWeb\\SwaggerBundle\\Tests\\Serialize\\Serializer\\Stubs\\$schema->class"; |
|
31
|
|
|
}); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @test |
|
36
|
|
|
* |
|
37
|
|
|
* @dataProvider deserializationProvider |
|
38
|
|
|
* |
|
39
|
|
|
* @param string $data |
|
40
|
|
|
* @param Foo $expected |
|
41
|
|
|
*/ |
|
42
|
|
|
public function canDeSerialize(string $data, Foo $expected) |
|
43
|
|
|
{ |
|
44
|
|
|
$actual = $this->serializer->deserialize($data, Foo::class, $this->specification); |
|
45
|
|
|
$this->assertEquals($expected, $actual); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @return array |
|
50
|
|
|
*/ |
|
51
|
|
|
public function deserializationProvider(): array |
|
52
|
|
|
{ |
|
53
|
|
|
return [ |
|
54
|
|
|
[ |
|
55
|
|
|
json_encode([ |
|
56
|
|
|
'a' => '5', |
|
57
|
|
|
'bar' => [ |
|
58
|
|
|
'b' => 6, |
|
59
|
|
|
'meh' => ['c' => 1], |
|
60
|
|
|
'mehs' => [ |
|
61
|
|
|
['c' => 2], |
|
62
|
|
|
['c' => 3], |
|
63
|
|
|
] |
|
64
|
|
|
] |
|
65
|
|
|
]), |
|
66
|
|
|
new Foo('5', new Bar(6, new Meh(1), new Meh(2), new Meh(3))), |
|
67
|
|
|
], |
|
68
|
|
|
[ |
|
69
|
|
|
json_encode([ |
|
70
|
|
|
'a' => '5', |
|
71
|
|
|
'bar' => [ |
|
72
|
|
|
'b' => 6, |
|
73
|
|
|
'meh' => ['c' => [1]], |
|
74
|
|
|
'mehs' => [ |
|
75
|
|
|
['c' => 2], |
|
76
|
|
|
['c' => 3], |
|
77
|
|
|
] |
|
78
|
|
|
] |
|
79
|
|
|
]), |
|
80
|
|
|
new Foo('5', new Bar(6, new Meh([1]), new Meh(2), new Meh(3))), |
|
81
|
|
|
], |
|
82
|
|
|
// Will ignore non-existent properties, should be handled by validation |
|
83
|
|
|
[ |
|
84
|
|
|
json_encode([ |
|
85
|
|
|
'a' => '5', |
|
86
|
|
|
'b' => '5', |
|
87
|
|
|
'bar' => [ |
|
88
|
|
|
'b' => 6, |
|
89
|
|
|
'meh' => ['c' => [1]], |
|
90
|
|
|
'mehs' => [ |
|
91
|
|
|
['c' => 2], |
|
92
|
|
|
['c' => 3], |
|
93
|
|
|
] |
|
94
|
|
|
] |
|
95
|
|
|
]), |
|
96
|
|
|
new Foo('5', new Bar(6, new Meh([1]), new Meh(2), new Meh(3))), |
|
97
|
|
|
], |
|
98
|
|
|
[ |
|
99
|
|
|
json_encode([ |
|
100
|
|
|
'a' => '5', |
|
101
|
|
|
'bar' => [ |
|
102
|
|
|
'b' => 6, |
|
103
|
|
|
'meh' => ['c' => [1]], |
|
104
|
|
|
'mehs' => [ |
|
105
|
|
|
['c' => 2], |
|
106
|
|
|
['c' => 3], |
|
107
|
|
|
] |
|
108
|
|
|
], |
|
109
|
|
|
'aDate' => (new \DateTime('midnight'))->format('Y-m-d'), |
|
110
|
|
|
'aDateTime' => (new \DateTime())->format(\DateTime::ISO8601), |
|
111
|
|
|
]), |
|
112
|
|
|
new Foo( |
|
113
|
|
|
'5', |
|
114
|
|
|
new Bar(6, new Meh([1]), new Meh(2), new Meh(3)), |
|
115
|
|
|
new \DateTime('midnight'), |
|
116
|
|
|
new \DateTime() |
|
117
|
|
|
), |
|
118
|
|
|
], |
|
119
|
|
|
]; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|