|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Lamoda\AtolClient\Tests\Unit\Converter; |
|
4
|
|
|
|
|
5
|
|
|
use Lamoda\AtolClient\Converter\ObjectConverter; |
|
6
|
|
|
use JMS\Serializer\SerializerInterface; |
|
7
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
use Symfony\Component\Validator\ConstraintViolationListInterface; |
|
10
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @group unit |
|
14
|
|
|
* @covers \Lamoda\AtolClient\Converter\ObjectConverter |
|
15
|
|
|
*/ |
|
16
|
|
|
class ObjectConverterTest extends TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var SerializerInterface | MockObject |
|
20
|
|
|
*/ |
|
21
|
|
|
private $serializer; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var ValidatorInterface | MockObject |
|
25
|
|
|
*/ |
|
26
|
|
|
private $validator; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var ObjectConverter |
|
30
|
|
|
*/ |
|
31
|
|
|
private $objectConverter; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* {@inheritdoc} |
|
35
|
|
|
*/ |
|
36
|
|
|
protected function setUp() |
|
37
|
|
|
{ |
|
38
|
|
|
$this->serializer = $this->createMock(SerializerInterface::class); |
|
|
|
|
|
|
39
|
|
|
$this->validator = $this->createMock(ValidatorInterface::class); |
|
|
|
|
|
|
40
|
|
|
$this->objectConverter = new ObjectConverter($this->serializer, $this->validator); |
|
|
|
|
|
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function testGetRequestString() |
|
44
|
|
|
{ |
|
45
|
|
|
$object = (object) []; |
|
46
|
|
|
$json = '{}'; |
|
47
|
|
|
|
|
48
|
|
|
/* @see ObjectConverter::assertValid() */ |
|
49
|
|
|
$this->validator |
|
|
|
|
|
|
50
|
|
|
->expects($this->once()) |
|
51
|
|
|
->method('validate') |
|
52
|
|
|
->with($object) |
|
53
|
|
|
->willReturn([]); |
|
54
|
|
|
|
|
55
|
|
|
/* @see ObjectConverter::serializeBodyObject() */ |
|
56
|
|
|
$this->serializer |
|
|
|
|
|
|
57
|
|
|
->expects($this->once()) |
|
58
|
|
|
->method('serialize') |
|
59
|
|
|
->with($object, 'atol_client', $this->anything()) |
|
60
|
|
|
->willReturn($json); |
|
61
|
|
|
|
|
62
|
|
|
$result = $this->objectConverter->getRequestString($object); |
|
63
|
|
|
$this->assertSame($json, $result); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @expectedException \Lamoda\AtolClient\Exception\ParseException |
|
68
|
|
|
*/ |
|
69
|
|
|
public function testGetRequestStringParseException() |
|
70
|
|
|
{ |
|
71
|
|
|
/* @see ObjectConverter::assertValid() */ |
|
72
|
|
|
$this->validator |
|
|
|
|
|
|
73
|
|
|
->method('validate') |
|
74
|
|
|
->willReturn([]); |
|
75
|
|
|
|
|
76
|
|
|
/* @see ObjectConverter::serializeBodyObject() */ |
|
77
|
|
|
$this->serializer |
|
|
|
|
|
|
78
|
|
|
->expects($this->once()) |
|
79
|
|
|
->method('serialize') |
|
80
|
|
|
->willThrowException(new \RuntimeException()); |
|
81
|
|
|
|
|
82
|
|
|
$this->objectConverter->getRequestString((object) []); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @expectedException \Lamoda\AtolClient\Exception\ValidationException |
|
87
|
|
|
* @expectedExceptionCode 2 |
|
88
|
|
|
*/ |
|
89
|
|
|
public function testGetRequestStringInvalid() |
|
90
|
|
|
{ |
|
91
|
|
|
$object = (object) []; |
|
92
|
|
|
$errors = $this->createMock(ConstraintViolationListInterface::class); |
|
93
|
|
|
|
|
94
|
|
|
/* @see ObjectConverter::assertValid() */ |
|
95
|
|
|
$this->validator |
|
|
|
|
|
|
96
|
|
|
->expects($this->once()) |
|
97
|
|
|
->method('validate') |
|
98
|
|
|
->with($object) |
|
99
|
|
|
->willReturn($errors); |
|
100
|
|
|
$errors |
|
101
|
|
|
->expects($this->once()) |
|
102
|
|
|
->method('count') |
|
103
|
|
|
->willReturn(1); |
|
104
|
|
|
|
|
105
|
|
|
$this->objectConverter->getRequestString($object); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function testGetResponseObject() |
|
109
|
|
|
{ |
|
110
|
|
|
$class = 'class'; |
|
111
|
|
|
$json = 'json'; |
|
112
|
|
|
$object = (object) []; |
|
113
|
|
|
|
|
114
|
|
|
/* @see ObjectConverter::deserialize() */ |
|
115
|
|
|
$this->serializer |
|
|
|
|
|
|
116
|
|
|
->expects($this->once()) |
|
117
|
|
|
->method('deserialize') |
|
118
|
|
|
->with($json, $class, 'atol_client') |
|
119
|
|
|
->willReturn($object); |
|
120
|
|
|
/* @see ObjectConverter::assertValid() */ |
|
121
|
|
|
$this->validator |
|
|
|
|
|
|
122
|
|
|
->expects($this->once()) |
|
123
|
|
|
->method('validate') |
|
124
|
|
|
->with($object) |
|
125
|
|
|
->willReturn([]); |
|
126
|
|
|
|
|
127
|
|
|
$result = $this->objectConverter->getResponseObject($class, $json); |
|
128
|
|
|
$this->assertSame($object, $result); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @expectedException \Lamoda\AtolClient\Exception\ParseException |
|
133
|
|
|
* @expectedExceptionCode 2 |
|
134
|
|
|
*/ |
|
135
|
|
|
public function testGetResponseObjectParseException() |
|
136
|
|
|
{ |
|
137
|
|
|
/* @see ObjectConverter::deserialize() */ |
|
138
|
|
|
$this->serializer |
|
|
|
|
|
|
139
|
|
|
->method('deserialize') |
|
140
|
|
|
->willThrowException(new \RuntimeException()); |
|
141
|
|
|
|
|
142
|
|
|
$this->objectConverter->getResponseObject('class', 'json'); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @expectedException \Lamoda\AtolClient\Exception\ValidationException |
|
147
|
|
|
* @expectedExceptionCode 2 |
|
148
|
|
|
*/ |
|
149
|
|
|
public function testGetResponseObjectInvalid() |
|
150
|
|
|
{ |
|
151
|
|
|
$errors = $this->createMock(ConstraintViolationListInterface::class); |
|
152
|
|
|
|
|
153
|
|
|
/* @see ObjectConverter::assertValid() */ |
|
154
|
|
|
$this->serializer |
|
|
|
|
|
|
155
|
|
|
->method('deserialize') |
|
156
|
|
|
->willReturn((object) []); |
|
157
|
|
|
$this->validator |
|
|
|
|
|
|
158
|
|
|
->method('validate') |
|
159
|
|
|
->willReturn($errors); |
|
160
|
|
|
$errors |
|
161
|
|
|
->expects($this->once()) |
|
162
|
|
|
->method('count') |
|
163
|
|
|
->willReturn(1); |
|
164
|
|
|
|
|
165
|
|
|
$this->objectConverter->getResponseObject('class', 'json'); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
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..