1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Maketok\DataMigration\Input\Shaper\Processor; |
4
|
|
|
|
5
|
|
|
use Maketok\DataMigration\ArrayMap; |
6
|
|
|
use Maketok\DataMigration\Expression\LanguageAdapter; |
7
|
|
|
use Maketok\DataMigration\Unit\SimpleBag; |
8
|
|
|
use Maketok\DataMigration\Unit\Type\Unit; |
9
|
|
|
use Maketok\DataMigration\Unit\UnitBagInterface; |
10
|
|
|
use Symfony\Component\ExpressionLanguage\ExpressionLanguage; |
11
|
|
|
|
12
|
|
|
class NullsTest extends \PHPUnit_Framework_TestCase |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @param UnitBagInterface $bag |
16
|
|
|
* @return Nulls |
17
|
|
|
*/ |
18
|
|
|
public function getShaper(UnitBagInterface $bag) |
19
|
|
|
{ |
20
|
|
|
return new Nulls( |
21
|
|
|
$bag, |
22
|
|
|
new ArrayMap(), |
23
|
|
|
new LanguageAdapter(new ExpressionLanguage()) |
24
|
|
|
); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testOneLevelParse() |
28
|
|
|
{ |
29
|
|
|
$unit = new Unit('test'); |
30
|
|
|
$bag = new SimpleBag(); |
31
|
|
|
$bag->add($unit); |
32
|
|
|
|
33
|
|
|
$rows = [ |
34
|
|
|
['code' => 'test', 'id' => 1, 'name' => 'bar'], |
35
|
|
|
['code' => 'test2', 'id' => 11, 'name' => 'baz'], |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
$shaper = $this->getShaper($bag); |
39
|
|
|
|
40
|
|
|
foreach ($rows as $row) { |
41
|
|
|
$this->assertSame([$row], $shaper->parse($row)); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testTwoLevelParse() |
46
|
|
|
{ |
47
|
|
|
$unit1 = new Unit('customer'); |
48
|
|
|
$unit1->setIsEntityCondition(function ($map) { |
49
|
|
|
return !empty($map['email']); |
50
|
|
|
}); |
51
|
|
|
$unit2 = new Unit('address'); |
52
|
|
|
$unit2->setParent($unit1); |
53
|
|
|
|
54
|
|
|
$unit3 = new Unit('address_data'); |
55
|
|
|
$unit3->addSibling($unit2); |
56
|
|
|
|
57
|
|
|
$bag = new SimpleBag(); |
58
|
|
|
$bag->addSet([$unit1, $unit2, $unit3]); |
59
|
|
|
|
60
|
|
|
$entities = [ |
61
|
|
|
[ |
62
|
|
|
'email' => '[email protected]', |
63
|
|
|
'name' => 'bob', |
64
|
|
|
'address' => [ |
65
|
|
|
[ |
66
|
|
|
'street' => 'charity str.', |
67
|
|
|
], |
68
|
|
|
] |
69
|
|
|
], |
70
|
|
|
[ |
71
|
|
|
'email' => '[email protected]', |
72
|
|
|
'name' => 'paul', |
73
|
|
|
'address' => [ |
74
|
|
|
[ |
75
|
|
|
'street' => 'buckingham ave.', |
76
|
|
|
], |
77
|
|
|
[ |
78
|
|
|
'street' => 'mirabelle str.', |
79
|
|
|
], |
80
|
|
|
] |
81
|
|
|
], |
82
|
|
|
]; |
83
|
|
|
$expected = [ |
84
|
|
|
[ |
85
|
|
|
['email' => '[email protected]', 'name' => 'bob', 'street' => 'charity str.'], |
86
|
|
|
], |
87
|
|
|
[ |
88
|
|
|
['email' => '[email protected]', 'name' => 'paul', 'street' => 'buckingham ave.'], |
89
|
|
|
['email' => null, 'name' => null, 'street' => 'mirabelle str.'], |
90
|
|
|
] |
91
|
|
|
]; |
92
|
|
|
|
93
|
|
|
$shaper = $this->getShaper($bag); |
94
|
|
|
|
95
|
|
|
for ($i = 0; $i<count($entities); $i++) { |
|
|
|
|
96
|
|
|
$this->assertSame($expected[$i], $shaper->parse($entities[$i])); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function testTwoLevelParse2() |
101
|
|
|
{ |
102
|
|
|
$unit1 = new Unit('customer'); |
103
|
|
|
$unit1->setIsEntityCondition(function ($map) { |
104
|
|
|
return !empty($map['email']); |
105
|
|
|
}); |
106
|
|
|
$unit2 = new Unit('address'); |
107
|
|
|
$unit2->setParent($unit1); |
108
|
|
|
|
109
|
|
|
$unit3 = new Unit('address_data'); |
110
|
|
|
$unit3->addSibling($unit2); |
111
|
|
|
|
112
|
|
|
$bag = new SimpleBag(); |
113
|
|
|
$bag->addSet([$unit1, $unit2, $unit3]); |
114
|
|
|
|
115
|
|
|
$entities = [ |
116
|
|
|
[ |
117
|
|
|
'email' => '[email protected]', |
118
|
|
|
'name' => 'bob', |
119
|
|
|
'address' => [ |
120
|
|
|
[ |
121
|
|
|
'addr_name' => 'billy', |
122
|
|
|
'street' => 'charity str.', |
123
|
|
|
], |
124
|
|
|
] |
125
|
|
|
], |
126
|
|
|
[ |
127
|
|
|
'email' => '[email protected]', |
128
|
|
|
'name' => 'paul', |
129
|
|
|
'address' => [ |
130
|
|
|
[ |
131
|
|
|
'addr_name' => 'billy', |
132
|
|
|
'street' => 'buckingham ave.', |
133
|
|
|
], |
134
|
|
|
[ |
135
|
|
|
'addr_name' => 'paul', |
136
|
|
|
'street' => 'mirabelle str.', |
137
|
|
|
], |
138
|
|
|
[ |
139
|
|
|
'addr_name' => 'megan', |
140
|
|
|
'street' => 'mirabelle str.', |
141
|
|
|
], |
142
|
|
|
] |
143
|
|
|
], |
144
|
|
|
]; |
145
|
|
|
$expected = [ |
146
|
|
|
[ |
147
|
|
|
['email' => '[email protected]', 'name' => 'bob', 'addr_name' => 'billy', 'street' => 'charity str.'], |
148
|
|
|
], |
149
|
|
|
[ |
150
|
|
|
['email' => '[email protected]', 'name' => 'paul', 'addr_name' => 'billy', 'street' => 'buckingham ave.'], |
151
|
|
|
['email' => null, 'name' => null, 'addr_name' => 'paul', 'street' => 'mirabelle str.'], |
152
|
|
|
['email' => null, 'name' => null, 'addr_name' => 'megan', 'street' => 'mirabelle str.'], |
153
|
|
|
] |
154
|
|
|
]; |
155
|
|
|
|
156
|
|
|
$shaper = $this->getShaper($bag); |
157
|
|
|
|
158
|
|
|
for ($i = 0; $i<count($entities); $i++) { |
|
|
|
|
159
|
|
|
$this->assertSame($expected[$i], $shaper->parse($entities[$i])); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function testThreeLevelParse() |
164
|
|
|
{ |
165
|
|
|
$unit1 = new Unit('customer'); |
166
|
|
|
$unit1->setIsEntityCondition(function ($map) { |
167
|
|
|
return !empty($map['email']); |
168
|
|
|
}); |
169
|
|
|
$unit2 = new Unit('address'); |
170
|
|
|
$unit2->setParent($unit1); |
171
|
|
|
$unit2->setIsEntityCondition(function ($map, $oldmap) { |
172
|
|
|
return $map['street'] != $oldmap['street']; |
173
|
|
|
}); |
174
|
|
|
|
175
|
|
|
$unit3 = new Unit('address_data'); |
176
|
|
|
$unit3->setParent($unit2); |
177
|
|
|
|
178
|
|
|
$bag = new SimpleBag(); |
179
|
|
|
$bag->addSet([$unit1, $unit2, $unit3]); |
180
|
|
|
|
181
|
|
|
$entities = [ |
182
|
|
|
[ |
183
|
|
|
'email' => '[email protected]', |
184
|
|
|
'name' => 'bob', |
185
|
|
|
'address' => [ |
186
|
|
|
[ |
187
|
|
|
'street' => 'charity str.', |
188
|
|
|
'addr_name' => 'bob', |
189
|
|
|
'address_data' => [ |
190
|
|
|
[ |
191
|
|
|
'phone' => '123', |
192
|
|
|
'fax' => '244', |
193
|
|
|
], |
194
|
|
|
[ |
195
|
|
|
'phone' => '432', |
196
|
|
|
'fax' => '6766', |
197
|
|
|
] |
198
|
|
|
], |
199
|
|
|
], |
200
|
|
|
] |
201
|
|
|
], |
202
|
|
|
[ |
203
|
|
|
'email' => '[email protected]', |
204
|
|
|
'name' => 'paul', |
205
|
|
|
'address' => [ |
206
|
|
|
[ |
207
|
|
|
'street' => 'buckingham ave.', |
208
|
|
|
'addr_name' => 'collin', |
209
|
|
|
'address_data' => [ |
210
|
|
|
[ |
211
|
|
|
'phone' => '222', |
212
|
|
|
'fax' => '333', |
213
|
|
|
] |
214
|
|
|
], |
215
|
|
|
], |
216
|
|
|
[ |
217
|
|
|
'street' => 'mirabelle str.', |
218
|
|
|
'addr_name' => 'rich', |
219
|
|
|
'address_data' => [ |
220
|
|
|
[ |
221
|
|
|
'phone' => '323', |
222
|
|
|
'fax' => '24234', |
223
|
|
|
] |
224
|
|
|
], |
225
|
|
|
], |
226
|
|
|
] |
227
|
|
|
], |
228
|
|
|
]; |
229
|
|
|
$expected = [ |
230
|
|
|
[ |
231
|
|
|
['email' => '[email protected]', 'name' => 'bob', 'street' => 'charity str.', 'addr_name' => 'bob', 'phone' => '123', 'fax' => '244'], |
232
|
|
|
['email' => null, 'name' => null, 'street' => null, 'addr_name' => null, 'phone' => '432', 'fax' => '6766'], |
233
|
|
|
], |
234
|
|
|
[ |
235
|
|
|
['email' => '[email protected]', 'name' => 'paul', 'street' => 'buckingham ave.', 'addr_name' => 'collin', 'phone' => '222', 'fax' => '333'], |
236
|
|
|
['email' => null, 'name' => null, 'street' => 'mirabelle str.', 'addr_name' => 'rich', 'phone' => '323', 'fax' => '24234'], |
237
|
|
|
] |
238
|
|
|
]; |
239
|
|
|
|
240
|
|
|
$shaper = $this->getShaper($bag); |
241
|
|
|
|
242
|
|
|
for ($i = 0; $i<count($entities); $i++) { |
|
|
|
|
243
|
|
|
$this->assertSame($expected[$i], $shaper->parse($entities[$i])); |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: