diff --git a/CHANGELOG.md b/CHANGELOG.md index 47a982d..668cc01 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/EnumDataProvider.php b/src/EnumDataProvider.php index e7cb4d4..2d8d2b7 100644 --- a/src/EnumDataProvider.php +++ b/src/EnumDataProvider.php @@ -7,6 +7,7 @@ use BackedEnum; use UnitEnum; +use function is_string; use function sprintf; /** @@ -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 $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 + * @phpstan-param class-string $enumClass Enum class name implementing UnitEnum. + * @phpstan-return array */ - 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}.", ]; } @@ -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, diff --git a/tests/EnumDataProviderTest.php b/tests/EnumDataProviderTest.php index ff8fb95..35425d4 100644 --- a/tests/EnumDataProviderTest.php +++ b/tests/EnumDataProviderTest.php @@ -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, @@ -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.', ); } diff --git a/tests/Support/Provider/EnumDataProviderProvider.php b/tests/Support/Provider/EnumDataProviderProvider.php index 4a0a47f..dc2dc85 100644 --- a/tests/Support/Provider/EnumDataProviderProvider.php +++ b/tests/Support/Provider/EnumDataProviderProvider.php @@ -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,