Skip to content

Commit 0f52179

Browse files
Revert "Remove deprecated methods"
This reverts commit b25c249.
1 parent 8aa25b8 commit 0f52179

7 files changed

+384
-2
lines changed

ChangeLog-12.0.md

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ All notable changes are documented in this file using the [Keep a CHANGELOG](htt
1010

1111
### Removed
1212

13-
* Methods `CodeCoverage::includeUncoveredFiles()` and `CodeCoverage::excludeUncoveredFiles()`
1413
* Method `CodeCoverage::detectsDeadCode()`
1514
* Optional argument `$linesToBeUsed` of `CodeCoverage::stop()` and `CodeCoverage::append()` methods
1615
* This component is no longer supported on PHP 8.2

src/CodeCoverage.php

+14-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ final class CodeCoverage
4949
private readonly Filter $filter;
5050
private ?Mapper $targetMapper = null;
5151
private bool $checkForUnintentionallyCoveredCode = false;
52+
private bool $includeUncoveredFiles = true;
5253
private bool $ignoreDeprecatedCode = false;
5354
private ?string $currentId = null;
5455
private ?TestSize $currentSize = null;
@@ -121,7 +122,9 @@ public function filter(): Filter
121122
public function getData(bool $raw = false): ProcessedCodeCoverageData
122123
{
123124
if (!$raw) {
124-
$this->addUncoveredFilesFromFilter();
125+
if ($this->includeUncoveredFiles) {
126+
$this->addUncoveredFilesFromFilter();
127+
}
125128
}
126129

127130
return $this->data;
@@ -287,6 +290,16 @@ public function disableCheckForUnintentionallyCoveredCode(): void
287290
$this->checkForUnintentionallyCoveredCode = false;
288291
}
289292

293+
public function includeUncoveredFiles(): void
294+
{
295+
$this->includeUncoveredFiles = true;
296+
}
297+
298+
public function excludeUncoveredFiles(): void
299+
{
300+
$this->includeUncoveredFiles = false;
301+
}
302+
290303
public function enableAnnotationsForIgnoringCode(): void
291304
{
292305
$this->useAnnotationsForIgnoringCode = true;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
3+
Code Coverage Report: 
4+
 %s 
5+
 
6+
 Summary: 
7+
 Classes: 0.00% (0/1)
8+
 Methods: 75.00% (3/4)
9+
 Lines: 62.50% (5/8)
10+
11+
SomeNamespace\BankAccount
12+
Methods: ( 0/ 0) Lines: ( 0/ 0)
13+
SomeNamespace\BankAccountTrait
14+
Methods: 75.00% ( 3/ 4) Lines: 62.50% ( 5/ 8)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
3+
Code Coverage Report:
4+
%s
5+
6+
Summary:
7+
Classes: 0.00% (0/1)
8+
Methods: 75.00% (3/4)
9+
Lines: 62.50% (5/8)
10+
11+
SomeNamespace\BankAccount
12+
Methods: ( 0/ 0) Lines: ( 0/ 0)
13+
SomeNamespace\BankAccountTrait
14+
Methods: 75.00% ( 3/ 4) Lines: 62.50% ( 5/ 8)
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
namespace SomeNamespace;
3+
class BankAccount
4+
{
5+
use BankAccountTrait;
6+
}
7+
8+
trait BankAccountTrait {
9+
protected $balance = 0;
10+
11+
public function getBalance()
12+
{
13+
return $this->balance;
14+
}
15+
16+
protected function setBalance($balance)
17+
{
18+
if ($balance >= 0) {
19+
$this->balance = $balance;
20+
} else {
21+
throw new \RuntimeException;
22+
}
23+
}
24+
25+
public function depositMoney($balance)
26+
{
27+
$this->setBalance($this->getBalance() + $balance);
28+
29+
return $this->getBalance();
30+
}
31+
32+
public function withdrawMoney($balance)
33+
{
34+
$this->setBalance($this->getBalance() - $balance);
35+
36+
return $this->getBalance();
37+
}
38+
}

tests/src/TestCase.php

+264
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use SebastianBergmann\CodeCoverage\Driver\Driver;
1919
use SebastianBergmann\CodeCoverage\Test\Target\Target;
2020
use SebastianBergmann\CodeCoverage\Test\Target\TargetCollection;
21+
use SomeNamespace\BankAccountTrait;
2122

2223
abstract class TestCase extends \PHPUnit\Framework\TestCase
2324
{
@@ -1182,6 +1183,127 @@ protected function getPathCoverageForSourceWithoutNamespace(): CodeCoverage
11821183
return $coverage;
11831184
}
11841185

1186+
protected function getXdebugDataForNamespacedBankAccount()
1187+
{
1188+
return [
1189+
RawCodeCoverageData::fromXdebugWithoutPathCoverage([
1190+
TEST_FILES_PATH . 'NamespacedBankAccount.php' => [
1191+
13 => 1,
1192+
14 => -2,
1193+
18 => -1,
1194+
19 => -1,
1195+
20 => -1,
1196+
21 => -1,
1197+
23 => -1,
1198+
27 => -1,
1199+
29 => -1,
1200+
30 => -2,
1201+
34 => -1,
1202+
36 => -1,
1203+
37 => -2,
1204+
],
1205+
]),
1206+
RawCodeCoverageData::fromXdebugWithoutPathCoverage([
1207+
TEST_FILES_PATH . 'NamespacedBankAccount.php' => [
1208+
13 => 1,
1209+
18 => 1,
1210+
21 => 1,
1211+
34 => 1,
1212+
],
1213+
]),
1214+
RawCodeCoverageData::fromXdebugWithoutPathCoverage([
1215+
TEST_FILES_PATH . 'NamespacedBankAccount.php' => [
1216+
13 => 1,
1217+
18 => 1,
1218+
21 => 1,
1219+
27 => 1,
1220+
],
1221+
]),
1222+
RawCodeCoverageData::fromXdebugWithoutPathCoverage([
1223+
TEST_FILES_PATH . 'NamespacedBankAccount.php' => [
1224+
13 => 1,
1225+
18 => 1,
1226+
19 => 1,
1227+
20 => 1,
1228+
23 => 1,
1229+
27 => 1,
1230+
29 => 1,
1231+
34 => 1,
1232+
36 => 1,
1233+
],
1234+
]),
1235+
];
1236+
}
1237+
1238+
protected function getLineCoverageForNamespacedBankAccount(): CodeCoverage
1239+
{
1240+
$data = $this->getXdebugDataForNamespacedBankAccount();
1241+
1242+
$stub = $this->createStub(Driver::class);
1243+
1244+
$stub->method('stop')
1245+
->willReturn(...$data);
1246+
1247+
$filter = new Filter;
1248+
$filter->includeFile(TEST_FILES_PATH . 'NamespacedBankAccount.php');
1249+
1250+
$coverage = new CodeCoverage($stub, $filter);
1251+
1252+
$coverage->start(
1253+
'BankAccountTest::testBalanceIsInitiallyZero',
1254+
null,
1255+
true,
1256+
);
1257+
1258+
$coverage->stop(
1259+
true,
1260+
null,
1261+
TargetCollection::fromArray([
1262+
Target::forMethod(BankAccountTrait::class, 'getBalance'),
1263+
]),
1264+
);
1265+
1266+
$coverage->start(
1267+
'BankAccountTest::testBalanceCannotBecomeNegative',
1268+
);
1269+
1270+
$coverage->stop(
1271+
true,
1272+
null,
1273+
TargetCollection::fromArray([
1274+
Target::forMethod(BankAccountTrait::class, 'withdrawMoney'),
1275+
]),
1276+
);
1277+
1278+
$coverage->start(
1279+
'BankAccountTest::testBalanceCannotBecomeNegative2',
1280+
);
1281+
1282+
$coverage->stop(
1283+
true,
1284+
null,
1285+
TargetCollection::fromArray([
1286+
Target::forMethod(BankAccountTrait::class, 'depositMoney'),
1287+
]),
1288+
);
1289+
1290+
$coverage->start(
1291+
'BankAccountTest::testDepositWithdrawMoney',
1292+
);
1293+
1294+
$coverage->stop(
1295+
true,
1296+
null,
1297+
TargetCollection::fromArray([
1298+
Target::forMethod(BankAccountTrait::class, 'getBalance'),
1299+
Target::forMethod(BankAccountTrait::class, 'depositMoney'),
1300+
Target::forMethod(BankAccountTrait::class, 'withdrawMoney'),
1301+
]),
1302+
);
1303+
1304+
return $coverage;
1305+
}
1306+
11851307
protected function getLineCoverageForBankAccountForFirstTwoTests(): CodeCoverage
11861308
{
11871309
$data = $this->getLineCoverageXdebugDataForBankAccount();
@@ -1791,4 +1913,146 @@ protected function removeTemporaryFiles(): void
17911913
$fileInfo->isDir() ? rmdir($pathname) : unlink($pathname);
17921914
}
17931915
}
1916+
1917+
protected function getCoverageForFilesWithUncoveredIncluded(): CodeCoverage
1918+
{
1919+
$data = $this->getLineCoverageXdebugDataForBankAccount();
1920+
1921+
$stub = $this->createStub(Driver::class);
1922+
1923+
$stub->method('stop')
1924+
->willReturn(...$data);
1925+
1926+
$filter = new Filter;
1927+
$filter->includeFile(TEST_FILES_PATH . 'BankAccount.php');
1928+
$filter->includeFile(TEST_FILES_PATH . 'NamespacedBankAccount.php');
1929+
1930+
$coverage = new CodeCoverage($stub, $filter);
1931+
$coverage->includeUncoveredFiles();
1932+
1933+
$coverage->start(
1934+
'BankAccountTest::testBalanceIsInitiallyZero',
1935+
null,
1936+
true,
1937+
);
1938+
1939+
$coverage->stop(
1940+
true,
1941+
null,
1942+
TargetCollection::fromArray([
1943+
Target::forMethod(BankAccount::class, 'getBalance'),
1944+
]),
1945+
);
1946+
1947+
$coverage->start(
1948+
'BankAccountTest::testBalanceCannotBecomeNegative',
1949+
);
1950+
1951+
$coverage->stop(
1952+
true,
1953+
null,
1954+
TargetCollection::fromArray([
1955+
Target::forMethod(BankAccount::class, 'withdrawMoney'),
1956+
]),
1957+
);
1958+
1959+
$coverage->start(
1960+
'BankAccountTest::testBalanceCannotBecomeNegative2',
1961+
);
1962+
1963+
$coverage->stop(
1964+
true,
1965+
null,
1966+
TargetCollection::fromArray([
1967+
Target::forMethod(BankAccount::class, 'depositMoney'),
1968+
]),
1969+
);
1970+
1971+
$coverage->start(
1972+
'BankAccountTest::testDepositWithdrawMoney',
1973+
);
1974+
1975+
$coverage->stop(
1976+
true,
1977+
null,
1978+
TargetCollection::fromArray([
1979+
Target::forMethod(BankAccount::class, 'getBalance'),
1980+
Target::forMethod(BankAccount::class, 'depositMoney'),
1981+
Target::forMethod(BankAccount::class, 'withdrawMoney'),
1982+
]),
1983+
);
1984+
1985+
return $coverage;
1986+
}
1987+
1988+
protected function getCoverageForFilesWithUncoveredExcluded(): CodeCoverage
1989+
{
1990+
$data = $this->getLineCoverageXdebugDataForBankAccount();
1991+
1992+
$stub = $this->createStub(Driver::class);
1993+
1994+
$stub->method('stop')
1995+
->willReturn(...$data);
1996+
1997+
$filter = new Filter;
1998+
$filter->includeFile(TEST_FILES_PATH . 'BankAccount.php');
1999+
$filter->includeFile(TEST_FILES_PATH . 'NamespacedBankAccount.php');
2000+
2001+
$coverage = new CodeCoverage($stub, $filter);
2002+
$coverage->excludeUncoveredFiles();
2003+
2004+
$coverage->start(
2005+
'BankAccountTest::testBalanceIsInitiallyZero',
2006+
null,
2007+
true,
2008+
);
2009+
2010+
$coverage->stop(
2011+
true,
2012+
null,
2013+
TargetCollection::fromArray([
2014+
Target::forMethod(BankAccount::class, 'getBalance'),
2015+
]),
2016+
);
2017+
2018+
$coverage->start(
2019+
'BankAccountTest::testBalanceCannotBecomeNegative',
2020+
);
2021+
2022+
$coverage->stop(
2023+
true,
2024+
null,
2025+
TargetCollection::fromArray([
2026+
Target::forMethod(BankAccount::class, 'withdrawMoney'),
2027+
]),
2028+
);
2029+
2030+
$coverage->start(
2031+
'BankAccountTest::testBalanceCannotBecomeNegative2',
2032+
);
2033+
2034+
$coverage->stop(
2035+
true,
2036+
null,
2037+
TargetCollection::fromArray([
2038+
Target::forMethod(BankAccount::class, 'depositMoney'),
2039+
]),
2040+
);
2041+
2042+
$coverage->start(
2043+
'BankAccountTest::testDepositWithdrawMoney',
2044+
);
2045+
2046+
$coverage->stop(
2047+
true,
2048+
null,
2049+
TargetCollection::fromArray([
2050+
Target::forMethod(BankAccount::class, 'getBalance'),
2051+
Target::forMethod(BankAccount::class, 'depositMoney'),
2052+
Target::forMethod(BankAccount::class, 'withdrawMoney'),
2053+
]),
2054+
);
2055+
2056+
return $coverage;
2057+
}
17942058
}

0 commit comments

Comments
 (0)