|
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\RDMBundle\Tests\Mapping; |
|
12
|
|
|
|
|
13
|
|
|
use PHPUnit\Framework\TestCase; |
|
14
|
|
|
use Addiks\RDMBundle\Mapping\ArrayMapping; |
|
15
|
|
|
use Addiks\RDMBundle\Mapping\MappingInterface; |
|
16
|
|
|
use Doctrine\DBAL\Schema\Column; |
|
17
|
|
|
use Addiks\RDMBundle\Hydration\HydrationContextInterface; |
|
18
|
|
|
use Addiks\RDMBundle\Tests\Hydration\EntityExample; |
|
19
|
|
|
use Addiks\RDMBundle\Exception\FailedRDMAssertionExceptionInterface; |
|
20
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
21
|
|
|
|
|
22
|
|
|
final class ArrayMappingTest extends TestCase |
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var ArrayMapping |
|
27
|
|
|
*/ |
|
28
|
|
|
private $arrayMapping; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var MappingInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
private $mappingA; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var MappingInterface |
|
37
|
|
|
*/ |
|
38
|
|
|
private $mappingB; |
|
39
|
|
|
|
|
40
|
|
|
public function setUp(): void |
|
41
|
|
|
{ |
|
42
|
|
|
$this->mappingA = $this->createMock(MappingInterface::class); |
|
|
|
|
|
|
43
|
|
|
$this->mappingB = $this->createMock(MappingInterface::class); |
|
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
$this->arrayMapping = new ArrayMapping([ |
|
46
|
|
|
'foo' => $this->mappingA, |
|
47
|
|
|
'bar' => $this->mappingB, |
|
48
|
|
|
], "Some Origin"); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @test |
|
53
|
|
|
*/ |
|
54
|
|
|
public function shouldHaveOrigin() |
|
55
|
|
|
{ |
|
56
|
|
|
$this->assertEquals("Some Origin", $this->arrayMapping->describeOrigin()); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @test |
|
61
|
|
|
*/ |
|
62
|
|
|
public function shouldStoreEntryMappings() |
|
63
|
|
|
{ |
|
64
|
|
|
$this->assertEquals([ |
|
65
|
|
|
'foo' => $this->mappingA, |
|
66
|
|
|
'bar' => $this->mappingB, |
|
67
|
|
|
], $this->arrayMapping->getEntryMappings()); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @test |
|
72
|
|
|
*/ |
|
73
|
|
|
public function shouldCollectDBALColumns() |
|
74
|
|
|
{ |
|
75
|
|
|
/** @var Column $columnA */ |
|
76
|
|
|
$columnA = $this->createMock(Column::class); |
|
77
|
|
|
|
|
78
|
|
|
/** @var Column $columnB */ |
|
79
|
|
|
$columnB = $this->createMock(Column::class); |
|
80
|
|
|
|
|
81
|
|
|
$this->mappingA->method('collectDBALColumns')->willReturn([ |
|
|
|
|
|
|
82
|
|
|
'lorem' => $columnA, |
|
83
|
|
|
]); |
|
84
|
|
|
|
|
85
|
|
|
$this->mappingB->method('collectDBALColumns')->willReturn([ |
|
86
|
|
|
'ipsum' => $columnB, |
|
87
|
|
|
]); |
|
88
|
|
|
|
|
89
|
|
|
$this->assertEquals([ |
|
90
|
|
|
'lorem' => $columnA, |
|
91
|
|
|
'ipsum' => $columnB |
|
92
|
|
|
], $this->arrayMapping->collectDBALColumns()); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @test |
|
97
|
|
|
*/ |
|
98
|
|
|
public function shouldResolveValueToArray() |
|
99
|
|
|
{ |
|
100
|
|
|
/** @var HydrationContextInterface $context */ |
|
101
|
|
|
$context = $this->createMock(HydrationContextInterface::class); |
|
102
|
|
|
$context->method('getEntityClass')->willReturn(EntityExample::class); |
|
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
/** @var mixed $dataFromAdditionalColumns */ |
|
105
|
|
|
$dataFromAdditionalColumns = array( |
|
106
|
|
|
'lorem' => 'ipsum', |
|
107
|
|
|
'dolor' => 'sit', |
|
108
|
|
|
); |
|
109
|
|
|
|
|
110
|
|
|
/** @var array<mixed> $expectedResult */ |
|
111
|
|
|
$expectedResult = [ |
|
112
|
|
|
'foo' => 'bar', |
|
113
|
|
|
'bar' => 3.1415 |
|
114
|
|
|
]; |
|
115
|
|
|
|
|
116
|
|
|
$this->mappingA->expects($this->once())->method('resolveValue')->with( |
|
|
|
|
|
|
117
|
|
|
$this->equalTo($context), |
|
118
|
|
|
$this->equalTo($dataFromAdditionalColumns) |
|
119
|
|
|
)->willReturn('bar'); |
|
120
|
|
|
|
|
121
|
|
|
$this->mappingB->expects($this->once())->method('resolveValue')->with( |
|
122
|
|
|
$this->equalTo($context), |
|
123
|
|
|
$this->equalTo($dataFromAdditionalColumns) |
|
124
|
|
|
)->willReturn(3.1415); |
|
125
|
|
|
|
|
126
|
|
|
/** @var mixed $actualResult */ |
|
127
|
|
|
$actualResult = $this->arrayMapping->resolveValue($context, $dataFromAdditionalColumns); |
|
128
|
|
|
|
|
129
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @test |
|
134
|
|
|
*/ |
|
135
|
|
|
public function shouldRevertValueFromArray() |
|
136
|
|
|
{ |
|
137
|
|
|
/** @var HydrationContextInterface $context */ |
|
138
|
|
|
$context = $this->createMock(HydrationContextInterface::class); |
|
139
|
|
|
$context->method('getEntityClass')->willReturn(EntityExample::class); |
|
140
|
|
|
|
|
141
|
|
|
/** @var mixed $valueFromEntityField */ |
|
142
|
|
|
$valueFromEntityField = array( |
|
143
|
|
|
'foo' => 'bar', |
|
144
|
|
|
'bar' => 3.1415 |
|
145
|
|
|
); |
|
146
|
|
|
|
|
147
|
|
|
/** @var array<string, string> $expectedResult */ |
|
148
|
|
|
$expectedResult = array( |
|
149
|
|
|
'lorem' => 'ipsum', |
|
150
|
|
|
'dolor' => 'sit', |
|
151
|
|
|
); |
|
152
|
|
|
|
|
153
|
|
|
$this->mappingA->expects($this->once())->method('revertValue')->with( |
|
154
|
|
|
$this->equalTo($context), |
|
155
|
|
|
$this->equalTo('bar') |
|
156
|
|
|
)->willReturn(['lorem' => 'ipsum']); |
|
157
|
|
|
|
|
158
|
|
|
$this->mappingB->expects($this->once())->method('revertValue')->with( |
|
159
|
|
|
$this->equalTo($context), |
|
160
|
|
|
$this->equalTo(3.1415) |
|
161
|
|
|
)->willReturn(['dolor' => 'sit']); |
|
162
|
|
|
|
|
163
|
|
|
/** @var mixed $actualResult */ |
|
164
|
|
|
$actualResult = $this->arrayMapping->revertValue($context, $valueFromEntityField); |
|
165
|
|
|
|
|
166
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @test |
|
171
|
|
|
*/ |
|
172
|
|
|
public function shouldNotRevertForNonArray() |
|
173
|
|
|
{ |
|
174
|
|
|
/** @var HydrationContextInterface $context */ |
|
175
|
|
|
$context = $this->createMock(HydrationContextInterface::class); |
|
176
|
|
|
$context->method('getEntityClass')->willReturn(EntityExample::class); |
|
177
|
|
|
|
|
178
|
|
|
$this->mappingA->expects($this->never())->method('revertValue'); |
|
179
|
|
|
$this->mappingB->expects($this->never())->method('revertValue'); |
|
180
|
|
|
|
|
181
|
|
|
/** @var mixed $actualResult */ |
|
182
|
|
|
$actualResult = $this->arrayMapping->revertValue($context, "a non-array"); |
|
183
|
|
|
|
|
184
|
|
|
$this->assertEquals([], $actualResult); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* @test |
|
189
|
|
|
*/ |
|
190
|
|
|
public function shouldAssertValue() |
|
191
|
|
|
{ |
|
192
|
|
|
$this->expectException(FailedRDMAssertionExceptionInterface::class); |
|
193
|
|
|
|
|
194
|
|
|
/** @var HydrationContextInterface $context */ |
|
195
|
|
|
$context = $this->createMock(HydrationContextInterface::class); |
|
196
|
|
|
$context->method('getEntityClass')->willReturn(EntityExample::class); |
|
197
|
|
|
|
|
198
|
|
|
$this->arrayMapping->assertValue($context, [], "foo"); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* @test |
|
203
|
|
|
*/ |
|
204
|
|
|
public function shouldWakeUpInnerMapping() |
|
205
|
|
|
{ |
|
206
|
|
|
/** @var ContainerInterface $container */ |
|
207
|
|
|
$container = $this->createMock(ContainerInterface::class); |
|
208
|
|
|
|
|
209
|
|
|
$this->mappingA->expects($this->once())->method("wakeUpMapping")->with( |
|
210
|
|
|
$this->equalTo($container) |
|
211
|
|
|
); |
|
212
|
|
|
|
|
213
|
|
|
$this->mappingB->expects($this->once())->method("wakeUpMapping")->with( |
|
214
|
|
|
$this->equalTo($container) |
|
215
|
|
|
); |
|
216
|
|
|
|
|
217
|
|
|
$this->arrayMapping->wakeUpMapping($container); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
} |
|
221
|
|
|
|
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..