From d590634ce31b2b6e9ed26c82a4446de9356bf1d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Sun, 23 Feb 2025 13:53:45 +0100 Subject: [PATCH] Enhancement: Yield --- src/Reporter/DefaultReporter.php | 71 +++++++++++--------------------- 1 file changed, 25 insertions(+), 46 deletions(-) diff --git a/src/Reporter/DefaultReporter.php b/src/Reporter/DefaultReporter.php index bd73fbe3..2841a740 100644 --- a/src/Reporter/DefaultReporter.php +++ b/src/Reporter/DefaultReporter.php @@ -16,7 +16,6 @@ use Ergebnis\PHPUnit\SlowTestDetector\Count; use Ergebnis\PHPUnit\SlowTestDetector\Formatter; use Ergebnis\PHPUnit\SlowTestDetector\MaximumCount; -use Ergebnis\PHPUnit\SlowTestDetector\SlowTest; use Ergebnis\PHPUnit\SlowTestDetector\SlowTestCount; use Ergebnis\PHPUnit\SlowTestDetector\SlowTestList; @@ -49,41 +48,27 @@ public function report(SlowTestList $slowTestList): string return ''; } - return \implode("\n", \array_merge( - $this->header($slowTestList), - $this->list($slowTestList), - $this->footer($slowTestList) - )); + return \implode("\n", \iterator_to_array($this->lines($slowTestList))); } /** - * @return list + * @return \Generator */ - private function header(SlowTestList $slowTestList): array + private function lines(SlowTestList $slowTestList): \Generator { $slowTestCount = $slowTestList->slowTestCount(); if ($slowTestCount->toCount()->equals(Count::fromInt(1))) { - return [ - 'Detected 1 test where the duration exceeded the maximum duration.', - '', - ]; - } - - return [ - \sprintf( + yield 'Detected 1 test where the duration exceeded the maximum duration.'; + } else { + yield \sprintf( 'Detected %d tests where the duration exceeded the maximum duration.', $slowTestCount->toCount()->toInt() - ), - '', - ]; - } + ); + } + + yield ''; - /** - * @return list - */ - private function list(SlowTestList $slowTestList): array - { $slowTestListThatWillBeReported = $slowTestList ->sortByTestDurationDescending() ->limitTo($this->maximumCount); @@ -105,44 +90,38 @@ private function list(SlowTestList $slowTestList): array $maximumDurationWidth ); - return \array_map(static function (int $number, SlowTest $slowTest) use ($template, $durationFormatter): string { - return \sprintf( + $number = 1; + + foreach ($slowTestListThatWillBeReported->toArray() as $slowTest) { + yield \sprintf( $template, (string) $number, $durationFormatter->format($slowTest->testDuration()->toDuration()), $durationFormatter->format($slowTest->maximumDuration()->toDuration()), $slowTest->testDescription()->toString() ); - }, \range(1, $slowTestListThatWillBeReported->slowTestCount()->toCount()->toInt()), $slowTestListThatWillBeReported->toArray()); - } - /** - * @return list - */ - private function footer(SlowTestList $slowTestList): array - { + ++$number; + } + $additionalSlowTestCount = SlowTestCount::fromCount(Count::fromInt(\max( 0, $slowTestList->slowTestCount()->toCount()->toInt() - $this->maximumCount->toCount()->toInt() ))); if ($additionalSlowTestCount->equals(SlowTestCount::fromCount(Count::fromInt(0)))) { - return []; + return; } - if ($additionalSlowTestCount->equals(SlowTestCount::fromCount(Count::fromInt(1)))) { - return [ - '', - 'There is 1 additional slow test that is not listed here.', - ]; - } + yield ''; - return [ - '', - \sprintf( + if ($additionalSlowTestCount->equals(SlowTestCount::fromCount(Count::fromInt(1)))) { + yield 'There is 1 additional slow test that is not listed here.'; + } else { + yield \sprintf( 'There are %d additional slow tests that are not listed here.', $additionalSlowTestCount->toCount()->toInt() - ), - ]; + ); + } } }