8
8
use Generator ;
9
9
use SplFixedArray ;
10
10
11
+ use function array_values ;
11
12
use function BenTools \IterableFunctions \iterable ;
13
+ use function func_num_args ;
12
14
use function it ;
13
15
use function iterator_to_array ;
14
16
use function PHPUnit \Framework \assertEquals ;
15
17
use function PHPUnit \Framework \assertInstanceOf ;
18
+ use function PHPUnit \Framework \assertSame ;
16
19
use function test ;
17
20
18
21
$ dataProvider = static function (): Generator {
@@ -52,11 +55,29 @@ static function ($value): bool {
52
55
];
53
56
};
54
57
58
+ /**
59
+ * @param iterable<mixed> $iterable
60
+ */
61
+ function create_iterable (iterable $ iterable , ?callable $ filter = null , ?callable $ map = null ): IterableObject
62
+ {
63
+ $ object = iterable ($ iterable );
64
+
65
+ if ($ filter !== null && func_num_args () > 1 ) {
66
+ $ object = $ object ->filter ($ filter );
67
+ }
68
+
69
+ if ($ map !== null ) {
70
+ $ object = $ object ->map ($ map );
71
+ }
72
+
73
+ return $ object ;
74
+ }
75
+
55
76
test (
56
77
'input: array | output: traversable ' ,
57
78
/** @param array<int, mixed> $data */
58
79
function (array $ data , ?callable $ filter , ?callable $ map , array $ expectedResult ): void {
59
- $ iterableObject = iterable ($ data , $ filter , $ map );
80
+ $ iterableObject = create_iterable ($ data , $ filter , $ map );
60
81
assertEquals ($ expectedResult , iterator_to_array ($ iterableObject ));
61
82
}
62
83
)->with ($ dataProvider ());
@@ -65,7 +86,7 @@ function (array $data, ?callable $filter, ?callable $map, array $expectedResult)
65
86
'input: array | output: array ' ,
66
87
/** @param array<int, mixed> $data */
67
88
function (array $ data , ?callable $ filter , ?callable $ map , array $ expectedResult ): void {
68
- $ iterableObject = iterable ($ data , $ filter , $ map );
89
+ $ iterableObject = create_iterable ($ data , $ filter , $ map );
69
90
assertEquals ($ expectedResult , $ iterableObject ->asArray ());
70
91
}
71
92
)->with ($ dataProvider ());
@@ -75,7 +96,7 @@ function (array $data, ?callable $filter, ?callable $map, array $expectedResult)
75
96
/** @param array<int, mixed> $data */
76
97
function (array $ data , ?callable $ filter , ?callable $ map , array $ expectedResult ): void {
77
98
$ data = SplFixedArray::fromArray ($ data );
78
- $ iterableObject = iterable ($ data , $ filter , $ map );
99
+ $ iterableObject = create_iterable ($ data , $ filter , $ map );
79
100
assertEquals ($ expectedResult , iterator_to_array ($ iterableObject ));
80
101
}
81
102
)->with ($ dataProvider ());
@@ -85,11 +106,32 @@ function (array $data, ?callable $filter, ?callable $map, array $expectedResult)
85
106
/** @param array<int, mixed> $data */
86
107
function (array $ data , ?callable $ filter , ?callable $ map , array $ expectedResult ): void {
87
108
$ data = SplFixedArray::fromArray ($ data );
88
- $ iterableObject = iterable ($ data , $ filter , $ map );
109
+ $ iterableObject = create_iterable ($ data , $ filter , $ map );
89
110
assertEquals ($ expectedResult , $ iterableObject ->asArray ());
90
111
}
91
112
)->with ($ dataProvider ());
92
113
114
+ it ('does not filter by default ' , function (): void {
115
+ $ data = [
116
+ null ,
117
+ false ,
118
+ true ,
119
+ 0 ,
120
+ 1 ,
121
+ '0 ' ,
122
+ '1 ' ,
123
+ '' ,
124
+ 'foo ' ,
125
+ ];
126
+
127
+ $ generator = function (array $ data ): Generator {
128
+ yield from $ data ;
129
+ };
130
+
131
+ assertSame ($ data , iterable ($ data )->asArray ());
132
+ assertSame ($ data , iterable ($ generator ($ data ))->asArray ());
133
+ });
134
+
93
135
it ('filters the subject ' , function (): void {
94
136
$ filter =
95
137
/** @param mixed $value */
@@ -100,6 +142,36 @@ static function ($value): bool {
100
142
assertEquals ([1 => 'bar ' ], iterator_to_array ($ iterableObject ));
101
143
});
102
144
145
+ it ('uses a truthy filter by default when filter() is invoked without arguments ' , function (): void {
146
+ $ data = [
147
+ null ,
148
+ false ,
149
+ true ,
150
+ 0 ,
151
+ 1 ,
152
+ '0 ' ,
153
+ '1 ' ,
154
+ '' ,
155
+ 'foo ' ,
156
+ ];
157
+
158
+ $ truthyValues = [
159
+ true ,
160
+ 1 ,
161
+ '1 ' ,
162
+ 'foo ' ,
163
+ ];
164
+
165
+ $ generator = function (array $ data ): Generator {
166
+ yield from $ data ;
167
+ };
168
+
169
+ assertSame ($ data , iterable ($ data )->asArray ());
170
+ assertSame ($ data , iterable ($ generator ($ data ))->asArray ());
171
+ assertSame ($ truthyValues , array_values (iterable ($ data )->filter ()->asArray ()));
172
+ assertSame ($ truthyValues , array_values (iterable ($ generator ($ data ))->filter ()->asArray ()));
173
+ });
174
+
103
175
it ('maps the subject ' , function (): void {
104
176
$ map = 'strtoupper ' ;
105
177
$ iterableObject = iterable (['foo ' , 'bar ' ])->map ($ map );
0 commit comments