Skip to content

Commit

Permalink
Revert "Remove deprecated methods"
Browse files Browse the repository at this point in the history
This reverts commit b25c249.
  • Loading branch information
sebastianbergmann committed Feb 6, 2025
1 parent 8aa25b8 commit 0f52179
Show file tree
Hide file tree
Showing 7 changed files with 384 additions and 2 deletions.
1 change: 0 additions & 1 deletion ChangeLog-12.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ All notable changes are documented in this file using the [Keep a CHANGELOG](htt

### Removed

* Methods `CodeCoverage::includeUncoveredFiles()` and `CodeCoverage::excludeUncoveredFiles()`
* Method `CodeCoverage::detectsDeadCode()`
* Optional argument `$linesToBeUsed` of `CodeCoverage::stop()` and `CodeCoverage::append()` methods
* This component is no longer supported on PHP 8.2
Expand Down
15 changes: 14 additions & 1 deletion src/CodeCoverage.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ final class CodeCoverage
private readonly Filter $filter;
private ?Mapper $targetMapper = null;
private bool $checkForUnintentionallyCoveredCode = false;
private bool $includeUncoveredFiles = true;
private bool $ignoreDeprecatedCode = false;
private ?string $currentId = null;
private ?TestSize $currentSize = null;
Expand Down Expand Up @@ -121,7 +122,9 @@ public function filter(): Filter
public function getData(bool $raw = false): ProcessedCodeCoverageData
{
if (!$raw) {
$this->addUncoveredFilesFromFilter();
if ($this->includeUncoveredFiles) {
$this->addUncoveredFilesFromFilter();
}
}

return $this->data;
Expand Down Expand Up @@ -287,6 +290,16 @@ public function disableCheckForUnintentionallyCoveredCode(): void
$this->checkForUnintentionallyCoveredCode = false;
}

public function includeUncoveredFiles(): void
{
$this->includeUncoveredFiles = true;
}

public function excludeUncoveredFiles(): void
{
$this->includeUncoveredFiles = false;
}

public function enableAnnotationsForIgnoringCode(): void
{
$this->useAnnotationsForIgnoringCode = true;
Expand Down
14 changes: 14 additions & 0 deletions tests/_files/NamespacedBankAccount-text-with-colors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@


Code Coverage Report: 
 %s 
 
 Summary: 
 Classes: 0.00% (0/1)
 Methods: 75.00% (3/4)
 Lines: 62.50% (5/8)

SomeNamespace\BankAccount
Methods: ( 0/ 0) Lines: ( 0/ 0)
SomeNamespace\BankAccountTrait
Methods: 75.00% ( 3/ 4) Lines: 62.50% ( 5/ 8)
14 changes: 14 additions & 0 deletions tests/_files/NamespacedBankAccount-text.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@


Code Coverage Report:
%s

Summary:
Classes: 0.00% (0/1)
Methods: 75.00% (3/4)
Lines: 62.50% (5/8)

SomeNamespace\BankAccount
Methods: ( 0/ 0) Lines: ( 0/ 0)
SomeNamespace\BankAccountTrait
Methods: 75.00% ( 3/ 4) Lines: 62.50% ( 5/ 8)
38 changes: 38 additions & 0 deletions tests/_files/NamespacedBankAccount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
namespace SomeNamespace;
class BankAccount
{
use BankAccountTrait;
}

trait BankAccountTrait {
protected $balance = 0;

public function getBalance()
{
return $this->balance;
}

protected function setBalance($balance)
{
if ($balance >= 0) {
$this->balance = $balance;
} else {
throw new \RuntimeException;
}
}

public function depositMoney($balance)
{
$this->setBalance($this->getBalance() + $balance);

return $this->getBalance();
}

public function withdrawMoney($balance)
{
$this->setBalance($this->getBalance() - $balance);

return $this->getBalance();
}
}
264 changes: 264 additions & 0 deletions tests/src/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use SebastianBergmann\CodeCoverage\Driver\Driver;
use SebastianBergmann\CodeCoverage\Test\Target\Target;
use SebastianBergmann\CodeCoverage\Test\Target\TargetCollection;
use SomeNamespace\BankAccountTrait;

abstract class TestCase extends \PHPUnit\Framework\TestCase
{
Expand Down Expand Up @@ -1182,6 +1183,127 @@ protected function getPathCoverageForSourceWithoutNamespace(): CodeCoverage
return $coverage;
}

protected function getXdebugDataForNamespacedBankAccount()
{
return [
RawCodeCoverageData::fromXdebugWithoutPathCoverage([
TEST_FILES_PATH . 'NamespacedBankAccount.php' => [
13 => 1,
14 => -2,
18 => -1,
19 => -1,
20 => -1,
21 => -1,
23 => -1,
27 => -1,
29 => -1,
30 => -2,
34 => -1,
36 => -1,
37 => -2,
],
]),
RawCodeCoverageData::fromXdebugWithoutPathCoverage([
TEST_FILES_PATH . 'NamespacedBankAccount.php' => [
13 => 1,
18 => 1,
21 => 1,
34 => 1,
],
]),
RawCodeCoverageData::fromXdebugWithoutPathCoverage([
TEST_FILES_PATH . 'NamespacedBankAccount.php' => [
13 => 1,
18 => 1,
21 => 1,
27 => 1,
],
]),
RawCodeCoverageData::fromXdebugWithoutPathCoverage([
TEST_FILES_PATH . 'NamespacedBankAccount.php' => [
13 => 1,
18 => 1,
19 => 1,
20 => 1,
23 => 1,
27 => 1,
29 => 1,
34 => 1,
36 => 1,
],
]),
];
}

protected function getLineCoverageForNamespacedBankAccount(): CodeCoverage
{
$data = $this->getXdebugDataForNamespacedBankAccount();

$stub = $this->createStub(Driver::class);

$stub->method('stop')
->willReturn(...$data);

$filter = new Filter;
$filter->includeFile(TEST_FILES_PATH . 'NamespacedBankAccount.php');

$coverage = new CodeCoverage($stub, $filter);

$coverage->start(
'BankAccountTest::testBalanceIsInitiallyZero',
null,
true,
);

$coverage->stop(
true,
null,
TargetCollection::fromArray([
Target::forMethod(BankAccountTrait::class, 'getBalance'),
]),
);

$coverage->start(
'BankAccountTest::testBalanceCannotBecomeNegative',
);

$coverage->stop(
true,
null,
TargetCollection::fromArray([
Target::forMethod(BankAccountTrait::class, 'withdrawMoney'),
]),
);

$coverage->start(
'BankAccountTest::testBalanceCannotBecomeNegative2',
);

$coverage->stop(
true,
null,
TargetCollection::fromArray([
Target::forMethod(BankAccountTrait::class, 'depositMoney'),
]),
);

$coverage->start(
'BankAccountTest::testDepositWithdrawMoney',
);

$coverage->stop(
true,
null,
TargetCollection::fromArray([
Target::forMethod(BankAccountTrait::class, 'getBalance'),
Target::forMethod(BankAccountTrait::class, 'depositMoney'),
Target::forMethod(BankAccountTrait::class, 'withdrawMoney'),
]),
);

return $coverage;
}

protected function getLineCoverageForBankAccountForFirstTwoTests(): CodeCoverage
{
$data = $this->getLineCoverageXdebugDataForBankAccount();
Expand Down Expand Up @@ -1791,4 +1913,146 @@ protected function removeTemporaryFiles(): void
$fileInfo->isDir() ? rmdir($pathname) : unlink($pathname);
}
}

protected function getCoverageForFilesWithUncoveredIncluded(): CodeCoverage
{
$data = $this->getLineCoverageXdebugDataForBankAccount();

$stub = $this->createStub(Driver::class);

$stub->method('stop')
->willReturn(...$data);

$filter = new Filter;
$filter->includeFile(TEST_FILES_PATH . 'BankAccount.php');
$filter->includeFile(TEST_FILES_PATH . 'NamespacedBankAccount.php');

$coverage = new CodeCoverage($stub, $filter);
$coverage->includeUncoveredFiles();

$coverage->start(
'BankAccountTest::testBalanceIsInitiallyZero',
null,
true,
);

$coverage->stop(
true,
null,
TargetCollection::fromArray([
Target::forMethod(BankAccount::class, 'getBalance'),
]),
);

$coverage->start(
'BankAccountTest::testBalanceCannotBecomeNegative',
);

$coverage->stop(
true,
null,
TargetCollection::fromArray([
Target::forMethod(BankAccount::class, 'withdrawMoney'),
]),
);

$coverage->start(
'BankAccountTest::testBalanceCannotBecomeNegative2',
);

$coverage->stop(
true,
null,
TargetCollection::fromArray([
Target::forMethod(BankAccount::class, 'depositMoney'),
]),
);

$coverage->start(
'BankAccountTest::testDepositWithdrawMoney',
);

$coverage->stop(
true,
null,
TargetCollection::fromArray([
Target::forMethod(BankAccount::class, 'getBalance'),
Target::forMethod(BankAccount::class, 'depositMoney'),
Target::forMethod(BankAccount::class, 'withdrawMoney'),
]),
);

return $coverage;
}

protected function getCoverageForFilesWithUncoveredExcluded(): CodeCoverage
{
$data = $this->getLineCoverageXdebugDataForBankAccount();

$stub = $this->createStub(Driver::class);

$stub->method('stop')
->willReturn(...$data);

$filter = new Filter;
$filter->includeFile(TEST_FILES_PATH . 'BankAccount.php');
$filter->includeFile(TEST_FILES_PATH . 'NamespacedBankAccount.php');

$coverage = new CodeCoverage($stub, $filter);
$coverage->excludeUncoveredFiles();

$coverage->start(
'BankAccountTest::testBalanceIsInitiallyZero',
null,
true,
);

$coverage->stop(
true,
null,
TargetCollection::fromArray([
Target::forMethod(BankAccount::class, 'getBalance'),
]),
);

$coverage->start(
'BankAccountTest::testBalanceCannotBecomeNegative',
);

$coverage->stop(
true,
null,
TargetCollection::fromArray([
Target::forMethod(BankAccount::class, 'withdrawMoney'),
]),
);

$coverage->start(
'BankAccountTest::testBalanceCannotBecomeNegative2',
);

$coverage->stop(
true,
null,
TargetCollection::fromArray([
Target::forMethod(BankAccount::class, 'depositMoney'),
]),
);

$coverage->start(
'BankAccountTest::testDepositWithdrawMoney',
);

$coverage->stop(
true,
null,
TargetCollection::fromArray([
Target::forMethod(BankAccount::class, 'getBalance'),
Target::forMethod(BankAccount::class, 'depositMoney'),
Target::forMethod(BankAccount::class, 'withdrawMoney'),
]),
);

return $coverage;
}
}
Loading

0 comments on commit 0f52179

Please sign in to comment.