Skip to content

Commit 419772a

Browse files
author
mstarikov
committed
Delivered new option to omit Uses strict coverage checking
1 parent 77a1ba8 commit 419772a

File tree

6 files changed

+213
-17
lines changed

6 files changed

+213
-17
lines changed

src/CodeCoverage.php

Lines changed: 66 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ class CodeCoverage
6666
*/
6767
private $checkForMissingCoversAnnotation = false;
6868

69+
/**
70+
* @var bool
71+
*/
72+
private $checkForMissingUsesAnnotation = false;
73+
6974
/**
7075
* @var bool
7176
*/
@@ -339,7 +344,7 @@ public function append(array $data, $id = null, $append = true, $linesToBeCovere
339344
}
340345

341346
if ($id != 'UNCOVERED_FILES_FROM_WHITELIST') {
342-
$this->applyCoversAnnotationFilter(
347+
$this->applyAnnotationFilter(
343348
$data,
344349
$linesToBeCovered,
345350
$linesToBeUsed
@@ -503,6 +508,23 @@ public function setCheckForMissingCoversAnnotation($flag)
503508
$this->checkForMissingCoversAnnotation = $flag;
504509
}
505510

511+
/**
512+
* @param bool $flag
513+
*
514+
* @throws InvalidArgumentException
515+
*/
516+
public function setCheckForMissingUsesAnnotation($flag)
517+
{
518+
if (!is_bool($flag)) {
519+
throw InvalidArgumentException::create(
520+
1,
521+
'boolean'
522+
);
523+
}
524+
525+
$this->checkForMissingUsesAnnotation = $flag;
526+
}
527+
506528
/**
507529
* @param bool $flag
508530
*
@@ -617,7 +639,7 @@ public function setUnintentionallyCoveredSubclassesWhitelist(array $whitelist)
617639
* @throws MissingCoversAnnotationException
618640
* @throws UnintentionallyCoveredCodeException
619641
*/
620-
private function applyCoversAnnotationFilter(array &$data, $linesToBeCovered, array $linesToBeUsed)
642+
private function applyAnnotationFilter(array &$data, $linesToBeCovered, array $linesToBeUsed)
621643
{
622644
if ($linesToBeCovered === false ||
623645
($this->forceCoversAnnotation && empty($linesToBeCovered))) {
@@ -630,10 +652,6 @@ private function applyCoversAnnotationFilter(array &$data, $linesToBeCovered, ar
630652
return;
631653
}
632654

633-
if (empty($linesToBeCovered)) {
634-
return;
635-
}
636-
637655
if ($this->checkForUnintentionallyCoveredCode &&
638656
(!$this->currentId instanceof TestCase ||
639657
(!$this->currentId->isMedium() && !$this->currentId->isLarge()))) {
@@ -932,6 +950,17 @@ private function performUnintentionallyCoveredCodeCheck(array &$data, array $lin
932950
$linesToBeUsed
933951
);
934952

953+
$allowedCoveredFilesOrClasses = $this->getAllowedFilesOrClasses(
954+
$linesToBeCovered
955+
);
956+
957+
$allowedClasses = [];
958+
foreach ($allowedCoveredFilesOrClasses as $coveredClassOrFile) {
959+
if (is_file($coveredClassOrFile)) continue;
960+
961+
$allowedClasses[] = $coveredClassOrFile;
962+
}
963+
935964
$unintentionallyCoveredUnits = [];
936965

937966
foreach ($data as $file => $_data) {
@@ -942,7 +971,7 @@ private function performUnintentionallyCoveredCodeCheck(array &$data, array $lin
942971
}
943972
}
944973

945-
$unintentionallyCoveredUnits = $this->processUnintentionallyCoveredUnits($unintentionallyCoveredUnits);
974+
$unintentionallyCoveredUnits = $this->processUnintentionallyCoveredUnits($unintentionallyCoveredUnits, $allowedClasses);
946975

947976
if (!empty($unintentionallyCoveredUnits)) {
948977
throw new UnintentionallyCoveredCodeException(
@@ -1027,6 +1056,22 @@ private function getAllowedLines(array $linesToBeCovered, array $linesToBeUsed)
10271056
return $allowedLines;
10281057
}
10291058

1059+
/**
1060+
* @param array $linesToBeCovered
1061+
*
1062+
* @return array
1063+
*/
1064+
private function getAllowedFilesOrClasses(array $linesToBeCovered)
1065+
{
1066+
$allowedFilesOrClasses = [];
1067+
1068+
foreach (array_keys($linesToBeCovered) as $fullFilePath) {
1069+
$allowedFilesOrClasses[] = $this->wizard->lookupClass($fullFilePath);
1070+
}
1071+
1072+
return array_unique($allowedFilesOrClasses);
1073+
}
1074+
10301075
/**
10311076
* @return Driver
10321077
*
@@ -1051,10 +1096,11 @@ private function selectDriver()
10511096

10521097
/**
10531098
* @param array $unintentionallyCoveredUnits
1099+
* @param array $allowedCoveredClasses
10541100
*
10551101
* @return array
10561102
*/
1057-
private function processUnintentionallyCoveredUnits(array $unintentionallyCoveredUnits)
1103+
private function processUnintentionallyCoveredUnits(array $unintentionallyCoveredUnits, array $allowedCoveredClasses = [])
10581104
{
10591105
$unintentionallyCoveredUnits = array_unique($unintentionallyCoveredUnits);
10601106
sort($unintentionallyCoveredUnits);
@@ -1066,6 +1112,18 @@ private function processUnintentionallyCoveredUnits(array $unintentionallyCovere
10661112
continue;
10671113
}
10681114

1115+
if (!$this->checkForMissingUsesAnnotation && !in_array($unit[0], $allowedCoveredClasses))
1116+
{
1117+
unset($unintentionallyCoveredUnits[$k]);
1118+
continue;
1119+
}
1120+
1121+
if (!$this->checkForMissingCoversAnnotation && in_array($unit[0], $allowedCoveredClasses))
1122+
{
1123+
unset($unintentionallyCoveredUnits[$k]);
1124+
continue;
1125+
}
1126+
10691127
$class = new \ReflectionClass($unit[0]);
10701128

10711129
foreach ($this->unintentionallyCoveredSubclassesWhitelist as $whitelisted) {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/*
3+
* This file is part of the php-code-coverage package.
4+
*
5+
* (c) Sebastian Bergmann <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace SebastianBergmann\CodeCoverage;
12+
13+
/**
14+
* Exception that is raised when @covers must be used but is not.
15+
*/
16+
class MissingUsesAnnotationException extends RuntimeException
17+
{
18+
}

tests/TestCase.php

Lines changed: 97 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,33 @@ protected function getXdebugDataForBankAccount()
3838
15 => -1,
3939
16 => -1,
4040
18 => -1,
41-
22 => -1,
4241
24 => -1,
43-
25 => -2,
44-
29 => -1,
42+
26 => -1,
43+
27 => -2,
4544
31 => -1,
46-
32 => -2
45+
33 => -1,
46+
34 => -2
4747
]
4848
],
4949
[
5050
TEST_FILES_PATH . 'BankAccount.php' => [
5151
8 => 1,
5252
13 => 1,
5353
16 => 1,
54-
29 => 1,
54+
31 => 1,
5555
]
5656
],
5757
[
5858
TEST_FILES_PATH . 'BankAccount.php' => [
5959
8 => 1,
6060
13 => 1,
6161
16 => 1,
62-
22 => 1,
62+
24 => 1,
63+
],
64+
TEST_FILES_PATH . 'CalculatorSum.php' => [
65+
10 => 1,
66+
11 => 1,
67+
22 => 1
6368
]
6469
],
6570
[
@@ -69,15 +74,98 @@ protected function getXdebugDataForBankAccount()
6974
14 => 1,
7075
15 => 1,
7176
18 => 1,
72-
22 => 1,
7377
24 => 1,
74-
29 => 1,
78+
26 => 1,
7579
31 => 1,
80+
33 => 1,
81+
],
82+
TEST_FILES_PATH . 'CalculatorSum.php' => [
83+
10 => 1,
84+
11 => 1,
85+
22 => 1
7686
]
7787
]
7888
];
7989
}
8090

91+
protected function getStrictCoverageForBankAccount()
92+
{
93+
$data = $this->getXdebugDataForBankAccount();
94+
require_once TEST_FILES_PATH . '/BankAccountTest.php';
95+
require_once TEST_FILES_PATH . '/CalculatorSum.php';
96+
97+
$stub = $this->createMock(Xdebug::class);
98+
99+
$stub->expects($this->any())
100+
->method('stop')
101+
->will($this->onConsecutiveCalls(
102+
$data[0],
103+
$data[1],
104+
$data[2],
105+
$data[3]
106+
));
107+
108+
$filter = new Filter;
109+
$filter->addFileToWhitelist(TEST_FILES_PATH . 'BankAccount.php');
110+
$filter->addFileToWhitelist(TEST_FILES_PATH . 'CalculatorSum.php');
111+
112+
$coverage = new CodeCoverage($stub, $filter);
113+
$coverage->setCheckForUnintentionallyCoveredCode(true);
114+
$coverage->setCheckForMissingCoversAnnotation(true);
115+
$coverage->setCheckForMissingUsesAnnotation(false);
116+
117+
$coverage->start(
118+
new \BankAccountTest('testBalanceIsInitiallyZero'),
119+
true
120+
);
121+
122+
$coverage->stop(
123+
true,
124+
[TEST_FILES_PATH . 'BankAccount.php' => range(6, 9)]
125+
);
126+
127+
$coverage->start(
128+
new \BankAccountTest('testBalanceCannotBecomeNegative')
129+
);
130+
131+
$coverage->stop(
132+
true,
133+
[
134+
TEST_FILES_PATH . 'BankAccount.php' => array_merge(
135+
range(6, 9),
136+
range(11, 18),
137+
range(29, 34)
138+
)
139+
]
140+
);
141+
142+
$coverage->start(
143+
new \BankAccountTest('testBalanceCannotBecomeNegative2')
144+
);
145+
146+
$coverage->stop(
147+
true,
148+
[TEST_FILES_PATH . 'BankAccount.php' => range(20, 27)]
149+
);
150+
151+
$coverage->start(
152+
new \BankAccountTest('testDepositWithdrawMoney')
153+
);
154+
155+
$coverage->stop(
156+
true,
157+
[
158+
TEST_FILES_PATH . 'BankAccount.php' => array_merge(
159+
range(6, 9),
160+
range(20, 27),
161+
range(29, 34)
162+
)
163+
]
164+
);
165+
166+
return $coverage;
167+
}
168+
81169
protected function getCoverageForBankAccount()
82170
{
83171
$data = $this->getXdebugDataForBankAccount();
@@ -96,6 +184,7 @@ protected function getCoverageForBankAccount()
96184

97185
$filter = new Filter;
98186
$filter->addFileToWhitelist(TEST_FILES_PATH . 'BankAccount.php');
187+
$filter->addFileToWhitelist(TEST_FILES_PATH . 'CalculatorSum.php');
99188

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

tests/_files/BankAccount.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ protected function setBalance($balance)
1919

2020
public function depositMoney($balance)
2121
{
22-
$this->setBalance($this->getBalance() + $balance);
22+
require_once TEST_FILES_PATH . '/CalculatorSum.php';
23+
$calculator = new CalculatorSum($this->getBalance(), $balance);
24+
$this->setBalance($calculator->add());
2325

2426
return $this->getBalance();
2527
}

tests/_files/CalculatorSum.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
class CalculatorSum
3+
{
4+
private $a;
5+
private $b;
6+
/**
7+
* CalculatorSum constructor.
8+
*/
9+
public function __construct($a, $b) {
10+
$this->a = $a;
11+
$this->b = $b;
12+
}
13+
14+
/**
15+
* @assert (0, 0) == 0
16+
* @assert (0, 1) == 1
17+
* @assert (1, 0) == 1
18+
* @assert (1, 1) == 2
19+
*/
20+
public function add()
21+
{
22+
return $this->a + $this->b;
23+
}
24+
}

tests/tests/CodeCoverageTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,11 @@ public function testCollect()
303303
);
304304
}
305305

306+
public function testStrictCoverage()
307+
{
308+
$coverage = $this->getStrictCoverageForBankAccount();
309+
}
310+
306311
public function testMerge()
307312
{
308313
$coverage = $this->getCoverageForBankAccountForFirstTwoTests();

0 commit comments

Comments
 (0)