Skip to content

Commit

Permalink
Merge pull request #8821 from paulbalandan/php-unit-attributes
Browse files Browse the repository at this point in the history
style: Enable `php_unit_attributes`
  • Loading branch information
kenjis authored May 5, 2024
2 parents fd9cb65 + c841fb8 commit ac06cff
Show file tree
Hide file tree
Showing 324 changed files with 1,209 additions and 1,571 deletions.
3 changes: 2 additions & 1 deletion .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@

$overrides = [
// for updating to coding-standard
'modernize_strpos' => true,
'modernize_strpos' => true,
'php_unit_attributes' => true,
];

$options = [
Expand Down
3 changes: 2 additions & 1 deletion .php-cs-fixer.no-header.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@

$overrides = [
// for updating to coding-standard
'modernize_strpos' => true,
'modernize_strpos' => true,
'php_unit_attributes' => true,
];

$options = [
Expand Down
3 changes: 2 additions & 1 deletion .php-cs-fixer.tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
$overrides = [
'void_return' => true,
// for updating to coding-standard
'modernize_strpos' => true,
'modernize_strpos' => true,
'php_unit_attributes' => true,
];

$options = [
Expand Down
3 changes: 2 additions & 1 deletion .php-cs-fixer.user-guide.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
'leading_backslash_in_global_namespace' => true,
],
// for updating to coding-standard
'modernize_strpos' => true,
'modernize_strpos' => true,
'php_unit_attributes' => true,
];

$options = [
Expand Down
4 changes: 2 additions & 2 deletions tests/system/API/ResponseTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace CodeIgniter\API;

use PHPUnit\Framework\Attributes\Group;
use CodeIgniter\Config\Factories;
use CodeIgniter\Format\FormatterInterface;
use CodeIgniter\Format\JSONFormatter;
Expand All @@ -28,9 +29,8 @@

/**
* @internal
*
* @group Others
*/
#[Group('Others')]
final class ResponseTraitTest extends CIUnitTestCase
{
private ?MockIncomingRequest $request = null;
Expand Down
8 changes: 4 additions & 4 deletions tests/system/AutoReview/ComposerJsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@

namespace CodeIgniter\AutoReview;

use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\Attributes\Group;
use InvalidArgumentException;
use JsonException;
use PHPUnit\Framework\TestCase;

/**
* @internal
*
* @coversNothing
*
* @group AutoReview
*/
#[CoversNothing]
#[Group('AutoReview')]
final class ComposerJsonTest extends TestCase
{
private array $devComposer;
Expand Down
37 changes: 21 additions & 16 deletions tests/system/AutoReview/FrameworkCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,28 @@

namespace CodeIgniter\AutoReview;

use PHPUnit\Framework\Attributes\DataProvider;
use FilesystemIterator;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use ReflectionAttribute;
use ReflectionClass;
use SplFileInfo;

/**
* @internal
*
* @group AutoReview
*/
#[Group('AutoReview')]
final class FrameworkCodeTest extends TestCase
{
/**
* Cache of discovered test class names.
*/
private static array $testClasses = [];

private static array $recognizedGroupAnnotations = [
private static array $recognizedGroupAttributeNames = [
'AutoReview',
'CacheLive',
'DatabaseLive',
Expand All @@ -41,11 +43,10 @@ final class FrameworkCodeTest extends TestCase
];

/**
* @dataProvider provideEachTestClassHasCorrectGroupAnnotation
*
* @param class-string $class
*/
public function testEachTestClassHasCorrectGroupAnnotation(string $class): void
#[DataProvider('provideEachTestClassHasCorrectGroupAttributeName')]
public function testEachTestClassHasCorrectGroupAttributeName(string $class): void
{
$reflection = new ReflectionClass($class);

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

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

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

$unrecognizedGroups = array_diff($matches[0], self::$recognizedGroupAnnotations);
return $groupAttribute->name();
}, $attributes),
self::$recognizedGroupAttributeNames
);
$this->assertEmpty($unrecognizedGroups, sprintf(
"[%s] Unexpected @group annotation%s:\n%s\nExpected annotations to be in \"%s\".",
"[%s] Unexpected #[Group] attribute%s:\n%s\nExpected group names to be in \"%s\".",
$class,
count($unrecognizedGroups) > 1 ? 's' : '',
implode("\n", array_map(
static fn (string $group): string => sprintf(' * @group %s', $group),
static fn (string $group): string => sprintf(' * #[Group(\'%s\')]', $group),
$unrecognizedGroups
)),
implode(', ', self::$recognizedGroupAnnotations)
implode(', ', self::$recognizedGroupAttributeNames)
));
}

public static function provideEachTestClassHasCorrectGroupAnnotation(): iterable
public static function provideEachTestClassHasCorrectGroupAttributeName(): iterable
{
foreach (self::getTestClasses() as $class) {
yield $class => [$class];
Expand Down
12 changes: 6 additions & 6 deletions tests/system/Autoloader/AutoloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

namespace CodeIgniter\Autoloader;

use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
use PHPUnit\Framework\Attributes\PreserveGlobalState;
use App\Controllers\Home;
use Closure;
use CodeIgniter\Exceptions\ConfigException;
Expand All @@ -27,9 +30,8 @@

/**
* @internal
*
* @group Others
*/
#[Group('Others')]
final class AutoloaderTest extends CIUnitTestCase
{
use ReflectionHelper;
Expand Down Expand Up @@ -390,10 +392,8 @@ public function testAutoloaderLoadsNonClassFiles(): void
$loader->unregister();
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
#[RunInSeparateProcess]
#[PreserveGlobalState(false)]
public function testLoadHelpers(): void
{
// Workaround for errors on PHPUnit 10 and PHP 8.3.
Expand Down
4 changes: 2 additions & 2 deletions tests/system/Autoloader/FileLocatorCachedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@

namespace CodeIgniter\Autoloader;

use PHPUnit\Framework\Attributes\Group;
use CodeIgniter\Cache\FactoriesCache\FileVarExportHandler;
use Config\Autoload;
use Config\Modules;

/**
* @internal
*
* @group Others
*/
#[Group('Others')]
final class FileLocatorCachedTest extends FileLocatorTest
{
private FileVarExportHandler $handler;
Expand Down
3 changes: 2 additions & 1 deletion tests/system/Autoloader/FileLocatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace CodeIgniter\Autoloader;

use PHPUnit\Framework\Attributes\Group;
use CodeIgniter\HTTP\Header;
use CodeIgniter\Test\CIUnitTestCase;
use Config\Autoload;
Expand All @@ -21,9 +22,9 @@
/**
* @internal
*
* @group Others
* @no-final
*/
#[Group('Others')]
class FileLocatorTest extends CIUnitTestCase
{
protected FileLocatorInterface $locator;
Expand Down
8 changes: 4 additions & 4 deletions tests/system/CLI/CLITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace CodeIgniter\CLI;

use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\DataProvider;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\PhpStreamWrapper;
use CodeIgniter\Test\StreamFilterTrait;
Expand All @@ -21,9 +23,8 @@

/**
* @internal
*
* @group Others
*/
#[Group('Others')]
final class CLITest extends CIUnitTestCase
{
use StreamFilterTrait;
Expand Down Expand Up @@ -452,12 +453,11 @@ public function testWindow(): void
}

/**
* @dataProvider provideTable
*
* @param array $tbody
* @param array $thead
* @param array $expected
*/
#[DataProvider('provideTable')]
public function testTable($tbody, $thead, $expected): void
{
CLI::table($tbody, $thead);
Expand Down
4 changes: 2 additions & 2 deletions tests/system/CLI/ConsoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace CodeIgniter\CLI;

use PHPUnit\Framework\Attributes\Group;
use CodeIgniter\CodeIgniter;
use CodeIgniter\Config\DotEnv;
use CodeIgniter\Events\Events;
Expand All @@ -23,9 +24,8 @@

/**
* @internal
*
* @group Others
*/
#[Group('Others')]
final class ConsoleTest extends CIUnitTestCase
{
use StreamFilterTrait;
Expand Down
4 changes: 2 additions & 2 deletions tests/system/Cache/CacheFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@

namespace CodeIgniter\Cache;

use PHPUnit\Framework\Attributes\Group;
use CodeIgniter\Cache\Exceptions\CacheException;
use CodeIgniter\Cache\Handlers\DummyHandler;
use CodeIgniter\Test\CIUnitTestCase;
use Config\Cache;

/**
* @internal
*
* @group Others
*/
#[Group('Others')]
final class CacheFactoryTest extends CIUnitTestCase
{
private static string $directory = 'CacheFactory';
Expand Down
4 changes: 2 additions & 2 deletions tests/system/Cache/CacheMockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@

namespace CodeIgniter\Cache;

use PHPUnit\Framework\Attributes\Group;
use CodeIgniter\Cache\Handlers\BaseHandler;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Mock\MockCache;

/**
* @internal
*
* @group Others
*/
#[Group('Others')]
final class CacheMockTest extends CIUnitTestCase
{
public function testMockReturnsMockCacheClass(): void
Expand Down
4 changes: 2 additions & 2 deletions tests/system/Cache/FactoriesCacheFileHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@

namespace CodeIgniter\Cache;

use PHPUnit\Framework\Attributes\Group;
use Config\Cache as CacheConfig;

/**
* @internal
*
* @group Others
*/
#[Group('Others')]
final class FactoriesCacheFileHandlerTest extends FactoriesCacheFileVarExportHandlerTest
{
/**
Expand Down
4 changes: 2 additions & 2 deletions tests/system/Cache/FactoriesCacheFileVarExportHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace CodeIgniter\Cache;

use PHPUnit\Framework\Attributes\Group;
use CodeIgniter\Cache\FactoriesCache\FileVarExportHandler;
use CodeIgniter\Config\Factories;
use CodeIgniter\Test\CIUnitTestCase;
Expand All @@ -21,9 +22,8 @@
/**
* @internal
* @no-final
*
* @group Others
*/
#[Group('Others')]
class FactoriesCacheFileVarExportHandlerTest extends CIUnitTestCase
{
protected FactoriesCache $cache;
Expand Down
8 changes: 4 additions & 4 deletions tests/system/Cache/Handlers/BaseHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@

namespace CodeIgniter\Cache\Handlers;

use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\DataProvider;
use CodeIgniter\Test\CIUnitTestCase;
use stdClass;
use Tests\Support\Cache\RestrictiveHandler;

/**
* @internal
*
* @group Others
*/
#[Group('Others')]
final class BaseHandlerTest extends CIUnitTestCase
{
/**
* @dataProvider provideValidateKeyInvalidType
*
* @param mixed $input
*/
#[DataProvider('provideValidateKeyInvalidType')]
public function testValidateKeyInvalidType($input): void
{
$this->expectException('InvalidArgumentException');
Expand Down
Loading

0 comments on commit ac06cff

Please sign in to comment.