From 5843dced0fb11c67fa3863e9ad40cfc319c32f33 Mon Sep 17 00:00:00 2001 From: Leo Viezens Date: Wed, 3 Nov 2021 11:16:06 +0100 Subject: [PATCH] Respect configuration options of text coverage (#626) * Respect configuration options of text coverage * Get coverage bounds for text coverage from html coverage config, if set Co-authored-by: Leo Viezens --- src/Coverage/CoverageReporter.php | 9 ++++ test/Unit/Coverage/CoverageReporterTest.php | 32 +++++++++++-- ...hpunit-coverage-text-show-only-summary.xml | 47 +++++++++++++++++++ test/fixtures/phpunit-fully-configured.xml | 2 +- 4 files changed, 86 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/phpunit-coverage-text-show-only-summary.xml diff --git a/src/Coverage/CoverageReporter.php b/src/Coverage/CoverageReporter.php index 584f2049..6e4f7010 100644 --- a/src/Coverage/CoverageReporter.php +++ b/src/Coverage/CoverageReporter.php @@ -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); } diff --git a/test/Unit/Coverage/CoverageReporterTest.php b/test/Unit/Coverage/CoverageReporterTest.php index 03c008ac..3d2b1472 100644 --- a/test/Unit/Coverage/CoverageReporterTest.php +++ b/test/Unit/Coverage/CoverageReporterTest.php @@ -29,6 +29,11 @@ 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); @@ -36,7 +41,7 @@ protected function setUpTest(): void __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()); } @@ -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 diff --git a/test/fixtures/phpunit-coverage-text-show-only-summary.xml b/test/fixtures/phpunit-coverage-text-show-only-summary.xml new file mode 100644 index 00000000..1872e208 --- /dev/null +++ b/test/fixtures/phpunit-coverage-text-show-only-summary.xml @@ -0,0 +1,47 @@ + + + + + ./passing_tests/GroupsTest.php + + + + + + src + + + + src/generated + src/autoload.php + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/fixtures/phpunit-fully-configured.xml b/test/fixtures/phpunit-fully-configured.xml index 1872e208..13bb6b5d 100644 --- a/test/fixtures/phpunit-fully-configured.xml +++ b/test/fixtures/phpunit-fully-configured.xml @@ -31,7 +31,7 @@ - +