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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Bug #14: Update descriptions in stub classes to clarify purpose (@terabytesoftw)
- Bug #15: Clarify parameter descriptions in `Message::getMessage()` method in `Message` enum (@terabytesoftw)
- Bug #16: Simplify `attributeCases()` method and update normalization logic in `EnumDataProvider` class (@terabytesoftw)

## 0.3.0 January 19, 2026

Expand Down
38 changes: 21 additions & 17 deletions src/EnumDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use BackedEnum;
use UnitEnum;

use function is_string;
use function sprintf;

/**
Expand All @@ -26,39 +27,31 @@ final class EnumDataProvider
/**
* Generates test cases for enum-based attribute scenarios.
*
* Normalizes each enum case and produces an expected value as either an attribute fragment (when `$asHtml` is
* `true`) or the enum case instance (when `$asHtml` is `false`).
*
* @phpstan-param class-string<UnitEnum> $enumClass Enum class name implementing UnitEnum.
* Normalizes each enum case and produces an expected value as either an attribute fragment.
*
* @param string $enumClass Enum class name implementing UnitEnum.
* @param string|UnitEnum $attribute Attribute name used to build the expected fragment.
* @param bool $asHtml Whether to generate expected output as an attribute fragment or enum instance. Default is
* `true`.
*
* @return array Structured test cases indexed by a normalized enum value key.
*
* @phpstan-return array<string, array{UnitEnum, mixed[], string|UnitEnum, string}>
* @phpstan-param class-string<UnitEnum> $enumClass Enum class name implementing UnitEnum.
* @phpstan-return array<string, array{UnitEnum, mixed[], UnitEnum, string, string}>
*/
public static function attributeCases(string $enumClass, string|UnitEnum $attribute, bool $asHtml = true): array
public static function attributeCases(string $enumClass, string|UnitEnum $attribute): array
{
$attribute = self::normalizeValue($attribute);
$cases = [];
$attributeName = is_string($attribute) ? $attribute : sprintf('%s', self::normalizeValue($attribute));

foreach ($enumClass::cases() as $case) {
$normalizedValue = self::normalizeValue($case);

$key = "enum: {$normalizedValue}";
$expected = $asHtml ? " {$attributeName}=\"{$normalizedValue}\"" : $case;
$message = $asHtml
? "Should return the '{$attributeName}' attribute value for enum case: {$normalizedValue}."
: "Should return the enum instance for case: {$normalizedValue}.";

$cases[$key] = [
$case,
[],
$expected,
$message,
$case,
" {$attribute}=\"{$normalizedValue}\"",
"Should return the '{$attribute}' attribute value for enum case: {$normalizedValue}.",
];
}

Expand Down Expand Up @@ -90,8 +83,19 @@ public static function tagCases(string $enumClass, string $category): array
return $data;
}

private static function normalizeValue(UnitEnum $enum): string
/**
* Normalizes the enum value to a string representation.
*
* @param string|UnitEnum $enum Enum instance or string value.
*
* @return string Normalized string value of the enum.
*/
private static function normalizeValue(string|UnitEnum $enum): string
{
if (is_string($enum)) {
return $enum;
}

return match ($enum instanceof BackedEnum) {
true => (string) $enum->value,
false => $enum->name,
Expand Down
9 changes: 3 additions & 6 deletions tests/EnumDataProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ public function testCasesGenerateExpectedStructure(
string $expectedAttributeCase,
string $expectedMessage,
): void {
$data = match ($asHtml) {
true => EnumDataProvider::attributeCases($enumClass, $attribute),
false => EnumDataProvider::attributeCases($enumClass, $attribute, false),
};
$data = EnumDataProvider::attributeCases($enumClass, $attribute);

self::assertNotEmpty(
$data,
Expand All @@ -65,14 +62,14 @@ public function testCasesGenerateExpectedStructure(
if ($asHtml) {
self::assertSame(
$expectedAttributeCase,
$data[$expectedKeyCase][2],
$data[$expectedKeyCase][3],
'Should return expected attribute value for enum case.',
);
}

self::assertSame(
$expectedMessage,
$data[$expectedKeyCase][3],
$data[$expectedKeyCase][4],
'Should return expected message for enum case.',
);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Support/Provider/EnumDataProviderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static function casesParameters(): array
false,
'enum: FOO',
' data-test="FOO"',
'Should return the enum instance for case: FOO.',
"Should return the 'data-test' attribute value for enum case: FOO.",
],
'as html' => [
TestEnum::class,
Expand Down
Loading