1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ddeboer\DataImport\Tests\Reader; |
4
|
|
|
|
5
|
|
|
use Ddeboer\DataImport\Reader\ArrayReader; |
6
|
|
|
use Ddeboer\DataImport\Reader\OneToManyReader; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class OneToManyReaderTest |
10
|
|
|
* @package Ddeboer\DataImport\Tests\Reader |
11
|
|
|
* @author Aydin Hassan <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class OneToManyReaderTest extends \PHPUnit_Framework_TestCase |
14
|
|
|
{ |
15
|
|
|
public function testReaderMergesOneToMany() |
16
|
|
|
{ |
17
|
|
|
$leftData = array( |
18
|
|
|
array( |
19
|
|
|
'OrderId' => 1, |
20
|
|
|
'Price' => 30, |
21
|
|
|
), |
22
|
|
|
array( |
23
|
|
|
'OrderId' => 2, |
24
|
|
|
'Price' => 15, |
25
|
|
|
), |
26
|
|
|
); |
27
|
|
|
|
28
|
|
|
$rightData = array( |
29
|
|
|
array( |
30
|
|
|
'OrderId' => 1, |
31
|
|
|
'Name' => 'Super Cool Item 1', |
32
|
|
|
), |
33
|
|
|
array( |
34
|
|
|
'OrderId' => 1, |
35
|
|
|
'Name' => 'Super Cool Item 2', |
36
|
|
|
), |
37
|
|
|
array( |
38
|
|
|
'OrderId' => 2, |
39
|
|
|
'Name' => 'Super Cool Item 1', |
40
|
|
|
), |
41
|
|
|
); |
42
|
|
|
|
43
|
|
|
$leftReader = new ArrayReader($leftData); |
44
|
|
|
$rightReader = new ArrayReader($rightData); |
45
|
|
|
$oneToManyReader = new OneToManyReader($leftReader, $rightReader, 'items', 'OrderId', 'OrderId'); |
46
|
|
|
|
47
|
|
|
$expected = array( |
48
|
|
|
array( |
49
|
|
|
'OrderId' => 1, |
50
|
|
|
'Price' => 30, |
51
|
|
|
'items' => array( |
52
|
|
|
array( |
53
|
|
|
'OrderId' => 1, |
54
|
|
|
'Name' => 'Super Cool Item 1', |
55
|
|
|
), |
56
|
|
|
array( |
57
|
|
|
'OrderId' => 1, |
58
|
|
|
'Name' => 'Super Cool Item 2', |
59
|
|
|
), |
60
|
|
|
), |
61
|
|
|
), |
62
|
|
|
array( |
63
|
|
|
'OrderId' => 2, |
64
|
|
|
'Price' => 15, |
65
|
|
|
'items' => array( |
66
|
|
|
array( |
67
|
|
|
'OrderId' => 2, |
68
|
|
|
'Name' => 'Super Cool Item 1', |
69
|
|
|
), |
70
|
|
|
) |
71
|
|
|
), |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
$i = 0; |
75
|
|
|
foreach($oneToManyReader as $row) { |
76
|
|
|
$this->assertEquals($row, $expected[$i++]); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testIfRightReaderIdFieldIsMissingLeftIsUsed() |
81
|
|
|
{ |
82
|
|
|
$leftData = array( |
83
|
|
|
array( |
84
|
|
|
'OrderId' => 1, |
85
|
|
|
'Price' => 30, |
86
|
|
|
), |
87
|
|
|
array( |
88
|
|
|
'OrderId' => 2, |
89
|
|
|
'Price' => 15, |
90
|
|
|
), |
91
|
|
|
); |
92
|
|
|
|
93
|
|
|
$rightData = array( |
94
|
|
|
array( |
95
|
|
|
'OrderId' => 1, |
96
|
|
|
'Name' => 'Super Cool Item 1', |
97
|
|
|
), |
98
|
|
|
array( |
99
|
|
|
'OrderId' => 1, |
100
|
|
|
'Name' => 'Super Cool Item 2', |
101
|
|
|
), |
102
|
|
|
array( |
103
|
|
|
'OrderId' => 2, |
104
|
|
|
'Name' => 'Super Cool Item 1', |
105
|
|
|
), |
106
|
|
|
); |
107
|
|
|
|
108
|
|
|
$leftReader = new ArrayReader($leftData); |
109
|
|
|
$rightReader = new ArrayReader($rightData); |
110
|
|
|
$oneToManyReader = new OneToManyReader($leftReader, $rightReader, 'items', 'OrderId'); |
111
|
|
|
|
112
|
|
|
$expected = array( |
113
|
|
|
array( |
114
|
|
|
'OrderId' => 1, |
115
|
|
|
'Price' => 30, |
116
|
|
|
'items' => array( |
117
|
|
|
array( |
118
|
|
|
'OrderId' => 1, |
119
|
|
|
'Name' => 'Super Cool Item 1', |
120
|
|
|
), |
121
|
|
|
array( |
122
|
|
|
'OrderId' => 1, |
123
|
|
|
'Name' => 'Super Cool Item 2', |
124
|
|
|
), |
125
|
|
|
), |
126
|
|
|
), |
127
|
|
|
array( |
128
|
|
|
'OrderId' => 2, |
129
|
|
|
'Price' => 15, |
130
|
|
|
'items' => array( |
131
|
|
|
array( |
132
|
|
|
'OrderId' => 2, |
133
|
|
|
'Name' => 'Super Cool Item 1', |
134
|
|
|
), |
135
|
|
|
) |
136
|
|
|
), |
137
|
|
|
); |
138
|
|
|
|
139
|
|
|
$i = 0; |
140
|
|
|
foreach($oneToManyReader as $row) { |
141
|
|
|
$this->assertEquals($row, $expected[$i++]); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function testReaderThrowsExceptionIfNestKeyExistsInLeftReaderRow() |
146
|
|
|
{ |
147
|
|
|
$leftData = array( |
148
|
|
|
array( |
149
|
|
|
'OrderId' => 1, |
150
|
|
|
'Price' => 30, |
151
|
|
|
'items' => null, |
152
|
|
|
), |
153
|
|
|
); |
154
|
|
|
|
155
|
|
|
$rightData = array( |
156
|
|
|
array( |
157
|
|
|
'OrderId' => 1, |
158
|
|
|
'Name' => 'Super Cool Item 1', |
159
|
|
|
), |
160
|
|
|
); |
161
|
|
|
|
162
|
|
|
$leftReader = new ArrayReader($leftData); |
163
|
|
|
$rightReader = new ArrayReader($rightData); |
164
|
|
|
$oneToManyReader = new OneToManyReader($leftReader, $rightReader, 'items', 'OrderId'); |
165
|
|
|
|
166
|
|
|
$this->setExpectedException('Ddeboer\DataImport\Exception\ReaderException', 'Left Row: "0" Reader already contains a field named "items". Please choose a different nest key field'); |
167
|
|
|
|
168
|
|
|
$oneToManyReader->rewind(); |
169
|
|
|
$oneToManyReader->current(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function testReaderThrowsExceptionIfIdFieldDoesNotExistInLeftRow() |
173
|
|
|
{ |
174
|
|
|
$leftData = array( |
175
|
|
|
array( |
176
|
|
|
'Price' => 30, |
177
|
|
|
), |
178
|
|
|
); |
179
|
|
|
|
180
|
|
|
$rightData = array( |
181
|
|
|
array( |
182
|
|
|
'Name' => 'Super Cool Item 1', |
183
|
|
|
), |
184
|
|
|
); |
185
|
|
|
|
186
|
|
|
$leftReader = new ArrayReader($leftData); |
187
|
|
|
$rightReader = new ArrayReader($rightData); |
188
|
|
|
$oneToManyReader = new OneToManyReader($leftReader, $rightReader, 'items', 'OrderId'); |
189
|
|
|
|
190
|
|
|
$this->setExpectedException('Ddeboer\DataImport\Exception\ReaderException', 'Row: "0" has no field named "OrderId"'); |
191
|
|
|
|
192
|
|
|
$oneToManyReader->rewind(); |
193
|
|
|
$oneToManyReader->current(); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function testReaderThrowsExceptionIfIdFieldDoesNotExistInRightRow() |
197
|
|
|
{ |
198
|
|
|
$leftData = array( |
199
|
|
|
array( |
200
|
|
|
'OrderId' => 1, |
201
|
|
|
'Price' => 30, |
202
|
|
|
), |
203
|
|
|
); |
204
|
|
|
|
205
|
|
|
$rightData = array( |
206
|
|
|
array( |
207
|
|
|
'Name' => 'Super Cool Item 1', |
208
|
|
|
), |
209
|
|
|
); |
210
|
|
|
|
211
|
|
|
$leftReader = new ArrayReader($leftData); |
212
|
|
|
$rightReader = new ArrayReader($rightData); |
213
|
|
|
$oneToManyReader = new OneToManyReader($leftReader, $rightReader, 'items', 'OrderId'); |
214
|
|
|
|
215
|
|
|
$this->setExpectedException('Ddeboer\DataImport\Exception\ReaderException', 'Row: "0" has no field named "OrderId"'); |
216
|
|
|
|
217
|
|
|
$oneToManyReader->rewind(); |
218
|
|
|
$oneToManyReader->current(); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
public function testGetKeysReturnsLeftReaderColumnsMergedWithNestKey() |
222
|
|
|
{ |
223
|
|
|
$leftReader = new ArrayReader(array(array('col1' => 'data1', 'col2' => 'data2'), array('data3', 'data4'))); |
224
|
|
|
$rightReader = new ArrayReader(array()); |
225
|
|
|
$oneToManyReader = new OneToManyReader($leftReader, $rightReader, 'items', 'OrderId'); |
226
|
|
|
|
227
|
|
|
$this->assertSame(array('col1', 'col2', 'items'), $oneToManyReader->getFields()); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
public function testCountReturnsTheCountOfTheLeftReader() |
231
|
|
|
{ |
232
|
|
|
$leftReader = new ArrayReader(array()); |
233
|
|
|
$rightReader = new ArrayReader(array()); |
234
|
|
|
|
235
|
|
|
$oneToManyReader = new OneToManyReader($leftReader, $rightReader, 'items', 'OrderId'); |
236
|
|
|
$this->assertEquals(0, $oneToManyReader->count()); |
237
|
|
|
|
238
|
|
|
$leftReader = new ArrayReader(array(array(), array(), array())); |
239
|
|
|
$oneToManyReader = new OneToManyReader($leftReader, $rightReader, 'items', 'OrderId'); |
240
|
|
|
$this->assertEquals(3, $oneToManyReader->count()); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* This is probably a limitation - but it's not need for current implementation |
245
|
|
|
* @dataProvider outOfOrderRowProvider |
246
|
|
|
*/ |
247
|
|
|
public function testOutOfOrderRowsInRightReaderAreNotNested($leftData, $rightData, $expected) |
248
|
|
|
{ |
249
|
|
|
//var_dump($leftData); |
250
|
|
|
//var_dump($rightData); |
251
|
|
|
$leftReader = new ArrayReader($leftData); |
252
|
|
|
$rightReader = new ArrayReader($rightData); |
253
|
|
|
$oneToManyReader = new OneToManyReader($leftReader, $rightReader, 'items', 'OrderId'); |
254
|
|
|
|
255
|
|
|
$i = 0; |
256
|
|
|
foreach($oneToManyReader as $row) { |
257
|
|
|
$this->assertEquals($row, $expected[$i++]); |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
public function outOfOrderRowProvider() |
262
|
|
|
{ |
263
|
|
|
return array( |
264
|
|
|
'skip-first-right-row' => array( |
265
|
|
|
'left' => array( |
266
|
|
|
array( |
267
|
|
|
'OrderId' => 3, |
268
|
|
|
'Price' => 30, |
269
|
|
|
), |
270
|
|
|
array( |
271
|
|
|
'OrderId' => 2, |
272
|
|
|
'Price' => 15, |
273
|
|
|
), |
274
|
|
|
), |
275
|
|
|
'right' => array( |
276
|
|
|
array( |
277
|
|
|
'OrderId' => 2, |
278
|
|
|
'Name' => 'Super Cool Item 1', |
279
|
|
|
), |
280
|
|
|
array( |
281
|
|
|
'OrderId' => 1, |
282
|
|
|
'Name' => 'Super Cool Item 1', |
283
|
|
|
), |
284
|
|
|
array( |
285
|
|
|
'OrderId' => 1, |
286
|
|
|
'Name' => 'Super Cool Item 2', |
287
|
|
|
), |
288
|
|
|
), |
289
|
|
|
'expected' => array( |
290
|
|
|
array( |
291
|
|
|
'OrderId' => 3, |
292
|
|
|
'Price' => 30, |
293
|
|
|
'items' => array() |
294
|
|
|
), |
295
|
|
|
array( |
296
|
|
|
'OrderId' => 2, |
297
|
|
|
'Price' => 15, |
298
|
|
|
'items' => array( |
299
|
|
|
array( |
300
|
|
|
'OrderId' => 2, |
301
|
|
|
'Name' => 'Super Cool Item 1', |
302
|
|
|
), |
303
|
|
|
) |
304
|
|
|
), |
305
|
|
|
), |
306
|
|
|
), |
307
|
|
|
'skip-out-of-order' => array( |
308
|
|
|
'left' => array( |
309
|
|
|
array( |
310
|
|
|
'OrderId' => 1, |
311
|
|
|
'Price' => 30, |
312
|
|
|
), |
313
|
|
|
array( |
314
|
|
|
'OrderId' => 2, |
315
|
|
|
'Price' => 15, |
316
|
|
|
), |
317
|
|
|
), |
318
|
|
|
'right' => array( |
319
|
|
|
array( |
320
|
|
|
'OrderId' => 0, |
321
|
|
|
'Name' => 'Super Cool Item 1', |
322
|
|
|
), |
323
|
|
|
array( |
324
|
|
|
'OrderId' => 2, |
325
|
|
|
'Name' => 'Super Cool Item 2', |
326
|
|
|
), |
327
|
|
|
array( |
328
|
|
|
'OrderId' => 1, |
329
|
|
|
'Name' => 'Super Cool Item 3', |
330
|
|
|
), |
331
|
|
|
), |
332
|
|
|
'expected' => array( |
333
|
|
|
array( |
334
|
|
|
'OrderId' => 1, |
335
|
|
|
'Price' => 30, |
336
|
|
|
'items' => array() |
337
|
|
|
), |
338
|
|
|
array( |
339
|
|
|
'OrderId' => 2, |
340
|
|
|
'Price' => 15, |
341
|
|
|
'items' => array(), |
342
|
|
|
), |
343
|
|
|
), |
344
|
|
|
), |
345
|
|
|
); |
346
|
|
|
} |
347
|
|
|
} |
348
|
|
|
|