Skip to content

Commit f1f78ba

Browse files
Closes #5733
1 parent a255c44 commit f1f78ba

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

ChangeLog-11.1.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ All notable changes of the PHPUnit 11.1 release series are documented in this fi
99
* [#5175](https://github.com/sebastianbergmann/phpunit/issues/5175): `#[CoversMethod]` and `#[UsesMethod]` attributes for more fine-grained code coverage targeting
1010
* [#5696](https://github.com/sebastianbergmann/phpunit/pull/5696): `#[DisableReturnValueGenerationForTestDoubles]` attribute for disabling return value generation for test doubles created using `createMock()`, `createMockForIntersectionOfInterfaces()`, `createPartialMock()`, `createStub()`, and `createStubForIntersectionOfInterfaces()`
1111
* [#5720](https://github.com/sebastianbergmann/phpunit/issues/5720): Support filtering using `--filter`, `--exclude-filter`, `--group`, and `--exclude-group` when listing tests using `--list-tests` and `--list-tests-xml` as well as listing groups with `--list-groups`
12+
* [#5733](https://github.com/sebastianbergmann/phpunit/issues/5733): Implicitly include (abstract) parent class(es) with `#[CoversClass]` and `#[UsesClass]` attributes
1213
* `--only-summary-for-coverage-text` CLI option to reduce the code coverage report in text format to a summary
1314
* `--show-uncovered-for-coverage-text` CLI option to expand the code coverage report in text format to include a list of uncovered files
1415

src/Metadata/Api/CodeCoverage.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
use PHPUnit\Metadata\UsesDefaultClass;
2828
use PHPUnit\Metadata\UsesFunction;
2929
use PHPUnit\Metadata\UsesMethod;
30+
use ReflectionClass;
31+
use ReflectionException;
3032
use SebastianBergmann\CodeUnit\CodeUnitCollection;
3133
use SebastianBergmann\CodeUnit\Exception as CodeUnitException;
3234
use SebastianBergmann\CodeUnit\InvalidCodeUnitException;
@@ -220,9 +222,39 @@ public function shouldCodeCoverageBeCollectedFor(string $className, string $meth
220222
private function mapToCodeUnits(CoversClass|CoversFunction|CoversMethod|UsesClass|UsesFunction|UsesMethod $metadata): CodeUnitCollection
221223
{
222224
$mapper = new Mapper;
225+
$names = [$metadata->asStringForCodeUnitMapper()];
226+
227+
if ($metadata->isCoversClass() || $metadata->isUsesClass()) {
228+
try {
229+
$reflector = new ReflectionClass($metadata->asStringForCodeUnitMapper());
230+
} catch (ReflectionException $e) {
231+
throw new InvalidCoversTargetException(
232+
sprintf(
233+
'Class "%s" is not a valid target for code coverage',
234+
$metadata->asStringForCodeUnitMapper(),
235+
),
236+
$e->getCode(),
237+
$e,
238+
);
239+
}
240+
241+
while ($reflector = $reflector->getParentClass()) {
242+
if (!$reflector->isUserDefined()) {
243+
break;
244+
}
245+
246+
$names[] = $reflector->getName();
247+
}
248+
}
249+
250+
$codeUnits = CodeUnitCollection::fromList();
223251

224252
try {
225-
return $mapper->stringToCodeUnits($metadata->asStringForCodeUnitMapper());
253+
foreach ($names as $name) {
254+
$codeUnits = $codeUnits->mergeWith(
255+
$mapper->stringToCodeUnits($name),
256+
);
257+
}
226258
} catch (CodeUnitException $e) {
227259
if ($metadata->isCoversClass() || $metadata->isUsesClass()) {
228260
if (interface_exists($metadata->className())) {
@@ -244,5 +276,7 @@ private function mapToCodeUnits(CoversClass|CoversFunction|CoversMethod|UsesClas
244276
$e,
245277
);
246278
}
279+
280+
return $codeUnits;
247281
}
248282
}

tests/unit/Metadata/Api/CodeCoverageTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public static function linesToBeCoveredProvider(): array
6363

6464
[
6565
[
66-
TEST_FILES_PATH . 'CoveredClass.php' => range(29, 46),
66+
TEST_FILES_PATH . 'CoveredClass.php' => array_merge(range(12, 27), range(29, 46)),
6767
],
6868
CoverageClassTest::class,
6969
'testSomething',
@@ -220,7 +220,7 @@ public static function linesToBeUsedProvider(): array
220220

221221
[
222222
[
223-
TEST_FILES_PATH . 'CoveredClass.php' => range(29, 46),
223+
TEST_FILES_PATH . 'CoveredClass.php' => array_merge(range(12, 27), range(29, 46)),
224224
],
225225
CoverageClassTest::class,
226226
'testSomething',

0 commit comments

Comments
 (0)