From f460b5c6cd6a4f138c8cbb5cb543f9e48af792ee Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Mon, 7 Oct 2024 14:26:44 +0200 Subject: [PATCH] Closes #5961 --- ChangeLog-12.0.md | 1 + DEPRECATIONS.md | 1 - src/Metadata/Api/CodeCoverage.php | 16 ++----- ...-coversmethod-attribute-is-deprecated.phpt | 46 ------------------- ...th-usesmethod-attribute-is-deprecated.phpt | 46 ------------------- 5 files changed, 5 insertions(+), 105 deletions(-) delete mode 100644 tests/end-to-end/metadata/targeting-traits-with-coversmethod-attribute-is-deprecated.phpt delete mode 100644 tests/end-to-end/metadata/targeting-traits-with-usesmethod-attribute-is-deprecated.phpt diff --git a/ChangeLog-12.0.md b/ChangeLog-12.0.md index b70e7bd6736..50a08dd6827 100644 --- a/ChangeLog-12.0.md +++ b/ChangeLog-12.0.md @@ -29,6 +29,7 @@ All notable changes of the PHPUnit 12.0 release series are documented in this fi * [#5710](https://github.com/sebastianbergmann/phpunit/issues/5710): Support for using comma-separated values with the `--group`, `--exclude-group`, `--covers`, `--uses`, and `--test-suffix` CLI options * [#5801](https://github.com/sebastianbergmann/phpunit/issues/5801): Support for targeting traits with `#[CoversClass]` and `#[UsesClass]` attributes * [#5952](https://github.com/sebastianbergmann/phpunit/issues/5952): `includeUncoveredFiles` configuration option +* [#5961](https://github.com/sebastianbergmann/phpunit/issues/5961): Support for targeting trait methods with the `#[CoversMethod]` and `#[UsesMethod]` attributes * [#5978](https://github.com/sebastianbergmann/phpunit/issues/5978): Support for PHP 8.2 [12.0.0]: https://github.com/sebastianbergmann/phpunit/compare/11.5...main diff --git a/DEPRECATIONS.md b/DEPRECATIONS.md index 41d40e8cda1..a431a847a5f 100644 --- a/DEPRECATIONS.md +++ b/DEPRECATIONS.md @@ -25,4 +25,3 @@ This functionality is currently [hard-deprecated](https://phpunit.de/backward-co |-------------------------------------------------------------------|-------------------------------------------------------------|--------|-----------------------------------------------------------------------------------------| | [#4505](https://github.com/sebastianbergmann/phpunit/issues/4505) | Metadata in doc-comments | 10.3.0 | Metadata in attributes | | [#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 | -| [#5960](https://github.com/sebastianbergmann/phpunit/issues/5960) | Targeting traits with `#[CoversMethod]` and `#[UsesMethod]` | 11.4.0 | | diff --git a/src/Metadata/Api/CodeCoverage.php b/src/Metadata/Api/CodeCoverage.php index 9cd800901c4..a94df8a533c 100644 --- a/src/Metadata/Api/CodeCoverage.php +++ b/src/Metadata/Api/CodeCoverage.php @@ -291,19 +291,11 @@ private function names(CoversClass|CoversFunction|CoversMethod|CoversTrait|UsesC ); } - if ($metadata->isCoversMethod() && trait_exists($metadata->className())) { - EventFacade::emitter()->testRunnerTriggeredDeprecation( - sprintf( - 'Targeting a trait such as %s with #[CoversMethod] is deprecated.', - $metadata->className(), - ), - ); - } - - if ($metadata->isUsesMethod() && trait_exists($metadata->className())) { - EventFacade::emitter()->testRunnerTriggeredDeprecation( + if (($metadata->isCoversMethod() || $metadata->isUsesMethod()) && + trait_exists($metadata->className())) { + throw new InvalidCoversTargetException( sprintf( - 'Targeting a trait such as %s with #[UsesMethod] is deprecated.', + 'Trait %s is not a valid target for code coverage', $metadata->className(), ), ); diff --git a/tests/end-to-end/metadata/targeting-traits-with-coversmethod-attribute-is-deprecated.phpt b/tests/end-to-end/metadata/targeting-traits-with-coversmethod-attribute-is-deprecated.phpt deleted file mode 100644 index f15886cbdc3..00000000000 --- a/tests/end-to-end/metadata/targeting-traits-with-coversmethod-attribute-is-deprecated.phpt +++ /dev/null @@ -1,46 +0,0 @@ ---TEST-- -The right events are emitted in the right order for a successful test that targets a trait with #[CoversMethod] ---SKIPIF-- -run($_SERVER['argv']); - -print file_get_contents($traceFile); - -unlink($traceFile); -unlink($coverageFile); ---EXPECTF-- -PHPUnit Started (PHPUnit %s using %s) -Test Runner Configured -Event Facade Sealed -Test Suite Loaded (1 test) -Test Runner Started -Test Suite Sorted -Test Runner Execution Started (1 test) -Test Suite Started (PHPUnit\DeprecatedAnnotationsTestFixture\TraitTargetedWithCoversMethodTest, 1 test) -Test Preparation Started (PHPUnit\DeprecatedAnnotationsTestFixture\TraitTargetedWithCoversMethodTest::testSomething) -Test Prepared (PHPUnit\DeprecatedAnnotationsTestFixture\TraitTargetedWithCoversMethodTest::testSomething) -Test Passed (PHPUnit\DeprecatedAnnotationsTestFixture\TraitTargetedWithCoversMethodTest::testSomething) -Test Runner Triggered Deprecation (Targeting a trait such as PHPUnit\TestFixture\CoveredTrait with #[CoversMethod] is deprecated.) -Test Finished (PHPUnit\DeprecatedAnnotationsTestFixture\TraitTargetedWithCoversMethodTest::testSomething) -Test Suite Finished (PHPUnit\DeprecatedAnnotationsTestFixture\TraitTargetedWithCoversMethodTest, 1 test) -Test Runner Execution Finished -Test Runner Finished -PHPUnit Finished (Shell Exit Code: 0) diff --git a/tests/end-to-end/metadata/targeting-traits-with-usesmethod-attribute-is-deprecated.phpt b/tests/end-to-end/metadata/targeting-traits-with-usesmethod-attribute-is-deprecated.phpt deleted file mode 100644 index 34e8813955c..00000000000 --- a/tests/end-to-end/metadata/targeting-traits-with-usesmethod-attribute-is-deprecated.phpt +++ /dev/null @@ -1,46 +0,0 @@ ---TEST-- -The right events are emitted in the right order for a successful test that targets a trait with #[UsesMethod] ---SKIPIF-- -run($_SERVER['argv']); - -print file_get_contents($traceFile); - -unlink($traceFile); -unlink($coverageFile); ---EXPECTF-- -PHPUnit Started (PHPUnit %s using %s) -Test Runner Configured -Event Facade Sealed -Test Suite Loaded (1 test) -Test Runner Started -Test Suite Sorted -Test Runner Execution Started (1 test) -Test Suite Started (PHPUnit\DeprecatedAnnotationsTestFixture\TraitTargetedWithUsesMethodTest, 1 test) -Test Preparation Started (PHPUnit\DeprecatedAnnotationsTestFixture\TraitTargetedWithUsesMethodTest::testSomething) -Test Prepared (PHPUnit\DeprecatedAnnotationsTestFixture\TraitTargetedWithUsesMethodTest::testSomething) -Test Passed (PHPUnit\DeprecatedAnnotationsTestFixture\TraitTargetedWithUsesMethodTest::testSomething) -Test Runner Triggered Deprecation (Targeting a trait such as PHPUnit\TestFixture\CoveredTrait with #[UsesMethod] is deprecated.) -Test Finished (PHPUnit\DeprecatedAnnotationsTestFixture\TraitTargetedWithUsesMethodTest::testSomething) -Test Suite Finished (PHPUnit\DeprecatedAnnotationsTestFixture\TraitTargetedWithUsesMethodTest, 1 test) -Test Runner Execution Finished -Test Runner Finished -PHPUnit Finished (Shell Exit Code: 0)