Skip to content

Commit daf3a3a

Browse files
committed
Adding support for configuration of method and class coverge thresholds
- methodCoverageThreshold: what percentage of lines must be tested for method to be marked as tested - classCoverageThreshold: what percentage of functions must be tested for class to be marked as tested Still pending their addition to command line parsing, have not found the way yet.
1 parent d1a1990 commit daf3a3a

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

PHP/CodeCoverage/Report/HTML.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,14 @@ public function __construct(array $options = array())
106106
$options['generator'] = '';
107107
}
108108

109+
if (!isset($options['methodCoverageThreshold'])) {
110+
$options['methodCoverageThreshold'] = 100;
111+
}
112+
113+
if (!isset($options['classCoverageThreshold'])) {
114+
$options['classCoverageThreshold'] = 100;
115+
}
116+
109117
$this->options = $options;
110118

111119
self::$templatePath = sprintf(
@@ -210,7 +218,10 @@ protected function addItems(PHP_CodeCoverage_Report_HTML_Node_Directory $root, a
210218
substr($key, 0, -2),
211219
$value,
212220
$this->options['yui'],
213-
$this->options['highlight']
221+
$this->options['highlight'],
222+
$this->options['methodCoverageThreshold'],
223+
$this->options['classCoverageThreshold']
224+
214225
);
215226
}
216227

PHP/CodeCoverage/Report/HTML/Node/Directory.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,15 @@ public function addDirectory($name)
146146
* @param array $lines
147147
* @param boolean $yui
148148
* @param boolean $highlight
149+
* @param string $methodCoverageThreshold
150+
* @param string $classCoverageThreshold
149151
* @return PHP_CodeCoverage_Report_HTML_Node_File
150152
* @throws RuntimeException
151153
*/
152-
public function addFile($name, array $lines, $yui, $highlight)
154+
public function addFile($name, array $lines, $yui, $highlight, $methodCoverageThreshold, $classCoverageThreshold)
153155
{
154156
$file = new PHP_CodeCoverage_Report_HTML_Node_File(
155-
$name, $this, $lines, $yui, $highlight
157+
$name, $this, $lines, $yui, $highlight, $methodCoverageThreshold, $classCoverageThreshold
156158
);
157159

158160
$this->children[] = $file;

PHP/CodeCoverage/Report/HTML/Node/File.php

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,16 @@ class PHP_CodeCoverage_Report_HTML_Node_File extends PHP_CodeCoverage_Report_HTM
138138
*/
139139
protected $endLines = array();
140140

141+
/**
142+
* @var string
143+
*/
144+
protected $methodCoverageThreshold;
145+
146+
/**
147+
* @var string
148+
*/
149+
protected $classCoverageThreshold;
150+
141151
/**
142152
* Constructor.
143153
*
@@ -146,9 +156,11 @@ class PHP_CodeCoverage_Report_HTML_Node_File extends PHP_CodeCoverage_Report_HTM
146156
* @param array $executedLines
147157
* @param boolean $yui
148158
* @param boolean $highlight
159+
* @param string $methodCoverageThreshold
160+
* @param string $classCoverageThreshold
149161
* @throws RuntimeException
150162
*/
151-
public function __construct($name, PHP_CodeCoverage_Report_HTML_Node $parent, array $executedLines, $yui = TRUE, $highlight = FALSE)
163+
public function __construct($name, PHP_CodeCoverage_Report_HTML_Node $parent, array $executedLines, $yui = TRUE, $highlight = FALSE, $methodCoverageThreshold = 100, $classCoverageThreshold = 100)
152164
{
153165
parent::__construct($name, $parent);
154166

@@ -167,7 +179,9 @@ public function __construct($name, PHP_CodeCoverage_Report_HTML_Node $parent, ar
167179
$this->ignoredLines = PHP_CodeCoverage_Util::getLinesToBeIgnored(
168180
$path
169181
);
170-
182+
$this->methodCoverageThreshold = $methodCoverageThreshold;
183+
$this->classCoverageThreshold = $classCoverageThreshold;
184+
171185
$this->calculateStatistics();
172186
}
173187

@@ -391,7 +405,7 @@ public function render($target, $title, $charset = 'UTF-8', $lowUpperBound = 35,
391405
$items = '';
392406

393407
foreach ($this->classes as $className => $classData) {
394-
if ($classData['executedLines'] == $classData['executableLines']) {
408+
if ($classData['coverage'] >= $this->classCoverageThreshold) {
395409
$numTestedClasses = 1;
396410
$testedClassesPercent = 100;
397411
} else {
@@ -403,7 +417,8 @@ public function render($target, $title, $charset = 'UTF-8', $lowUpperBound = 35,
403417
$numMethods = count($classData['methods']);
404418

405419
foreach ($classData['methods'] as $method) {
406-
if ($method['executedLines'] == $method['executableLines']) {
420+
421+
if ($method['coverage'] >= $this->methodCoverageThreshold) {
407422
$numTestedMethods++;
408423
}
409424
}
@@ -439,8 +454,8 @@ public function render($target, $title, $charset = 'UTF-8', $lowUpperBound = 35,
439454
);
440455

441456
foreach ($classData['methods'] as $methodData) {
442-
if ($methodData['executedLines'] ==
443-
$methodData['executableLines']) {
457+
458+
if ($methodData['coverage'] >= $this->methodCoverageThreshold) {
444459
$numTestedMethods = 1;
445460
$testedMethodsPercent = 100;
446461
} else {
@@ -595,7 +610,7 @@ protected function calculateStatistics()
595610
$method['coverage'] = 100;
596611
}
597612

598-
if ($method['coverage'] == 100) {
613+
if ($method['coverage'] >= $this->methodCoverageThreshold) {
599614
$this->numTestedMethods++;
600615
}
601616

@@ -614,7 +629,7 @@ protected function calculateStatistics()
614629
$class['coverage'] = 100;
615630
}
616631

617-
if ($class['coverage'] == 100) {
632+
if ($class['coverage'] >= $this->classCoverageThreshold) {
618633
$this->numTestedClasses++;
619634
}
620635

0 commit comments

Comments
 (0)