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