Skip to content

Commit

Permalink
Make #[CoversClass] and #[UsesClass] attributes behave more like the …
Browse files Browse the repository at this point in the history
…annotations they replace
  • Loading branch information
sebastianbergmann committed Feb 25, 2024
1 parent bc6b6fe commit 05b7216
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
1 change: 1 addition & 0 deletions ChangeLog-10.5.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ All notable changes of the PHPUnit 10.5 release series are documented in this fi

* [#5704](https://github.com/sebastianbergmann/phpunit/issues/5704#issuecomment-1951105254): No warning when CLI options are used multiple times
* [#5707](https://github.com/sebastianbergmann/phpunit/issues/5707): `--fail-on-empty-test-suite` CLI option is not documented in `--help` output
* No warning when the `#[CoversClass]` and `#[UsesClass]` attributes are used with the name of an interface
* Resource usage information is printed when the `--debug` CLI option is used

## [10.5.10] - 2024-02-04
Expand Down
12 changes: 10 additions & 2 deletions src/Metadata/Api/CodeCoverage.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ public function linesToBeCovered(string $className, string $methodName): array|f
);
} catch (InvalidCodeUnitException $e) {
if ($metadata->isCoversClass()) {
$type = 'Class';
if (interface_exists($metadata->className())) {
$type = 'Interface';
} else {
$type = 'Class';
}
} else {
$type = 'Function';
}
Expand Down Expand Up @@ -182,7 +186,11 @@ public function linesToBeUsed(string $className, string $methodName): array
);
} catch (InvalidCodeUnitException $e) {
if ($metadata->isUsesClass()) {
$type = 'Class';
if (interface_exists($metadata->className())) {
$type = 'Interface';
} else {
$type = 'Class';
}
} else {
$type = 'Function';
}
Expand Down
24 changes: 24 additions & 0 deletions tests/_files/InterfaceAsTargetWithAttributeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\TestFixture;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;
use Throwable;

#[CoversClass(Throwable::class)]
#[UsesClass(Throwable::class)]
final class InterfaceAsTargetWithAttributeTest extends TestCase
{
public function testOne(): void
{
}
}
17 changes: 17 additions & 0 deletions tests/unit/Metadata/Api/CodeCoverageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use PHPUnit\TestFixture\CoverageNamespacedFunctionTest;
use PHPUnit\TestFixture\CoverageNoneTest;
use PHPUnit\TestFixture\IgnoringCodeUnitsTest;
use PHPUnit\TestFixture\InterfaceAsTargetWithAttributeTest;
use PHPUnit\TestFixture\InterfaceTargetTest;
use PHPUnit\TestFixture\InvalidClassTargetWithAnnotationTest;
use PHPUnit\TestFixture\InvalidClassTargetWithAttributeTest;
Expand Down Expand Up @@ -387,6 +388,22 @@ public function testRejectsInterfaceClassTarget(): void
(new CodeCoverage)->linesToBeCovered(InterfaceTargetTest::class, 'testOne');
}

public function testRejectsInterfaceAsCoversClassTargetWithAttribute(): void
{
$this->expectException(CodeCoverageException::class);
$this->expectExceptionMessage('Interface "Throwable" is not a valid target for code coverage');

(new CodeCoverage)->linesToBeCovered(InterfaceAsTargetWithAttributeTest::class, 'testOne');
}

public function testRejectsInterfaceAsUsesClassTargetWithAttribute(): void
{
$this->expectException(CodeCoverageException::class);
$this->expectExceptionMessage('Interface "Throwable" is not a valid target for code coverage');

(new CodeCoverage)->linesToBeUsed(InterfaceAsTargetWithAttributeTest::class, 'testOne');
}

public function testRejectsInvalidCoversClassTargetWithAttribute(): void
{
$this->expectException(CodeCoverageException::class);
Expand Down

0 comments on commit 05b7216

Please sign in to comment.