Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/PHPUnit/DataProviders/EnumCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@

use LogicException;
use ReflectionEnum;
use ReflectionEnumUnitCase;
use UnitEnum;
use ValueError;

use function array_filter;
use function array_map;
use function array_rand;
use function array_reduce;
use function count;
use function in_array;

Expand Down Expand Up @@ -78,4 +81,22 @@ public static function options(UnitEnum ...$options): iterable
yield "$case->name" => [new self($case, ...$options)];
}
}

/**
* @param class-string<TValue> $enumFQCN
* @return iterable<array<self<TValue>>>
*/
public static function except(string $enumFQCN, UnitEnum ...$except): iterable
{
$options = array_map(function (ReflectionEnumUnitCase $reflection) use ($except): ?UnitEnum {
$case = $reflection->getValue();

return match (in_array($case, $except, true)) {
true => null,
false => $case,
};
}, new ReflectionEnum($enumFQCN)->getCases());

yield from self::options(...array_filter($options));
}
}
16 changes: 16 additions & 0 deletions src/PHPUnit/DataProviders/EnumCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,20 @@ public function itCanProvideFromGivenOptions(string $enumFQCN): void
$this->assertContains($case[0]->instance, $expected);
});
}

#[Test]
#[DataProvider('enumFQCNs')] /** @param class-string<UnitEnumInterface> $enumFQCN */
public function itCanProvideFromEnumFQCNsExceptSomeCases(string $enumFQCN): void
{
$options = $enumFQCN::cases();
[$keep, $except] = collect($options)->shuffle()->split(2);

$cases = iterator_to_array(EnumCase::except($enumFQCN, ...$except));

$this->assertCount(count($keep), $cases);
collect($cases)->each(function (array $case) use ($keep): void {
$this->assertInstanceOf(EnumCase::class, $case[0]);
$this->assertContains($case[0]->instance, $keep);
});
}
}
1 change: 1 addition & 0 deletions src/PHPUnit/Doubles/Enums/IntBackedEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ enum IntBackedEnum: int
case One = 1;
case Two = 2;
case Three = 3;
case Four = 4;
}
1 change: 1 addition & 0 deletions src/PHPUnit/Doubles/Enums/StringBackedEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ enum StringBackedEnum: string
case One = 'One';
case Two = 'Two';
case Three = 'Three';
case Four = 'Four';
}
1 change: 1 addition & 0 deletions src/PHPUnit/Doubles/Enums/UnitEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ enum UnitEnum
case One;
case Two;
case Three;
case Four;
}