generated from ergebnis/php-package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d751330
commit 1075dd0
Showing
2 changed files
with
262 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]; | ||
} | ||
} | ||
} |