@@ -143,6 +143,39 @@ public function testApply(array $source, callable $viewGetter, callable $mapper,
143143 $ this ->assertSame ($ expected , $ source );
144144 }
145145
146+ /**
147+ * @dataProvider dataProviderForApplyWith
148+ */
149+ public function testApplyWith (array $ source , callable $ viewGetter , callable $ mapper , array $ arg , array $ expected )
150+ {
151+ // Given
152+ $ view = $ viewGetter ($ source );
153+
154+ // When
155+ $ view ->applyWith ($ arg , $ mapper );
156+
157+ // Then
158+ $ this ->assertSame ($ expected , $ source );
159+ }
160+
161+ /**
162+ * @dataProvider dataProviderForIsAndFilter
163+ */
164+ public function testIsAndFilter (array $ source , callable $ predicate , array $ expectedMask , array $ expectedArray )
165+ {
166+ // Given
167+ $ view = ArrayView::toView ($ source );
168+
169+ // When
170+ $ boolMask = $ view ->is ($ predicate );
171+ $ filtered = $ view ->filter ($ predicate );
172+
173+ // Then
174+ $ this ->assertSame ($ expectedMask , $ boolMask ->getValue ());
175+ $ this ->assertSame ($ expectedArray , $ view ->subview ($ boolMask )->toArray ());
176+ $ this ->assertSame ($ expectedArray , $ filtered ->toArray ());
177+ }
178+
146179 public function dataProviderForArrayWrite (): array
147180 {
148181 return [
@@ -326,4 +359,75 @@ public function dataProviderForApply(): array
326359 ],
327360 ];
328361 }
362+
363+ public function dataProviderForApplyWith (): array
364+ {
365+ return [
366+ [
367+ [],
368+ fn (array &$ source ) => ArrayView::toView ($ source ),
369+ fn (int $ lhs , int $ rhs ) => $ lhs + $ rhs ,
370+ [],
371+ [],
372+ ],
373+ [
374+ [1 ],
375+ fn (array &$ source ) => ArrayView::toView ($ source ),
376+ fn (int $ lhs , int $ rhs ) => $ lhs + $ rhs ,
377+ [2 ],
378+ [3 ],
379+ ],
380+ [
381+ [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ],
382+ fn (array &$ source ) => ArrayView::toView ($ source ),
383+ fn (int $ lhs , int $ rhs ) => $ lhs + $ rhs ,
384+ [10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 , 100 ],
385+ [11 , 22 , 33 , 44 , 55 , 66 , 77 , 88 , 99 , 110 ],
386+ ],
387+ [
388+ [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ],
389+ fn (array &$ source ) => ArrayView::toView ($ source ),
390+ fn (int $ lhs , int $ rhs , int $ index ) => $ index % 2 === 0 ? $ lhs : $ rhs ,
391+ [10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 , 100 ],
392+ [1 , 20 , 3 , 40 , 5 , 60 , 7 , 80 , 9 , 100 ],
393+ ],
394+ [
395+ [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ],
396+ fn (array &$ source ) => ArrayView::toView ($ source )->subview ('::2 ' ),
397+ fn (int $ lhs , int $ rhs ) => $ lhs * $ rhs ,
398+ [1 , 2 , 3 , 4 , 5 ],
399+ [1 , 2 , 6 , 4 , 15 , 6 , 28 , 8 , 45 , 10 ],
400+ ],
401+ ];
402+ }
403+
404+ public function dataProviderForIsAndFilter (): array
405+ {
406+ return [
407+ [
408+ [],
409+ fn (int $ x ) => $ x % 2 === 0 ,
410+ [],
411+ [],
412+ ],
413+ [
414+ [1 ],
415+ fn (int $ x ) => $ x % 2 === 0 ,
416+ [false ],
417+ [],
418+ ],
419+ [
420+ [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ],
421+ fn (int $ x ) => $ x % 2 === 0 ,
422+ [false , true , false , true , false , true , false , true , false , true ],
423+ [2 , 4 , 6 , 8 , 10 ],
424+ ],
425+ [
426+ [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ],
427+ fn (int $ _ , int $ i ) => $ i % 2 === 0 ,
428+ [true , false , true , false , true , false , true , false , true , false ],
429+ [1 , 3 , 5 , 7 , 9 ],
430+ ],
431+ ];
432+ }
329433}
0 commit comments