Skip to content

Commit a098669

Browse files
authored
feat: drop annotations support (#75)
1 parent ddfc426 commit a098669

File tree

7 files changed

+21
-80
lines changed

7 files changed

+21
-80
lines changed

README.md

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ To run them, e.g. create a test case like in the following example:
8686

8787
use Cdn77\TestUtils\TestCheck\TestCheck;
8888
use PHPUnit\Framework\TestCase;
89+
use PHPUnit\Framework\Attributes\CoversNothing;
90+
use PHPUnit\Framework\Attributes\Group;
8991

90-
/**
91-
* @group integration
92-
* @coversNothing
93-
*/
92+
#[CoversNothing]
93+
#[Group('integration')]
9494
final class SuiteComplianceTest extends TestCaseBase
9595
{
9696
/** @dataProvider providerChecks */
@@ -119,7 +119,7 @@ final class SuiteComplianceTest extends TestCaseBase
119119

120120
### Every test has group
121121

122-
Asserts that all tests have a `@group` annotation
122+
Asserts that all tests have a `#[Group('x')]` attribute
123123

124124
:x:
125125
```php
@@ -128,7 +128,9 @@ final class FooTest extends TestCase
128128

129129
:heavy_check_mark:
130130
```php
131-
/** @group unit */
131+
use PHPUnit\Framework\Attributes\Group;
132+
133+
#[Group('unit')]
132134
final class FooTest extends TestCase
133135
```
134136

@@ -145,12 +147,10 @@ yield 'Every test has group' => [
145147
Asserts that all test share same namespace with class they're testing.
146148
Consider src namespace `Ns` and test namespace `Ns/Tests` then for test `Ns/Tests/UnitTest` must exist class `Ns/Unit`.
147149

148-
You can use `@covers` or `@coversDefaultClass` annotations to link test with tested class.
149-
Use `@coversNothing` annotation to skip this check.
150-
151-
`#[CoversNothing]` and `#[CoversClass]` attributes are supported.
150+
You can use `#[CoversClass]` attribute to link test with tested class.
151+
Use `#[CoversNothing]` attribute to skip this check.
152152

153-
Don't forget to enable `"forceCoversAnnotation="true"` in phpunit config file.
153+
Don't forget to enable `requireCoverageMetadata="true"` in phpunit config file.
154154

155155
```php
156156
namespace Ns;
@@ -181,7 +181,9 @@ final class UnitTest extends TestCase {}
181181
```php
182182
namespace Ns\Tests\Sub;
183183

184-
/** @covers \Ns\Unit */
184+
use PHPUnit\Framework\Attributes\CoversClass;
185+
186+
#[CoversClass('\Ns\Unit')]
185187
final class UnitTest extends TestCase {}
186188
```
187189

src/TestCheck/EveryTestHasGroup.php

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use function array_intersect;
1414
use function array_map;
1515
use function in_array;
16-
use function Safe\preg_match_all;
1716
use function sprintf;
1817

1918
final class EveryTestHasGroup implements TestCheck
@@ -45,26 +44,10 @@ public function run(TestCase $testCaseContext): void
4544
continue;
4645
}
4746

48-
$groupAttributes = $classReflection->getAttributes(Group::class);
49-
if ($groupAttributes !== []) {
50-
$groups = array_map(
51-
static fn ($groupAttribute) => $groupAttribute->getArguments()[0],
52-
$groupAttributes,
53-
);
54-
} else {
55-
$docComment = $classReflection->getDocComment();
56-
if ($docComment === false) {
57-
$testCaseContext::fail(sprintf('Test "%s" is missing phpdoc comment', $classReflection->getName()));
58-
}
59-
60-
if (preg_match_all('~\* @group +(?<group>\w+)(\n| \*/)~', $docComment, $matches) === 0) {
61-
$testCaseContext::fail(
62-
sprintf('Test "%s" is missing @group annotation', $classReflection->getName()),
63-
);
64-
}
65-
66-
$groups = $matches['group'];
67-
}
47+
$groups = array_map(
48+
static fn ($groupAttribute) => $groupAttribute->getArguments()[0],
49+
$classReflection->getAttributes(Group::class),
50+
);
6851

6952
$hasRequiredGroup = false;
7053
foreach ($groups as $group) {

src/TestCheck/EveryTestHasSameNamespaceAsCoveredClass.php

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
use ReflectionClass;
1212

1313
use function class_exists;
14-
use function Safe\preg_match;
15-
use function Safe\preg_match_all;
1614
use function sprintf;
1715
use function strlen;
1816
use function strpos;
@@ -22,9 +20,6 @@
2220

2321
final class EveryTestHasSameNamespaceAsCoveredClass implements TestCheck
2422
{
25-
private const PatternCovers = '~\* @covers(DefaultClass)? +(?<coveredClass>.+?)(?:\n| \*/)~';
26-
private const PatternCoversNothing = '~\* @coversNothing~';
27-
2823
private string $testsNamespaceSuffix;
2924

3025
/** @param iterable<string> $filePathNames */
@@ -43,15 +38,8 @@ public function run(TestCase $testCaseContext): void
4338
$attributesCoversClass = $classReflection->getAttributes(CoversClass::class);
4439
$attributesCoversNothing = $classReflection->getAttributes(CoversNothing::class);
4540

46-
$docComment = $classReflection->getDocComment();
47-
if ($docComment === false) {
48-
$docComment = '';
49-
}
50-
51-
$hasCovers = preg_match_all(self::PatternCovers, $docComment, $coversMatches) > 0
52-
|| $attributesCoversClass !== [];
53-
$hasCoversNothing = preg_match(self::PatternCoversNothing, $docComment) === 1
54-
|| $attributesCoversNothing !== [];
41+
$hasCovers = $attributesCoversClass !== [];
42+
$hasCoversNothing = $attributesCoversNothing !== [];
5543

5644
if ($hasCovers && $hasCoversNothing) {
5745
$testCaseContext::fail(sprintf(
@@ -85,7 +73,7 @@ public function run(TestCase $testCaseContext): void
8573
$testCaseContext::fail(
8674
sprintf(
8775
'Test "%s" is in the wrong namespace, ' .
88-
'has name different from tested class or is missing @covers annotation',
76+
'has name different from tested class or is missing CoversClass attribute',
8977
$classReflection->getName(),
9078
),
9179
);

tests/TestCheck/EveryTestHasGroupTest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ public static function providerSuccess(): Generator
3333
yield 'required, supported' => ['WithGroups.php', ['a'], ['b']];
3434
yield 'no required, supported' => ['WithGroups.php', null, ['a', 'b']];
3535
yield 'no required, any supported' => ['WithGroups.php', null, null];
36-
yield 'required, any supported - A' => ['WithGroupsAnnotations.php', ['a'], null];
37-
yield 'required, supported - A' => ['WithGroupsAnnotations.php', ['a'], ['b']];
38-
yield 'no required, supported - A' => ['WithGroupsAnnotations.php', null, ['a', 'b']];
39-
yield 'no required, any supported - A' => ['WithGroupsAnnotations.php', null, null];
4036
}
4137

4238
/**
@@ -65,6 +61,5 @@ public static function providerFail(): Generator
6561
{
6662
yield 'has unsupported group, required' => ['WithGroups.php', null, []];
6763
yield 'has no group, required' => ['WithoutGroup.php', ['a'], null];
68-
yield 'has unsupported group, required - A' => ['WithGroupsAnnotations.php', null, []];
6964
}
7065
}

tests/TestCheck/EveryTestHasSameNamespaceAsCoveredClassTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ public static function providerSuccess(): Generator
3030
'SameNamespaceAsLinkedCoveredClassTest.php',
3131
'CoveredClassWithSomeWhitespaceTest.php',
3232
'CoversNothingTest.php',
33-
'CoversDefaultClassTest.php',
3433
];
3534

3635
foreach ($files as $file) {

tests/TestCheck/Fixtures/EveryTestHasSameNamespaceAsCoveredClass/tests/CoversDefaultClassTest.php

Lines changed: 0 additions & 11 deletions
This file was deleted.

tests/TestCheck/Fixtures/WithGroupsAnnotations.php

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)