Skip to content

Commit c9672f4

Browse files
authored
addition: EnumCase::except provider (#21)
1 parent fef2978 commit c9672f4

File tree

5 files changed

+40
-0
lines changed

5 files changed

+40
-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
}

src/PHPUnit/Doubles/Enums/IntBackedEnum.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ enum IntBackedEnum: int
99
case One = 1;
1010
case Two = 2;
1111
case Three = 3;
12+
case Four = 4;
1213
}

src/PHPUnit/Doubles/Enums/StringBackedEnum.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ enum StringBackedEnum: string
99
case One = 'One';
1010
case Two = 'Two';
1111
case Three = 'Three';
12+
case Four = 'Four';
1213
}

src/PHPUnit/Doubles/Enums/UnitEnum.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ enum UnitEnum
99
case One;
1010
case Two;
1111
case Three;
12+
case Four;
1213
}

0 commit comments

Comments
 (0)