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\Mapping\NullableMapping; |
15
|
|
|
use Doctrine\DBAL\Schema\Column; |
16
|
|
|
use Addiks\RDMBundle\Mapping\MappingInterface; |
17
|
|
|
use Addiks\RDMBundle\Hydration\HydrationContextInterface; |
18
|
|
|
use Addiks\RDMBundle\Exception\InvalidMappingException; |
19
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
20
|
|
|
use PHPUnit\Framework\MockObject\Rule\InvokedCount; |
21
|
|
|
|
22
|
|
|
final class NullableMappingTest extends TestCase |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var NullableMapping |
27
|
|
|
*/ |
28
|
|
|
private $mapping; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var MappingInterface |
32
|
|
|
*/ |
33
|
|
|
private $innerMapping; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var Column |
37
|
|
|
*/ |
38
|
|
|
private $dbalColumn; |
39
|
|
|
|
40
|
|
|
public function setUp(): void |
41
|
|
|
{ |
42
|
|
|
$this->innerMapping = $this->createMock(MappingInterface::class); |
|
|
|
|
43
|
|
|
$this->dbalColumn = $this->createMock(Column::class); |
|
|
|
|
44
|
|
|
|
45
|
|
|
$this->mapping = new NullableMapping($this->innerMapping, $this->dbalColumn, "some origin"); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @test |
50
|
|
|
*/ |
51
|
|
|
public function shouldHaveDBALColumn() |
52
|
|
|
{ |
53
|
|
|
$this->assertSame($this->dbalColumn, $this->mapping->getDBALColumn()); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @test |
58
|
|
|
*/ |
59
|
|
|
public function shouldHaveDeterminatorColumnName() |
60
|
|
|
{ |
61
|
|
|
$this->dbalColumn->method('getName')->willReturn("some_column"); |
|
|
|
|
62
|
|
|
$this->assertSame("some_column", $this->mapping->getDeterminatorColumnName()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @test |
67
|
|
|
*/ |
68
|
|
|
public function shouldHaveInnerMapping() |
69
|
|
|
{ |
70
|
|
|
$this->assertSame($this->innerMapping, $this->mapping->getInnerMapping()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @test |
75
|
|
|
*/ |
76
|
|
|
public function shouldHaveOrigin() |
77
|
|
|
{ |
78
|
|
|
$this->assertSame("some origin", $this->mapping->describeOrigin()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @test |
83
|
|
|
*/ |
84
|
|
|
public function shouldCollectDBALColumns() |
85
|
|
|
{ |
86
|
|
|
/** @var Column $innerColumn */ |
87
|
|
|
$innerColumn = $this->createMock(Column::class); |
88
|
|
|
|
89
|
|
|
$innerColumn->expects($this->once())->method("setNotnull")->with( |
|
|
|
|
90
|
|
|
$this->equalTo(false) |
91
|
|
|
); |
92
|
|
|
|
93
|
|
|
$this->innerMapping->method('collectDBALColumns')->willReturn([$innerColumn]); |
|
|
|
|
94
|
|
|
|
95
|
|
|
/** @var mixed $expectedColumns */ |
96
|
|
|
$expectedColumns = array( |
97
|
|
|
$innerColumn, |
98
|
|
|
$this->dbalColumn |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
/** @var mixed $actualColumns */ |
102
|
|
|
$actualColumns = $this->mapping->collectDBALColumns(); |
103
|
|
|
|
104
|
|
|
$this->assertEquals($expectedColumns, $actualColumns); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @test |
109
|
|
|
*/ |
110
|
|
|
public function shouldResolveNullableValue() |
111
|
|
|
{ |
112
|
|
|
$this->dbalColumn->method("getName")->willReturn("some_column"); |
113
|
|
|
|
114
|
|
|
/** @var HydrationContextInterface $context */ |
115
|
|
|
$context = $this->createMock(HydrationContextInterface::class); |
116
|
|
|
|
117
|
|
|
/** @var array $dataFromAdditionalColumns */ |
118
|
|
|
$dataFromAdditionalColumns = [ |
119
|
|
|
'some_column' => 'foo' |
120
|
|
|
]; |
121
|
|
|
|
122
|
|
|
/** @var mixed $expectedResult */ |
123
|
|
|
$expectedResult = 'bar'; |
124
|
|
|
|
125
|
|
|
$this->innerMapping->expects($this->once())->method('resolveValue')->with( |
|
|
|
|
126
|
|
|
$this->equalTo($context), |
127
|
|
|
$dataFromAdditionalColumns |
128
|
|
|
)->willReturn($expectedResult); |
129
|
|
|
|
130
|
|
|
/** @var mixed $actualResult */ |
131
|
|
|
$actualResult = $this->mapping->resolveValue( |
132
|
|
|
$context, |
133
|
|
|
$dataFromAdditionalColumns |
134
|
|
|
); |
135
|
|
|
|
136
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @test |
141
|
|
|
*/ |
142
|
|
|
public function shouldThrowExceptionOnMissingColumn() |
143
|
|
|
{ |
144
|
|
|
$this->expectException(InvalidMappingException::class); |
145
|
|
|
|
146
|
|
|
/** @var MappingInterface $fieldMapping */ |
147
|
|
|
$innerMapping = $this->createMock(MappingInterface::class); |
148
|
|
|
|
149
|
|
|
$mapping = new NullableMapping($innerMapping, null, "some origin"); |
150
|
|
|
|
151
|
|
|
$mapping->resolveValue( |
152
|
|
|
$this->createMock(HydrationContextInterface::class), |
153
|
|
|
[] |
154
|
|
|
); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @test |
159
|
|
|
*/ |
160
|
|
|
public function shouldNotResolveValueOnNull() |
161
|
|
|
{ |
162
|
|
|
$this->dbalColumn->method('getName')->willReturn("some_column"); |
163
|
|
|
|
164
|
|
|
/** @var HydrationContextInterface $context */ |
165
|
|
|
$context = $this->createMock(HydrationContextInterface::class); |
166
|
|
|
|
167
|
|
|
/** @var array $dataFromAdditionalColumns */ |
168
|
|
|
$dataFromAdditionalColumns = [ |
169
|
|
|
'some_column' => false |
170
|
|
|
]; |
171
|
|
|
|
172
|
|
|
/** @var mixed $expectedResult */ |
173
|
|
|
$expectedResult = 'bar'; |
|
|
|
|
174
|
|
|
|
175
|
|
|
$this->innerMapping->expects($this->never())->method('resolveValue'); |
176
|
|
|
|
177
|
|
|
$this->assertNull($this->mapping->resolveValue( |
178
|
|
|
$context, |
179
|
|
|
$dataFromAdditionalColumns |
180
|
|
|
)); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @test |
185
|
|
|
* @dataProvider dataProviderForShouldRevertValue |
186
|
|
|
*/ |
187
|
|
|
public function shouldRevertNullableValue( |
188
|
|
|
$expectedResult, |
189
|
|
|
array $revertedData, |
190
|
|
|
string $columnName, |
191
|
|
|
$valueFromEntityField, |
192
|
|
|
InvokedCount $innerRevertCount |
193
|
|
|
) { |
194
|
|
|
$this->dbalColumn->method('getName')->willReturn($columnName); |
195
|
|
|
|
196
|
|
|
/** @var HydrationContextInterface $context */ |
197
|
|
|
$context = $this->createMock(HydrationContextInterface::class); |
198
|
|
|
|
199
|
|
|
$this->innerMapping->expects($innerRevertCount)->method('revertValue')->with( |
200
|
|
|
$this->equalTo($context), |
201
|
|
|
$valueFromEntityField |
202
|
|
|
)->willReturn($revertedData); |
203
|
|
|
|
204
|
|
|
/** @var mixed $actualResult */ |
205
|
|
|
$actualResult = $this->mapping->revertValue( |
206
|
|
|
$context, |
207
|
|
|
$valueFromEntityField |
208
|
|
|
); |
209
|
|
|
|
210
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function dataProviderForShouldRevertValue() |
214
|
|
|
{ |
215
|
|
|
return array( |
216
|
|
|
[ |
217
|
|
|
[ |
218
|
|
|
'some_column' => true |
219
|
|
|
], |
220
|
|
|
[], |
221
|
|
|
"some_column", |
222
|
|
|
"foo", |
223
|
|
|
$this->once() |
224
|
|
|
], |
225
|
|
|
[ |
226
|
|
|
[ |
227
|
|
|
'some_column' => '0' |
228
|
|
|
], |
229
|
|
|
[], |
230
|
|
|
"some_column", |
231
|
|
|
null, |
232
|
|
|
$this->never() |
233
|
|
|
], |
234
|
|
|
[ |
235
|
|
|
[ |
236
|
|
|
'some_column' => 123 |
237
|
|
|
], |
238
|
|
|
[ |
239
|
|
|
'some_column' => 123 |
240
|
|
|
], |
241
|
|
|
"some_column", |
242
|
|
|
"foo", |
243
|
|
|
$this->once() |
244
|
|
|
], |
245
|
|
|
[ |
246
|
|
|
[], |
247
|
|
|
[], |
248
|
|
|
"", |
249
|
|
|
"foo", |
250
|
|
|
$this->once() |
251
|
|
|
], |
252
|
|
|
); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @test |
257
|
|
|
*/ |
258
|
|
|
public function shouldAssertValue() |
259
|
|
|
{ |
260
|
|
|
$this->assertNull($this->mapping->assertValue( |
|
|
|
|
261
|
|
|
$this->createMock(HydrationContextInterface::class), |
262
|
|
|
[], |
263
|
|
|
null |
264
|
|
|
)); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @test |
269
|
|
|
*/ |
270
|
|
|
public function shouldWakeUpInnerMapping() |
271
|
|
|
{ |
272
|
|
|
/** @var ContainerInterface $container */ |
273
|
|
|
$container = $this->createMock(ContainerInterface::class); |
274
|
|
|
|
275
|
|
|
$this->innerMapping->expects($this->once())->method("wakeUpMapping")->with( |
276
|
|
|
$this->equalTo($container) |
277
|
|
|
); |
278
|
|
|
|
279
|
|
|
$this->mapping->wakeUpMapping($container); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @test |
284
|
|
|
*/ |
285
|
|
|
public function shouldChooseFirstColumnWhenMultipleDefined() |
286
|
|
|
{ |
287
|
|
|
/** @var Column $expectedColumn */ |
288
|
|
|
$expectedColumn = $this->createMock(Column::class); |
289
|
|
|
$expectedColumn->method('getName')->willReturn("expected_column"); |
290
|
|
|
|
291
|
|
|
$this->dbalColumn->method('getName')->willReturn("some_column"); |
292
|
|
|
|
293
|
|
|
/** @var HydrationContextInterface $context */ |
294
|
|
|
$context = $this->createMock(HydrationContextInterface::class); |
295
|
|
|
|
296
|
|
|
$this->innerMapping->method('collectDBALColumns')->willReturn([ |
297
|
|
|
$expectedColumn, |
298
|
|
|
$this->dbalColumn |
299
|
|
|
]); |
300
|
|
|
|
301
|
|
|
$this->innerMapping->method('resolveValue')->willReturn("expected_result"); |
302
|
|
|
|
303
|
|
|
$mapping = new NullableMapping($this->innerMapping, null, "some origin"); |
304
|
|
|
|
305
|
|
|
/** @var mixed $actualResult */ |
306
|
|
|
$actualResult = $mapping->resolveValue($context, [ |
307
|
|
|
"expected_column" => 'expected_value', |
308
|
|
|
]); |
309
|
|
|
|
310
|
|
|
$this->assertEquals('expected_result', $actualResult); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
} |
314
|
|
|
|
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..