Skip to content

Commit 8c7ccf6

Browse files
Closes #5960
1 parent 85ea34f commit 8c7ccf6

7 files changed

+166
-8
lines changed

ChangeLog-11.4.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@ All notable changes of the PHPUnit 11.4 release series are documented in this fi
2121

2222
* [#5951](https://github.com/sebastianbergmann/phpunit/issues/5951): `includeUncoveredFiles` configuration option
2323
* [#5958](https://github.com/sebastianbergmann/phpunit/issues/5958): Support for `#[CoversTrait]` and `#[UsesTrait]` attributes
24+
* [#5960](https://github.com/sebastianbergmann/phpunit/issues/5960): Support for targeting trait methods with the `#[CoversMethod]` and `#[UsesMethod]` attributes (and respective annotations)
2425

2526
[11.4.0]: https://github.com/sebastianbergmann/phpunit/compare/11.3...main

DEPRECATIONS.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,12 @@ This functionality is currently [hard-deprecated](https://phpunit.de/backward-co
5252

5353
#### Miscellaneous
5454

55-
| Issue | Description | Since | Replacement |
56-
|-------------------------------------------------------------------|-----------------------------------------------------------|--------|-----------------------------------------------------------------------------------------|
57-
| [#4505](https://github.com/sebastianbergmann/phpunit/issues/4505) | Metadata in doc-comments | 10.3.0 | Metadata in attributes |
58-
| [#5214](https://github.com/sebastianbergmann/phpunit/issues/5214) | `TestCase::iniSet()` | 10.3.0 | |
59-
| [#5216](https://github.com/sebastianbergmann/phpunit/issues/5216) | `TestCase::setLocale()` | 10.3.0 | |
60-
| [#5800](https://github.com/sebastianbergmann/phpunit/issues/5800) | Targeting traits with `#[CoversClass]` and `#[UsesClass]` | 11.2.0 | `#[CoversClass]` and `#[UsesClass]` also target the traits used by the targeted classes |
61-
| [#5951](https://github.com/sebastianbergmann/phpunit/issues/5951) | `includeUncoveredFiles` configuration option | 11.4.0 | |
62-
| [#5958](https://github.com/sebastianbergmann/phpunit/issues/5958) | `#[CoversTrait]` and `#[UsesTrait]` attributes | 11.4.0 | `#[CoversClass]` and `#[UsesClass]` also target the traits used by the targeted classes |
55+
| Issue | Description | Since | Replacement |
56+
|-------------------------------------------------------------------|-------------------------------------------------------------|--------|-----------------------------------------------------------------------------------------|
57+
| [#4505](https://github.com/sebastianbergmann/phpunit/issues/4505) | Metadata in doc-comments | 10.3.0 | Metadata in attributes |
58+
| [#5214](https://github.com/sebastianbergmann/phpunit/issues/5214) | `TestCase::iniSet()` | 10.3.0 | |
59+
| [#5216](https://github.com/sebastianbergmann/phpunit/issues/5216) | `TestCase::setLocale()` | 10.3.0 | |
60+
| [#5800](https://github.com/sebastianbergmann/phpunit/issues/5800) | Targeting traits with `#[CoversClass]` and `#[UsesClass]` | 11.2.0 | `#[CoversClass]` and `#[UsesClass]` also target the traits used by the targeted classes |
61+
| [#5951](https://github.com/sebastianbergmann/phpunit/issues/5951) | `includeUncoveredFiles` configuration option | 11.4.0 | |
62+
| [#5958](https://github.com/sebastianbergmann/phpunit/issues/5958) | `#[CoversTrait]` and `#[UsesTrait]` attributes | 11.4.0 | `#[CoversClass]` and `#[UsesClass]` also target the traits used by the targeted classes |
63+
| [#5960](https://github.com/sebastianbergmann/phpunit/issues/5960) | Targeting traits with `#[CoversMethod]` and `#[UsesMethod]` | 11.4.0 | |

src/Metadata/Api/CodeCoverage.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,24 @@ private function names(CoversClass|CoversFunction|CoversMethod|CoversTrait|UsesC
291291
);
292292
}
293293

294+
if ($metadata->isCoversMethod() && trait_exists($metadata->className())) {
295+
EventFacade::emitter()->testRunnerTriggeredDeprecation(
296+
sprintf(
297+
'Targeting a trait such as %s with #[CoversMethod] is deprecated.',
298+
$metadata->className(),
299+
),
300+
);
301+
}
302+
303+
if ($metadata->isUsesMethod() && trait_exists($metadata->className())) {
304+
EventFacade::emitter()->testRunnerTriggeredDeprecation(
305+
sprintf(
306+
'Targeting a trait such as %s with #[UsesMethod] is deprecated.',
307+
$metadata->className(),
308+
),
309+
);
310+
}
311+
294312
if ($metadata->isCoversClass() || $metadata->isUsesClass()) {
295313
if (isset($this->withParents[$name])) {
296314
return $this->withParents[$name];
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\DeprecatedAnnotationsTestFixture;
11+
12+
use PHPUnit\Framework\Attributes\CoversMethod;
13+
use PHPUnit\Framework\TestCase;
14+
use PHPUnit\TestFixture\CoveredTrait;
15+
16+
#[CoversMethod(CoveredTrait::class, 'm')]
17+
final class TraitTargetedWithCoversMethodTest extends TestCase
18+
{
19+
public function testSomething(): void
20+
{
21+
$this->assertTrue(true);
22+
}
23+
}
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\DeprecatedAnnotationsTestFixture;
11+
12+
use PHPUnit\Framework\Attributes\UsesMethod;
13+
use PHPUnit\Framework\TestCase;
14+
use PHPUnit\TestFixture\CoveredTrait;
15+
16+
#[UsesMethod(CoveredTrait::class, 'm')]
17+
final class TraitTargetedWithUsesMethodTest extends TestCase
18+
{
19+
public function testSomething(): void
20+
{
21+
$this->assertTrue(true);
22+
}
23+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
--TEST--
2+
The right events are emitted in the right order for a successful test that targets a trait with #[CoversMethod]
3+
--SKIPIF--
4+
<?php declare(strict_types=1);
5+
require __DIR__ . '/../../_files/skip-if-requires-code-coverage-driver.php';
6+
--FILE--
7+
<?php declare(strict_types=1);
8+
$traceFile = tempnam(sys_get_temp_dir(), __FILE__);
9+
$coverageFile = tempnam(sys_get_temp_dir(), __FILE__);
10+
11+
$_SERVER['argv'][] = '--do-not-cache-result';
12+
$_SERVER['argv'][] = '--no-configuration';
13+
$_SERVER['argv'][] = '--no-output';
14+
$_SERVER['argv'][] = '--log-events-text';
15+
$_SERVER['argv'][] = $traceFile;
16+
$_SERVER['argv'][] = '--coverage-text=' . $coverageFile;
17+
$_SERVER['argv'][] = '--coverage-filter';
18+
$_SERVER['argv'][] = __DIR__ . '/../_files';
19+
$_SERVER['argv'][] = __DIR__ . '/../_files/TraitTargetedWithCoversMethodTest.php';
20+
21+
require __DIR__ . '/../../bootstrap.php';
22+
23+
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
24+
25+
print file_get_contents($traceFile);
26+
27+
unlink($traceFile);
28+
unlink($coverageFile);
29+
--EXPECTF--
30+
PHPUnit Started (PHPUnit %s using %s)
31+
Test Runner Configured
32+
Event Facade Sealed
33+
Test Suite Loaded (1 test)
34+
Test Runner Started
35+
Test Suite Sorted
36+
Test Runner Execution Started (1 test)
37+
Test Suite Started (PHPUnit\DeprecatedAnnotationsTestFixture\TraitTargetedWithCoversMethodTest, 1 test)
38+
Test Preparation Started (PHPUnit\DeprecatedAnnotationsTestFixture\TraitTargetedWithCoversMethodTest::testSomething)
39+
Test Prepared (PHPUnit\DeprecatedAnnotationsTestFixture\TraitTargetedWithCoversMethodTest::testSomething)
40+
Test Passed (PHPUnit\DeprecatedAnnotationsTestFixture\TraitTargetedWithCoversMethodTest::testSomething)
41+
Test Runner Triggered Deprecation (Targeting a trait such as PHPUnit\TestFixture\CoveredTrait with #[CoversMethod] is deprecated.)
42+
Test Finished (PHPUnit\DeprecatedAnnotationsTestFixture\TraitTargetedWithCoversMethodTest::testSomething)
43+
Test Suite Finished (PHPUnit\DeprecatedAnnotationsTestFixture\TraitTargetedWithCoversMethodTest, 1 test)
44+
Test Runner Execution Finished
45+
Test Runner Finished
46+
PHPUnit Finished (Shell Exit Code: 0)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
--TEST--
2+
The right events are emitted in the right order for a successful test that targets a trait with #[UsesMethod]
3+
--SKIPIF--
4+
<?php declare(strict_types=1);
5+
require __DIR__ . '/../../_files/skip-if-requires-code-coverage-driver.php';
6+
--FILE--
7+
<?php declare(strict_types=1);
8+
$traceFile = tempnam(sys_get_temp_dir(), __FILE__);
9+
$coverageFile = tempnam(sys_get_temp_dir(), __FILE__);
10+
11+
$_SERVER['argv'][] = '--do-not-cache-result';
12+
$_SERVER['argv'][] = '--no-configuration';
13+
$_SERVER['argv'][] = '--no-output';
14+
$_SERVER['argv'][] = '--log-events-text';
15+
$_SERVER['argv'][] = $traceFile;
16+
$_SERVER['argv'][] = '--coverage-text=' . $coverageFile;
17+
$_SERVER['argv'][] = '--coverage-filter';
18+
$_SERVER['argv'][] = __DIR__ . '/../_files';
19+
$_SERVER['argv'][] = __DIR__ . '/../_files/TraitTargetedWithUsesMethodTest.php';
20+
21+
require __DIR__ . '/../../bootstrap.php';
22+
23+
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
24+
25+
print file_get_contents($traceFile);
26+
27+
unlink($traceFile);
28+
unlink($coverageFile);
29+
--EXPECTF--
30+
PHPUnit Started (PHPUnit %s using %s)
31+
Test Runner Configured
32+
Event Facade Sealed
33+
Test Suite Loaded (1 test)
34+
Test Runner Started
35+
Test Suite Sorted
36+
Test Runner Execution Started (1 test)
37+
Test Suite Started (PHPUnit\DeprecatedAnnotationsTestFixture\TraitTargetedWithUsesMethodTest, 1 test)
38+
Test Preparation Started (PHPUnit\DeprecatedAnnotationsTestFixture\TraitTargetedWithUsesMethodTest::testSomething)
39+
Test Prepared (PHPUnit\DeprecatedAnnotationsTestFixture\TraitTargetedWithUsesMethodTest::testSomething)
40+
Test Passed (PHPUnit\DeprecatedAnnotationsTestFixture\TraitTargetedWithUsesMethodTest::testSomething)
41+
Test Runner Triggered Deprecation (Targeting a trait such as PHPUnit\TestFixture\CoveredTrait with #[UsesMethod] is deprecated.)
42+
Test Finished (PHPUnit\DeprecatedAnnotationsTestFixture\TraitTargetedWithUsesMethodTest::testSomething)
43+
Test Suite Finished (PHPUnit\DeprecatedAnnotationsTestFixture\TraitTargetedWithUsesMethodTest, 1 test)
44+
Test Runner Execution Finished
45+
Test Runner Finished
46+
PHPUnit Finished (Shell Exit Code: 0)

0 commit comments

Comments
 (0)