Skip to content

Commit

Permalink
Closes #519
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Dec 7, 2024
1 parent 02adea8 commit eca5d61
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 46 deletions.
4 changes: 4 additions & 0 deletions ChangeLog-12.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes are documented in this file using the [Keep a CHANGELOG](htt

## [12.0.0] - 2025-02-07

### Changed

* `CodeCoverage::stop()` and `CodeCoverage::append()` now expect arguments of type `TargetCollection` instead of `array` to configure code coverage targets

### Removed

* Methods `CodeCoverage::includeUncoveredFiles()` and `CodeCoverage::excludeUncoveredFiles()`
Expand Down
29 changes: 24 additions & 5 deletions src/CodeCoverage.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,11 @@ public function start(string $id, ?TestSize $size = null, bool $clear = false):
$this->cachedReport = null;
}

public function stop(bool $append = true, ?TestStatus $status = null, array|false $linesToBeCovered = [], array $linesToBeUsed = []): RawCodeCoverageData
public function stop(bool $append = true, ?TestStatus $status = null, null|false|TargetCollection $covers = null, ?TargetCollection $uses = null): RawCodeCoverageData
{
$data = $this->driver->stop();

$this->append($data, null, $append, $status, $linesToBeCovered, $linesToBeUsed);
$this->append($data, null, $append, $status, $covers, $uses);

$this->currentId = null;
$this->currentSize = null;
Expand All @@ -188,7 +188,7 @@ public function stop(bool $append = true, ?TestStatus $status = null, array|fals
* @throws TestIdMissingException
* @throws UnintentionallyCoveredCodeException
*/
public function append(RawCodeCoverageData $rawData, ?string $id = null, bool $append = true, ?TestStatus $status = null, array|false $linesToBeCovered = [], array $linesToBeUsed = []): void
public function append(RawCodeCoverageData $rawData, ?string $id = null, bool $append = true, ?TestStatus $status = null, null|false|TargetCollection $covers = null, ?TargetCollection $uses = null): void
{
if ($id === null) {
$id = $this->currentId;
Expand All @@ -198,18 +198,26 @@ public function append(RawCodeCoverageData $rawData, ?string $id = null, bool $a
throw new TestIdMissingException;
}

$this->cachedReport = null;

if ($status === null) {
$status = TestStatus::unknown();
}

if ($covers === null) {
$covers = TargetCollection::fromArray([]);
}

if ($uses === null) {
$uses = TargetCollection::fromArray([]);
}

$size = $this->currentSize;

if ($size === null) {
$size = TestSize::unknown();
}

$this->cachedReport = null;

$this->applyFilter($rawData);

$this->applyExecutableLinesFilter($rawData);
Expand All @@ -228,6 +236,17 @@ public function append(RawCodeCoverageData $rawData, ?string $id = null, bool $a
return;
}

$linesToBeCovered = false;
$linesToBeUsed = [];

if ($covers !== false) {
$linesToBeCovered = $this->targetMapper()->mapTargets($covers);
}

if ($linesToBeCovered !== false && $uses !== null) {
$linesToBeUsed = $this->targetMapper()->mapTargets($uses);
}

$this->applyCoversAndUsesFilter(
$rawData,
$linesToBeCovered,
Expand Down
132 changes: 91 additions & 41 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
namespace SebastianBergmann\CodeCoverage;

use SebastianBergmann\CodeCoverage\Data\RawCodeCoverageData;
use SebastianBergmann\CodeCoverage\Test\Target\Target;
use SebastianBergmann\CodeCoverage\Test\Target\TargetCollection;
use function array_merge;
use function range;
use function rmdir;
use function unlink;
use BankAccountTest;
use BankAccount;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use SebastianBergmann\CodeCoverage\Driver\Driver;
Expand Down Expand Up @@ -1029,7 +1031,11 @@ protected function getLineCoverageForBankAccount(): CodeCoverage
$coverage->stop(
true,
null,
[TEST_FILES_PATH . 'BankAccount.php' => range(6, 9)]
TargetCollection::fromArray(
[
Target::forMethod(BankAccount::class, 'getBalance'),
]
)
);

$coverage->start(
Expand All @@ -1039,7 +1045,11 @@ protected function getLineCoverageForBankAccount(): CodeCoverage
$coverage->stop(
true,
null,
[TEST_FILES_PATH . 'BankAccount.php' => range(27, 32)]
TargetCollection::fromArray(
[
Target::forMethod(BankAccount::class, 'withdrawMoney'),
]
)
);

$coverage->start(
Expand All @@ -1049,7 +1059,11 @@ protected function getLineCoverageForBankAccount(): CodeCoverage
$coverage->stop(
true,
null,
[TEST_FILES_PATH . 'BankAccount.php' => range(20, 25)]
TargetCollection::fromArray(
[
Target::forMethod(BankAccount::class, 'depositMoney'),
]
)
);

$coverage->start(
Expand All @@ -1059,13 +1073,13 @@ protected function getLineCoverageForBankAccount(): CodeCoverage
$coverage->stop(
true,
null,
[
TEST_FILES_PATH . 'BankAccount.php' => array_merge(
range(6, 9),
range(20, 25),
range(27, 32)
),
]
TargetCollection::fromArray(
[
Target::forMethod(BankAccount::class, 'getBalance'),
Target::forMethod(BankAccount::class, 'depositMoney'),
Target::forMethod(BankAccount::class, 'withdrawMoney'),
]
)
);

return $coverage;
Expand Down Expand Up @@ -1096,7 +1110,11 @@ protected function getPathCoverageForBankAccount(): CodeCoverage
$coverage->stop(
true,
null,
[TEST_FILES_PATH . 'BankAccount.php' => range(6, 9)]
TargetCollection::fromArray(
[
Target::forMethod(BankAccount::class, 'getBalance'),
]
)
);

$coverage->start(
Expand All @@ -1106,7 +1124,11 @@ protected function getPathCoverageForBankAccount(): CodeCoverage
$coverage->stop(
true,
null,
[TEST_FILES_PATH . 'BankAccount.php' => range(27, 32)]
TargetCollection::fromArray(
[
Target::forMethod(BankAccount::class, 'withdrawMoney'),
]
)
);

$coverage->start(
Expand All @@ -1116,7 +1138,11 @@ protected function getPathCoverageForBankAccount(): CodeCoverage
$coverage->stop(
true,
null,
[TEST_FILES_PATH . 'BankAccount.php' => range(20, 25)]
TargetCollection::fromArray(
[
Target::forMethod(BankAccount::class, 'depositMoney'),
]
)
);

$coverage->start(
Expand All @@ -1126,13 +1152,13 @@ protected function getPathCoverageForBankAccount(): CodeCoverage
$coverage->stop(
true,
null,
[
TEST_FILES_PATH . 'BankAccount.php' => array_merge(
range(6, 9),
range(20, 25),
range(27, 32)
),
]
TargetCollection::fromArray(
[
Target::forMethod(BankAccount::class, 'getBalance'),
Target::forMethod(BankAccount::class, 'depositMoney'),
Target::forMethod(BankAccount::class, 'withdrawMoney'),
]
)
);

return $coverage;
Expand Down Expand Up @@ -1188,7 +1214,11 @@ protected function getLineCoverageForBankAccountForFirstTwoTests(): CodeCoverage
$coverage->stop(
true,
null,
[TEST_FILES_PATH . 'BankAccount.php' => range(6, 9)]
TargetCollection::fromArray(
[
Target::forMethod(BankAccount::class, 'getBalance'),
]
)
);

$coverage->start(
Expand All @@ -1198,7 +1228,11 @@ protected function getLineCoverageForBankAccountForFirstTwoTests(): CodeCoverage
$coverage->stop(
true,
null,
[TEST_FILES_PATH . 'BankAccount.php' => range(27, 32)]
TargetCollection::fromArray(
[
Target::forMethod(BankAccount::class, 'withdrawMoney'),
]
)
);

return $coverage;
Expand All @@ -1225,7 +1259,11 @@ protected function getLineCoverageForBankAccountForLastTwoTests(): CodeCoverage
$coverage->stop(
true,
null,
[TEST_FILES_PATH . 'BankAccount.php' => range(20, 25)]
TargetCollection::fromArray(
[
Target::forMethod(BankAccount::class, 'depositMoney'),
]
)
);

$coverage->start(
Expand All @@ -1235,13 +1273,13 @@ protected function getLineCoverageForBankAccountForLastTwoTests(): CodeCoverage
$coverage->stop(
true,
null,
[
TEST_FILES_PATH . 'BankAccount.php' => array_merge(
range(6, 9),
range(20, 25),
range(27, 32)
),
]
TargetCollection::fromArray(
[
Target::forMethod(BankAccount::class, 'getBalance'),
Target::forMethod(BankAccount::class, 'depositMoney'),
Target::forMethod(BankAccount::class, 'withdrawMoney'),
]
)
);

return $coverage;
Expand Down Expand Up @@ -1330,7 +1368,11 @@ protected function getPathCoverageForBankAccountForFirstTwoTests(): CodeCoverage
$coverage->stop(
true,
null,
[TEST_FILES_PATH . 'BankAccount.php' => range(6, 9)]
TargetCollection::fromArray(
[
Target::forMethod(BankAccount::class, 'getBalance'),
]
)
);

$coverage->start(
Expand All @@ -1340,7 +1382,11 @@ protected function getPathCoverageForBankAccountForFirstTwoTests(): CodeCoverage
$coverage->stop(
true,
null,
[TEST_FILES_PATH . 'BankAccount.php' => range(27, 32)]
TargetCollection::fromArray(
[
Target::forMethod(BankAccount::class, 'withdrawMoney'),
]
)
);

return $coverage;
Expand All @@ -1367,7 +1413,11 @@ protected function getPathCoverageForBankAccountForLastTwoTests(): CodeCoverage
$coverage->stop(
true,
null,
[TEST_FILES_PATH . 'BankAccount.php' => range(20, 25)]
TargetCollection::fromArray(
[
Target::forMethod(BankAccount::class, 'depositMoney'),
]
)
);

$coverage->start(
Expand All @@ -1377,13 +1427,13 @@ protected function getPathCoverageForBankAccountForLastTwoTests(): CodeCoverage
$coverage->stop(
true,
null,
[
TEST_FILES_PATH . 'BankAccount.php' => array_merge(
range(6, 9),
range(20, 25),
range(27, 32)
),
]
TargetCollection::fromArray(
[
Target::forMethod(BankAccount::class, 'getBalance'),
Target::forMethod(BankAccount::class, 'depositMoney'),
Target::forMethod(BankAccount::class, 'withdrawMoney'),
]
)
);

return $coverage;
Expand Down

0 comments on commit eca5d61

Please sign in to comment.