Skip to content

Commit

Permalink
Complete abstraction of php-code-coverage library into unittest.cover…
Browse files Browse the repository at this point in the history
…age.impl
  • Loading branch information
thekid committed Oct 4, 2020
1 parent b1c2578 commit bac8e9a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 14 deletions.
22 changes: 10 additions & 12 deletions src/main/php/unittest/coverage/CoverageDetails.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,52 +8,50 @@
* @test xp://unittest.coverage.tests.CoverageDetailsTest
*/
class CoverageDetails extends Metric {
private $coverage, $reports, $executed, $executable, $classes;
private $report, $reports;

/**
* Creates a new detailled coverage instance
*
* @param SebastianBergmann.CodeCoverage.Node.Directory $coverage
* @param unittest.coverage.impl.Report $report
* @param string[] $reports
*/
public function __construct($coverage, $reports) {
$this->coverage= $coverage;
public function __construct($report, $reports) {
$this->report= $report;
$this->reports= $reports;
}

/** @return void */
protected function calculate() {
$this->executed= $this->coverage->getNumExecutedLines();
$this->executable= $this->coverage->getNumExecutableLines();
$this->classes= $this->coverage->getClasses();
/* NOOP */
}

/** @return string */
protected function format() {

// Summary
$percent= $this->executed / $this->executable * 100;
$percent= $this->report->executed() / $this->report->executable() * 100;
$s= sprintf(
"%s%.2f%%\033[0m lines covered (%d/%d)%s\n\n",
$percent < 50.0 ? "\033[31;1m" : ($percent < 90.0 ? "\033[33;1m" : "\033[32;1m"),
$percent,
$this->executed,
$this->executable,
$this->report->executed(),
$this->report->executable(),
$this->reports ? " > \033[36;4m".implode(' & ', $this->reports)."\033[0m" : ''
);

// Details by class
$s.= "┌──────────────────────────────────────────────────────┬─────────┬──────┐\n";
$s.= "│ Class │ % Lines │ Not │\n";
$s.= "╞══════════════════════════════════════════════════════╪═════════╪══════╡\n";
foreach ($this->classes as $name => $details) {
foreach ($this->report->summary() as $class => $details) {
$percent= $details['coverage'];
$color= $percent < 50.0 ? "\033[31;1m" : ($percent < 90.0 ? "\033[33;1m" : "\033[32;1m");
$uncovered= $details['executableLines'] - $details['executedLines'];

$s.= sprintf(
"│ %-52s │ %s%6.2f%%\033[0m │ %4s │\n",
(new ClassName($name))->shortenedTo(52),
(new ClassName($class))->shortenedTo(52),
$color,
$percent,
$uncovered ?: ''
Expand Down
7 changes: 6 additions & 1 deletion src/main/php/unittest/coverage/impl/Coverage8.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public function stop() {
}

public function report() {
return $this->backing->getReport();
$report= $this->backing->getReport();
return new Report(
$report->getNumExecutedLines(),
$report->getNumExecutableLines(),
$report->getClasses()
);
}
}
7 changes: 6 additions & 1 deletion src/main/php/unittest/coverage/impl/Coverage9.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public function stop() {
}

public function report() {
return $this->backing->getReport();
$report= $this->backing->getReport();
return new Report(
$report->numberOfExecutedLines(),
$report->numberOfExecutableLines(),
$report->classes()
);
}
}
27 changes: 27 additions & 0 deletions src/main/php/unittest/coverage/impl/Report.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php namespace unittest\coverage\impl;

class Report {
private $executed, $executable, $summary;

/**
* Creates a new report
*
* @param int $executed
* @param int $executable
* @param [:var] $summary Details, keyed by class name
*/
public function __construct($executed, $executable, $summary) {
$this->executed= $executed;
$this->executable= $executable;
$this->summary= $summary;
}

/** @return int */
public function executed() { return $this->executed; }

/** @return int */
public function executable() { return $this->executable; }

/** @return [:var] */
public function summary() { return $this->summary; }
}

0 comments on commit bac8e9a

Please sign in to comment.