Skip to content

Commit

Permalink
Warn when trying to cover an interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
mad-briller authored and ondrejmirtes committed Mar 25, 2023
1 parent ceea85e commit 9e1b9de
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Rules/PHPUnit/CoversHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ public function processCovers(
if ($this->reflectionProvider->hasClass($className)) {
$class = $this->reflectionProvider->getClass($className);

if ($class->isInterface()) {
$errors[] = RuleErrorBuilder::message(sprintf(
'@covers value %s references an interface.',
$fullName
))->build();
}

if (isset($method) && $method !== '' && !$class->hasMethod($method)) {
$errors[] = RuleErrorBuilder::message(sprintf(
'@covers value %s references an invalid method.',
Expand All @@ -105,7 +112,6 @@ public function processCovers(
return $errors;
} elseif (!isset($method) && $this->reflectionProvider->hasFunction(new Name($className, []), null)) {
return $errors;

} else {
$error = RuleErrorBuilder::message(sprintf(
'@covers value %s references an invalid %s.',
Expand Down
4 changes: 4 additions & 0 deletions tests/Rules/PHPUnit/ClassCoversExistsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public function testRule(): void
50,
'The @covers annotation requires a fully qualified name.',
],
[
'@covers value \DateTimeInterface references an interface.',
64,
],
]);
}

Expand Down
7 changes: 7 additions & 0 deletions tests/Rules/PHPUnit/data/class-coverage.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,10 @@ class CoversNotFullyQualified extends \PHPUnit\Framework\TestCase
class CoversGlobalFunction extends \PHPUnit\Framework\TestCase
{
}

/**
* @covers \DateTimeInterface
*/
class CoversInterface extends \PHPUnit\Framework\TestCase
{
}

0 comments on commit 9e1b9de

Please sign in to comment.