Skip to content

Commit 6b241e4

Browse files
committed
Add auto review test for group annotations
1 parent 55ff428 commit 6b241e4

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <[email protected]>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace CodeIgniter\AutoReview;
13+
14+
use FilesystemIterator;
15+
use PHPUnit\Framework\TestCase;
16+
use RecursiveDirectoryIterator;
17+
use RecursiveIteratorIterator;
18+
use ReflectionClass;
19+
use SplFileInfo;
20+
21+
/**
22+
* @internal
23+
*
24+
* @group AutoReview
25+
*/
26+
final class FrameworkCodeTest extends TestCase
27+
{
28+
/**
29+
* Cache of discovered test class names.
30+
*/
31+
private static array $testClasses = [];
32+
33+
private static array $recognizedGroupAnnotations = [
34+
'AutoReview',
35+
'CacheLive',
36+
'DatabaseLive',
37+
'Others',
38+
'SeparateProcess',
39+
];
40+
41+
/**
42+
* @dataProvider provideTestClassCases
43+
*
44+
* @phpstan-param class-string $class
45+
*/
46+
public function testEachTestClassHasCorrectGroupAnnotation(string $class): void
47+
{
48+
$reflection = new ReflectionClass($class);
49+
50+
if ($reflection->isAbstract()) {
51+
$this->addToAssertionCount(1);
52+
53+
return;
54+
}
55+
56+
$docComment = (string) $reflection->getDocComment();
57+
$this->assertNotEmpty($docComment, sprintf('[%s] Test class is missing a class-level PHPDoc.', $class));
58+
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));
62+
63+
$unrecognizedGroups = array_diff($matches[0], self::$recognizedGroupAnnotations);
64+
$this->assertEmpty($unrecognizedGroups, sprintf(
65+
"[%s] Unexpected @group annotation%s:\n%s\nExpected annotations to be in \"%s\".",
66+
$class,
67+
count($unrecognizedGroups) > 1 ? 's' : '',
68+
implode("\n", array_map(
69+
static fn (string $group): string => sprintf(' * @group %s', $group),
70+
$unrecognizedGroups
71+
)),
72+
implode(', ', self::$recognizedGroupAnnotations)
73+
));
74+
}
75+
76+
public function provideTestClassCases(): iterable
77+
{
78+
foreach ($this->getTestClasses() as $class) {
79+
yield $class => [$class];
80+
}
81+
}
82+
83+
private function getTestClasses(): array
84+
{
85+
if (self::$testClasses !== []) {
86+
return self::$testClasses;
87+
}
88+
89+
helper('filesystem');
90+
91+
$directory = set_realpath(dirname(__DIR__), true);
92+
93+
$iterator = new RecursiveIteratorIterator(
94+
new RecursiveDirectoryIterator(
95+
$directory,
96+
FilesystemIterator::SKIP_DOTS
97+
),
98+
RecursiveIteratorIterator::CHILD_FIRST
99+
);
100+
101+
$testClasses = array_map(
102+
static function (SplFileInfo $file) use ($directory): string {
103+
$relativePath = substr_replace(
104+
$file->getPathname(),
105+
'',
106+
0,
107+
strlen($directory)
108+
);
109+
$relativePath = substr_replace(
110+
$relativePath,
111+
'',
112+
strlen($relativePath) - strlen(DIRECTORY_SEPARATOR . $file->getBasename())
113+
);
114+
115+
return sprintf(
116+
'CodeIgniter\\%s%s%s',
117+
strtr($relativePath, DIRECTORY_SEPARATOR, '\\'),
118+
$relativePath === '' ? '' : '\\',
119+
$file->getBasename('.' . $file->getExtension())
120+
);
121+
},
122+
array_filter(
123+
iterator_to_array($iterator, false),
124+
static fn (SplFileInfo $file): bool => $file->isFile()
125+
&& strpos($file->getPathname(), DIRECTORY_SEPARATOR . 'fixtures' . DIRECTORY_SEPARATOR) === false
126+
&& strpos($file->getPathname(), DIRECTORY_SEPARATOR . 'Views' . DIRECTORY_SEPARATOR) === false
127+
)
128+
);
129+
130+
$testClasses = array_filter(
131+
$testClasses,
132+
static fn (string $class) => is_subclass_of($class, TestCase::class)
133+
);
134+
135+
sort($testClasses);
136+
137+
self::$testClasses = $testClasses;
138+
139+
return $testClasses;
140+
}
141+
}

0 commit comments

Comments
 (0)