55namespace Yokai \Batch \Tests \Job \Item \Reader ;
66
77use ArrayIterator ;
8- use Generator ;
98use PHPUnit \Framework \TestCase ;
109use Yokai \Batch \Job \Item \Reader \IndexWithReader ;
1110use Yokai \Batch \Job \Item \Reader \StaticIterableReader ;
1211use Yokai \Batch \JobExecution ;
1312
1413class IndexWithReaderTest extends TestCase
1514{
16- /**
17- * @dataProvider provider
18- */
19- public function test (IndexWithReader $ reader , array $ expected ): void
15+ public function testArrayKey (): void
16+ {
17+ $ john = ['name ' => 'John ' , 'location ' => 'Washington ' ];
18+ $ marie = ['name ' => 'Marie ' , 'location ' => 'London ' ];
19+ $ reader = IndexWithReader::withArrayKey (
20+ new StaticIterableReader ([$ john , $ marie ]),
21+ 'name '
22+ );
23+
24+ $ this ->doTest ($ reader , ['John ' => $ john , 'Marie ' => $ marie ]);
25+ }
26+
27+ public function testObjectProperty (): void
28+ {
29+ $ john = (object )['name ' => 'John ' , 'location ' => 'Washington ' ];
30+ $ marie = (object )['name ' => 'Marie ' , 'location ' => 'London ' ];
31+ $ reader = IndexWithReader::withProperty (
32+ new StaticIterableReader ([$ john , $ marie ]),
33+ 'name '
34+ );
35+
36+ $ this ->doTest ($ reader , ['John ' => $ john , 'Marie ' => $ marie ]);
37+ }
38+
39+ public function testObjectGetter (): void
40+ {
41+ $ three = new ArrayIterator ([1 , 2 , 3 ]);
42+ $ six = new ArrayIterator ([1 , 2 , 3 , 4 , 5 , 6 ]);
43+ $ reader = IndexWithReader::withGetter (
44+ new StaticIterableReader ([$ three , $ six ]),
45+ 'count '
46+ );
47+
48+ $ this ->doTest ($ reader , [3 => $ three , 6 => $ six ]);
49+ }
50+
51+ public function testArbitraryClosure (): void
52+ {
53+ $ reader = new IndexWithReader (
54+ new StaticIterableReader ([1 , 2 , 3 ]),
55+ fn (int $ value ) => $ value * $ value
56+ );
57+
58+ $ this ->doTest ($ reader , [1 => 1 , 4 => 2 , 9 => 3 ]);
59+ }
60+
61+ private function doTest (IndexWithReader $ reader , array $ expected ): void
2062 {
2163 $ reader ->setJobExecution (JobExecution::createRoot ('123456 ' , 'testing ' ));
2264 $ reader ->initialize ();
@@ -30,45 +72,4 @@ public function test(IndexWithReader $reader, array $expected): void
3072
3173 self ::assertSame ($ expected , $ actual );
3274 }
33-
34- public function provider (): Generator
35- {
36- $ john = ['name ' => 'John ' , 'location ' => 'Washington ' ];
37- $ marie = ['name ' => 'Marie ' , 'location ' => 'London ' ];
38- yield 'Index with array key ' => [
39- IndexWithReader::withArrayKey (
40- new StaticIterableReader ([$ john , $ marie ]),
41- 'name '
42- ),
43- ['John ' => $ john , 'Marie ' => $ marie ],
44- ];
45-
46- $ john = (object )$ john ;
47- $ marie = (object )$ marie ;
48- yield 'Index with object property ' => [
49- IndexWithReader::withProperty (
50- new StaticIterableReader ([$ john , $ marie ]),
51- 'name '
52- ),
53- ['John ' => $ john , 'Marie ' => $ marie ],
54- ];
55-
56- $ three = new ArrayIterator ([1 , 2 , 3 ]);
57- $ six = new ArrayIterator ([1 , 2 , 3 , 4 , 5 , 6 ]);
58- yield 'Index with object method ' => [
59- IndexWithReader::withGetter (
60- new StaticIterableReader ([$ three , $ six ]),
61- 'count '
62- ),
63- [3 => $ three , 6 => $ six ],
64- ];
65-
66- yield 'Index with arbitrary closure ' => [
67- new IndexWithReader (
68- new StaticIterableReader ([1 , 2 , 3 ]),
69- fn (int $ value ) => $ value * $ value
70- ),
71- [1 => 1 , 4 => 2 , 9 => 3 ],
72- ];
73- }
7475}
0 commit comments