These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Port\Tests\Reader; |
||
4 | |||
5 | use Port\Reader\ArrayReader; |
||
6 | use Port\Reader\OneToManyReader; |
||
7 | |||
8 | /** |
||
9 | * Class OneToManyReaderTest |
||
10 | * @package Port\Tests\Reader |
||
11 | * @author Aydin Hassan <[email protected]> |
||
12 | */ |
||
13 | class OneToManyReaderTest extends \PHPUnit_Framework_TestCase |
||
14 | { |
||
15 | View Code Duplication | 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 | View Code Duplication | 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('Port\Exception\ReaderException', 'Left Row: "0" Reader already contains a field named "items". Please choose a different nest key field'); |
||
0 ignored issues
–
show
|
|||
167 | |||
168 | $oneToManyReader->rewind(); |
||
169 | $oneToManyReader->current(); |
||
170 | } |
||
171 | |||
172 | View Code Duplication | 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('Port\Exception\ReaderException', 'Row: "0" has no field named "OrderId"'); |
||
0 ignored issues
–
show
The method
PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead
This method has been deprecated. The supplier of the class has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.
Loading history...
|
|||
191 | |||
192 | $oneToManyReader->rewind(); |
||
193 | $oneToManyReader->current(); |
||
194 | } |
||
195 | |||
196 | View Code Duplication | 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('Port\Exception\ReaderException', 'Row: "0" has no field named "OrderId"'); |
||
0 ignored issues
–
show
The method
PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead
This method has been deprecated. The supplier of the class has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.
Loading history...
|
|||
216 | |||
217 | $oneToManyReader->rewind(); |
||
218 | $oneToManyReader->current(); |
||
219 | } |
||
220 | |||
221 | public function testCountReturnsTheCountOfTheLeftReader() |
||
222 | { |
||
223 | $leftReader = new ArrayReader(array()); |
||
224 | $rightReader = new ArrayReader(array()); |
||
225 | |||
226 | $oneToManyReader = new OneToManyReader($leftReader, $rightReader, 'items', 'OrderId'); |
||
227 | $this->assertEquals(0, $oneToManyReader->count()); |
||
228 | |||
229 | $leftReader = new ArrayReader(array(array(), array(), array())); |
||
230 | $oneToManyReader = new OneToManyReader($leftReader, $rightReader, 'items', 'OrderId'); |
||
231 | $this->assertEquals(3, $oneToManyReader->count()); |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * This is probably a limitation - but it's not need for current implementation |
||
236 | * @dataProvider outOfOrderRowProvider |
||
237 | */ |
||
238 | public function testOutOfOrderRowsInRightReaderAreNotNested($leftData, $rightData, $expected) |
||
239 | { |
||
240 | //var_dump($leftData); |
||
241 | //var_dump($rightData); |
||
242 | $leftReader = new ArrayReader($leftData); |
||
243 | $rightReader = new ArrayReader($rightData); |
||
244 | $oneToManyReader = new OneToManyReader($leftReader, $rightReader, 'items', 'OrderId'); |
||
245 | |||
246 | $i = 0; |
||
247 | foreach($oneToManyReader as $row) { |
||
248 | $this->assertEquals($row, $expected[$i++]); |
||
249 | } |
||
250 | } |
||
251 | |||
252 | public function outOfOrderRowProvider() |
||
253 | { |
||
254 | return array( |
||
255 | 'skip-first-right-row' => array( |
||
256 | 'left' => array( |
||
257 | array( |
||
258 | 'OrderId' => 3, |
||
259 | 'Price' => 30, |
||
260 | ), |
||
261 | array( |
||
262 | 'OrderId' => 2, |
||
263 | 'Price' => 15, |
||
264 | ), |
||
265 | ), |
||
266 | 'right' => array( |
||
267 | array( |
||
268 | 'OrderId' => 2, |
||
269 | 'Name' => 'Super Cool Item 1', |
||
270 | ), |
||
271 | array( |
||
272 | 'OrderId' => 1, |
||
273 | 'Name' => 'Super Cool Item 1', |
||
274 | ), |
||
275 | array( |
||
276 | 'OrderId' => 1, |
||
277 | 'Name' => 'Super Cool Item 2', |
||
278 | ), |
||
279 | ), |
||
280 | 'expected' => array( |
||
281 | array( |
||
282 | 'OrderId' => 3, |
||
283 | 'Price' => 30, |
||
284 | 'items' => array() |
||
285 | ), |
||
286 | array( |
||
287 | 'OrderId' => 2, |
||
288 | 'Price' => 15, |
||
289 | 'items' => array( |
||
290 | array( |
||
291 | 'OrderId' => 2, |
||
292 | 'Name' => 'Super Cool Item 1', |
||
293 | ), |
||
294 | ) |
||
295 | ), |
||
296 | ), |
||
297 | ), |
||
298 | 'skip-out-of-order' => array( |
||
299 | 'left' => array( |
||
300 | array( |
||
301 | 'OrderId' => 1, |
||
302 | 'Price' => 30, |
||
303 | ), |
||
304 | array( |
||
305 | 'OrderId' => 2, |
||
306 | 'Price' => 15, |
||
307 | ), |
||
308 | ), |
||
309 | 'right' => array( |
||
310 | array( |
||
311 | 'OrderId' => 0, |
||
312 | 'Name' => 'Super Cool Item 1', |
||
313 | ), |
||
314 | array( |
||
315 | 'OrderId' => 2, |
||
316 | 'Name' => 'Super Cool Item 2', |
||
317 | ), |
||
318 | array( |
||
319 | 'OrderId' => 1, |
||
320 | 'Name' => 'Super Cool Item 3', |
||
321 | ), |
||
322 | ), |
||
323 | 'expected' => array( |
||
324 | array( |
||
325 | 'OrderId' => 1, |
||
326 | 'Price' => 30, |
||
327 | 'items' => array() |
||
328 | ), |
||
329 | array( |
||
330 | 'OrderId' => 2, |
||
331 | 'Price' => 15, |
||
332 | 'items' => array(), |
||
333 | ), |
||
334 | ), |
||
335 | ), |
||
336 | ); |
||
337 | } |
||
338 | } |
||
339 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.