File tree Expand file tree Collapse file tree 2 files changed +37
-0
lines changed
src/PHPUnit/DataProviders Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change 6
6
7
7
use LogicException ;
8
8
use ReflectionEnum ;
9
+ use ReflectionEnumUnitCase ;
9
10
use UnitEnum ;
10
11
use ValueError ;
11
12
12
13
use function array_filter ;
14
+ use function array_map ;
13
15
use function array_rand ;
16
+ use function array_reduce ;
14
17
use function count ;
15
18
use function in_array ;
16
19
@@ -78,4 +81,22 @@ public static function options(UnitEnum ...$options): iterable
78
81
yield "$ case ->name " => [new self ($ case , ...$ options )];
79
82
}
80
83
}
84
+
85
+ /**
86
+ * @param class-string<TValue> $enumFQCN
87
+ * @return iterable<array<self<TValue>>>
88
+ */
89
+ public static function except (string $ enumFQCN , UnitEnum ...$ except ): iterable
90
+ {
91
+ $ options = array_map (function (ReflectionEnumUnitCase $ reflection ) use ($ except ): ?UnitEnum {
92
+ $ case = $ reflection ->getValue ();
93
+
94
+ return match (in_array ($ case , $ except , true )) {
95
+ true => null ,
96
+ false => $ case ,
97
+ };
98
+ }, new ReflectionEnum ($ enumFQCN )->getCases ());
99
+
100
+ yield from self ::options (...array_filter ($ options ));
101
+ }
81
102
}
Original file line number Diff line number Diff line change @@ -164,4 +164,20 @@ public function itCanProvideFromGivenOptions(string $enumFQCN): void
164
164
$ this ->assertContains ($ case [0 ]->instance , $ expected );
165
165
});
166
166
}
167
+
168
+ #[Test]
169
+ #[DataProvider('enumFQCNs ' )] /** @param class-string<UnitEnumInterface> $enumFQCN */
170
+ public function itCanProvideFromEnumFQCNsExceptSomeCases (string $ enumFQCN ): void
171
+ {
172
+ $ options = $ enumFQCN ::cases ();
173
+ [$ keep , $ except ] = collect ($ options )->shuffle ()->split (2 );
174
+
175
+ $ cases = iterator_to_array (EnumCase::except ($ enumFQCN , ...$ except ));
176
+
177
+ $ this ->assertCount (count ($ keep ), $ cases );
178
+ collect ($ cases )->each (function (array $ case ) use ($ keep ): void {
179
+ $ this ->assertInstanceOf (EnumCase::class, $ case [0 ]);
180
+ $ this ->assertContains ($ case [0 ]->instance , $ keep );
181
+ });
182
+ }
167
183
}
You can’t perform that action at this time.
0 commit comments