Skip to content

Commit

Permalink
Merge pull request #21 from ergebnis/feature/extract
Browse files Browse the repository at this point in the history
Enhancement: Rename Reporter\Reporter to Reporter\DefaultReporter and extract Reporter\Reporter interface
  • Loading branch information
ergebnis-bot authored Jan 24, 2021
2 parents 954f3a2 + b7cbbe7 commit cd097a9
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 143 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
152 changes: 152 additions & 0 deletions src/Reporter/DefaultReporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2021 Andreas Möller
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/ergebnis/phpunit-slow-test-detector
*/

namespace Ergebnis\PHPUnit\SlowTestDetector\Reporter;

use Ergebnis\PHPUnit\SlowTestDetector\Comparator;
use Ergebnis\PHPUnit\SlowTestDetector\Exception;
use Ergebnis\PHPUnit\SlowTestDetector\SlowTest;
use PHPUnit\Event;

final class DefaultReporter implements Reporter
{
private Comparator\DurationComparator $durationComparator;

private Event\Telemetry\DurationFormatter $durationFormatter;

private int $maximumNumber;

private Event\Telemetry\Duration $maximumDuration;

/**
* @throws Exception\MaximumNumberNotGreaterThanZero
*/
public function __construct(
Event\Telemetry\DurationFormatter $durationFormatter,
Event\Telemetry\Duration $maximumDuration,
int $maximumNumber
) {
if (0 >= $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 <<<TXT
{$header}
{$list}
{$footer}
TXT;
}

private function header(SlowTest ...$slowTests): string
{
$count = \count($slowTests);

$formattedMaximumDuration = $this->durationFormatter->format($this->maximumDuration);

return <<<TXT
Detected {$count} tests that took longer than {$formattedMaximumDuration}.
TXT;
}

private function list(SlowTest ...$slowTests): string
{
$durationComparator = $this->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 <<<TXT
{$label}: {$testName}
TXT;
}, $slowTestsToReport);

return \implode(
"\n",
$items
);
}

private function footer(SlowTest ...$slowTests): string
{
$remainingCount = \max(
\count($slowTests) - $this->maximumNumber,
0
);

if (0 === $remainingCount) {
return '';
}

if (1 === $remainingCount) {
return <<<'TXT'
There is one additional slow test that is not listed here.
TXT;
}

return <<<TXT
There are {$remainingCount} additional slow tests that are not listed here.
TXT;
}
}
135 changes: 2 additions & 133 deletions src/Reporter/Reporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,140 +13,9 @@

namespace Ergebnis\PHPUnit\SlowTestDetector\Reporter;

use Ergebnis\PHPUnit\SlowTestDetector\Comparator;
use Ergebnis\PHPUnit\SlowTestDetector\Exception;
use Ergebnis\PHPUnit\SlowTestDetector\SlowTest;
use PHPUnit\Event;

final class Reporter
interface Reporter
{
private Comparator\DurationComparator $durationComparator;

private Event\Telemetry\DurationFormatter $durationFormatter;

private int $maximumNumber;

private Event\Telemetry\Duration $maximumDuration;

/**
* @throws Exception\MaximumNumberNotGreaterThanZero
*/
public function __construct(
Event\Telemetry\DurationFormatter $durationFormatter,
Event\Telemetry\Duration $maximumDuration,
int $maximumNumber
) {
if (0 >= $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 <<<TXT
{$header}
{$list}
{$footer}
TXT;
}

private function header(SlowTest ...$slowTests): string
{
$count = \count($slowTests);

$formattedMaximumDuration = $this->durationFormatter->format($this->maximumDuration);

return <<<TXT
Detected {$count} tests that took longer than {$formattedMaximumDuration}.
TXT;
}

private function list(SlowTest ...$slowTests): string
{
$durationComparator = $this->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 <<<TXT
{$label}: {$testName}
TXT;
}, $slowTestsToReport);

return \implode(
"\n",
$items
);
}

private function footer(SlowTest ...$slowTests): string
{
$remainingCount = \max(
\count($slowTests) - $this->maximumNumber,
0
);

if (0 === $remainingCount) {
return '';
}

if (1 === $remainingCount) {
return <<<'TXT'
There is one additional slow test that is not listed here.
TXT;
}

return <<<TXT
There are {$remainingCount} additional slow tests that are not listed here.
TXT;
}
public function report(SlowTest ...$slowTests): string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

use Ergebnis\PHPUnit\SlowTestDetector\Exception;
use Ergebnis\PHPUnit\SlowTestDetector\Formatter\ToMillisecondsDurationFormatter;
use Ergebnis\PHPUnit\SlowTestDetector\Reporter\Reporter;
use Ergebnis\PHPUnit\SlowTestDetector\Reporter\DefaultReporter;
use Ergebnis\PHPUnit\SlowTestDetector\SlowTest;
use Ergebnis\PHPUnit\SlowTestDetector\Test\Fixture;
use Ergebnis\Test\Util;
Expand All @@ -25,14 +25,14 @@
/**
* @internal
*
* @covers \Ergebnis\PHPUnit\SlowTestDetector\Reporter\Reporter
* @covers \Ergebnis\PHPUnit\SlowTestDetector\Reporter\DefaultReporter
*
* @uses \Ergebnis\PHPUnit\SlowTestDetector\Comparator\DurationComparator
* @uses \Ergebnis\PHPUnit\SlowTestDetector\Exception\MaximumNumberNotGreaterThanZero
* @uses \Ergebnis\PHPUnit\SlowTestDetector\Formatter\ToMillisecondsDurationFormatter
* @uses \Ergebnis\PHPUnit\SlowTestDetector\SlowTest
*/
final class ReporterTest extends Framework\TestCase
final class DefaultReporterTest extends Framework\TestCase
{
use Util\Helper;

Expand All @@ -52,7 +52,7 @@ public function testConstructorRejectsMaximumCountLessThanOne(int $maximumCount)

$this->expectException(Exception\MaximumNumberNotGreaterThanZero::class);

new Reporter(
new DefaultReporter(
$durationFormatter,
$maximumDuration,
$maximumCount
Expand All @@ -70,7 +70,7 @@ public function testReportReturnsEmptyStringWhenNoSlowTestsHaveBeenSpecified():
);
$maximumCount = $faker->numberBetween();

$reporter = new Reporter(
$reporter = new DefaultReporter(
$durationFormatter,
$maximumDuration,
$maximumCount
Expand Down Expand Up @@ -152,7 +152,7 @@ public function testReportReturnsReportWhenTheNumberOfSlowTestsIsSmallerThanTheM

$maximumNumber = $faker->numberBetween(\count($slowTests) + 1);

$reporter = new Reporter(
$reporter = new DefaultReporter(
$durationFormatter,
$maximumDuration,
$maximumNumber
Expand Down Expand Up @@ -243,7 +243,7 @@ public function testReportReturnsReportWhenTheNumberOfSlowTestsIsEqualToTheMaxim

$maximumNumber = \count($slowTests);

$reporter = new Reporter(
$reporter = new DefaultReporter(
$durationFormatter,
$maximumDuration,
$maximumNumber
Expand Down Expand Up @@ -334,7 +334,7 @@ public function testReportReturnsReportWhenTheNumberOfSlowTestsIsOneMoreThanTheM

$maximumNumber = \count($slowTests) - 1;

$reporter = new Reporter(
$reporter = new DefaultReporter(
$durationFormatter,
$maximumDuration,
$maximumNumber
Expand Down Expand Up @@ -425,7 +425,7 @@ public function testReportReturnsReportWhenTheNumberOfSlowTestsIsGreaterThanTheM

$maximumNumber = \count($slowTests) - 2;

$reporter = new Reporter(
$reporter = new DefaultReporter(
$durationFormatter,
$maximumDuration,
$maximumNumber
Expand Down

0 comments on commit cd097a9

Please sign in to comment.