|
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\ListMapping; |
|
15
|
|
|
use Doctrine\DBAL\Schema\Column; |
|
16
|
|
|
use Addiks\RDMBundle\Mapping\MappingInterface; |
|
17
|
|
|
use Addiks\RDMBundle\Hydration\HydrationContextInterface; |
|
18
|
|
|
use Addiks\RDMBundle\Exception\FailedRDMAssertionException; |
|
19
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
20
|
|
|
|
|
21
|
|
|
final class ListMappingTest extends TestCase |
|
22
|
|
|
{ |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var SubjectClass |
|
26
|
|
|
*/ |
|
27
|
|
|
private $mapping; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var Column |
|
31
|
|
|
*/ |
|
32
|
|
|
private $column; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var MappingInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
private $entryMapping; |
|
38
|
|
|
|
|
39
|
|
|
public function setUp() |
|
40
|
|
|
{ |
|
41
|
|
|
$this->column = $this->createMock(Column::class); |
|
|
|
|
|
|
42
|
|
|
$this->entryMapping = $this->createMock(MappingInterface::class); |
|
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
$this->mapping = new ListMapping($this->column, $this->entryMapping, "some origin"); |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @test |
|
49
|
|
|
*/ |
|
50
|
|
|
public function shouldHaveDBALColumn() |
|
51
|
|
|
{ |
|
52
|
|
|
$this->assertSame($this->column, $this->mapping->getDBALColumn()); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @test |
|
57
|
|
|
*/ |
|
58
|
|
|
public function shouldHaveEntryMapping() |
|
59
|
|
|
{ |
|
60
|
|
|
$this->assertSame($this->entryMapping, $this->mapping->getEntryMapping()); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @test |
|
65
|
|
|
*/ |
|
66
|
|
|
public function shouldHaveOrigin() |
|
67
|
|
|
{ |
|
68
|
|
|
$this->assertSame("some origin", $this->mapping->describeOrigin()); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @test |
|
73
|
|
|
*/ |
|
74
|
|
|
public function shouldCollectDBALColumns() |
|
75
|
|
|
{ |
|
76
|
|
|
$this->assertSame([$this->column], $this->mapping->collectDBALColumns()); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @test |
|
81
|
|
|
*/ |
|
82
|
|
|
public function shouldResolveListValue() |
|
83
|
|
|
{ |
|
84
|
|
|
/** @var HydrationContextInterface $context */ |
|
85
|
|
|
$context = $this->createMock(HydrationContextInterface::class); |
|
86
|
|
|
|
|
87
|
|
|
$this->column->method('getName')->willReturn('some_column'); |
|
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
/** @var array $dataFromAdditionalColumns */ |
|
90
|
|
|
$dataFromAdditionalColumns = [ |
|
91
|
|
|
'some_column' => '{"a":"LOREM","b":"IPSUM","c":"DOLOR","d":"SIT","e":"AMET"}', |
|
92
|
|
|
]; |
|
93
|
|
|
|
|
94
|
|
|
$this->entryMapping->method('resolveValue')->will($this->returnCallback( |
|
|
|
|
|
|
95
|
|
|
function ($context, $data) { |
|
96
|
|
|
return strtolower($data['']); |
|
97
|
|
|
} |
|
98
|
|
|
)); |
|
99
|
|
|
|
|
100
|
|
|
/** @var mixed $expectedResult */ |
|
101
|
|
|
$expectedResult = [ |
|
102
|
|
|
'a' => 'lorem', |
|
103
|
|
|
'b' => 'ipsum', |
|
104
|
|
|
'c' => 'dolor', |
|
105
|
|
|
'd' => 'sit', |
|
106
|
|
|
'e' => 'amet' |
|
107
|
|
|
]; |
|
108
|
|
|
|
|
109
|
|
|
/** @var mixed $actualResult */ |
|
110
|
|
|
$actualResult = $this->mapping->resolveValue( |
|
111
|
|
|
$context, |
|
112
|
|
|
$dataFromAdditionalColumns |
|
113
|
|
|
); |
|
114
|
|
|
|
|
115
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @test |
|
120
|
|
|
*/ |
|
121
|
|
|
public function shouldRevertListValue() |
|
122
|
|
|
{ |
|
123
|
|
|
/** @var HydrationContextInterface $context */ |
|
124
|
|
|
$context = $this->createMock(HydrationContextInterface::class); |
|
125
|
|
|
|
|
126
|
|
|
$this->column->method('getName')->willReturn('some_column'); |
|
|
|
|
|
|
127
|
|
|
|
|
128
|
|
|
$this->entryMapping->method('revertValue')->will($this->returnCallback( |
|
|
|
|
|
|
129
|
|
|
function ($context, $line) { |
|
130
|
|
|
return ['' => strtoupper($line)]; |
|
131
|
|
|
} |
|
132
|
|
|
)); |
|
133
|
|
|
|
|
134
|
|
|
/** @var mixed $valueFromEntityField */ |
|
135
|
|
|
$valueFromEntityField = [ |
|
136
|
|
|
'lorem', |
|
137
|
|
|
'ipsum', |
|
138
|
|
|
'dolor', |
|
139
|
|
|
'sit', |
|
140
|
|
|
'amet', |
|
141
|
|
|
]; |
|
142
|
|
|
|
|
143
|
|
|
/** @var mixed $expectedResult */ |
|
144
|
|
|
$expectedResult = [ |
|
145
|
|
|
'some_column' => '["LOREM","IPSUM","DOLOR","SIT","AMET"]', |
|
146
|
|
|
]; |
|
147
|
|
|
|
|
148
|
|
|
/** @var mixed $actualResult */ |
|
149
|
|
|
$actualResult = $this->mapping->revertValue( |
|
150
|
|
|
$context, |
|
151
|
|
|
$valueFromEntityField |
|
152
|
|
|
); |
|
153
|
|
|
|
|
154
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @test |
|
159
|
|
|
*/ |
|
160
|
|
|
public function shouldRevertMultidimensionalListValue() |
|
161
|
|
|
{ |
|
162
|
|
|
/** @var HydrationContextInterface $context */ |
|
163
|
|
|
$context = $this->createMock(HydrationContextInterface::class); |
|
164
|
|
|
|
|
165
|
|
|
$this->column->method('getName')->willReturn('some_column'); |
|
|
|
|
|
|
166
|
|
|
|
|
167
|
|
|
$this->entryMapping->method('revertValue')->will($this->returnCallback( |
|
|
|
|
|
|
168
|
|
|
function ($context, $data) { |
|
169
|
|
|
return $data; |
|
170
|
|
|
} |
|
171
|
|
|
)); |
|
172
|
|
|
|
|
173
|
|
|
/** @var mixed $valueFromEntityField */ |
|
174
|
|
|
$valueFromEntityField = [ |
|
175
|
|
|
['a' => 123, 'b' => true], |
|
176
|
|
|
['a' => 456, 'b' => false], |
|
177
|
|
|
[] |
|
178
|
|
|
]; |
|
179
|
|
|
|
|
180
|
|
|
/** @var mixed $expectedResult */ |
|
181
|
|
|
$expectedResult = [ |
|
182
|
|
|
'some_column' => '[{"a":123,"b":true},{"a":456,"b":false}]', |
|
183
|
|
|
]; |
|
184
|
|
|
|
|
185
|
|
|
/** @var mixed $actualResult */ |
|
186
|
|
|
$actualResult = $this->mapping->revertValue( |
|
187
|
|
|
$context, |
|
188
|
|
|
$valueFromEntityField |
|
189
|
|
|
); |
|
190
|
|
|
|
|
191
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @test |
|
196
|
|
|
*/ |
|
197
|
|
|
public function shouldAssertValue() |
|
198
|
|
|
{ |
|
199
|
|
|
$this->assertNull($this->mapping->assertValue( |
|
200
|
|
|
$this->createMock(HydrationContextInterface::class), |
|
201
|
|
|
[], |
|
202
|
|
|
null |
|
203
|
|
|
)); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* @test |
|
208
|
|
|
*/ |
|
209
|
|
|
public function shouldThrowExceptionOnFailedAssertion() |
|
210
|
|
|
{ |
|
211
|
|
|
$this->expectException(FailedRDMAssertionException::class); |
|
212
|
|
|
|
|
213
|
|
|
$this->mapping->assertValue( |
|
214
|
|
|
$this->createMock(HydrationContextInterface::class), |
|
215
|
|
|
[], |
|
216
|
|
|
"Lorem ipsum!" |
|
217
|
|
|
); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* @test |
|
222
|
|
|
*/ |
|
223
|
|
View Code Duplication |
public function shouldWakeUpInnerMapping() |
|
|
|
|
|
|
224
|
|
|
{ |
|
225
|
|
|
/** @var ContainerInterface $container */ |
|
226
|
|
|
$container = $this->createMock(ContainerInterface::class); |
|
227
|
|
|
|
|
228
|
|
|
$this->entryMapping->expects($this->once())->method("wakeUpMapping")->with( |
|
|
|
|
|
|
229
|
|
|
$this->equalTo($container) |
|
230
|
|
|
); |
|
231
|
|
|
|
|
232
|
|
|
$this->mapping->wakeUpMapping($container); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
} |
|
236
|
|
|
|
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..