Skip to content

Commit

Permalink
Closes #5798
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Apr 6, 2024
1 parent e570295 commit 1b743cf
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 7 deletions.
7 changes: 7 additions & 0 deletions ChangeLog-11.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

All notable changes of the PHPUnit 11.1 release series are documented in this file using the [Keep a CHANGELOG](https://keepachangelog.com/) principles.

## [11.1.1] - 2024-MM-DD

### Fixed

* [#5798](https://github.com/sebastianbergmann/phpunit/issues/5798): The `#[CoversClass]` and `#[UsesClass]` attributes can no longer target traits

## [11.1.0] - 2024-04-05

### Added
Expand All @@ -26,4 +32,5 @@ All notable changes of the PHPUnit 11.1 release series are documented in this fi
* [#5689](https://github.com/sebastianbergmann/phpunit/issues/5689): The `restrictDeprecations` attribute on the `<source>` element of the XML configuration file is now deprecated in favor of the `ignoreSelfDeprecations`, `ignoreDirectDeprecations`, and `ignoreIndirectDeprecations` attributes
* [#5709](https://github.com/sebastianbergmann/phpunit/issues/5709): Deprecate support for using comma-separated values with the `--group`, `--exclude-group`, `--covers`, `--uses`, and `--test-suffix` CLI options

[11.1.1]: https://github.com/sebastianbergmann/phpunit/compare/11.1.0...11.1
[11.1.0]: https://github.com/sebastianbergmann/phpunit/compare/11.0.10...11.1.0
7 changes: 4 additions & 3 deletions src/Metadata/Api/CodeCoverage.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use function interface_exists;
use function sprintf;
use function str_starts_with;
use function trait_exists;
use PHPUnit\Framework\CodeCoverageException;
use PHPUnit\Framework\InvalidCoversTargetException;
use PHPUnit\Metadata\Covers;
Expand Down Expand Up @@ -279,16 +280,16 @@ private function names(CoversClass|CoversFunction|CoversMethod|UsesClass|UsesFun
);
}

if (!class_exists($name)) {
if (!(class_exists($name) || trait_exists($name))) {
throw new InvalidCoversTargetException(
sprintf(
'Class "%s" is not a valid target for code coverage',
'"%s" is not a valid target for code coverage',
$name,
),
);
}

assert(class_exists($names[0]));
assert(class_exists($names[0]) || trait_exists($names[0]));

$reflector = new ReflectionClass($name);

Expand Down
23 changes: 23 additions & 0 deletions tests/_files/CoverageTraitMethodTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <[email protected]>
*
* 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\CoversMethod;
use PHPUnit\Framework\Attributes\UsesMethod;
use PHPUnit\Framework\TestCase;

#[CoversMethod(CoveredTrait::class, 'm')]
#[UsesMethod(CoveredTrait::class, 'm')]
final class CoverageTraitMethodTest extends TestCase
{
public function testSomething(): void
{
}
}
23 changes: 23 additions & 0 deletions tests/_files/CoverageTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <[email protected]>
*
* 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;

#[CoversClass(CoveredTrait::class)]
#[UsesClass(CoveredTrait::class)]
final class CoverageTraitTest extends TestCase
{
public function testSomething(): void
{
}
}
18 changes: 18 additions & 0 deletions tests/_files/CoveredTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\TestFixture;

trait CoveredTrait
{
public function m(int $x, int $y): int
{
return $x + $y;
}
}
2 changes: 1 addition & 1 deletion tests/end-to-end/event/invalid-coverage-metadata.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Test Preparation Started (PHPUnit\TestFixture\Event\InvalidCoverageMetadata\Inva
Test Prepared (PHPUnit\TestFixture\Event\InvalidCoverageMetadata\InvalidCoverageMetadataTest::testOne)
Test Passed (PHPUnit\TestFixture\Event\InvalidCoverageMetadata\InvalidCoverageMetadataTest::testOne)
Test Triggered PHPUnit Warning (PHPUnit\TestFixture\Event\InvalidCoverageMetadata\InvalidCoverageMetadataTest::testOne)
Class "PHPUnit\TestFixture\Event\InvalidCoverageMetadata\This\Does\Not\Exist" is not a valid target for code coverage
"PHPUnit\TestFixture\Event\InvalidCoverageMetadata\This\Does\Not\Exist" is not a valid target for code coverage
Test Finished (PHPUnit\TestFixture\Event\InvalidCoverageMetadata\InvalidCoverageMetadataTest::testOne)
Test Suite Finished (PHPUnit\TestFixture\Event\InvalidCoverageMetadata\InvalidCoverageMetadataTest, 1 test)
Test Suite Finished (default, 1 test)
Expand Down
2 changes: 1 addition & 1 deletion tests/end-to-end/regression/5351.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Time: %s, Memory: %s MB
1 test triggered 1 PHPUnit warning:

1) PHPUnit\TestFixture\Issue5351\GreeterTest::testGreets
Class "PHPUnit\TestFixture\Issue5351\DoesNotExist" is not a valid target for code coverage
"PHPUnit\TestFixture\Issue5351\DoesNotExist" is not a valid target for code coverage

%sGreeterTest.php:18

Expand Down
38 changes: 36 additions & 2 deletions tests/unit/Metadata/Api/CodeCoverageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
use PHPUnit\TestFixture\CoverageMethodTest;
use PHPUnit\TestFixture\CoverageNamespacedFunctionTest;
use PHPUnit\TestFixture\CoverageNoneTest;
use PHPUnit\TestFixture\CoverageTraitMethodTest;
use PHPUnit\TestFixture\CoverageTraitTest;
use PHPUnit\TestFixture\InterfaceAsTargetWithAttributeTest;
use PHPUnit\TestFixture\InterfaceTargetTest;
use PHPUnit\TestFixture\InvalidClassTargetWithAnnotationTest;
Expand Down Expand Up @@ -206,6 +208,22 @@ public static function linesToBeCoveredProvider(): array
Test3194::class,
'testOne',
],

[
[
TEST_FILES_PATH . 'CoveredTrait.php' => range(12, 18),
],
CoverageTraitTest::class,
'testSomething',
],

[
[
TEST_FILES_PATH . 'CoveredTrait.php' => range(14, 17),
],
CoverageTraitMethodTest::class,
'testSomething',
],
];
}

Expand Down Expand Up @@ -321,6 +339,22 @@ public static function linesToBeUsedProvider(): array
CoverageNamespacedFunctionTest::class,
'testFunc',
],

[
[
TEST_FILES_PATH . 'CoveredTrait.php' => range(12, 18),
],
CoverageTraitTest::class,
'testSomething',
],

[
[
TEST_FILES_PATH . 'CoveredTrait.php' => range(14, 17),
],
CoverageTraitMethodTest::class,
'testSomething',
],
];
}

Expand Down Expand Up @@ -423,15 +457,15 @@ public function testRejectsInterfaceAsUsesClassTargetWithAttribute(): void
public function testRejectsInvalidCoversClassTargetWithAttribute(): void
{
$this->expectException(CodeCoverageException::class);
$this->expectExceptionMessage('Class "InvalidClass" is not a valid target for code coverage');
$this->expectExceptionMessage('"InvalidClass" is not a valid target for code coverage');

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

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

(new CodeCoverage)->linesToBeUsed(InvalidClassTargetWithAttributeTest::class, 'testOne');
}
Expand Down

0 comments on commit 1b743cf

Please sign in to comment.