Skip to content

Commit

Permalink
feat: drop annotations support (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
simPod authored Mar 13, 2023
1 parent ddfc426 commit a098669
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 80 deletions.
26 changes: 14 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ To run them, e.g. create a test case like in the following example:

use Cdn77\TestUtils\TestCheck\TestCheck;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\Attributes\Group;

/**
* @group integration
* @coversNothing
*/
#[CoversNothing]
#[Group('integration')]
final class SuiteComplianceTest extends TestCaseBase
{
/** @dataProvider providerChecks */
Expand Down Expand Up @@ -119,7 +119,7 @@ final class SuiteComplianceTest extends TestCaseBase

### Every test has group

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

:x:
```php
Expand All @@ -128,7 +128,9 @@ final class FooTest extends TestCase

:heavy_check_mark:
```php
/** @group unit */
use PHPUnit\Framework\Attributes\Group;

#[Group('unit')]
final class FooTest extends TestCase
```

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

You can use `@covers` or `@coversDefaultClass` annotations to link test with tested class.
Use `@coversNothing` annotation to skip this check.

`#[CoversNothing]` and `#[CoversClass]` attributes are supported.
You can use `#[CoversClass]` attribute to link test with tested class.
Use `#[CoversNothing]` attribute to skip this check.

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

```php
namespace Ns;
Expand Down Expand Up @@ -181,7 +181,9 @@ final class UnitTest extends TestCase {}
```php
namespace Ns\Tests\Sub;

/** @covers \Ns\Unit */
use PHPUnit\Framework\Attributes\CoversClass;

#[CoversClass('\Ns\Unit')]
final class UnitTest extends TestCase {}
```

Expand Down
25 changes: 4 additions & 21 deletions src/TestCheck/EveryTestHasGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use function array_intersect;
use function array_map;
use function in_array;
use function Safe\preg_match_all;
use function sprintf;

final class EveryTestHasGroup implements TestCheck
Expand Down Expand Up @@ -45,26 +44,10 @@ public function run(TestCase $testCaseContext): void
continue;
}

$groupAttributes = $classReflection->getAttributes(Group::class);
if ($groupAttributes !== []) {
$groups = array_map(
static fn ($groupAttribute) => $groupAttribute->getArguments()[0],
$groupAttributes,
);
} else {
$docComment = $classReflection->getDocComment();
if ($docComment === false) {
$testCaseContext::fail(sprintf('Test "%s" is missing phpdoc comment', $classReflection->getName()));
}

if (preg_match_all('~\* @group +(?<group>\w+)(\n| \*/)~', $docComment, $matches) === 0) {
$testCaseContext::fail(
sprintf('Test "%s" is missing @group annotation', $classReflection->getName()),
);
}

$groups = $matches['group'];
}
$groups = array_map(
static fn ($groupAttribute) => $groupAttribute->getArguments()[0],
$classReflection->getAttributes(Group::class),
);

$hasRequiredGroup = false;
foreach ($groups as $group) {
Expand Down
18 changes: 3 additions & 15 deletions src/TestCheck/EveryTestHasSameNamespaceAsCoveredClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
use ReflectionClass;

use function class_exists;
use function Safe\preg_match;
use function Safe\preg_match_all;
use function sprintf;
use function strlen;
use function strpos;
Expand All @@ -22,9 +20,6 @@

final class EveryTestHasSameNamespaceAsCoveredClass implements TestCheck
{
private const PatternCovers = '~\* @covers(DefaultClass)? +(?<coveredClass>.+?)(?:\n| \*/)~';
private const PatternCoversNothing = '~\* @coversNothing~';

private string $testsNamespaceSuffix;

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

$docComment = $classReflection->getDocComment();
if ($docComment === false) {
$docComment = '';
}

$hasCovers = preg_match_all(self::PatternCovers, $docComment, $coversMatches) > 0
|| $attributesCoversClass !== [];
$hasCoversNothing = preg_match(self::PatternCoversNothing, $docComment) === 1
|| $attributesCoversNothing !== [];
$hasCovers = $attributesCoversClass !== [];
$hasCoversNothing = $attributesCoversNothing !== [];

if ($hasCovers && $hasCoversNothing) {
$testCaseContext::fail(sprintf(
Expand Down Expand Up @@ -85,7 +73,7 @@ public function run(TestCase $testCaseContext): void
$testCaseContext::fail(
sprintf(
'Test "%s" is in the wrong namespace, ' .
'has name different from tested class or is missing @covers annotation',
'has name different from tested class or is missing CoversClass attribute',
$classReflection->getName(),
),
);
Expand Down
5 changes: 0 additions & 5 deletions tests/TestCheck/EveryTestHasGroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ public static function providerSuccess(): Generator
yield 'required, supported' => ['WithGroups.php', ['a'], ['b']];
yield 'no required, supported' => ['WithGroups.php', null, ['a', 'b']];
yield 'no required, any supported' => ['WithGroups.php', null, null];
yield 'required, any supported - A' => ['WithGroupsAnnotations.php', ['a'], null];
yield 'required, supported - A' => ['WithGroupsAnnotations.php', ['a'], ['b']];
yield 'no required, supported - A' => ['WithGroupsAnnotations.php', null, ['a', 'b']];
yield 'no required, any supported - A' => ['WithGroupsAnnotations.php', null, null];
}

/**
Expand Down Expand Up @@ -65,6 +61,5 @@ public static function providerFail(): Generator
{
yield 'has unsupported group, required' => ['WithGroups.php', null, []];
yield 'has no group, required' => ['WithoutGroup.php', ['a'], null];
yield 'has unsupported group, required - A' => ['WithGroupsAnnotations.php', null, []];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public static function providerSuccess(): Generator
'SameNamespaceAsLinkedCoveredClassTest.php',
'CoveredClassWithSomeWhitespaceTest.php',
'CoversNothingTest.php',
'CoversDefaultClassTest.php',
];

foreach ($files as $file) {
Expand Down

This file was deleted.

15 changes: 0 additions & 15 deletions tests/TestCheck/Fixtures/WithGroupsAnnotations.php

This file was deleted.

0 comments on commit a098669

Please sign in to comment.