Skip to content

Commit c1157d8

Browse files
committed
Add EnumCase::except() provider
1 parent ecfc744 commit c1157d8

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/PHPUnit/DataProviders/EnumCase.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66

77
use LogicException;
88
use ReflectionEnum;
9+
use ReflectionEnumUnitCase;
910
use UnitEnum;
1011
use ValueError;
1112

1213
use function array_filter;
14+
use function array_map;
1315
use function array_rand;
16+
use function array_reduce;
1417
use function count;
1518
use function in_array;
1619

@@ -78,4 +81,22 @@ public static function options(UnitEnum ...$options): iterable
7881
yield "$case->name" => [new self($case, ...$options)];
7982
}
8083
}
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+
}
81102
}

src/PHPUnit/DataProviders/EnumCaseTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,20 @@ public function itCanProvideFromGivenOptions(string $enumFQCN): void
164164
$this->assertContains($case[0]->instance, $expected);
165165
});
166166
}
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+
}
167183
}

0 commit comments

Comments
 (0)