|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ComradeReader\Test\Service; |
|
4
|
|
|
|
|
5
|
|
|
use ComradeReader\Model\Entity\PaginatedResults; |
|
6
|
|
|
use ComradeReader\Service\ComradeDeserializer; |
|
7
|
|
|
use ComradeReader\Test\Helpers\SimpleTestEntity; |
|
8
|
|
|
use Symfony\Component\Serializer\Encoder\JsonDecode; |
|
9
|
|
|
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer; |
|
10
|
|
|
use Symfony\Component\Serializer\Serializer; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @see ComradeDeserializer |
|
14
|
|
|
* @package ComradeReader\Test\Service |
|
15
|
|
|
*/ |
|
16
|
|
|
class ComradeDeserializerTest extends \PHPUnit_Framework_TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @param array $response |
|
20
|
|
|
* @return ComradeDeserializer |
|
21
|
|
|
*/ |
|
22
|
|
|
private function constructComrade($response) |
|
23
|
|
|
{ |
|
24
|
|
|
return new ComradeDeserializer(json_encode($response), new Serializer([ |
|
25
|
|
|
new PropertyNormalizer() |
|
26
|
|
|
], [ |
|
27
|
|
|
new JsonDecode() |
|
28
|
|
|
])); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @throws \ComradeReader\Exception\Deserializer\DeserializerException |
|
33
|
|
|
* @expectedException \ComradeReader\Exception\Deserializer\DeserializerException |
|
34
|
|
|
*/ |
|
35
|
|
|
public function testInvalidResponse() |
|
36
|
|
|
{ |
|
37
|
|
|
$comrade = $this->constructComrade(false); |
|
|
|
|
|
|
38
|
|
|
$comrade->getResponse(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @see ComradeDeserializer::decode() |
|
43
|
|
|
*/ |
|
44
|
|
|
public function testDecode() |
|
45
|
|
|
{ |
|
46
|
|
|
$comrade = $this->constructComrade([ |
|
47
|
|
|
'success' => true, |
|
48
|
|
|
'data' => [ |
|
49
|
|
|
'id' => 1, |
|
50
|
|
|
'colorName' => 'red', |
|
51
|
|
|
] |
|
52
|
|
|
]); |
|
53
|
|
|
|
|
54
|
|
|
/** @var SimpleTestEntity $decoded */ |
|
55
|
|
|
$decoded = $comrade->decode(SimpleTestEntity::class); |
|
56
|
|
|
|
|
57
|
|
|
$this->assertSame(1, $decoded->getId()); |
|
58
|
|
|
$this->assertSame('red', $decoded->getColorName()); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @see ComradeDeserializer::decodeMultiple() |
|
63
|
|
|
*/ |
|
64
|
|
|
public function testDecodeMultiple() |
|
65
|
|
|
{ |
|
66
|
|
|
$comrade = $this->constructComrade([ |
|
67
|
|
|
'success' => true, |
|
68
|
|
|
'data' => [ |
|
69
|
|
|
[ |
|
70
|
|
|
'id' => 1, |
|
71
|
|
|
'colorName' => 'red', |
|
72
|
|
|
], |
|
73
|
|
|
[ |
|
74
|
|
|
'id' => 2, |
|
75
|
|
|
'colorName' => 'black', |
|
76
|
|
|
] |
|
77
|
|
|
] |
|
78
|
|
|
]); |
|
79
|
|
|
|
|
80
|
|
|
/** @var SimpleTestEntity[] $decoded */ |
|
81
|
|
|
$decoded = $comrade->decodeMultiple(SimpleTestEntity::class); |
|
82
|
|
|
|
|
83
|
|
|
$this->assertCount(2, $decoded); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @see ComradeDeserializer::getData() |
|
88
|
|
|
*/ |
|
89
|
|
|
public function testDecodeIntoArray() |
|
90
|
|
|
{ |
|
91
|
|
|
$comrade = $this->constructComrade([ |
|
92
|
|
|
'success' => true, |
|
93
|
|
|
'data' => [ |
|
94
|
|
|
'id' => 1, |
|
95
|
|
|
'colorName' => 'Black & Red', |
|
96
|
|
|
] |
|
97
|
|
|
]); |
|
98
|
|
|
|
|
99
|
|
|
/** @var array $decoded */ |
|
100
|
|
|
$decoded = $comrade->getData(); |
|
101
|
|
|
|
|
102
|
|
|
$this->assertInternalType('array', $decoded); |
|
103
|
|
|
$this->assertSame('Black & Red', $decoded['colorName']); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @see ComradeDeserializer::getPlainResponse() |
|
108
|
|
|
*/ |
|
109
|
|
|
public function testGetPlainResponse() |
|
110
|
|
|
{ |
|
111
|
|
|
$request = [ |
|
112
|
|
|
'success' => true, |
|
113
|
|
|
'data' => [ |
|
114
|
|
|
'id' => 1, |
|
115
|
|
|
'colorName' => 'Black & Red', |
|
116
|
|
|
] |
|
117
|
|
|
]; |
|
118
|
|
|
$comrade = $this->constructComrade($request); |
|
119
|
|
|
|
|
120
|
|
|
$this->assertSame(json_encode($request), $comrade->getPlainResponse()); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @see ComradeDeserializer::decodeMultiple() |
|
125
|
|
|
*/ |
|
126
|
|
|
public function testPaginatedResults() |
|
127
|
|
|
{ |
|
128
|
|
|
$comrade = $this->constructComrade([ |
|
129
|
|
|
'success' => true, |
|
130
|
|
|
'data' => [ |
|
131
|
|
|
'results' => [ |
|
132
|
|
|
[ |
|
133
|
|
|
'id' => 1, |
|
134
|
|
|
'colorName' => 'Black & Red', |
|
135
|
|
|
], |
|
136
|
|
|
], |
|
137
|
|
|
|
|
138
|
|
|
'current_page' => 1, |
|
139
|
|
|
'max_pages' => 1, |
|
140
|
|
|
] |
|
141
|
|
|
]); |
|
142
|
|
|
|
|
143
|
|
|
/** @var PaginatedResults $results */ |
|
144
|
|
|
$results = $comrade->decodeMultiple(SimpleTestEntity::class); |
|
145
|
|
|
|
|
146
|
|
|
$this->assertInstanceOf( |
|
147
|
|
|
PaginatedResults::class, |
|
148
|
|
|
$results |
|
149
|
|
|
); |
|
150
|
|
|
|
|
151
|
|
|
$this->assertInstanceOf(SimpleTestEntity::class, current($results->getResults())); |
|
152
|
|
|
} |
|
153
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: