2323use ArrayIterator ;
2424use PHPUnit \Framework \TestCase ;
2525use Pipeline \Standard ;
26+ use PHPUnit \Framework \Attributes \DataProvider ;
2627
2728use function iterator_to_array ;
2829use function Pipeline \fromValues ;
3334 *
3435 * @internal
3536 */
36- final class FilterTest extends TestCase
37+ final class SelectTest extends TestCase
3738{
3839 private const NON_STRICT_FALSE_VALUES = [
3940 0 ,
@@ -46,20 +47,20 @@ final class FilterTest extends TestCase
4647 public function testStandardStringFunctions (): void
4748 {
4849 $ pipeline = new Standard (new ArrayIterator ([1 , 2 , 'foo ' , 'bar ' ]));
49- $ pipeline ->filter ('is_int ' );
50+ $ pipeline ->select ('is_int ' );
5051
5152 $ this ->assertSame ([1 , 2 ], iterator_to_array ($ pipeline ));
5253 }
5354
5455 public function testStandardFunctions (): void
5556 {
5657 $ pipeline = new Standard (new ArrayIterator ([1 , 2 , 'foo ' , 'bar ' ]));
57- $ pipeline ->filter (is_int (...));
58+ $ pipeline ->select (is_int (...));
5859
5960 $ this ->assertSame ([1 , 2 ], iterator_to_array ($ pipeline ));
6061 }
6162
62- public function testFilterAnyFalseValueDefaultCallback (): void
63+ public function testSelectAnyFalseValueDefaultCallback (): void
6364 {
6465 $ pipeline = map (function () {
6566 yield false ;
@@ -71,12 +72,12 @@ public function testFilterAnyFalseValueDefaultCallback(): void
7172 yield null ;
7273 });
7374
74- $ pipeline ->filter ( );
75+ $ pipeline ->select (strict: false );
7576
7677 $ this ->assertCount (0 , $ pipeline ->toList ());
7778 }
7879
79- public function testFilterAnyFalseValueDefaultCallbackStrict (): void
80+ public function testSelectAnyFalseValueDefaultCallbackStrict (): void
8081 {
8182 $ pipeline = map (function () {
8283 yield false ;
@@ -88,12 +89,12 @@ public function testFilterAnyFalseValueDefaultCallbackStrict(): void
8889 yield null ;
8990 });
9091
91- $ pipeline ->filter (strict: true );
92+ $ pipeline ->select ( );
9293
9394 $ this ->assertCount (5 , $ pipeline ->toList ());
9495 }
9596
96- public function testFilterAnyFalseValueCustomCallback (): void
97+ public function testSelectAnyFalseValueCustomCallback (): void
9798 {
9899 $ pipeline = map (function () {
99100 yield false ;
@@ -106,12 +107,12 @@ public function testFilterAnyFalseValueCustomCallback(): void
106107 yield 1 ;
107108 });
108109
109- $ pipeline ->filter ('intval ' , strict: false );
110+ $ pipeline ->select ('intval ' , strict: false );
110111
111112 $ this ->assertSame ([1 ], $ pipeline ->toList ());
112113 }
113114
114- public function testFilterStrictMode (): void
115+ public function testSelectStrictMode (): void
115116 {
116117 $ pipeline = map (function () {
117118 yield false ;
@@ -120,12 +121,12 @@ public function testFilterStrictMode(): void
120121 yield from self ::NON_STRICT_FALSE_VALUES ;
121122 });
122123
123- $ pipeline ->filter (strict: true );
124+ $ pipeline ->select (strict: true );
124125
125126 $ this ->assertSame (self ::NON_STRICT_FALSE_VALUES , $ pipeline ->toList ());
126127 }
127128
128- public function testFilterStrictModeWithPredicate (): void
129+ public function testSelectStrictModeWithPredicate (): void
129130 {
130131 $ pipeline = map (function () {
131132 yield false ;
@@ -134,23 +135,62 @@ public function testFilterStrictModeWithPredicate(): void
134135 yield from self ::NON_STRICT_FALSE_VALUES ;
135136 });
136137
137- $ pipeline ->filter (fn ($ value ) => $ value , strict: true );
138+ $ pipeline ->select (fn ($ value ) => $ value , strict: true );
138139
139140 $ this ->assertSame (self ::NON_STRICT_FALSE_VALUES , $ pipeline ->toList ());
140141 }
141142
142- public function testFilterNonStrictMode (): void
143+ public function testSelectNonStrictMode (): void
143144 {
144145 $ pipeline = fromValues (false , null , '' );
145- $ pipeline ->filter (strict: false );
146+ $ pipeline ->select (strict: false );
146147 $ this ->assertCount (0 , $ pipeline );
147148 }
148149
149- public function testFilterUnprimed (): void
150+ public function testSelectUnprimed (): void
150151 {
151152 $ pipeline = new Standard ();
152- $ pipeline ->filter ()->unpack ();
153+ $ pipeline ->select ()->unpack ();
153154
154155 $ this ->assertSame ([], $ pipeline ->toList ());
155156 }
157+
158+ public function testFilterIsNotStrictByDefault (): void
159+ {
160+ $ pipeline = $ this ->getMockBuilder (Standard::class)
161+ ->setConstructorArgs ([[1 ]])
162+ ->onlyMethods (['select ' ])
163+ ->getMock ();
164+
165+ $ pipeline ->expects ($ this ->once ())
166+ ->method ('select ' )
167+ ->with (null , false )
168+ ->willReturn ($ pipeline );
169+
170+ $ pipeline ->filter ();
171+ }
172+
173+ public static function provideFilterIsEquivalentToSelect (): iterable
174+ {
175+ yield [null , false ];
176+ yield [null , true ];
177+ yield [fn ($ value ) => true , false ];
178+ yield [fn ($ value ) => true , true ];
179+ }
180+
181+ #[DataProvider('provideFilterIsEquivalentToSelect ' )]
182+ public function testFilterIsEquivalentToSelect (?callable $ func , bool $ strict ): void
183+ {
184+ $ pipeline = $ this ->getMockBuilder (Standard::class)
185+ ->setConstructorArgs ([[1 ]])
186+ ->onlyMethods (['select ' ])
187+ ->getMock ();
188+
189+ $ pipeline ->expects ($ this ->once ())
190+ ->method ('select ' )
191+ ->with ($ func , $ strict )
192+ ->willReturn ($ pipeline );
193+
194+ $ pipeline ->filter ($ func , $ strict );
195+ }
156196}
0 commit comments