Skip to content

Commit

Permalink
Closes #5984 and #5985
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Dec 7, 2024
1 parent 36a2aeb commit 67ae4d8
Show file tree
Hide file tree
Showing 18 changed files with 1,092 additions and 0 deletions.
5 changes: 5 additions & 0 deletions ChangeLog-12.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes of the PHPUnit 12.0 release series are documented in this fi

## [12.0.0] - 2025-02-07

### Added

* [#5984](https://github.com/sebastianbergmann/phpunit/issues/5984): `#[CoversClassesThatExtendClass]` and `#[UsesClassesThatExtendClass]`
* [#5985](https://github.com/sebastianbergmann/phpunit/issues/5985): `#[CoversClassesThatImplementInterface]` and `#[UsesClassesThatImplementInterface]`

### Changed

* [#5872](https://github.com/sebastianbergmann/phpunit/issues/5872): The default value for `shortenArraysForExportThreshold` is now `10` (limit export of arrays to 10 levels) instead of `0` (do not limit export of arrays)
Expand Down
42 changes: 42 additions & 0 deletions src/Framework/Attributes/CoversClassesThatExtendClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?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\Framework\Attributes;

use Attribute;

/**
* @immutable
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
*/
#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
final readonly class CoversClassesThatExtendClass
{
/**
* @var class-string
*/
private string $className;

/**
* @param class-string $className
*/
public function __construct(string $className)
{
$this->className = $className;
}

/**
* @return class-string
*/
public function className(): string
{
return $this->className;
}
}
42 changes: 42 additions & 0 deletions src/Framework/Attributes/CoversClassesThatImplementInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?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\Framework\Attributes;

use Attribute;

/**
* @immutable
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
*/
#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
final readonly class CoversClassesThatImplementInterface
{
/**
* @var class-string
*/
private string $interfaceName;

/**
* @param class-string $interfaceName
*/
public function __construct(string $interfaceName)
{
$this->interfaceName = $interfaceName;
}

/**
* @return class-string
*/
public function interfaceName(): string
{
return $this->interfaceName;
}
}
42 changes: 42 additions & 0 deletions src/Framework/Attributes/UsesClassesThatExtendClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?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\Framework\Attributes;

use Attribute;

/**
* @immutable
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
*/
#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
final readonly class UsesClassesThatExtendClass
{
/**
* @var class-string
*/
private string $className;

/**
* @param class-string $className
*/
public function __construct(string $className)
{
$this->className = $className;
}

/**
* @return class-string
*/
public function className(): string
{
return $this->className;
}
}
42 changes: 42 additions & 0 deletions src/Framework/Attributes/UsesClassesThatImplementInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?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\Framework\Attributes;

use Attribute;

/**
* @immutable
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
*/
#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
final readonly class UsesClassesThatImplementInterface
{
/**
* @var class-string
*/
private string $interfaceName;

/**
* @param class-string $interfaceName
*/
public function __construct(string $interfaceName)
{
$this->interfaceName = $interfaceName;
}

/**
* @return class-string
*/
public function interfaceName(): string
{
return $this->interfaceName;
}
}
28 changes: 28 additions & 0 deletions src/Metadata/Api/CodeCoverage.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@

use function assert;
use PHPUnit\Metadata\CoversClass;
use PHPUnit\Metadata\CoversClassesThatExtendClass;
use PHPUnit\Metadata\CoversClassesThatImplementInterface;
use PHPUnit\Metadata\CoversFunction;
use PHPUnit\Metadata\CoversMethod;
use PHPUnit\Metadata\Parser\Registry;
use PHPUnit\Metadata\UsesClass;
use PHPUnit\Metadata\UsesClassesThatExtendClass;
use PHPUnit\Metadata\UsesClassesThatImplementInterface;
use PHPUnit\Metadata\UsesFunction;
use PHPUnit\Metadata\UsesMethod;
use SebastianBergmann\CodeCoverage\Test\Target\Target;
Expand Down Expand Up @@ -46,6 +50,18 @@ public function coversTargets(string $className, string $methodName): false|Targ
$targets[] = Target::forClass($metadata->className());
}

if ($metadata->isCoversClassesThatExtendClass()) {
assert($metadata instanceof CoversClassesThatExtendClass);

$targets[] = Target::forClassesThatExtendClass($metadata->className());
}

if ($metadata->isCoversClassesThatImplementInterface()) {
assert($metadata instanceof CoversClassesThatImplementInterface);

$targets[] = Target::forClassesThatImplementInterface($metadata->interfaceName());
}

if ($metadata->isCoversMethod()) {
assert($metadata instanceof CoversMethod);

Expand Down Expand Up @@ -77,6 +93,18 @@ public function usesTargets(string $className, string $methodName): TargetCollec
$targets[] = Target::forClass($metadata->className());
}

if ($metadata->isUsesClassesThatExtendClass()) {
assert($metadata instanceof UsesClassesThatExtendClass);

$targets[] = Target::forClassesThatExtendClass($metadata->className());
}

if ($metadata->isUsesClassesThatImplementInterface()) {
assert($metadata instanceof UsesClassesThatImplementInterface);

$targets[] = Target::forClassesThatImplementInterface($metadata->interfaceName());
}

if ($metadata->isUsesMethod()) {
assert($metadata instanceof UsesMethod);

Expand Down
46 changes: 46 additions & 0 deletions src/Metadata/CoversClassesThatExtendClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?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\Metadata;

/**
* @immutable
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
*/
final readonly class CoversClassesThatExtendClass extends Metadata
{
/**
* @var class-string
*/
private string $className;

/**
* @param 0|1 $level
*/
protected function __construct(int $level, string $className)
{
parent::__construct($level);

$this->className = $className;
}

public function isCoversClassesThatExtendClass(): true
{
return true;
}

/**
* @return class-string
*/
public function className(): string
{
return $this->className;
}
}
47 changes: 47 additions & 0 deletions src/Metadata/CoversClassesThatImplementInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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\Metadata;

/**
* @immutable
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
*/
final readonly class CoversClassesThatImplementInterface extends Metadata
{
/**
* @var class-string
*/
private string $interfaceName;

/**
* @param 0|1 $level
* @param class-string $interfaceName
*/
protected function __construct(int $level, string $interfaceName)
{
parent::__construct($level);

$this->interfaceName = $interfaceName;
}

public function isCoversClassesThatImplementInterface(): true
{
return true;
}

/**
* @return class-string
*/
public function interfaceName(): string
{
return $this->interfaceName;
}
}
Loading

0 comments on commit 67ae4d8

Please sign in to comment.