Skip to content

Commit

Permalink
Respect configuration options of text coverage (#626)
Browse files Browse the repository at this point in the history
* Respect configuration options of text coverage
* Get coverage bounds for text coverage from html coverage config, if set

Co-authored-by: Leo Viezens <l.viezens@queo-group.com>
  • Loading branch information
LeoVie and Leo Viezens committed Nov 3, 2021
1 parent 77ca033 commit 5843dce
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 4 deletions.
9 changes: 9 additions & 0 deletions src/Coverage/CoverageReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ public function php(string $target): void
public function text(): string
{
$text = new Text();
if ($this->codeCoverageConfiguration !== null && $this->codeCoverageConfiguration->hasText()) {
$hasHtml = $this->codeCoverageConfiguration->hasHtml();
$text = new Text(
$hasHtml ? $this->codeCoverageConfiguration->html()->lowUpperBound() : 50,
$hasHtml ? $this->codeCoverageConfiguration->html()->highLowerBound() : 90,
$this->codeCoverageConfiguration->text()->showUncoveredFiles(),
$this->codeCoverageConfiguration->text()->showOnlySummary()
);
}

return $text->process($this->coverage);
}
Expand Down
32 changes: 29 additions & 3 deletions test/Unit/Coverage/CoverageReporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,19 @@ protected function setUpTest(): void
{
static::skipIfCodeCoverageNotEnabled();

$this->createCoverageReporter('phpunit-fully-configured.xml');
}

private function createCoverageReporter(string $fixtureFile): void
{
$filter = new Filter();
$filter->includeFile(__FILE__);
$codeCoverage = new CodeCoverage((new Selector())->forLineCoverage($filter), $filter);
$codeCoverage->append(RawCodeCoverageData::fromXdebugWithoutPathCoverage([
__FILE__ => [__LINE__ => 1],
]), uniqid());

$configuration = (new Loader())->load($this->fixture('phpunit-fully-configured.xml'));
$configuration = (new Loader())->load($this->fixture($fixtureFile));

$this->coverageReporter = new CoverageReporter($codeCoverage, $configuration->codeCoverage());
}
Expand Down Expand Up @@ -99,11 +104,32 @@ public function testGeneratePhp(): void
static::assertFileExists($target);
}

public function testGenerateText(): void
/**
* @dataProvider generateTextProvider
*/
public function testGenerateText(string $fixtureFile, string $expectedContainedString): void
{
$this->createCoverageReporter($fixtureFile);
$output = $this->coverageReporter->text();

static::assertStringContainsString('Code Coverage Report:', $output);
static::assertStringContainsString($expectedContainedString, $output);
}

/**
* @return string[][]
*/
public function generateTextProvider(): array
{
return [
'showOnlySummary = false' => [
'fixtureFile' => 'phpunit-fully-configured.xml',
'expectedContainedString' => 'Code Coverage Report:',
],
'showOnlySummary = true' => [
'fixtureFile' => 'phpunit-coverage-text-show-only-summary.xml',
'expectedContainedString' => 'Code Coverage Report Summary:',
],
];
}

public function testGenerateXml(): void
Expand Down
47 changes: 47 additions & 0 deletions test/fixtures/phpunit-coverage-text-show-only-summary.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.4/phpunit.xsd"
colors="true"
>
<testsuites>
<testsuite name="ParaTest Fixtures">
<file>./passing_tests/GroupsTest.php</file>
</testsuite>
</testsuites>
<!--
processUncoveredFiles="true" omitted due to
https://github.com/sebastianbergmann/php-code-coverage/issues/801
-->
<coverage includeUncoveredFiles="true"
ignoreDeprecatedCodeUnits="true"
disableCodeCoverageIgnore="true">
<include>
<directory suffix=".php">src</directory>
</include>

<exclude>
<directory suffix=".php">src/generated</directory>
<file>src/autoload.php</file>
</exclude>

<report>
<clover outputFile="../tmp/clover.xml"/>
<cobertura outputFile="../tmp/cobertura.xml"/>
<crap4j outputFile="../tmp/crap4j.xml" threshold="50"/>
<html outputDirectory="../tmp/html-coverage" lowUpperBound="50" highLowerBound="90"/>
<php outputFile="../tmp/coverage.php"/>
<text outputFile="../tmp/coverage.txt" showUncoveredFiles="false" showOnlySummary="true"/>
<xml outputDirectory="../tmp/xml-coverage"/>
</report>
</coverage>

<logging>
<junit outputFile="../tmp/junit.xml"/>
<teamcity outputFile="../tmp/teamcity.txt"/>
<testdoxHtml outputFile="../tmp/testdox.html"/>
<testdoxText outputFile="../tmp/testdox.txt"/>
<testdoxXml outputFile="../tmp/testdox.xml"/>
<text outputFile="../tmp/logfile.txt"/>
</logging>
</phpunit>
2 changes: 1 addition & 1 deletion test/fixtures/phpunit-fully-configured.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<crap4j outputFile="../tmp/crap4j.xml" threshold="50"/>
<html outputDirectory="../tmp/html-coverage" lowUpperBound="50" highLowerBound="90"/>
<php outputFile="../tmp/coverage.php"/>
<text outputFile="../tmp/coverage.txt" showUncoveredFiles="false" showOnlySummary="true"/>
<text outputFile="../tmp/coverage.txt" showUncoveredFiles="false" showOnlySummary="false"/>
<xml outputDirectory="../tmp/xml-coverage"/>
</report>
</coverage>
Expand Down

0 comments on commit 5843dce

Please sign in to comment.