Skip to content

Commit add2bbd

Browse files
committed
Adapt FrameworkCodeTest to recognize group attributes
1 parent fe997e4 commit add2bbd

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

tests/system/AutoReview/FrameworkCodeTest.php

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,26 @@
1414
namespace CodeIgniter\AutoReview;
1515

1616
use FilesystemIterator;
17+
use PHPUnit\Framework\Attributes\Group;
1718
use PHPUnit\Framework\TestCase;
1819
use RecursiveDirectoryIterator;
1920
use RecursiveIteratorIterator;
21+
use ReflectionAttribute;
2022
use ReflectionClass;
2123
use SplFileInfo;
2224

2325
/**
2426
* @internal
2527
*/
26-
#[\PHPUnit\Framework\Attributes\Group('AutoReview')]
28+
#[Group('AutoReview')]
2729
final class FrameworkCodeTest extends TestCase
2830
{
2931
/**
3032
* Cache of discovered test class names.
3133
*/
3234
private static array $testClasses = [];
3335

34-
private static array $recognizedGroupAnnotations = [
36+
private static array $recognizedGroupAttributeNames = [
3537
'AutoReview',
3638
'CacheLive',
3739
'DatabaseLive',
@@ -42,8 +44,8 @@ final class FrameworkCodeTest extends TestCase
4244
/**
4345
* @param class-string $class
4446
*/
45-
#[\PHPUnit\Framework\Attributes\DataProvider('provideEachTestClassHasCorrectGroupAnnotation')]
46-
public function testEachTestClassHasCorrectGroupAnnotation(string $class): void
47+
#[\PHPUnit\Framework\Attributes\DataProvider('provideEachTestClassHasCorrectGroupAttributeName')]
48+
public function testEachTestClassHasCorrectGroupAttributeName(string $class): void
4749
{
4850
$reflection = new ReflectionClass($class);
4951

@@ -53,27 +55,31 @@ public function testEachTestClassHasCorrectGroupAnnotation(string $class): void
5355
return;
5456
}
5557

56-
$docComment = (string) $reflection->getDocComment();
57-
$this->assertNotEmpty($docComment, sprintf('[%s] Test class is missing a class-level PHPDoc.', $class));
58+
$attributes = $reflection->getAttributes(Group::class);
59+
$this->assertNotEmpty($attributes, sprintf('[%s] Test class is missing a #[Group] attribute.', $class));
5860

59-
preg_match_all('/@group (\S+)/', $docComment, $matches);
60-
array_shift($matches);
61-
$this->assertNotEmpty($matches[0], sprintf('[%s] Test class is missing a @group annotation.', $class));
61+
$unrecognizedGroups = array_diff(
62+
array_map(static function (ReflectionAttribute $attribute): string {
63+
$groupAttribute = $attribute->newInstance();
64+
assert($groupAttribute instanceof Group);
6265

63-
$unrecognizedGroups = array_diff($matches[0], self::$recognizedGroupAnnotations);
66+
return $groupAttribute->name();
67+
}, $attributes),
68+
self::$recognizedGroupAttributeNames
69+
);
6470
$this->assertEmpty($unrecognizedGroups, sprintf(
65-
"[%s] Unexpected @group annotation%s:\n%s\nExpected annotations to be in \"%s\".",
71+
"[%s] Unexpected #[Group] attribute%s:\n%s\nExpected group names to be in \"%s\".",
6672
$class,
6773
count($unrecognizedGroups) > 1 ? 's' : '',
6874
implode("\n", array_map(
69-
static fn (string $group): string => sprintf(' * @group %s', $group),
75+
static fn (string $group): string => sprintf(' * #[Group(\'%s\')]', $group),
7076
$unrecognizedGroups
7177
)),
72-
implode(', ', self::$recognizedGroupAnnotations)
78+
implode(', ', self::$recognizedGroupAttributeNames)
7379
));
7480
}
7581

76-
public static function provideEachTestClassHasCorrectGroupAnnotation(): iterable
82+
public static function provideEachTestClassHasCorrectGroupAttributeName(): iterable
7783
{
7884
foreach (self::getTestClasses() as $class) {
7985
yield $class => [$class];

0 commit comments

Comments
 (0)