Skip to content

Commit 1b743cf

Browse files
Closes #5798
1 parent e570295 commit 1b743cf

File tree

8 files changed

+113
-7
lines changed

8 files changed

+113
-7
lines changed

ChangeLog-11.1.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

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

5+
## [11.1.1] - 2024-MM-DD
6+
7+
### Fixed
8+
9+
* [#5798](https://github.com/sebastianbergmann/phpunit/issues/5798): The `#[CoversClass]` and `#[UsesClass]` attributes can no longer target traits
10+
511
## [11.1.0] - 2024-04-05
612

713
### Added
@@ -26,4 +32,5 @@ All notable changes of the PHPUnit 11.1 release series are documented in this fi
2632
* [#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
2733
* [#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
2834

35+
[11.1.1]: https://github.com/sebastianbergmann/phpunit/compare/11.1.0...11.1
2936
[11.1.0]: https://github.com/sebastianbergmann/phpunit/compare/11.0.10...11.1.0

src/Metadata/Api/CodeCoverage.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use function interface_exists;
1616
use function sprintf;
1717
use function str_starts_with;
18+
use function trait_exists;
1819
use PHPUnit\Framework\CodeCoverageException;
1920
use PHPUnit\Framework\InvalidCoversTargetException;
2021
use PHPUnit\Metadata\Covers;
@@ -279,16 +280,16 @@ private function names(CoversClass|CoversFunction|CoversMethod|UsesClass|UsesFun
279280
);
280281
}
281282

282-
if (!class_exists($name)) {
283+
if (!(class_exists($name) || trait_exists($name))) {
283284
throw new InvalidCoversTargetException(
284285
sprintf(
285-
'Class "%s" is not a valid target for code coverage',
286+
'"%s" is not a valid target for code coverage',
286287
$name,
287288
),
288289
);
289290
}
290291

291-
assert(class_exists($names[0]));
292+
assert(class_exists($names[0]) || trait_exists($names[0]));
292293

293294
$reflector = new ReflectionClass($name);
294295

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\TestFixture;
11+
12+
use PHPUnit\Framework\Attributes\CoversMethod;
13+
use PHPUnit\Framework\Attributes\UsesMethod;
14+
use PHPUnit\Framework\TestCase;
15+
16+
#[CoversMethod(CoveredTrait::class, 'm')]
17+
#[UsesMethod(CoveredTrait::class, 'm')]
18+
final class CoverageTraitMethodTest extends TestCase
19+
{
20+
public function testSomething(): void
21+
{
22+
}
23+
}

tests/_files/CoverageTraitTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\TestFixture;
11+
12+
use PHPUnit\Framework\Attributes\CoversClass;
13+
use PHPUnit\Framework\Attributes\UsesClass;
14+
use PHPUnit\Framework\TestCase;
15+
16+
#[CoversClass(CoveredTrait::class)]
17+
#[UsesClass(CoveredTrait::class)]
18+
final class CoverageTraitTest extends TestCase
19+
{
20+
public function testSomething(): void
21+
{
22+
}
23+
}

tests/_files/CoveredTrait.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\TestFixture;
11+
12+
trait CoveredTrait
13+
{
14+
public function m(int $x, int $y): int
15+
{
16+
return $x + $y;
17+
}
18+
}

tests/end-to-end/event/invalid-coverage-metadata.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Test Preparation Started (PHPUnit\TestFixture\Event\InvalidCoverageMetadata\Inva
3939
Test Prepared (PHPUnit\TestFixture\Event\InvalidCoverageMetadata\InvalidCoverageMetadataTest::testOne)
4040
Test Passed (PHPUnit\TestFixture\Event\InvalidCoverageMetadata\InvalidCoverageMetadataTest::testOne)
4141
Test Triggered PHPUnit Warning (PHPUnit\TestFixture\Event\InvalidCoverageMetadata\InvalidCoverageMetadataTest::testOne)
42-
Class "PHPUnit\TestFixture\Event\InvalidCoverageMetadata\This\Does\Not\Exist" is not a valid target for code coverage
42+
"PHPUnit\TestFixture\Event\InvalidCoverageMetadata\This\Does\Not\Exist" is not a valid target for code coverage
4343
Test Finished (PHPUnit\TestFixture\Event\InvalidCoverageMetadata\InvalidCoverageMetadataTest::testOne)
4444
Test Suite Finished (PHPUnit\TestFixture\Event\InvalidCoverageMetadata\InvalidCoverageMetadataTest, 1 test)
4545
Test Suite Finished (default, 1 test)

tests/end-to-end/regression/5351.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Time: %s, Memory: %s MB
2929
1 test triggered 1 PHPUnit warning:
3030

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

3434
%sGreeterTest.php:18
3535

tests/unit/Metadata/Api/CodeCoverageTest.php

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
use PHPUnit\TestFixture\CoverageMethodTest;
3535
use PHPUnit\TestFixture\CoverageNamespacedFunctionTest;
3636
use PHPUnit\TestFixture\CoverageNoneTest;
37+
use PHPUnit\TestFixture\CoverageTraitMethodTest;
38+
use PHPUnit\TestFixture\CoverageTraitTest;
3739
use PHPUnit\TestFixture\InterfaceAsTargetWithAttributeTest;
3840
use PHPUnit\TestFixture\InterfaceTargetTest;
3941
use PHPUnit\TestFixture\InvalidClassTargetWithAnnotationTest;
@@ -206,6 +208,22 @@ public static function linesToBeCoveredProvider(): array
206208
Test3194::class,
207209
'testOne',
208210
],
211+
212+
[
213+
[
214+
TEST_FILES_PATH . 'CoveredTrait.php' => range(12, 18),
215+
],
216+
CoverageTraitTest::class,
217+
'testSomething',
218+
],
219+
220+
[
221+
[
222+
TEST_FILES_PATH . 'CoveredTrait.php' => range(14, 17),
223+
],
224+
CoverageTraitMethodTest::class,
225+
'testSomething',
226+
],
209227
];
210228
}
211229

@@ -321,6 +339,22 @@ public static function linesToBeUsedProvider(): array
321339
CoverageNamespacedFunctionTest::class,
322340
'testFunc',
323341
],
342+
343+
[
344+
[
345+
TEST_FILES_PATH . 'CoveredTrait.php' => range(12, 18),
346+
],
347+
CoverageTraitTest::class,
348+
'testSomething',
349+
],
350+
351+
[
352+
[
353+
TEST_FILES_PATH . 'CoveredTrait.php' => range(14, 17),
354+
],
355+
CoverageTraitMethodTest::class,
356+
'testSomething',
357+
],
324358
];
325359
}
326360

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

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

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

436470
(new CodeCoverage)->linesToBeUsed(InvalidClassTargetWithAttributeTest::class, 'testOne');
437471
}

0 commit comments

Comments
 (0)