Skip to content

Commit 09a0cfa

Browse files
Target parent class(es) with #[CoversClass] attribute
1 parent 13088f5 commit 09a0cfa

File tree

7 files changed

+137
-8
lines changed

7 files changed

+137
-8
lines changed

ChangeLog-12.0.md

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
All notable changes are documented in this file using the [Keep a CHANGELOG](http://keepachangelog.com/) principles.
44

5+
## [12.0.3] - 2025-MM-DD
6+
7+
### Fixed
8+
9+
* `#CoversClass` does not target code in parent class(es)
10+
511
## [12.0.2] - 2025-02-08
612

713
### Changed
@@ -27,6 +33,7 @@ All notable changes are documented in this file using the [Keep a CHANGELOG](htt
2733
* This component is no longer supported on PHP 8.2
2834
* This component no longer supports Xdebug versions before Xdebug 3.1
2935

36+
[12.0.3]: https://github.com/sebastianbergmann/php-code-coverage/compare/12.0.2...main
3037
[12.0.2]: https://github.com/sebastianbergmann/php-code-coverage/compare/12.0.1...12.0.2
3138
[12.0.1]: https://github.com/sebastianbergmann/php-code-coverage/compare/12.0.0...12.0.1
3239
[12.0.0]: https://github.com/sebastianbergmann/php-code-coverage/compare/11.0...12.0.0

src/Target/MapBuilder.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use function array_keys;
1313
use function array_merge;
14+
use function array_merge_recursive;
1415
use function array_slice;
1516
use function array_unique;
1617
use function count;
@@ -156,11 +157,16 @@ public function build(Filter $filter, FileAnalyser $analyser): array
156157
continue;
157158
}
158159

159-
if (!isset($classesThatExtendClass[$class->parentClass()])) {
160-
continue;
160+
if (isset($classes[$class->parentClass()])) {
161+
$classes[$class->namespacedName()] = array_merge_recursive(
162+
$classes[$class->namespacedName()],
163+
$classes[$class->parentClass()],
164+
);
161165
}
162166

163-
$this->process($classesThatExtendClass, $class->parentClass(), $class->file(), $class->startLine(), $class->endLine());
167+
if (isset($classesThatExtendClass[$class->parentClass()])) {
168+
$this->process($classesThatExtendClass, $class->parentClass(), $class->file(), $class->startLine(), $class->endLine());
169+
}
164170
}
165171

166172
foreach (array_keys($classesThatImplementInterface) as $className) {

tests/_files/Target/ChildClass.php

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php declare(strict_types=1);
2+
namespace SebastianBergmann\CodeCoverage\TestFixture\Target;
3+
4+
final class ChildClass extends ParentClass
5+
{
6+
public function three(): void
7+
{
8+
}
9+
}
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php declare(strict_types=1);
2+
namespace SebastianBergmann\CodeCoverage\TestFixture\Target;
3+
4+
abstract class GrandParentClass
5+
{
6+
public function one(): void
7+
{
8+
}
9+
}

tests/_files/Target/ParentClass.php

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php declare(strict_types=1);
2+
namespace SebastianBergmann\CodeCoverage\TestFixture\Target;
3+
4+
abstract class ParentClass extends GrandParentClass
5+
{
6+
public function two(): void
7+
{
8+
}
9+
}

tests/tests/Target/MapBuilderTest.php

+93-5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
use PHPUnit\Framework\TestCase;
1919
use SebastianBergmann\CodeCoverage\Filter;
2020
use SebastianBergmann\CodeCoverage\StaticAnalysis\ParsingFileAnalyser;
21+
use SebastianBergmann\CodeCoverage\TestFixture\Target\ChildClass;
22+
use SebastianBergmann\CodeCoverage\TestFixture\Target\GrandParentClass;
23+
use SebastianBergmann\CodeCoverage\TestFixture\Target\ParentClass;
2124
use SebastianBergmann\CodeCoverage\TestFixture\Target\T1;
2225
use SebastianBergmann\CodeCoverage\TestFixture\Target\T2;
2326
use SebastianBergmann\CodeCoverage\TestFixture\Target\TargetEnumeration;
@@ -36,11 +39,14 @@ final class MapBuilderTest extends TestCase
3639
*/
3740
public static function provider(): array
3841
{
39-
$file = realpath(__DIR__ . '/../../_files/source_with_interfaces_classes_traits_functions.php');
40-
$traitOne = realpath(__DIR__ . '/../../_files/Target/TraitOne.php');
41-
$traitTwo = realpath(__DIR__ . '/../../_files/Target/TraitTwo.php');
42-
$twoTraits = realpath(__DIR__ . '/../../_files/Target/two_traits.php');
43-
$enum = realpath(__DIR__ . '/../../_files/Target/TargetEnumeration.php');
42+
$file = realpath(__DIR__ . '/../../_files/source_with_interfaces_classes_traits_functions.php');
43+
$traitOne = realpath(__DIR__ . '/../../_files/Target/TraitOne.php');
44+
$traitTwo = realpath(__DIR__ . '/../../_files/Target/TraitTwo.php');
45+
$twoTraits = realpath(__DIR__ . '/../../_files/Target/two_traits.php');
46+
$enum = realpath(__DIR__ . '/../../_files/Target/TargetEnumeration.php');
47+
$grandParentClass = realpath(__DIR__ . '/../../_files/Target/GrandParentClass.php');
48+
$parentClass = realpath(__DIR__ . '/../../_files/Target/ParentClass.php');
49+
$childClass = realpath(__DIR__ . '/../../_files/Target/ChildClass.php');
4450

4551
return [
4652
'generic' => [
@@ -84,6 +90,7 @@ public static function provider(): array
8490
$file => array_merge(
8591
range(33, 52),
8692
range(19, 24),
93+
range(26, 31),
8794
),
8895
],
8996
],
@@ -319,6 +326,87 @@ public static function provider(): array
319326
$enum,
320327
],
321328
],
329+
'class with parent classes' => [
330+
[
331+
'namespaces' => [
332+
'SebastianBergmann' => [
333+
$grandParentClass => range(4, 9),
334+
$parentClass => range(4, 9),
335+
$childClass => range(4, 9),
336+
],
337+
'SebastianBergmann\CodeCoverage' => [
338+
$grandParentClass => range(4, 9),
339+
$parentClass => range(4, 9),
340+
$childClass => range(4, 9),
341+
],
342+
'SebastianBergmann\CodeCoverage\TestFixture' => [
343+
$grandParentClass => range(4, 9),
344+
$parentClass => range(4, 9),
345+
$childClass => range(4, 9),
346+
],
347+
'SebastianBergmann\CodeCoverage\TestFixture\Target' => [
348+
$grandParentClass => range(4, 9),
349+
$parentClass => range(4, 9),
350+
$childClass => range(4, 9),
351+
],
352+
],
353+
'traits' => [
354+
],
355+
'classes' => [
356+
GrandParentClass::class => [
357+
$grandParentClass => range(4, 9),
358+
],
359+
ParentClass::class => [
360+
$parentClass => range(4, 9),
361+
$grandParentClass => range(4, 9),
362+
],
363+
ChildClass::class => [
364+
$childClass => range(4, 9),
365+
$parentClass => range(4, 9),
366+
$grandParentClass => range(4, 9),
367+
],
368+
],
369+
'classesThatExtendClass' => [
370+
GrandParentClass::class => [
371+
$parentClass => range(4, 9),
372+
],
373+
ParentClass::class => [
374+
$childClass => range(4, 9),
375+
],
376+
],
377+
'classesThatImplementInterface' => [
378+
],
379+
'methods' => [
380+
GrandParentClass::class . '::one' => [
381+
$grandParentClass => range(6, 8),
382+
],
383+
ParentClass::class . '::two' => [
384+
$parentClass => range(6, 8),
385+
],
386+
ChildClass::class . '::three' => [
387+
$childClass => range(6, 8),
388+
],
389+
],
390+
'functions' => [
391+
],
392+
'reverseLookup' => [
393+
$grandParentClass . ':6' => 'SebastianBergmann\CodeCoverage\TestFixture\Target\GrandParentClass::one',
394+
$grandParentClass . ':7' => 'SebastianBergmann\CodeCoverage\TestFixture\Target\GrandParentClass::one',
395+
$grandParentClass . ':8' => 'SebastianBergmann\CodeCoverage\TestFixture\Target\GrandParentClass::one',
396+
$parentClass . ':6' => 'SebastianBergmann\CodeCoverage\TestFixture\Target\ParentClass::two',
397+
$parentClass . ':7' => 'SebastianBergmann\CodeCoverage\TestFixture\Target\ParentClass::two',
398+
$parentClass . ':8' => 'SebastianBergmann\CodeCoverage\TestFixture\Target\ParentClass::two',
399+
$childClass . ':6' => 'SebastianBergmann\CodeCoverage\TestFixture\Target\ChildClass::three',
400+
$childClass . ':7' => 'SebastianBergmann\CodeCoverage\TestFixture\Target\ChildClass::three',
401+
$childClass . ':8' => 'SebastianBergmann\CodeCoverage\TestFixture\Target\ChildClass::three',
402+
],
403+
],
404+
[
405+
$grandParentClass,
406+
$parentClass,
407+
$childClass,
408+
],
409+
],
322410
];
323411
}
324412

tests/tests/Target/MapperTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public static function provider(): array
4343
$file => array_merge(
4444
range(33, 52),
4545
range(19, 24),
46+
range(26, 31),
4647
),
4748
],
4849
TargetCollection::fromArray(

0 commit comments

Comments
 (0)