Skip to content

Commit

Permalink
Closes #5960
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Oct 4, 2024
1 parent 85ea34f commit 8c7ccf6
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 8 deletions.
1 change: 1 addition & 0 deletions ChangeLog-11.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ All notable changes of the PHPUnit 11.4 release series are documented in this fi

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

[11.4.0]: https://github.com/sebastianbergmann/phpunit/compare/11.3...main
17 changes: 9 additions & 8 deletions DEPRECATIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ This functionality is currently [hard-deprecated](https://phpunit.de/backward-co

#### Miscellaneous

| Issue | Description | Since | Replacement |
|-------------------------------------------------------------------|-----------------------------------------------------------|--------|-----------------------------------------------------------------------------------------|
| [#4505](https://github.com/sebastianbergmann/phpunit/issues/4505) | Metadata in doc-comments | 10.3.0 | Metadata in attributes |
| [#5214](https://github.com/sebastianbergmann/phpunit/issues/5214) | `TestCase::iniSet()` | 10.3.0 | |
| [#5216](https://github.com/sebastianbergmann/phpunit/issues/5216) | `TestCase::setLocale()` | 10.3.0 | |
| [#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 |
| [#5951](https://github.com/sebastianbergmann/phpunit/issues/5951) | `includeUncoveredFiles` configuration option | 11.4.0 | |
| [#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 |
| Issue | Description | Since | Replacement |
|-------------------------------------------------------------------|-------------------------------------------------------------|--------|-----------------------------------------------------------------------------------------|
| [#4505](https://github.com/sebastianbergmann/phpunit/issues/4505) | Metadata in doc-comments | 10.3.0 | Metadata in attributes |
| [#5214](https://github.com/sebastianbergmann/phpunit/issues/5214) | `TestCase::iniSet()` | 10.3.0 | |
| [#5216](https://github.com/sebastianbergmann/phpunit/issues/5216) | `TestCase::setLocale()` | 10.3.0 | |
| [#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 |
| [#5951](https://github.com/sebastianbergmann/phpunit/issues/5951) | `includeUncoveredFiles` configuration option | 11.4.0 | |
| [#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 | |
18 changes: 18 additions & 0 deletions src/Metadata/Api/CodeCoverage.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,24 @@ 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(
sprintf(
'Targeting a trait such as %s with #[UsesMethod] is deprecated.',
$metadata->className(),
),
);
}

if ($metadata->isCoversClass() || $metadata->isUsesClass()) {
if (isset($this->withParents[$name])) {
return $this->withParents[$name];
Expand Down
23 changes: 23 additions & 0 deletions tests/end-to-end/_files/TraitTargetedWithCoversMethodTest.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\DeprecatedAnnotationsTestFixture;

use PHPUnit\Framework\Attributes\CoversMethod;
use PHPUnit\Framework\TestCase;
use PHPUnit\TestFixture\CoveredTrait;

#[CoversMethod(CoveredTrait::class, 'm')]
final class TraitTargetedWithCoversMethodTest extends TestCase
{
public function testSomething(): void
{
$this->assertTrue(true);
}
}
23 changes: 23 additions & 0 deletions tests/end-to-end/_files/TraitTargetedWithUsesMethodTest.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\DeprecatedAnnotationsTestFixture;

use PHPUnit\Framework\Attributes\UsesMethod;
use PHPUnit\Framework\TestCase;
use PHPUnit\TestFixture\CoveredTrait;

#[UsesMethod(CoveredTrait::class, 'm')]
final class TraitTargetedWithUsesMethodTest extends TestCase
{
public function testSomething(): void
{
$this->assertTrue(true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
--TEST--
The right events are emitted in the right order for a successful test that targets a trait with #[CoversMethod]
--SKIPIF--
<?php declare(strict_types=1);
require __DIR__ . '/../../_files/skip-if-requires-code-coverage-driver.php';
--FILE--
<?php declare(strict_types=1);
$traceFile = tempnam(sys_get_temp_dir(), __FILE__);
$coverageFile = tempnam(sys_get_temp_dir(), __FILE__);

$_SERVER['argv'][] = '--do-not-cache-result';
$_SERVER['argv'][] = '--no-configuration';
$_SERVER['argv'][] = '--no-output';
$_SERVER['argv'][] = '--log-events-text';
$_SERVER['argv'][] = $traceFile;
$_SERVER['argv'][] = '--coverage-text=' . $coverageFile;
$_SERVER['argv'][] = '--coverage-filter';
$_SERVER['argv'][] = __DIR__ . '/../_files';
$_SERVER['argv'][] = __DIR__ . '/../_files/TraitTargetedWithCoversMethodTest.php';

require __DIR__ . '/../../bootstrap.php';

(new PHPUnit\TextUI\Application)->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)
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
--TEST--
The right events are emitted in the right order for a successful test that targets a trait with #[UsesMethod]
--SKIPIF--
<?php declare(strict_types=1);
require __DIR__ . '/../../_files/skip-if-requires-code-coverage-driver.php';
--FILE--
<?php declare(strict_types=1);
$traceFile = tempnam(sys_get_temp_dir(), __FILE__);
$coverageFile = tempnam(sys_get_temp_dir(), __FILE__);

$_SERVER['argv'][] = '--do-not-cache-result';
$_SERVER['argv'][] = '--no-configuration';
$_SERVER['argv'][] = '--no-output';
$_SERVER['argv'][] = '--log-events-text';
$_SERVER['argv'][] = $traceFile;
$_SERVER['argv'][] = '--coverage-text=' . $coverageFile;
$_SERVER['argv'][] = '--coverage-filter';
$_SERVER['argv'][] = __DIR__ . '/../_files';
$_SERVER['argv'][] = __DIR__ . '/../_files/TraitTargetedWithUsesMethodTest.php';

require __DIR__ . '/../../bootstrap.php';

(new PHPUnit\TextUI\Application)->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)

0 comments on commit 8c7ccf6

Please sign in to comment.