Skip to content

Commit 55c73d9

Browse files
committed
fix(support): Simplify attributeCases() method and update normalization logic in EnumDataProvider class.
1 parent e7bc7b3 commit 55c73d9

File tree

4 files changed

+26
-24
lines changed

4 files changed

+26
-24
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

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

89
## 0.3.0 January 19, 2026
910

src/EnumDataProvider.php

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use BackedEnum;
88
use UnitEnum;
99

10+
use function is_string;
1011
use function sprintf;
1112

1213
/**
@@ -26,39 +27,31 @@ final class EnumDataProvider
2627
/**
2728
* Generates test cases for enum-based attribute scenarios.
2829
*
29-
* Normalizes each enum case and produces an expected value as either an attribute fragment (when `$asHtml` is
30-
* `true`) or the enum case instance (when `$asHtml` is `false`).
31-
*
32-
* @phpstan-param class-string<UnitEnum> $enumClass Enum class name implementing UnitEnum.
30+
* Normalizes each enum case and produces an expected value as either an attribute fragment.
3331
*
3432
* @param string $enumClass Enum class name implementing UnitEnum.
3533
* @param string|UnitEnum $attribute Attribute name used to build the expected fragment.
36-
* @param bool $asHtml Whether to generate expected output as an attribute fragment or enum instance. Default is
37-
* `true`.
3834
*
3935
* @return array Structured test cases indexed by a normalized enum value key.
4036
*
41-
* @phpstan-return array<string, array{UnitEnum, mixed[], string|UnitEnum, string}>
37+
* @phpstan-param class-string<UnitEnum> $enumClass Enum class name implementing UnitEnum.
38+
* @phpstan-return array<string, array{UnitEnum, mixed[], UnitEnum, string, string}>
4239
*/
43-
public static function attributeCases(string $enumClass, string|UnitEnum $attribute, bool $asHtml = true): array
40+
public static function attributeCases(string $enumClass, string|UnitEnum $attribute): array
4441
{
42+
$attribute = self::normalizeValue($attribute);
4543
$cases = [];
46-
$attributeName = is_string($attribute) ? $attribute : sprintf('%s', self::normalizeValue($attribute));
4744

4845
foreach ($enumClass::cases() as $case) {
4946
$normalizedValue = self::normalizeValue($case);
50-
5147
$key = "enum: {$normalizedValue}";
52-
$expected = $asHtml ? " {$attributeName}=\"{$normalizedValue}\"" : $case;
53-
$message = $asHtml
54-
? "Should return the '{$attributeName}' attribute value for enum case: {$normalizedValue}."
55-
: "Should return the enum instance for case: {$normalizedValue}.";
5648

5749
$cases[$key] = [
5850
$case,
5951
[],
60-
$expected,
61-
$message,
52+
$case,
53+
" {$attribute}=\"{$normalizedValue}\"",
54+
"Should return the '{$attribute}' attribute value for enum case: {$normalizedValue}.",
6255
];
6356
}
6457

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

93-
private static function normalizeValue(UnitEnum $enum): string
86+
/**
87+
* Normalizes the enum value to a string representation.
88+
*
89+
* @param string|UnitEnum $enum Enum instance or string value.
90+
*
91+
* @return string Normalized string value of the enum.
92+
*/
93+
private static function normalizeValue(string|UnitEnum $enum): string
9494
{
95+
if (is_string($enum)) {
96+
return $enum;
97+
}
98+
9599
return match ($enum instanceof BackedEnum) {
96100
true => (string) $enum->value,
97101
false => $enum->name,

tests/EnumDataProviderTest.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@ public function testCasesGenerateExpectedStructure(
4242
string $expectedAttributeCase,
4343
string $expectedMessage,
4444
): void {
45-
$data = match ($asHtml) {
46-
true => EnumDataProvider::attributeCases($enumClass, $attribute),
47-
false => EnumDataProvider::attributeCases($enumClass, $attribute, false),
48-
};
45+
$data = EnumDataProvider::attributeCases($enumClass, $attribute);
4946

5047
self::assertNotEmpty(
5148
$data,
@@ -65,14 +62,14 @@ public function testCasesGenerateExpectedStructure(
6562
if ($asHtml) {
6663
self::assertSame(
6764
$expectedAttributeCase,
68-
$data[$expectedKeyCase][2],
65+
$data[$expectedKeyCase][3],
6966
'Should return expected attribute value for enum case.',
7067
);
7168
}
7269

7370
self::assertSame(
7471
$expectedMessage,
75-
$data[$expectedKeyCase][3],
72+
$data[$expectedKeyCase][4],
7673
'Should return expected message for enum case.',
7774
);
7875
}

tests/Support/Provider/EnumDataProviderProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public static function casesParameters(): array
2828
false,
2929
'enum: FOO',
3030
' data-test="FOO"',
31-
'Should return the enum instance for case: FOO.',
31+
"Should return the 'data-test' attribute value for enum case: FOO.",
3232
],
3333
'as html' => [
3434
TestEnum::class,

0 commit comments

Comments
 (0)