Skip to content

Commit 68f9290

Browse files
Closes #4892
1 parent 5f2e2f0 commit 68f9290

File tree

8 files changed

+124
-7
lines changed

8 files changed

+124
-7
lines changed

ChangeLog-10.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ All notable changes of the PHPUnit 10.0 release series are documented in this fi
1313
* [#4709](https://github.com/sebastianbergmann/phpunit/issues/4709): Support `never` type in test double code generator
1414
* [#4737](https://github.com/sebastianbergmann/phpunit/issues/4737): Support intersection types in test double code generator
1515
* [#4818](https://github.com/sebastianbergmann/phpunit/pull/4818): `assertArrayIsList`
16+
* [#4892](https://github.com/sebastianbergmann/phpunit/issues/4892): Make colors used in HTML code coverage report configurable
1617
* `@excludeGlobalVariableFromBackup variable` annotation for excluding a global variable from the backup/restore of global and super-global variables
1718
* `#[ExcludeGlobalVariableFromBackup('variable')]` attribute for excluding a global variable from the backup/restore of global and super-global variables
1819
* `@excludeStaticPropertyFromBackup className propertyName` annotation for excluding a static property from the backup/restore of static properties in user-defined classes

phpunit.xsd

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,11 @@
308308
<xs:attribute name="outputDirectory" type="xs:anyURI" use="required"/>
309309
<xs:attribute name="lowUpperBound" type="xs:integer" default="50"/>
310310
<xs:attribute name="highLowerBound" type="xs:integer" default="90"/>
311+
<xs:attribute name="colorSuccessLow" type="xs:string" default="#dff0d8"/>
312+
<xs:attribute name="colorSuccessMedium" type="xs:string" default="#c3e3b5"/>
313+
<xs:attribute name="colorSuccessHigh" type="xs:string" default="#99cb84"/>
314+
<xs:attribute name="colorWarning" type="xs:string" default="#fcf8e3"/>
315+
<xs:attribute name="colorDanger" type="xs:string" default="#f2dede"/>
311316
</xs:complexType>
312317
<xs:complexType name="coverageReportTextType">
313318
<xs:attribute name="outputFile" type="xs:anyURI" use="required"/>

src/TextUI/Configuration/Configuration.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ final class Configuration
3434
private ?string $coverageHtml;
3535
private int $coverageHtmlLowUpperBound;
3636
private int $coverageHtmlHighLowerBound;
37+
private string $coverageHtmlColorSuccessLow;
38+
private string $coverageHtmlColorSuccessMedium;
39+
private string $coverageHtmlColorSuccessHigh;
40+
private string $coverageHtmlColorWarning;
41+
private string $coverageHtmlColorDanger;
3742
private ?string $coveragePhp;
3843
private ?string $coverageText;
3944
private bool $coverageTextShowUncoveredFiles;
@@ -112,7 +117,7 @@ final class Configuration
112117
*/
113118
private array $warnings;
114119

115-
public function __construct(?string $configurationFile, ?string $bootstrap, bool $cacheResult, ?string $cacheDirectory, ?string $coverageCacheDirectory, string $testResultCacheFile, ?string $coverageClover, ?string $coverageCobertura, ?string $coverageCrap4j, int $coverageCrap4jThreshold, ?string $coverageHtml, int $coverageHtmlLowUpperBound, int $coverageHtmlHighLowerBound, ?string $coveragePhp, ?string $coverageText, bool $coverageTextShowUncoveredFiles, bool $coverageTextShowOnlySummary, ?string $coverageXml, bool $pathCoverage, bool $ignoreDeprecatedCodeUnitsFromCodeCoverage, bool $disableCodeCoverageIgnore, bool $failOnEmptyTestSuite, bool $failOnIncomplete, bool $failOnRisky, bool $failOnSkipped, bool $failOnWarning, bool $outputToStandardErrorStream, int|string $columns, bool $tooFewColumnsRequested, bool $loadPharExtensions, ?string $pharExtensionDirectory, bool $backupGlobals, bool $backupStaticProperties, bool $beStrictAboutChangesToGlobalState, bool $colors, bool $convertDeprecationsToExceptions, bool $convertErrorsToExceptions, bool $convertNoticesToExceptions, bool $convertWarningsToExceptions, bool $processIsolation, bool $stopOnDefect, bool $stopOnError, bool $stopOnFailure, bool $stopOnWarning, bool $stopOnIncomplete, bool $stopOnRisky, bool $stopOnSkipped, bool $enforceTimeLimit, int $defaultTimeLimit, int $timeoutForSmallTests, int $timeoutForMediumTests, int $timeoutForLargeTests, bool $reportUselessTests, bool $strictCoverage, bool $disallowTestOutput, bool $verbose, bool $reverseDefectList, bool $requireCoverageMetadata, bool $registerMockObjectsFromTestArgumentsRecursively, bool $noInteraction, int $executionOrder, int $executionOrderDefects, bool $resolveDependencies, ?string $logfileText, ?string $logfileTeamcity, ?string $logfileJunit, ?string $logfileTestdoxHtml, ?string $logfileTestdoxText, ?string $logfileTestdoxXml, ?string $plainTextTrace, bool $defaultOutput, bool $teamCityOutput, bool $testDoxOutput, int $repeat, ?array $testsCovering, ?array $testsUsing, ?string $filter, ?array $groups, ?array $excludeGroups, array $testdoxGroups, array $testdoxExcludeGroups, ?string $includePath, int $randomOrderSeed, bool $includeUncoveredFiles, ?string $xmlValidationErrors, array $warnings)
120+
public function __construct(?string $configurationFile, ?string $bootstrap, bool $cacheResult, ?string $cacheDirectory, ?string $coverageCacheDirectory, string $testResultCacheFile, ?string $coverageClover, ?string $coverageCobertura, ?string $coverageCrap4j, int $coverageCrap4jThreshold, ?string $coverageHtml, int $coverageHtmlLowUpperBound, int $coverageHtmlHighLowerBound, string $coverageHtmlColorSuccessLow, string $coverageHtmlColorSuccessMedium, string $coverageHtmlColorSuccessHigh, string $coverageHtmlColorWarning, string $coverageHtmlColorDanger, ?string $coveragePhp, ?string $coverageText, bool $coverageTextShowUncoveredFiles, bool $coverageTextShowOnlySummary, ?string $coverageXml, bool $pathCoverage, bool $ignoreDeprecatedCodeUnitsFromCodeCoverage, bool $disableCodeCoverageIgnore, bool $failOnEmptyTestSuite, bool $failOnIncomplete, bool $failOnRisky, bool $failOnSkipped, bool $failOnWarning, bool $outputToStandardErrorStream, int|string $columns, bool $tooFewColumnsRequested, bool $loadPharExtensions, ?string $pharExtensionDirectory, bool $backupGlobals, bool $backupStaticProperties, bool $beStrictAboutChangesToGlobalState, bool $colors, bool $convertDeprecationsToExceptions, bool $convertErrorsToExceptions, bool $convertNoticesToExceptions, bool $convertWarningsToExceptions, bool $processIsolation, bool $stopOnDefect, bool $stopOnError, bool $stopOnFailure, bool $stopOnWarning, bool $stopOnIncomplete, bool $stopOnRisky, bool $stopOnSkipped, bool $enforceTimeLimit, int $defaultTimeLimit, int $timeoutForSmallTests, int $timeoutForMediumTests, int $timeoutForLargeTests, bool $reportUselessTests, bool $strictCoverage, bool $disallowTestOutput, bool $verbose, bool $reverseDefectList, bool $requireCoverageMetadata, bool $registerMockObjectsFromTestArgumentsRecursively, bool $noInteraction, int $executionOrder, int $executionOrderDefects, bool $resolveDependencies, ?string $logfileText, ?string $logfileTeamcity, ?string $logfileJunit, ?string $logfileTestdoxHtml, ?string $logfileTestdoxText, ?string $logfileTestdoxXml, ?string $plainTextTrace, bool $defaultOutput, bool $teamCityOutput, bool $testDoxOutput, int $repeat, ?array $testsCovering, ?array $testsUsing, ?string $filter, ?array $groups, ?array $excludeGroups, array $testdoxGroups, array $testdoxExcludeGroups, ?string $includePath, int $randomOrderSeed, bool $includeUncoveredFiles, ?string $xmlValidationErrors, array $warnings)
116121
{
117122
$this->configurationFile = $configurationFile;
118123
$this->bootstrap = $bootstrap;
@@ -127,6 +132,11 @@ public function __construct(?string $configurationFile, ?string $bootstrap, bool
127132
$this->coverageHtml = $coverageHtml;
128133
$this->coverageHtmlLowUpperBound = $coverageHtmlLowUpperBound;
129134
$this->coverageHtmlHighLowerBound = $coverageHtmlHighLowerBound;
135+
$this->coverageHtmlColorSuccessLow = $coverageHtmlColorSuccessLow;
136+
$this->coverageHtmlColorSuccessMedium = $coverageHtmlColorSuccessMedium;
137+
$this->coverageHtmlColorSuccessHigh = $coverageHtmlColorSuccessHigh;
138+
$this->coverageHtmlColorWarning = $coverageHtmlColorWarning;
139+
$this->coverageHtmlColorDanger = $coverageHtmlColorDanger;
130140
$this->coveragePhp = $coveragePhp;
131141
$this->coverageText = $coverageText;
132142
$this->coverageTextShowUncoveredFiles = $coverageTextShowUncoveredFiles;
@@ -412,6 +422,31 @@ public function coverageHtmlHighLowerBound(): int
412422
return $this->coverageHtmlHighLowerBound;
413423
}
414424

425+
public function coverageHtmlColorSuccessLow(): string
426+
{
427+
return $this->coverageHtmlColorSuccessLow;
428+
}
429+
430+
public function coverageHtmlColorSuccessMedium(): string
431+
{
432+
return $this->coverageHtmlColorSuccessMedium;
433+
}
434+
435+
public function coverageHtmlColorSuccessHigh(): string
436+
{
437+
return $this->coverageHtmlColorSuccessHigh;
438+
}
439+
440+
public function coverageHtmlColorWarning(): string
441+
{
442+
return $this->coverageHtmlColorWarning;
443+
}
444+
445+
public function coverageHtmlColorDanger(): string
446+
{
447+
return $this->coverageHtmlColorDanger;
448+
}
449+
415450
/**
416451
* @psalm-assert-if-true !null $this->coveragePhp
417452
*/

src/TextUI/Configuration/Merger.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use PHPUnit\TextUI\XmlConfiguration\Configuration as XmlConfiguration;
2323
use PHPUnit\TextUI\XmlConfiguration\LoadedFromFileConfiguration;
2424
use PHPUnit\Util\Filesystem;
25+
use SebastianBergmann\CodeCoverage\Report\Html\Colors;
2526
use SebastianBergmann\Environment\Console;
2627

2728
/**
@@ -168,13 +169,20 @@ public function merge(CliConfiguration $cliConfiguration, XmlConfiguration $xmlC
168169
$pathCoverage = $xmlConfiguration->codeCoverage()->pathCoverage();
169170
}
170171

172+
$defaultColors = Colors::default();
173+
171174
$coverageClover = null;
172175
$coverageCobertura = null;
173176
$coverageCrap4j = null;
174177
$coverageCrap4jThreshold = 30;
175178
$coverageHtml = null;
176179
$coverageHtmlLowUpperBound = 50;
177180
$coverageHtmlHighLowerBound = 90;
181+
$coverageHtmlColorSuccessLow = $defaultColors->successLow();
182+
$coverageHtmlColorSuccessMedium = $defaultColors->successMedium();
183+
$coverageHtmlColorSuccessHigh = $defaultColors->successHigh();
184+
$coverageHtmlColorWarning = $defaultColors->warning();
185+
$coverageHtmlColorDanger = $defaultColors->danger();
178186
$coveragePhp = null;
179187
$coverageText = null;
180188
$coverageTextShowUncoveredFiles = false;
@@ -216,6 +224,12 @@ public function merge(CliConfiguration $cliConfiguration, XmlConfiguration $xmlC
216224
$coverageHtmlLowUpperBound = 50;
217225
$coverageHtmlHighLowerBound = 90;
218226
}
227+
228+
$coverageHtmlColorSuccessLow = $xmlConfiguration->codeCoverage()->html()->colorSuccessLow();
229+
$coverageHtmlColorSuccessMedium = $xmlConfiguration->codeCoverage()->html()->colorSuccessMedium();
230+
$coverageHtmlColorSuccessHigh = $xmlConfiguration->codeCoverage()->html()->colorSuccessHigh();
231+
$coverageHtmlColorWarning = $xmlConfiguration->codeCoverage()->html()->colorWarning();
232+
$coverageHtmlColorDanger = $xmlConfiguration->codeCoverage()->html()->colorDanger();
219233
}
220234

221235
if ($cliConfiguration->hasCoverageHtml()) {
@@ -566,6 +580,11 @@ public function merge(CliConfiguration $cliConfiguration, XmlConfiguration $xmlC
566580
$coverageHtml,
567581
$coverageHtmlLowUpperBound,
568582
$coverageHtmlHighLowerBound,
583+
$coverageHtmlColorSuccessLow,
584+
$coverageHtmlColorSuccessMedium,
585+
$coverageHtmlColorSuccessHigh,
586+
$coverageHtmlColorWarning,
587+
$coverageHtmlColorDanger,
569588
$coveragePhp,
570589
$coverageText,
571590
$coverageTextShowUncoveredFiles,

src/TextUI/Configuration/Xml/CodeCoverage/Report/Html.php

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,22 @@ final class Html
2020
private Directory $target;
2121
private int $lowUpperBound;
2222
private int $highLowerBound;
23+
private string $colorSuccessLow;
24+
private string $colorSuccessMedium;
25+
private string $colorSuccessHigh;
26+
private string $colorWarning;
27+
private string $colorDanger;
2328

24-
public function __construct(Directory $target, int $lowUpperBound, int $highLowerBound)
29+
public function __construct(Directory $target, int $lowUpperBound, int $highLowerBound, string $colorSuccessLow, string $colorSuccessMedium, string $colorSuccessHigh, string $colorWarning, string $colorDanger)
2530
{
26-
$this->target = $target;
27-
$this->lowUpperBound = $lowUpperBound;
28-
$this->highLowerBound = $highLowerBound;
31+
$this->target = $target;
32+
$this->lowUpperBound = $lowUpperBound;
33+
$this->highLowerBound = $highLowerBound;
34+
$this->colorSuccessLow = $colorSuccessLow;
35+
$this->colorSuccessMedium = $colorSuccessMedium;
36+
$this->colorSuccessHigh = $colorSuccessHigh;
37+
$this->colorWarning = $colorWarning;
38+
$this->colorDanger = $colorDanger;
2939
}
3040

3141
public function target(): Directory
@@ -42,4 +52,29 @@ public function highLowerBound(): int
4252
{
4353
return $this->highLowerBound;
4454
}
55+
56+
public function colorSuccessLow(): string
57+
{
58+
return $this->colorSuccessLow;
59+
}
60+
61+
public function colorSuccessMedium(): string
62+
{
63+
return $this->colorSuccessMedium;
64+
}
65+
66+
public function colorSuccessHigh(): string
67+
{
68+
return $this->colorSuccessHigh;
69+
}
70+
71+
public function colorWarning(): string
72+
{
73+
return $this->colorWarning;
74+
}
75+
76+
public function colorDanger(): string
77+
{
78+
return $this->colorDanger;
79+
}
4580
}

src/TextUI/Configuration/Xml/Loader.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
use PHPUnit\Util\Xml\Loader as XmlLoader;
5656
use PHPUnit\Util\Xml\SchemaFinder;
5757
use PHPUnit\Util\Xml\Validator;
58+
use SebastianBergmann\CodeCoverage\Report\Html\Colors;
5859

5960
/**
6061
* @internal This class is not covered by the backward compatibility promise for PHPUnit
@@ -391,6 +392,8 @@ private function codeCoverage(string $filename, DOMXPath $xpath): CodeCoverage
391392
$element = $this->element($xpath, 'coverage/report/html');
392393

393394
if ($element) {
395+
$defaultColors = Colors::default();
396+
394397
$html = new CodeCoverageHtml(
395398
new Directory(
396399
$this->toAbsolutePath(
@@ -399,7 +402,12 @@ private function codeCoverage(string $filename, DOMXPath $xpath): CodeCoverage
399402
)
400403
),
401404
$this->getIntegerAttribute($element, 'lowUpperBound', 50),
402-
$this->getIntegerAttribute($element, 'highLowerBound', 90)
405+
$this->getIntegerAttribute($element, 'highLowerBound', 90),
406+
$this->getStringAttributeWithDefault($element, 'colorSuccessLow', $defaultColors->successLow()),
407+
$this->getStringAttributeWithDefault($element, 'colorSuccessMedium', $defaultColors->successMedium()),
408+
$this->getStringAttributeWithDefault($element, 'colorSuccessHigh', $defaultColors->successHigh()),
409+
$this->getStringAttributeWithDefault($element, 'colorWarning', $defaultColors->warning()),
410+
$this->getStringAttributeWithDefault($element, 'colorDanger', $defaultColors->danger()),
403411
);
404412
}
405413

src/TextUI/TestRunner.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,13 @@ public function run(TestSuite $suite): TestResult
455455
' and <a href="https://phpunit.de/">PHPUnit %s</a>',
456456
Version::id()
457457
),
458-
Colors::default(),
458+
Colors::from(
459+
$this->configuration->coverageHtmlColorSuccessLow(),
460+
$this->configuration->coverageHtmlColorSuccessMedium(),
461+
$this->configuration->coverageHtmlColorSuccessHigh(),
462+
$this->configuration->coverageHtmlColorWarning(),
463+
$this->configuration->coverageHtmlColorDanger(),
464+
),
459465
Thresholds::from(
460466
$this->configuration->coverageHtmlLowUpperBound(),
461467
$this->configuration->coverageHtmlHighLowerBound()

tests/unit/TextUI/XmlConfigurationTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use PHPUnit\Framework\TestCase;
3232
use PHPUnit\Runner\TestSuiteSorter;
3333
use PHPUnit\TextUI\XmlConfiguration\CodeCoverage\Filter\Directory;
34+
use SebastianBergmann\CodeCoverage\Report\Html\Colors;
3435
use stdClass;
3536

3637
#[Medium]
@@ -189,10 +190,17 @@ public function testCodeCoverageConfigurationIsReadCorrectly(): void
189190
$this->assertTrue($codeCoverage->hasCrap4j());
190191
$this->assertSame(TEST_FILES_PATH . 'crap4j.xml', $codeCoverage->crap4j()->target()->path());
191192

193+
$defaultColors = Colors::default();
194+
192195
$this->assertTrue($codeCoverage->hasHtml());
193196
$this->assertSame(TEST_FILES_PATH . 'coverage', $codeCoverage->html()->target()->path());
194197
$this->assertSame(50, $codeCoverage->html()->lowUpperBound());
195198
$this->assertSame(90, $codeCoverage->html()->highLowerBound());
199+
$this->assertSame($defaultColors->successLow(), $codeCoverage->html()->colorSuccessLow());
200+
$this->assertSame($defaultColors->successMedium(), $codeCoverage->html()->colorSuccessMedium());
201+
$this->assertSame($defaultColors->successHigh(), $codeCoverage->html()->colorSuccessHigh());
202+
$this->assertSame($defaultColors->warning(), $codeCoverage->html()->colorWarning());
203+
$this->assertSame($defaultColors->danger(), $codeCoverage->html()->colorDanger());
196204

197205
$this->assertTrue($codeCoverage->hasPhp());
198206
$this->assertSame(TEST_FILES_PATH . 'coverage.php', $codeCoverage->php()->target()->path());

0 commit comments

Comments
 (0)