Skip to content

Commit

Permalink
Fix: Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Nov 3, 2023
1 parent d751330 commit 1075dd0
Show file tree
Hide file tree
Showing 2 changed files with 262 additions and 0 deletions.
5 changes: 5 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@
<code>provideDurationAndFormattedDuration</code>
</PossiblyUnusedMethod>
</file>
<file src="test/Unit/Reporter/DefaultReporterTest.php">
<PossiblyUnusedMethod>
<code>provideExpectedReportMaximumDurationMaximumCountAndSlowTests</code>
</PossiblyUnusedMethod>
</file>
<file src="test/Unit/TimeTest.php">
<PossiblyUnusedMethod>
<code>provideStartEndAndDuration</code>
Expand Down
257 changes: 257 additions & 0 deletions test/Unit/Reporter/DefaultReporterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,257 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2021-2023 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\Test\Unit\Reporter;

use Ergebnis\PHPUnit\SlowTestDetector\Comparator;
use Ergebnis\PHPUnit\SlowTestDetector\Count;
use Ergebnis\PHPUnit\SlowTestDetector\Duration;
use Ergebnis\PHPUnit\SlowTestDetector\Formatter;
use Ergebnis\PHPUnit\SlowTestDetector\Reporter;
use Ergebnis\PHPUnit\SlowTestDetector\SlowTest;
use Ergebnis\PHPUnit\SlowTestDetector\Test;
use Ergebnis\PHPUnit\SlowTestDetector\TestIdentifier;
use PHPUnit\Framework;

#[Framework\Attributes\CoversClass(Reporter\DefaultReporter::class)]
#[Framework\Attributes\UsesClass(Comparator\DurationComparator::class)]
#[Framework\Attributes\UsesClass(Count::class)]
#[Framework\Attributes\UsesClass(Duration::class)]
#[Framework\Attributes\UsesClass(Formatter\DefaultDurationFormatter::class)]
#[Framework\Attributes\UsesClass(SlowTest::class)]
#[Framework\Attributes\UsesClass(TestIdentifier::class)]
final class DefaultReporterTest extends Framework\TestCase
{
use Test\Util\Helper;

public function testReportReturnsEmptyStringWhenThereAreNoSlowTests(): void
{
$faker = self::faker();

$reporter = new Reporter\DefaultReporter(
new Formatter\DefaultDurationFormatter(),
Duration::fromMilliseconds($faker->numberBetween(0)),
Count::fromInt($faker->numberBetween(1)),
);

$report = $reporter->report();

self::assertSame('', $report);
}

#[Framework\Attributes\DataProvider('provideExpectedReportMaximumDurationMaximumCountAndSlowTests')]
public function testReportReturnsReportWhenThereAreFewerSlowTestsThanMaximumCount(
string $expectedReport,
Duration $maximumDuration,
Count $maximumCount,
SlowTest ...$slowTests,
): void {
$reporter = new Reporter\DefaultReporter(
new Formatter\DefaultDurationFormatter(),
$maximumDuration,
$maximumCount,
);

$report = $reporter->report(...$slowTests);

self::assertSame($expectedReport, $report);
}

public static function provideExpectedReportMaximumDurationMaximumCountAndSlowTests(): \Generator
{
$values = [
'header-singular' => [
<<<'TXT'
Detected 1 test that took longer than expected.
1. 0.300 (0.100) FooTest::test
TXT,
Duration::fromMilliseconds(500),
Count::fromInt(1),
[
SlowTest::create(
TestIdentifier::fromString('FooTest::test'),
Duration::fromMilliseconds(300),
Duration::fromMilliseconds(100),
),
],
],
'header-plural' => [
<<<'TXT'
Detected 2 tests that took longer than expected.
1. 0.300 (0.100) FooTest::test
2. 0.275 (0.100) BarTest::test
TXT,
Duration::fromMilliseconds(500),
Count::fromInt(2),
[
SlowTest::create(
TestIdentifier::fromString('FooTest::test'),
Duration::fromMilliseconds(300),
Duration::fromMilliseconds(100),
),
SlowTest::create(
TestIdentifier::fromString('BarTest::test'),
Duration::fromMilliseconds(275),
Duration::fromMilliseconds(100),
),
],
],
'list-sorted' => [
<<<'TXT'
Detected 3 tests that took longer than expected.
1. 0.300 (0.100) FooTest::test
2. 0.275 (0.100) BarTest::test
3. 0.250 (0.100) BazTest::test
TXT,
Duration::fromMilliseconds(500),
Count::fromInt(3),
[
SlowTest::create(
TestIdentifier::fromString('FooTest::test'),
Duration::fromMilliseconds(300),
Duration::fromMilliseconds(100),
),
SlowTest::create(
TestIdentifier::fromString('BarTest::test'),
Duration::fromMilliseconds(275),
Duration::fromMilliseconds(100),
),
SlowTest::create(
TestIdentifier::fromString('BazTest::test'),
Duration::fromMilliseconds(250),
Duration::fromMilliseconds(100),
),
],
],
'list-unsorted' => [
<<<'TXT'
Detected 3 tests that took longer than expected.
1. 0.300 (0.100) FooTest::test
2. 0.275 (0.100) BarTest::test
3. 0.250 (0.100) BazTest::test
TXT,
Duration::fromMilliseconds(500),
Count::fromInt(3),
[
SlowTest::create(
TestIdentifier::fromString('BazTest::test'),
Duration::fromMilliseconds(250),
Duration::fromMilliseconds(100),
),
SlowTest::create(
TestIdentifier::fromString('BarTest::test'),
Duration::fromMilliseconds(275),
Duration::fromMilliseconds(100),
),
SlowTest::create(
TestIdentifier::fromString('FooTest::test'),
Duration::fromMilliseconds(300),
Duration::fromMilliseconds(100),
),
],
],
'list-different-maximum-duration' => [
<<<'TXT'
Detected 3 tests that took longer than expected.
1. 20:50.000 (16:40.000) FooTest::test
2. 9:35.000 ( 8:20.000) BarTest::test
3. 0.250 ( 0.100) BazTest::test
TXT,
Duration::fromMilliseconds(500),
Count::fromInt(3),
[
SlowTest::create(
TestIdentifier::fromString('FooTest::test'),
Duration::fromMilliseconds(1_250_000),
Duration::fromMilliseconds(1_000_000),
),
SlowTest::create(
TestIdentifier::fromString('BarTest::test'),
Duration::fromMilliseconds(575_000),
Duration::fromMilliseconds(500_000),
),
SlowTest::create(
TestIdentifier::fromString('BazTest::test'),
Duration::fromMilliseconds(250),
Duration::fromMilliseconds(100),
),
],
],
'footer-singular' => [
<<<'TXT'
Detected 2 tests that took longer than expected.
1. 0.300 (0.100) FooTest::test
There is 1 additional slow test that is not listed here.
TXT,
Duration::fromMilliseconds(500),
Count::fromInt(1),
[
SlowTest::create(
TestIdentifier::fromString('FooTest::test'),
Duration::fromMilliseconds(300),
Duration::fromMilliseconds(100),
),
SlowTest::create(
TestIdentifier::fromString('BarTest::test'),
Duration::fromMilliseconds(275),
Duration::fromMilliseconds(100),
),
],
],
'footer-plural' => [
<<<'TXT'
Detected 3 tests that took longer than expected.
1. 0.300 (0.100) FooTest::test
There are 2 additional slow tests that are not listed here.
TXT,
Duration::fromMilliseconds(500),
Count::fromInt(1),
[
SlowTest::create(
TestIdentifier::fromString('FooTest::test'),
Duration::fromMilliseconds(300),
Duration::fromMilliseconds(100),
),
SlowTest::create(
TestIdentifier::fromString('BarTest::test'),
Duration::fromMilliseconds(275),
Duration::fromMilliseconds(100),
),
SlowTest::create(
TestIdentifier::fromString('BazTest::test'),
Duration::fromMilliseconds(250),
Duration::fromMilliseconds(100),
),
],
],
];

foreach ($values as $key => [$expected, $maximumDuration, $maximumCount, $slowTests]) {
yield $key => [
$expected,
$maximumDuration,
$maximumCount,
...$slowTests,
];
}
}
}

0 comments on commit 1075dd0

Please sign in to comment.