@@ -12,7 +12,7 @@ class CollectionTest extends TestCase
1212 /**
1313 * @dataProvider uniqueDataProvider
1414 */
15- public function testUnique ($ input , $ expected ): void
15+ public function testUnique ($ input , $ expected )
1616 {
1717 $ service = new Collection (...$ input );
1818 $ this ->assertSame ($ expected , $ service ->unique ()->toArray ());
@@ -51,7 +51,7 @@ public function uniqueDataProvider(): iterable
5151 ];
5252 }
5353
54- public function testSortFlatMap (): void
54+ public function testSortFlatMap ()
5555 {
5656 $ dateCollection = new class (
5757 new \DateTimeImmutable ('now ' ),
@@ -94,7 +94,37 @@ public function __construct(\DateTimeInterface ...$items)
9494 $ this ->assertSame ($ expected , $ result ->toArray ());
9595 }
9696
97- public function testFirst (): void
97+ public function testFlatMapOnDeepAssociative ()
98+ {
99+ $ input = [
100+ ['foo ' => 'bar ' ],
101+ ['foo ' => 'baz ' ],
102+ ];
103+ $ expected = ['BAR ' , 'BAZ ' ];
104+
105+ $ result = Collection::from ($ input )
106+ ->flatMap (function (array $ values ): array {
107+ return array_map ('strtoupper ' , $ values );
108+ });
109+
110+ $ this ->assertSame ($ expected , $ result ->toArray ());
111+ }
112+
113+ public function testFlatten ()
114+ {
115+ $ input = [
116+ ['foo ' => 'bar ' ],
117+ ['foo ' => 'baz ' ],
118+ ];
119+ $ expected = ['bar ' , 'baz ' ];
120+
121+ $ collection = Collection::from ($ input );
122+ $ result = $ collection ->flatten ();
123+
124+ $ this ->assertSame ($ expected , $ result ->toArray ());
125+ }
126+
127+ public function testFirst ()
98128 {
99129 $ collection = Collection::from (range (1 , 10 ));
100130 $ sum = 0 ;
@@ -110,7 +140,7 @@ public function testFirst(): void
110140 $ this ->assertSame ($ emptyCollection ->first (), null );
111141 }
112142
113- public function testAdd (): void
143+ public function testAdd ()
114144 {
115145 $ collection = Collection::from (range (1 , 3 ));
116146 $ collection = $ collection ->add (5 );
@@ -121,11 +151,44 @@ public function testAdd(): void
121151 $ this ->assertSame ($ collection ->toArray (), [1 , 2 , 3 , 5 , 5 , 3 ]);
122152 }
123153
124- public function testFromAssociativeArray (): void
154+ public function testSlice ()
155+ {
156+ $ emptyCollection = new Collection ();
157+ $ collection = new Collection (...['a ' , 'b ' , 'c ' , 'd ' , 'e ' ]);
158+
159+ $ this ->assertSame ([], $ emptyCollection ->slice (null )->toArray ());
160+ $ this ->assertSame (['a ' , 'b ' , 'c ' , 'd ' , 'e ' ], $ collection ->slice (null )->toArray ());
161+
162+ $ this ->assertSame ([], $ emptyCollection ->slice (3 )->toArray ());
163+ $ this ->assertSame (['a ' , 'b ' , 'c ' ], $ collection ->slice (3 )->toArray ());
164+
165+ $ this ->assertSame ([], $ emptyCollection ->slice (null )->toArray ());
166+ $ this ->assertSame (['c ' , 'd ' , 'e ' ], $ collection ->slice (null , 2 )->toArray ());
167+
168+ $ this ->assertSame ([], $ emptyCollection ->slice (2 , 1 )->toArray ());
169+ $ this ->assertSame (['b ' , 'c ' ], $ collection ->slice (2 , 1 )->toArray ());
170+ }
171+
172+ public function testFromAssociativeArray ()
125173 {
126174 $ input = ['foo ' => 'bar ' , 'bar ' => 'baz ' ];
127175
128- $ service2 = Collection::from ($ input );
129- $ this ->assertSame (array_values ($ input ), $ service2 ->toArray ());
176+ $ collection1 = Collection::from ($ input );
177+ $ this ->assertSame (array_values ($ input ), $ collection1 ->toArray ());
178+ $ collection2 = new Collection (...$ input );
179+ $ this ->assertSame (array_values ($ input ), $ collection2 ->toArray ());
180+ }
181+
182+ public function testFromAssociativeIterable ()
183+ {
184+ $ inputIterable = function (): iterable {
185+ yield 'foo ' => 'bar ' ;
186+ yield 'bar ' => 'baz ' ;
187+ };
188+
189+ $ collection1 = Collection::from ($ inputIterable ());
190+ $ this ->assertSame (array_values (iterator_to_array ($ inputIterable ())), $ collection1 ->toArray ());
191+ $ collection2 = new Collection (...$ inputIterable ());
192+ $ this ->assertSame (array_values (iterator_to_array ($ inputIterable ())), $ collection2 ->toArray ());
130193 }
131194}
0 commit comments