Skip to content

Commit ac06cff

Browse files
authored
Merge pull request #8821 from paulbalandan/php-unit-attributes
style: Enable `php_unit_attributes`
2 parents fd9cb65 + c841fb8 commit ac06cff

File tree

324 files changed

+1209
-1571
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

324 files changed

+1209
-1571
lines changed

.php-cs-fixer.dist.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343

4444
$overrides = [
4545
// for updating to coding-standard
46-
'modernize_strpos' => true,
46+
'modernize_strpos' => true,
47+
'php_unit_attributes' => true,
4748
];
4849

4950
$options = [

.php-cs-fixer.no-header.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131

3232
$overrides = [
3333
// for updating to coding-standard
34-
'modernize_strpos' => true,
34+
'modernize_strpos' => true,
35+
'php_unit_attributes' => true,
3536
];
3637

3738
$options = [

.php-cs-fixer.tests.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
$overrides = [
3737
'void_return' => true,
3838
// for updating to coding-standard
39-
'modernize_strpos' => true,
39+
'modernize_strpos' => true,
40+
'php_unit_attributes' => true,
4041
];
4142

4243
$options = [

.php-cs-fixer.user-guide.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
'leading_backslash_in_global_namespace' => true,
4141
],
4242
// for updating to coding-standard
43-
'modernize_strpos' => true,
43+
'modernize_strpos' => true,
44+
'php_unit_attributes' => true,
4445
];
4546

4647
$options = [

tests/system/API/ResponseTraitTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace CodeIgniter\API;
1515

16+
use PHPUnit\Framework\Attributes\Group;
1617
use CodeIgniter\Config\Factories;
1718
use CodeIgniter\Format\FormatterInterface;
1819
use CodeIgniter\Format\JSONFormatter;
@@ -28,9 +29,8 @@
2829

2930
/**
3031
* @internal
31-
*
32-
* @group Others
3332
*/
33+
#[Group('Others')]
3434
final class ResponseTraitTest extends CIUnitTestCase
3535
{
3636
private ?MockIncomingRequest $request = null;

tests/system/AutoReview/ComposerJsonTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@
1313

1414
namespace CodeIgniter\AutoReview;
1515

16+
use PHPUnit\Framework\Attributes\CoversNothing;
17+
use PHPUnit\Framework\Attributes\Group;
1618
use InvalidArgumentException;
1719
use JsonException;
1820
use PHPUnit\Framework\TestCase;
1921

2022
/**
2123
* @internal
22-
*
23-
* @coversNothing
24-
*
25-
* @group AutoReview
2624
*/
25+
#[CoversNothing]
26+
#[Group('AutoReview')]
2727
final class ComposerJsonTest extends TestCase
2828
{
2929
private array $devComposer;

tests/system/AutoReview/FrameworkCodeTest.php

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,28 @@
1313

1414
namespace CodeIgniter\AutoReview;
1515

16+
use PHPUnit\Framework\Attributes\DataProvider;
1617
use FilesystemIterator;
18+
use PHPUnit\Framework\Attributes\Group;
1719
use PHPUnit\Framework\TestCase;
1820
use RecursiveDirectoryIterator;
1921
use RecursiveIteratorIterator;
22+
use ReflectionAttribute;
2023
use ReflectionClass;
2124
use SplFileInfo;
2225

2326
/**
2427
* @internal
25-
*
26-
* @group AutoReview
2728
*/
29+
#[Group('AutoReview')]
2830
final class FrameworkCodeTest extends TestCase
2931
{
3032
/**
3133
* Cache of discovered test class names.
3234
*/
3335
private static array $testClasses = [];
3436

35-
private static array $recognizedGroupAnnotations = [
37+
private static array $recognizedGroupAttributeNames = [
3638
'AutoReview',
3739
'CacheLive',
3840
'DatabaseLive',
@@ -41,11 +43,10 @@ final class FrameworkCodeTest extends TestCase
4143
];
4244

4345
/**
44-
* @dataProvider provideEachTestClassHasCorrectGroupAnnotation
45-
*
4646
* @param class-string $class
4747
*/
48-
public function testEachTestClassHasCorrectGroupAnnotation(string $class): void
48+
#[DataProvider('provideEachTestClassHasCorrectGroupAttributeName')]
49+
public function testEachTestClassHasCorrectGroupAttributeName(string $class): void
4950
{
5051
$reflection = new ReflectionClass($class);
5152

@@ -55,27 +56,31 @@ public function testEachTestClassHasCorrectGroupAnnotation(string $class): void
5556
return;
5657
}
5758

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

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

65-
$unrecognizedGroups = array_diff($matches[0], self::$recognizedGroupAnnotations);
67+
return $groupAttribute->name();
68+
}, $attributes),
69+
self::$recognizedGroupAttributeNames
70+
);
6671
$this->assertEmpty($unrecognizedGroups, sprintf(
67-
"[%s] Unexpected @group annotation%s:\n%s\nExpected annotations to be in \"%s\".",
72+
"[%s] Unexpected #[Group] attribute%s:\n%s\nExpected group names to be in \"%s\".",
6873
$class,
6974
count($unrecognizedGroups) > 1 ? 's' : '',
7075
implode("\n", array_map(
71-
static fn (string $group): string => sprintf(' * @group %s', $group),
76+
static fn (string $group): string => sprintf(' * #[Group(\'%s\')]', $group),
7277
$unrecognizedGroups
7378
)),
74-
implode(', ', self::$recognizedGroupAnnotations)
79+
implode(', ', self::$recognizedGroupAttributeNames)
7580
));
7681
}
7782

78-
public static function provideEachTestClassHasCorrectGroupAnnotation(): iterable
83+
public static function provideEachTestClassHasCorrectGroupAttributeName(): iterable
7984
{
8085
foreach (self::getTestClasses() as $class) {
8186
yield $class => [$class];

tests/system/Autoloader/AutoloaderTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
namespace CodeIgniter\Autoloader;
1515

16+
use PHPUnit\Framework\Attributes\Group;
17+
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
18+
use PHPUnit\Framework\Attributes\PreserveGlobalState;
1619
use App\Controllers\Home;
1720
use Closure;
1821
use CodeIgniter\Exceptions\ConfigException;
@@ -27,9 +30,8 @@
2730

2831
/**
2932
* @internal
30-
*
31-
* @group Others
3233
*/
34+
#[Group('Others')]
3335
final class AutoloaderTest extends CIUnitTestCase
3436
{
3537
use ReflectionHelper;
@@ -390,10 +392,8 @@ public function testAutoloaderLoadsNonClassFiles(): void
390392
$loader->unregister();
391393
}
392394

393-
/**
394-
* @runInSeparateProcess
395-
* @preserveGlobalState disabled
396-
*/
395+
#[RunInSeparateProcess]
396+
#[PreserveGlobalState(false)]
397397
public function testLoadHelpers(): void
398398
{
399399
// Workaround for errors on PHPUnit 10 and PHP 8.3.

tests/system/Autoloader/FileLocatorCachedTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@
1313

1414
namespace CodeIgniter\Autoloader;
1515

16+
use PHPUnit\Framework\Attributes\Group;
1617
use CodeIgniter\Cache\FactoriesCache\FileVarExportHandler;
1718
use Config\Autoload;
1819
use Config\Modules;
1920

2021
/**
2122
* @internal
22-
*
23-
* @group Others
2423
*/
24+
#[Group('Others')]
2525
final class FileLocatorCachedTest extends FileLocatorTest
2626
{
2727
private FileVarExportHandler $handler;

tests/system/Autoloader/FileLocatorTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace CodeIgniter\Autoloader;
1515

16+
use PHPUnit\Framework\Attributes\Group;
1617
use CodeIgniter\HTTP\Header;
1718
use CodeIgniter\Test\CIUnitTestCase;
1819
use Config\Autoload;
@@ -21,9 +22,9 @@
2122
/**
2223
* @internal
2324
*
24-
* @group Others
2525
* @no-final
2626
*/
27+
#[Group('Others')]
2728
class FileLocatorTest extends CIUnitTestCase
2829
{
2930
protected FileLocatorInterface $locator;

0 commit comments

Comments
 (0)