1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (C) 2017 Gerrit Addiks. |
4
|
|
|
* This package (including this file) was released under the terms of the GPL-3.0. |
5
|
|
|
* You should have received a copy of the GNU General Public License along with this program. |
6
|
|
|
* If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy. |
7
|
|
|
* @license GPL-3.0 |
8
|
|
|
* @author Gerrit Addiks <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Addiks; |
12
|
|
|
|
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
use Addiks\RDMBundle\ValueResolver\NullableValueResolver; |
15
|
|
|
use Addiks\RDMBundle\ValueResolver\ValueResolverInterface; |
16
|
|
|
use Addiks\RDMBundle\Mapping\MappingInterface; |
17
|
|
|
use Addiks\RDMBundle\Hydration\HydrationContextInterface; |
18
|
|
|
use Addiks\RDMBundle\Mapping\NullableMappingInterface; |
19
|
|
|
use Addiks\RDMBundle\Exception\InvalidMappingException; |
20
|
|
|
use Doctrine\DBAL\Schema\Column; |
21
|
|
|
|
22
|
|
|
final class NullableValueResolverTest extends TestCase |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var NullableValueResolver |
27
|
|
|
*/ |
28
|
|
|
private $valueResolver; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var ValueResolverInterface |
32
|
|
|
*/ |
33
|
|
|
private $rootValueResolver; |
34
|
|
|
|
35
|
|
|
public function setUp() |
36
|
|
|
{ |
37
|
|
|
$this->rootValueResolver = $this->createMock(ValueResolverInterface::class); |
|
|
|
|
38
|
|
|
|
39
|
|
|
$this->valueResolver = new NullableValueResolver($this->rootValueResolver); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @test |
44
|
|
|
*/ |
45
|
|
|
public function shouldResolveValue() |
46
|
|
|
{ |
47
|
|
|
/** @var NullableMappingInterface $fieldMapping */ |
48
|
|
|
$fieldMapping = $this->createMock(NullableMappingInterface::class); |
49
|
|
|
|
50
|
|
|
/** @var MappingInterface $fieldMapping */ |
51
|
|
|
$innerMapping = $this->createMock(MappingInterface::class); |
52
|
|
|
|
53
|
|
|
/** @var Column $column */ |
54
|
|
|
$column = $this->createMock(Column::class); |
55
|
|
|
$column->method("getName")->willReturn("some_column"); |
|
|
|
|
56
|
|
|
|
57
|
|
|
$fieldMapping->method('getInnerMapping')->willReturn($innerMapping); |
|
|
|
|
58
|
|
|
$fieldMapping->method('collectDBALColumns')->willReturn([ |
|
|
|
|
59
|
|
|
$column |
60
|
|
|
]); |
61
|
|
|
|
62
|
|
|
/** @var HydrationContextInterface $context */ |
63
|
|
|
$context = $this->createMock(HydrationContextInterface::class); |
64
|
|
|
|
65
|
|
|
/** @var array $dataFromAdditionalColumns */ |
66
|
|
|
$dataFromAdditionalColumns = [ |
67
|
|
|
'some_column' => 'foo' |
68
|
|
|
]; |
69
|
|
|
|
70
|
|
|
/** @var mixed $expectedResult */ |
71
|
|
|
$expectedResult = 'bar'; |
72
|
|
|
|
73
|
|
|
$this->rootValueResolver->expects($this->once())->method('resolveValue')->with( |
|
|
|
|
74
|
|
|
$this->equalTo($innerMapping), |
75
|
|
|
$this->equalTo($context), |
76
|
|
|
$dataFromAdditionalColumns |
77
|
|
|
)->willReturn($expectedResult); |
78
|
|
|
|
79
|
|
|
/** @var mixed $actualResult */ |
80
|
|
|
$actualResult = $this->valueResolver->resolveValue( |
81
|
|
|
$fieldMapping, |
82
|
|
|
$context, |
83
|
|
|
$dataFromAdditionalColumns |
84
|
|
|
); |
85
|
|
|
|
86
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @test |
91
|
|
|
*/ |
92
|
|
|
public function shouldThrowExceptionOnMissingColumn() |
93
|
|
|
{ |
94
|
|
|
$this->expectException(InvalidMappingException::class); |
95
|
|
|
|
96
|
|
|
/** @var NullableMappingInterface $fieldMapping */ |
97
|
|
|
$fieldMapping = $this->createMock(NullableMappingInterface::class); |
98
|
|
|
|
99
|
|
|
/** @var MappingInterface $fieldMapping */ |
100
|
|
|
$innerMapping = $this->createMock(MappingInterface::class); |
101
|
|
|
|
102
|
|
|
$fieldMapping->method('getInnerMapping')->willReturn($innerMapping); |
|
|
|
|
103
|
|
|
|
104
|
|
|
$this->valueResolver->resolveValue( |
105
|
|
|
$fieldMapping, |
106
|
|
|
$this->createMock(HydrationContextInterface::class), |
|
|
|
|
107
|
|
|
[] |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @test |
113
|
|
|
*/ |
114
|
|
|
public function shouldNotResolveValueOnNull() |
115
|
|
|
{ |
116
|
|
|
/** @var NullableMappingInterface $fieldMapping */ |
117
|
|
|
$fieldMapping = $this->createMock(NullableMappingInterface::class); |
118
|
|
|
|
119
|
|
|
/** @var MappingInterface $fieldMapping */ |
120
|
|
|
$innerMapping = $this->createMock(MappingInterface::class); |
121
|
|
|
|
122
|
|
|
/** @var Column $column */ |
123
|
|
|
$column = $this->createMock(Column::class); |
124
|
|
|
$column->method("getName")->willReturn("some_column"); |
|
|
|
|
125
|
|
|
|
126
|
|
|
$fieldMapping->method('getInnerMapping')->willReturn($innerMapping); |
|
|
|
|
127
|
|
|
$fieldMapping->method('collectDBALColumns')->willReturn([ |
|
|
|
|
128
|
|
|
$column |
129
|
|
|
]); |
130
|
|
|
|
131
|
|
|
/** @var HydrationContextInterface $context */ |
132
|
|
|
$context = $this->createMock(HydrationContextInterface::class); |
133
|
|
|
|
134
|
|
|
/** @var array $dataFromAdditionalColumns */ |
135
|
|
|
$dataFromAdditionalColumns = [ |
136
|
|
|
'some_column' => false |
137
|
|
|
]; |
138
|
|
|
|
139
|
|
|
/** @var mixed $expectedResult */ |
140
|
|
|
$expectedResult = 'bar'; |
|
|
|
|
141
|
|
|
|
142
|
|
|
$this->rootValueResolver->expects($this->never())->method('resolveValue'); |
|
|
|
|
143
|
|
|
|
144
|
|
|
$this->assertNull($this->valueResolver->resolveValue( |
145
|
|
|
$fieldMapping, |
146
|
|
|
$context, |
147
|
|
|
$dataFromAdditionalColumns |
148
|
|
|
)); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @test |
153
|
|
|
* @dataProvider dataProviderForShouldRevertValue |
154
|
|
|
*/ |
155
|
|
|
public function shouldRevertValue( |
156
|
|
|
$expectedResult, |
157
|
|
|
array $revertedData, |
158
|
|
|
string $columnName |
159
|
|
|
) { |
160
|
|
|
/** @var NullableMappingInterface $fieldMapping */ |
161
|
|
|
$fieldMapping = $this->createMock(NullableMappingInterface::class); |
162
|
|
|
|
163
|
|
|
/** @var MappingInterface $fieldMapping */ |
164
|
|
|
$innerMapping = $this->createMock(MappingInterface::class); |
165
|
|
|
|
166
|
|
|
$fieldMapping->method('getInnerMapping')->willReturn($innerMapping); |
|
|
|
|
167
|
|
|
$fieldMapping->method('getDeterminatorColumnName')->willReturn($columnName); |
|
|
|
|
168
|
|
|
|
169
|
|
|
/** @var HydrationContextInterface $context */ |
170
|
|
|
$context = $this->createMock(HydrationContextInterface::class); |
171
|
|
|
|
172
|
|
|
/** @var mixed $valueFromEntityField */ |
173
|
|
|
$valueFromEntityField = "foo"; |
174
|
|
|
|
175
|
|
|
$this->rootValueResolver->expects($this->once())->method('revertValue')->with( |
|
|
|
|
176
|
|
|
$this->equalTo($innerMapping), |
177
|
|
|
$this->equalTo($context), |
178
|
|
|
$valueFromEntityField |
179
|
|
|
)->willReturn($revertedData); |
180
|
|
|
|
181
|
|
|
/** @var mixed $actualResult */ |
182
|
|
|
$actualResult = $this->valueResolver->revertValue( |
183
|
|
|
$fieldMapping, |
184
|
|
|
$context, |
185
|
|
|
$valueFromEntityField |
186
|
|
|
); |
187
|
|
|
|
188
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function dataProviderForShouldRevertValue() |
192
|
|
|
{ |
193
|
|
|
return array( |
194
|
|
|
[ |
195
|
|
|
[ |
196
|
|
|
'some_column' => true |
197
|
|
|
], |
198
|
|
|
[], |
199
|
|
|
"some_column" |
200
|
|
|
], |
201
|
|
|
[ |
202
|
|
|
[ |
203
|
|
|
'some_column' => 123 |
204
|
|
|
], |
205
|
|
|
[ |
206
|
|
|
'some_column' => 123 |
207
|
|
|
], |
208
|
|
|
"some_column" |
209
|
|
|
], |
210
|
|
|
[ |
211
|
|
|
[], |
212
|
|
|
[], |
213
|
|
|
"" |
214
|
|
|
], |
215
|
|
|
); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @test |
220
|
|
|
*/ |
221
|
|
View Code Duplication |
public function shouldNotRevertValueForNonNullableMapping() |
|
|
|
|
222
|
|
|
{ |
223
|
|
|
/** @var MappingInterface $fieldMapping */ |
224
|
|
|
$fieldMapping = $this->createMock(MappingInterface::class); |
225
|
|
|
|
226
|
|
|
/** @var HydrationContextInterface $context */ |
227
|
|
|
$context = $this->createMock(HydrationContextInterface::class); |
228
|
|
|
|
229
|
|
|
/** @var mixed $valueFromEntityField */ |
230
|
|
|
$valueFromEntityField = "foo"; |
231
|
|
|
|
232
|
|
|
/** @var mixed $actualResult */ |
233
|
|
|
$this->assertEmpty($this->valueResolver->revertValue( |
234
|
|
|
$fieldMapping, |
235
|
|
|
$context, |
236
|
|
|
$valueFromEntityField |
237
|
|
|
)); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @test |
242
|
|
|
*/ |
243
|
|
View Code Duplication |
public function shouldAssertValue() |
|
|
|
|
244
|
|
|
{ |
245
|
|
|
$this->assertNull($this->valueResolver->assertValue( |
246
|
|
|
$this->createMock(MappingInterface::class), |
|
|
|
|
247
|
|
|
$this->createMock(HydrationContextInterface::class), |
|
|
|
|
248
|
|
|
[], |
249
|
|
|
null |
250
|
|
|
)); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
} |
254
|
|
|
|
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..