From b7cbbe71fdc3a9989cf7939158bcd5c5bc08822d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Sun, 24 Jan 2021 16:14:41 +0100 Subject: [PATCH] Enhancement: Rename Reporter\Reporter to Reporter\DefaultReporter and extract Reporter\Reporter interface --- CHANGELOG.md | 4 +- src/Reporter/DefaultReporter.php | 152 ++++++++++++++++++ src/Reporter/Reporter.php | 135 +--------------- ...porterTest.php => DefaultReporterTest.php} | 18 +-- 4 files changed, 166 insertions(+), 143 deletions(-) create mode 100644 src/Reporter/DefaultReporter.php rename test/Unit/Reporter/{ReporterTest.php => DefaultReporterTest.php} (97%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3da963c9..ffad0d9a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,7 +20,8 @@ For a full diff see [`7afa59c...main`][7afa59c...main]. ### Changed -* Renamed `SlowTestReporter` to `Reporter\DefaultReporter` ([#20]), by [@localheinz] +* Renamed `SlowTestReporter` to `Reporter\Reporter` ([#20]), by [@localheinz] +* Renamed `Reporter\Reporter` to `Reporter\DefaultReporter` and extracted `Reporter\Reporter` interface ([#21]), by [@localheinz] [7afa59c...main]: https://github.com/ergebnis/phpunit-slow-test-detector/compare/7afa59c...main @@ -32,5 +33,6 @@ For a full diff see [`7afa59c...main`][7afa59c...main]. [#18]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/18 [#19]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/19 [#20]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/20 +[#21]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/21 [@localheinz]: https://github.com/localheinz diff --git a/src/Reporter/DefaultReporter.php b/src/Reporter/DefaultReporter.php new file mode 100644 index 00000000..eeb64acd --- /dev/null +++ b/src/Reporter/DefaultReporter.php @@ -0,0 +1,152 @@ += $maximumNumber) { + throw Exception\MaximumNumberNotGreaterThanZero::create($maximumNumber); + } + + $this->durationComparator = new Comparator\DurationComparator(); + $this->durationFormatter = $durationFormatter; + $this->maximumDuration = $maximumDuration; + $this->maximumNumber = $maximumNumber; + } + + public function report(SlowTest ...$slowTests): string + { + if ([] === $slowTests) { + return ''; + } + + $header = $this->header(...$slowTests); + $list = $this->list(...$slowTests); + $footer = $this->footer(...$slowTests); + + return <<durationFormatter->format($this->maximumDuration); + + return <<durationComparator; + + \usort($slowTests, static function (SlowTest $one, SlowTest $two) use ($durationComparator): int { + return $durationComparator->compare( + $two->duration(), + $one->duration() + ); + }); + + $slowTestsToReport = \array_slice( + $slowTests, + 0, + $this->maximumNumber + ); + + /** @var SlowTest $slowestTest */ + $slowestTest = \reset($slowTestsToReport); + + $durationFormatter = $this->durationFormatter; + + $width = \strlen($durationFormatter->format($slowestTest->duration())); + + $items = \array_map(static function (SlowTest $slowTest) use ($durationFormatter, $width): string { + $label = \str_pad( + $durationFormatter->format($slowTest->duration()), + $width, + ' ', + \STR_PAD_LEFT + ); + + $test = $slowTest->test(); + + $testName = \sprintf( + '%s::%s', + $test->className(), + $test->methodNameWithDataSet() + ); + + return <<maximumNumber, + 0 + ); + + if (0 === $remainingCount) { + return ''; + } + + if (1 === $remainingCount) { + return <<<'TXT' + +There is one additional slow test that is not listed here. +TXT; + } + + return <<= $maximumNumber) { - throw Exception\MaximumNumberNotGreaterThanZero::create($maximumNumber); - } - - $this->durationComparator = new Comparator\DurationComparator(); - $this->durationFormatter = $durationFormatter; - $this->maximumDuration = $maximumDuration; - $this->maximumNumber = $maximumNumber; - } - - public function report(SlowTest ...$slowTests): string - { - if ([] === $slowTests) { - return ''; - } - - $header = $this->header(...$slowTests); - $list = $this->list(...$slowTests); - $footer = $this->footer(...$slowTests); - - return <<durationFormatter->format($this->maximumDuration); - - return <<durationComparator; - - \usort($slowTests, static function (SlowTest $one, SlowTest $two) use ($durationComparator): int { - return $durationComparator->compare( - $two->duration(), - $one->duration() - ); - }); - - $slowTestsToReport = \array_slice( - $slowTests, - 0, - $this->maximumNumber - ); - - /** @var SlowTest $slowestTest */ - $slowestTest = \reset($slowTestsToReport); - - $durationFormatter = $this->durationFormatter; - - $width = \strlen($durationFormatter->format($slowestTest->duration())); - - $items = \array_map(static function (SlowTest $slowTest) use ($durationFormatter, $width): string { - $label = \str_pad( - $durationFormatter->format($slowTest->duration()), - $width, - ' ', - \STR_PAD_LEFT - ); - - $test = $slowTest->test(); - - $testName = \sprintf( - '%s::%s', - $test->className(), - $test->methodNameWithDataSet() - ); - - return <<maximumNumber, - 0 - ); - - if (0 === $remainingCount) { - return ''; - } - - if (1 === $remainingCount) { - return <<<'TXT' - -There is one additional slow test that is not listed here. -TXT; - } - - return <<expectException(Exception\MaximumNumberNotGreaterThanZero::class); - new Reporter( + new DefaultReporter( $durationFormatter, $maximumDuration, $maximumCount @@ -70,7 +70,7 @@ public function testReportReturnsEmptyStringWhenNoSlowTestsHaveBeenSpecified(): ); $maximumCount = $faker->numberBetween(); - $reporter = new Reporter( + $reporter = new DefaultReporter( $durationFormatter, $maximumDuration, $maximumCount @@ -152,7 +152,7 @@ public function testReportReturnsReportWhenTheNumberOfSlowTestsIsSmallerThanTheM $maximumNumber = $faker->numberBetween(\count($slowTests) + 1); - $reporter = new Reporter( + $reporter = new DefaultReporter( $durationFormatter, $maximumDuration, $maximumNumber @@ -243,7 +243,7 @@ public function testReportReturnsReportWhenTheNumberOfSlowTestsIsEqualToTheMaxim $maximumNumber = \count($slowTests); - $reporter = new Reporter( + $reporter = new DefaultReporter( $durationFormatter, $maximumDuration, $maximumNumber @@ -334,7 +334,7 @@ public function testReportReturnsReportWhenTheNumberOfSlowTestsIsOneMoreThanTheM $maximumNumber = \count($slowTests) - 1; - $reporter = new Reporter( + $reporter = new DefaultReporter( $durationFormatter, $maximumDuration, $maximumNumber @@ -425,7 +425,7 @@ public function testReportReturnsReportWhenTheNumberOfSlowTestsIsGreaterThanTheM $maximumNumber = \count($slowTests) - 2; - $reporter = new Reporter( + $reporter = new DefaultReporter( $durationFormatter, $maximumDuration, $maximumNumber