Skip to content

Commit 57d6d59

Browse files
More work on mapping code coverage targets to source locations
1 parent fc06264 commit 57d6d59

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

src/Target/Mapper.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,21 @@ private function mapTarget(Target $target): array
7171
if ($target->isClass()) {
7272
assert($target instanceof Class_);
7373

74-
if (!isset($this->map['classes'][$target->className()])) {
75-
throw new InvalidCodeCoverageTargetException('Class ' . $target->className());
74+
if (!isset($this->map['classes'][$target->asString()])) {
75+
throw new InvalidCodeCoverageTargetException('Class ' . $target->asString());
7676
}
7777

78-
return $this->map['classes'][$target->className()];
78+
return $this->map['classes'][$target->asString()];
79+
}
80+
81+
if ($target->isMethod()) {
82+
assert($target instanceof Method);
83+
84+
if (!isset($this->map['methods'][$target->asString()])) {
85+
throw new InvalidCodeCoverageTargetException('Method ' . $target->asString());
86+
}
87+
88+
return $this->map['methods'][$target->asString()];
7989
}
8090
}
8191
}

tests/tests/Target/MapperTest.php

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace SebastianBergmann\CodeCoverage\Test\Target;
1111

1212
use function array_keys;
13+
use function array_merge;
1314
use function range;
1415
use function realpath;
1516
use PHPUnit\Framework\Attributes\CoversClass;
@@ -44,6 +45,29 @@ public static function provider(): array
4445
],
4546
),
4647
],
48+
[
49+
'single method',
50+
[
51+
$file => range(37, 39),
52+
],
53+
TargetCollection::fromArray(
54+
[
55+
Target::forMethod('SebastianBergmann\\CodeCoverage\\StaticAnalysis\\ChildClass', 'six'),
56+
],
57+
),
58+
],
59+
[
60+
'multiple methods',
61+
[
62+
$file => array_merge(range(37, 39), range(41, 43)),
63+
],
64+
TargetCollection::fromArray(
65+
[
66+
Target::forMethod('SebastianBergmann\\CodeCoverage\\StaticAnalysis\\ChildClass', 'six'),
67+
Target::forMethod('SebastianBergmann\\CodeCoverage\\StaticAnalysis\\ChildClass', 'one'),
68+
],
69+
),
70+
],
4771
];
4872
}
4973

@@ -55,10 +79,19 @@ public static function invalidProvider(): array
5579
return [
5680
[
5781
'single class',
58-
'Class SebastianBergmann\CodeCoverage\StaticAnalysis\ChildClass is not a valid target for code coverage',
82+
'Class DoesNotExist is not a valid target for code coverage',
5983
TargetCollection::fromArray(
6084
[
61-
Target::forClass('SebastianBergmann\\CodeCoverage\\StaticAnalysis\\ChildClass'),
85+
Target::forClass('DoesNotExist'),
86+
],
87+
),
88+
],
89+
[
90+
'single method',
91+
'Method DoesNotExist::doesNotExist is not a valid target for code coverage',
92+
TargetCollection::fromArray(
93+
[
94+
Target::forMethod('DoesNotExist', 'doesNotExist'),
6295
],
6396
),
6497
],

0 commit comments

Comments
 (0)