Skip to content

Commit a9859e8

Browse files
authored
Merge pull request #6772 from paulbalandan/auto-review-group
Auto-review tests to enforce `@group` annotations
2 parents e86ef99 + 15225f6 commit a9859e8

19 files changed

+164
-98
lines changed

.github/workflows/test-autoreview.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,4 @@ jobs:
4949
COMPOSER_AUTH: ${{ secrets.COMPOSER_AUTH }}
5050

5151
- name: Run AutoReview Tests
52-
run: vendor/bin/phpunit --color=always --group=auto-review --no-coverage
52+
run: vendor/bin/phpunit --color=always --group=AutoReview --no-coverage

.github/workflows/test-phpunit.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ jobs:
170170
COVERAGE_NAME: php-v${{ env.COVERAGE_PHP_VERSION }}-${{ matrix.db-platforms }}
171171

172172
- name: Test with PHPUnit
173-
run: script -e -c "vendor/bin/phpunit --color=always --exclude-group=auto-review ${{ steps.phpunit-coverage-option.outputs.result }}"
173+
run: script -e -c "vendor/bin/phpunit --color=always --exclude-group=AutoReview ${{ steps.phpunit-coverage-option.outputs.result }}"
174174
env:
175175
DB: ${{ matrix.db-platforms }}
176176
TERM: xterm-256color

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@
6868
"autoload-dev": {
6969
"psr-4": {
7070
"CodeIgniter\\": "tests/system/",
71-
"CodeIgniter\\AutoReview\\": "tests/AutoReview/",
7271
"Utils\\": "utils/"
7372
}
7473
},

phpunit.xml.dist

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,30 @@
77
beStrictAboutTodoAnnotatedTests="true"
88
cacheResultFile="build/.phpunit.cache/test-results"
99
colors="true"
10+
columns="max"
1011
failOnRisky="true"
1112
failOnWarning="true"
1213
verbose="true">
1314

14-
<coverage processUncoveredFiles="true" ignoreDeprecatedCodeUnits="true">
15+
<coverage cacheDirectory="build/.phpunit.cache/code-coverage"
16+
processUncoveredFiles="true"
17+
ignoreDeprecatedCodeUnits="true">
1518
<include>
16-
<directory suffix=".php">./system</directory>
19+
<directory suffix=".php">system</directory>
1720
</include>
1821

1922
<exclude>
20-
<directory>./system/Commands/Generators/Views</directory>
21-
<directory>./system/Debug/Toolbar/Views</directory>
22-
<directory>./system/Pager/Views</directory>
23-
<directory>./system/ThirdParty</directory>
24-
<directory>./system/Validation/Views</directory>
25-
<file>./system/bootstrap.php</file>
26-
<file>./system/ComposerScripts.php</file>
27-
<file>./system/Config/Routes.php</file>
28-
<file>./system/Test/bootstrap.php</file>
29-
<file>./system/Test/ControllerTester.php</file>
30-
<file>./system/Test/FeatureTestCase.php</file>
23+
<directory>system/Commands/Generators/Views</directory>
24+
<directory>system/Debug/Toolbar/Views</directory>
25+
<directory>system/Pager/Views</directory>
26+
<directory>system/ThirdParty</directory>
27+
<directory>system/Validation/Views</directory>
28+
<file>system/bootstrap.php</file>
29+
<file>system/ComposerScripts.php</file>
30+
<file>system/Config/Routes.php</file>
31+
<file>system/Test/bootstrap.php</file>
32+
<file>system/Test/ControllerTester.php</file>
33+
<file>system/Test/FeatureTestCase.php</file>
3134
</exclude>
3235

3336
<report>
@@ -36,15 +39,8 @@
3639
</coverage>
3740

3841
<testsuites>
39-
<testsuite name="AutoReview">
40-
<directory>./tests/AutoReview</directory>
41-
</testsuite>
4242
<testsuite name="System">
43-
<directory>./tests/system</directory>
44-
<exclude>./tests/system/Database</exclude>
45-
</testsuite>
46-
<testsuite name="Database">
47-
<directory>./tests/system/Database</directory>
43+
<directory>tests/system</directory>
4844
</testsuite>
4945
</testsuites>
5046

tests/AutoReview/ComposerJsonTest.php renamed to tests/system/AutoReview/ComposerJsonTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
*
2222
* @coversNothing
2323
*
24-
* @group auto-review
24+
* @group AutoReview
2525
*/
2626
final class ComposerJsonTest extends TestCase
2727
{
@@ -32,8 +32,8 @@ protected function setUp(): void
3232
{
3333
parent::setUp();
3434

35-
$this->devComposer = $this->getComposerJson(dirname(__DIR__, 2) . '/composer.json');
36-
$this->frameworkComposer = $this->getComposerJson(dirname(__DIR__, 2) . '/admin/framework/composer.json');
35+
$this->devComposer = $this->getComposerJson(dirname(__DIR__, 3) . '/composer.json');
36+
$this->frameworkComposer = $this->getComposerJson(dirname(__DIR__, 3) . '/admin/framework/composer.json');
3737
}
3838

3939
public function testFrameworkRequireIsTheSameWithDevRequire(): void
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+
}

tests/system/Database/BaseConnectionTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,6 @@ public function testCanConnectAndStoreConnection()
100100
$this->assertSame(123, $db->getConnection());
101101
}
102102

103-
/**
104-
* @group single
105-
*/
106103
public function testCanConnectToFailoverWhenNoConnectionAvailable()
107104
{
108105
$options = $this->options;

tests/system/Database/BaseQueryTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,6 @@ public function testSimpleBindsWithNamedBindPlaceholderElsewhere()
305305
}
306306

307307
/**
308-
* @group single
309-
*
310308
* @see https://github.com/codeigniter4/CodeIgniter4/issues/201
311309
*/
312310
public function testSimilarNamedBinds()

tests/system/Database/Builder/LikeTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,6 @@ public function testOrNotLike()
189189
$this->assertSame($expectedBinds, $builder->getBinds());
190190
}
191191

192-
/**
193-
* @group single
194-
*/
195192
public function testCaseInsensitiveLike()
196193
{
197194
$builder = new BaseBuilder('job', $this->db);

tests/system/Database/Live/DeleteTest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,6 @@ public function testDeleteWithInternalWhere()
5353
$this->dontSeeInDatabase('job', ['name' => 'Developer']);
5454
}
5555

56-
/**
57-
* @group single
58-
*
59-
* @throws DatabaseException
60-
*/
6156
public function testDeleteWithLimit()
6257
{
6358
$this->seeNumRecords(2, 'user', ['country' => 'US']);

tests/system/Database/Live/UpdateTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,7 @@ public function testUpdateWithWhereSameColumn3()
181181
}
182182

183183
/**
184-
* @group single
185-
*
186-
* @see https://github.com/codeigniter4/CodeIgniter4/issues/324
184+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/324
187185
*/
188186
public function testUpdatePeriods()
189187
{

tests/system/Database/Live/WhereTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,6 @@ public function testWhereIn()
106106
$this->assertSame('Accountant', $jobs[1]->name);
107107
}
108108

109-
/**
110-
* @group single
111-
*/
112109
public function testWhereNotIn()
113110
{
114111
$jobs = $this->db->table('job')

tests/system/HTTP/Files/FileCollectionTest.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,6 @@ public function testExtensionGuessing()
218218
$this->assertSame('zip', $file->guessExtension());
219219
}
220220

221-
/**
222-
* @group single
223-
*/
224221
public function testAllReturnsValidSingleFileNestedName()
225222
{
226223
$_FILES = [
@@ -309,9 +306,6 @@ public function testHasFileWithMultipleFilesWithDifferentNames()
309306
$this->assertTrue($collection->hasFile('userfile2'));
310307
}
311308

312-
/**
313-
* @group single
314-
*/
315309
public function testHasFileWithSingleFileNestedName()
316310
{
317311
$_FILES = [

tests/system/HTTP/NegotiateTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,6 @@ public function testNegotiateMediaSupportsStrictMatching()
8181
$this->assertSame('', $this->negotiate->media(['text/plain'], true));
8282
}
8383

84-
/**
85-
* @group single
86-
*/
8784
public function testAcceptCharsetMatchesBasics()
8885
{
8986
$this->request->setHeader('Accept-Charset', 'iso-8859-5, unicode-1-1;q=0.8');

tests/system/HTTP/URITest.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -663,8 +663,6 @@ public function testResolveRelativeURI($rel, $expected)
663663
/**
664664
* @dataProvider defaultResolutions
665665
*
666-
* @group single
667-
*
668666
* @param mixed $rel
669667
* @param mixed $expected
670668
*/
@@ -792,8 +790,6 @@ public function testGetQueryWithStrings()
792790

793791
/**
794792
* @see https://github.com/codeigniter4/CodeIgniter4/issues/331
795-
*
796-
* @group single
797793
*/
798794
public function testNoExtraSlashes()
799795
{

tests/system/Helpers/NumberHelperTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,6 @@ public function testQuadrillions()
123123
$this->assertSame('123.5 quadrillion', number_to_amount('123,456,700,000,000,000', 1, 'en_US'));
124124
}
125125

126-
/**
127-
* @group single
128-
*/
129126
public function testCurrencyCurrentLocale()
130127
{
131128
$this->assertSame('$1,235', number_to_currency(1234.56, 'USD', 'en_US'));

tests/system/Session/SessionTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -463,9 +463,6 @@ public function testSetTempDataArraySingleTTL()
463463
$this->assertLessThanOrEqual($_SESSION['__ci_vars']['baz'], $time + 200);
464464
}
465465

466-
/**
467-
* @group single
468-
*/
469466
public function testGetTestDataReturnsAll()
470467
{
471468
$session = $this->getInstance();

tests/system/Throttle/ThrottleTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,6 @@ public function testRemove()
107107
$this->assertTrue($throttler->check('127.0.0.1', 1, MINUTE));
108108
}
109109

110-
/**
111-
* @group single
112-
*/
113110
public function testDecrementsValues()
114111
{
115112
$throttler = new Throttler($this->cache);

0 commit comments

Comments
 (0)