Skip to content

Commit

Permalink
Enhancement: Yield
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Feb 23, 2025
1 parent 0b8b519 commit d590634
Showing 1 changed file with 25 additions and 46 deletions.
71 changes: 25 additions & 46 deletions src/Reporter/DefaultReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<string>
* @return \Generator<string>
*/
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<string>
*/
private function list(SlowTestList $slowTestList): array
{
$slowTestListThatWillBeReported = $slowTestList
->sortByTestDurationDescending()
->limitTo($this->maximumCount);
Expand All @@ -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<string>
*/
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()
),
];
);
}
}
}

0 comments on commit d590634

Please sign in to comment.