Skip to content

Commit

Permalink
Closes #4893
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Mar 8, 2022
1 parent 078538a commit 8fdacbb
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 5 deletions.
8 changes: 6 additions & 2 deletions .psalm/baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@
<code>hasCacheDirectory</code>
<code>hasCacheResultFile</code>
</DeprecatedMethod>
<MissingThrowsDocblock occurrences="81">
<MissingThrowsDocblock occurrences="84">
<code>backupGlobals</code>
<code>backupStaticProperties</code>
<code>beStrictAboutChangesToGlobalState</code>
Expand All @@ -760,6 +760,7 @@
<code>coverageXml</code>
<code>crap4j</code>
<code>crap4j</code>
<code>customCssFile</code>
<code>defaultTimeLimit</code>
<code>disableCodeCoverageIgnore</code>
<code>disallowTestOutput</code>
Expand All @@ -783,6 +784,8 @@
<code>html</code>
<code>html</code>
<code>html</code>
<code>html</code>
<code>html</code>
<code>includePath</code>
<code>junitLogfile</code>
<code>noInteraction</code>
Expand Down Expand Up @@ -1077,7 +1080,7 @@
<code>$e</code>
<code>$e</code>
</InvalidArgument>
<MissingThrowsDocblock occurrences="38">
<MissingThrowsDocblock occurrences="39">
<code>bootstrap</code>
<code>configurationFile</code>
<code>configurationFile</code>
Expand All @@ -1087,6 +1090,7 @@
<code>coverageCobertura</code>
<code>coverageCrap4j</code>
<code>coverageHtml</code>
<code>coverageHtmlCustomCssFile</code>
<code>coveragePhp</code>
<code>coverageText</code>
<code>coverageText</code>
Expand Down
1 change: 1 addition & 0 deletions ChangeLog-10.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ All notable changes of the PHPUnit 10.0 release series are documented in this fi
* [#4737](https://github.com/sebastianbergmann/phpunit/issues/4737): Support intersection types in test double code generator
* [#4818](https://github.com/sebastianbergmann/phpunit/pull/4818): `assertArrayIsList`
* [#4892](https://github.com/sebastianbergmann/phpunit/issues/4892): Make colors used in HTML code coverage report configurable
* [#4893](https://github.com/sebastianbergmann/phpunit/issues/4893): Make path to custom.css for HTML code coverage report configurable
* `@excludeGlobalVariableFromBackup variable` annotation for excluding a global variable from the backup/restore of global and super-global variables
* `#[ExcludeGlobalVariableFromBackup('variable')]` attribute for excluding a global variable from the backup/restore of global and super-global variables
* `@excludeStaticPropertyFromBackup className propertyName` annotation for excluding a static property from the backup/restore of static properties in user-defined classes
Expand Down
1 change: 1 addition & 0 deletions phpunit.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@
<xs:attribute name="colorSuccessHigh" type="xs:string" default="#99cb84"/>
<xs:attribute name="colorWarning" type="xs:string" default="#fcf8e3"/>
<xs:attribute name="colorDanger" type="xs:string" default="#f2dede"/>
<xs:attribute name="customCssFile" type="xs:string"/>
</xs:complexType>
<xs:complexType name="coverageReportTextType">
<xs:attribute name="outputFile" type="xs:anyURI" use="required"/>
Expand Down
24 changes: 23 additions & 1 deletion src/TextUI/Configuration/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ final class Configuration
private string $coverageHtmlColorSuccessHigh;
private string $coverageHtmlColorWarning;
private string $coverageHtmlColorDanger;
private ?string $coverageHtmlCustomCssFile;
private ?string $coveragePhp;
private ?string $coverageText;
private bool $coverageTextShowUncoveredFiles;
Expand Down Expand Up @@ -117,7 +118,7 @@ final class Configuration
*/
private array $warnings;

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)
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 $coverageHtmlCustomCssFile, ?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)
{
$this->configurationFile = $configurationFile;
$this->bootstrap = $bootstrap;
Expand All @@ -137,6 +138,7 @@ public function __construct(?string $configurationFile, ?string $bootstrap, bool
$this->coverageHtmlColorSuccessHigh = $coverageHtmlColorSuccessHigh;
$this->coverageHtmlColorWarning = $coverageHtmlColorWarning;
$this->coverageHtmlColorDanger = $coverageHtmlColorDanger;
$this->coverageHtmlCustomCssFile = $coverageHtmlCustomCssFile;
$this->coveragePhp = $coveragePhp;
$this->coverageText = $coverageText;
$this->coverageTextShowUncoveredFiles = $coverageTextShowUncoveredFiles;
Expand Down Expand Up @@ -447,6 +449,26 @@ public function coverageHtmlColorDanger(): string
return $this->coverageHtmlColorDanger;
}

/**
* @psalm-assert-if-true !null $this->coverageHtmlCustomCssFile
*/
public function hasCoverageHtmlCustomCssFile(): bool
{
return $this->coverageHtmlCustomCssFile !== null;
}

/**
* @throws NoCustomCssFileException
*/
public function coverageHtmlCustomCssFile(): string
{
if (!$this->hasCoverageHtmlCustomCssFile()) {
throw new NoCustomCssFileException;
}

return $this->coverageHtmlCustomCssFile;
}

/**
* @psalm-assert-if-true !null $this->coveragePhp
*/
Expand Down
19 changes: 19 additions & 0 deletions src/TextUI/Configuration/Exception/NoCustomCssFileException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\TextUI\Configuration;

use RuntimeException;

/**
* @internal This class is not covered by the backward compatibility promise for PHPUnit
*/
final class NoCustomCssFileException extends RuntimeException implements Exception
{
}
6 changes: 6 additions & 0 deletions src/TextUI/Configuration/Merger.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ public function merge(CliConfiguration $cliConfiguration, XmlConfiguration $xmlC
$coverageHtmlColorSuccessHigh = $defaultColors->successHigh();
$coverageHtmlColorWarning = $defaultColors->warning();
$coverageHtmlColorDanger = $defaultColors->danger();
$coverageHtmlCustomCssFile = null;
$coveragePhp = null;
$coverageText = null;
$coverageTextShowUncoveredFiles = false;
Expand Down Expand Up @@ -232,6 +233,10 @@ public function merge(CliConfiguration $cliConfiguration, XmlConfiguration $xmlC
$coverageHtmlColorSuccessHigh = $xmlConfiguration->codeCoverage()->html()->colorSuccessHigh();
$coverageHtmlColorWarning = $xmlConfiguration->codeCoverage()->html()->colorWarning();
$coverageHtmlColorDanger = $xmlConfiguration->codeCoverage()->html()->colorDanger();

if ($xmlConfiguration->codeCoverage()->html()->hasCustomCssFile()) {
$coverageHtmlCustomCssFile = $xmlConfiguration->codeCoverage()->html()->customCssFile();
}
}

if ($cliConfiguration->hasCoverageHtml()) {
Expand Down Expand Up @@ -587,6 +592,7 @@ public function merge(CliConfiguration $cliConfiguration, XmlConfiguration $xmlC
$coverageHtmlColorSuccessHigh,
$coverageHtmlColorWarning,
$coverageHtmlColorDanger,
$coverageHtmlCustomCssFile,
$coveragePhp,
$coverageText,
$coverageTextShowUncoveredFiles,
Expand Down
25 changes: 24 additions & 1 deletion src/TextUI/Configuration/Xml/CodeCoverage/Report/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
namespace PHPUnit\TextUI\XmlConfiguration\CodeCoverage\Report;

use PHPUnit\TextUI\Configuration\NoCustomCssFileException;
use PHPUnit\TextUI\XmlConfiguration\Directory;

/**
Expand All @@ -25,8 +26,9 @@ final class Html
private string $colorSuccessHigh;
private string $colorWarning;
private string $colorDanger;
private ?string $customCssFile;

public function __construct(Directory $target, int $lowUpperBound, int $highLowerBound, string $colorSuccessLow, string $colorSuccessMedium, string $colorSuccessHigh, string $colorWarning, string $colorDanger)
public function __construct(Directory $target, int $lowUpperBound, int $highLowerBound, string $colorSuccessLow, string $colorSuccessMedium, string $colorSuccessHigh, string $colorWarning, string $colorDanger, ?string $customCssFile)
{
$this->target = $target;
$this->lowUpperBound = $lowUpperBound;
Expand All @@ -36,6 +38,7 @@ public function __construct(Directory $target, int $lowUpperBound, int $highLowe
$this->colorSuccessHigh = $colorSuccessHigh;
$this->colorWarning = $colorWarning;
$this->colorDanger = $colorDanger;
$this->customCssFile = $customCssFile;
}

public function target(): Directory
Expand Down Expand Up @@ -77,4 +80,24 @@ public function colorDanger(): string
{
return $this->colorDanger;
}

/**
* @psalm-assert-if-true !null $this->customCssFile
*/
public function hasCustomCssFile(): bool
{
return $this->customCssFile !== null;
}

/**
* @throws NoCustomCssFileException
*/
public function customCssFile(): string
{
if (!$this->hasCustomCssFile()) {
throw new NoCustomCssFileException;
}

return $this->customCssFile;
}
}
1 change: 1 addition & 0 deletions src/TextUI/Configuration/Xml/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ private function codeCoverage(string $filename, DOMXPath $xpath): CodeCoverage
$this->getStringAttributeWithDefault($element, 'colorSuccessHigh', $defaultColors->successHigh()),
$this->getStringAttributeWithDefault($element, 'colorWarning', $defaultColors->warning()),
$this->getStringAttributeWithDefault($element, 'colorDanger', $defaultColors->danger()),
$this->getStringAttribute($element, 'customCssFile')
);
}

Expand Down
10 changes: 9 additions & 1 deletion src/TextUI/TestRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
use SebastianBergmann\CodeCoverage\Report\Cobertura as CoberturaReport;
use SebastianBergmann\CodeCoverage\Report\Crap4j as Crap4jReport;
use SebastianBergmann\CodeCoverage\Report\Html\Colors;
use SebastianBergmann\CodeCoverage\Report\Html\CustomCssFile;
use SebastianBergmann\CodeCoverage\Report\Html\Facade as HtmlReport;
use SebastianBergmann\CodeCoverage\Report\PHP as PhpReport;
use SebastianBergmann\CodeCoverage\Report\Text as TextReport;
Expand Down Expand Up @@ -450,6 +451,12 @@ public function run(TestSuite $suite): TestResult
$this->codeCoverageGenerationStart('HTML');

try {
$customCssFile = CustomCssFile::default();

if ($this->configuration->hasCoverageHtmlCustomCssFile()) {
$customCssFile = CustomCssFile::from($this->configuration->coverageHtmlCustomCssFile());
}

$writer = new HtmlReport(
sprintf(
' and <a href="https://phpunit.de/">PHPUnit %s</a>',
Expand All @@ -465,7 +472,8 @@ public function run(TestSuite $suite): TestResult
Thresholds::from(
$this->configuration->coverageHtmlLowUpperBound(),
$this->configuration->coverageHtmlHighLowerBound()
)
),
$customCssFile
);

$writer->process(CodeCoverage::instance(), $this->configuration->coverageHtml());
Expand Down
1 change: 1 addition & 0 deletions tests/unit/TextUI/XmlConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ public function testCodeCoverageConfigurationIsReadCorrectly(): void
$this->assertSame($defaultColors->successHigh(), $codeCoverage->html()->colorSuccessHigh());
$this->assertSame($defaultColors->warning(), $codeCoverage->html()->colorWarning());
$this->assertSame($defaultColors->danger(), $codeCoverage->html()->colorDanger());
$this->assertFalse($codeCoverage->html()->hasCustomCssFile());

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

0 comments on commit 8fdacbb

Please sign in to comment.