generated from ergebnis/php-package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 6
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
1b39847
commit 0d3b97b
Showing
7 changed files
with
315 additions
and
53 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,63 @@ | ||
<?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; | ||
|
||
use PHPUnit\Event; | ||
|
||
final class Collector | ||
{ | ||
/** | ||
* @var array<string, SlowTest> | ||
*/ | ||
private array $slowTests = []; | ||
|
||
public function collect(SlowTest $slowTest): void | ||
{ | ||
$key = self::key($slowTest->test()); | ||
|
||
if (\array_key_exists($key, $this->slowTests)) { | ||
$previousSlowTest = $this->slowTests[$key]; | ||
|
||
if (!$slowTest->duration()->isGreaterThan($previousSlowTest->duration())) { | ||
return; | ||
} | ||
|
||
$this->slowTests[$key] = $slowTest; | ||
|
||
return; | ||
} | ||
|
||
$this->slowTests[$key] = $slowTest; | ||
} | ||
|
||
/** | ||
* @phpstan-return list<SlowTest> | ||
* @psalm-return list<SlowTest> | ||
* | ||
* @return array<int, SlowTest> | ||
*/ | ||
public function slowTests(): array | ||
{ | ||
return \array_values($this->slowTests); | ||
} | ||
|
||
private static function key(Event\Code\Test $test): string | ||
{ | ||
return \sprintf( | ||
'%s::%s', | ||
$test->className(), | ||
$test->methodNameWithDataSet(), | ||
); | ||
} | ||
} |
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,204 @@ | ||
<?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\Test\Unit; | ||
|
||
use Ergebnis\PHPUnit\SlowTestDetector\Collector; | ||
use Ergebnis\PHPUnit\SlowTestDetector\SlowTest; | ||
use Ergebnis\PHPUnit\SlowTestDetector\Test\Fixture; | ||
use Ergebnis\Test\Util; | ||
use PHPUnit\Event; | ||
use PHPUnit\Framework; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @covers \Ergebnis\PHPUnit\SlowTestDetector\Collector | ||
* | ||
* @uses \Ergebnis\PHPUnit\SlowTestDetector\SlowTest | ||
*/ | ||
final class CollectorTest extends Framework\TestCase | ||
{ | ||
use Util\Helper; | ||
|
||
public function testDefaults() | ||
{ | ||
$collector = new Collector(); | ||
|
||
self::assertSame([], $collector->slowTests()); | ||
} | ||
|
||
public function testCollectCollectsSlowTests(): void | ||
{ | ||
$faker = self::faker(); | ||
|
||
$first = SlowTest::fromTestAndDuration( | ||
new Event\Code\Test( | ||
Fixture\ExampleTest::class, | ||
'foo', | ||
'foo with data set #123', | ||
), | ||
Event\Telemetry\Duration::fromSecondsAndNanoseconds( | ||
$faker->numberBetween(), | ||
$faker->numberBetween(0, 999_999_999) | ||
) | ||
); | ||
|
||
$second = SlowTest::fromTestAndDuration( | ||
new Event\Code\Test( | ||
Fixture\ExampleTest::class, | ||
'bar', | ||
'bar', | ||
), | ||
Event\Telemetry\Duration::fromSecondsAndNanoseconds( | ||
$faker->numberBetween(), | ||
$faker->numberBetween(0, 999_999_999) | ||
) | ||
); | ||
|
||
$third = SlowTest::fromTestAndDuration( | ||
new Event\Code\Test( | ||
Fixture\ExampleTest::class, | ||
'baz', | ||
'baz with data set "string"', | ||
), | ||
Event\Telemetry\Duration::fromSecondsAndNanoseconds( | ||
$faker->numberBetween(), | ||
$faker->numberBetween(0, 999_999_999) | ||
) | ||
); | ||
|
||
$collector = new Collector(); | ||
|
||
$collector->collect($first); | ||
$collector->collect($second); | ||
$collector->collect($third); | ||
|
||
$expected = [ | ||
$first, | ||
$second, | ||
$third, | ||
]; | ||
|
||
self::assertSame($expected, $collector->slowTests()); | ||
} | ||
|
||
public function testCollectDoesNotReplaceSlowTestWhenDurationIsLessThanPreviousSlowTest(): void | ||
{ | ||
$faker = self::faker(); | ||
|
||
$first = SlowTest::fromTestAndDuration( | ||
new Event\Code\Test( | ||
Fixture\ExampleTest::class, | ||
'foo', | ||
'foo with data set #123', | ||
), | ||
Event\Telemetry\Duration::fromSecondsAndNanoseconds( | ||
$faker->numberBetween(1), | ||
$faker->numberBetween(0, 999_999_999) | ||
) | ||
); | ||
|
||
$second = SlowTest::fromTestAndDuration( | ||
new Event\Code\Test( | ||
Fixture\ExampleTest::class, | ||
'bar', | ||
'bar', | ||
), | ||
Event\Telemetry\Duration::fromSecondsAndNanoseconds( | ||
$faker->numberBetween(), | ||
$faker->numberBetween(0, 999_999_999) | ||
) | ||
); | ||
|
||
$thirdForSameTest = SlowTest::fromTestAndDuration( | ||
new Event\Code\Test( | ||
$first->test()->className(), | ||
$first->test()->methodName(), | ||
$first->test()->methodNameWithDataSet(), | ||
), | ||
Event\Telemetry\Duration::fromSecondsAndNanoseconds( | ||
$faker->numberBetween(0, $first->duration()->seconds() - 1), | ||
$faker->numberBetween(0, 999_999_999) | ||
) | ||
); | ||
|
||
$collector = new Collector(); | ||
|
||
$collector->collect($first); | ||
$collector->collect($second); | ||
$collector->collect($thirdForSameTest); | ||
|
||
$expected = [ | ||
$first, | ||
$second, | ||
]; | ||
|
||
self::assertSame($expected, $collector->slowTests()); | ||
} | ||
|
||
public function testCollectReplacesSlowTestWhenDurationIsGreaterThanPreviousSlowTest(): void | ||
{ | ||
$faker = self::faker(); | ||
|
||
$first = SlowTest::fromTestAndDuration( | ||
new Event\Code\Test( | ||
Fixture\ExampleTest::class, | ||
'foo', | ||
'foo with data set #123', | ||
), | ||
Event\Telemetry\Duration::fromSecondsAndNanoseconds( | ||
$faker->numberBetween(), | ||
$faker->numberBetween(0, 999_999_999) | ||
) | ||
); | ||
|
||
$second = SlowTest::fromTestAndDuration( | ||
new Event\Code\Test( | ||
Fixture\ExampleTest::class, | ||
'bar', | ||
'bar', | ||
), | ||
Event\Telemetry\Duration::fromSecondsAndNanoseconds( | ||
$faker->numberBetween(), | ||
$faker->numberBetween(0, 999_999_999) | ||
) | ||
); | ||
|
||
$thirdForSameTest = SlowTest::fromTestAndDuration( | ||
new Event\Code\Test( | ||
$first->test()->className(), | ||
$first->test()->methodName(), | ||
$first->test()->methodNameWithDataSet(), | ||
), | ||
Event\Telemetry\Duration::fromSecondsAndNanoseconds( | ||
$faker->numberBetween($first->duration()->seconds() + 1), | ||
$faker->numberBetween(0, 999_999_999) | ||
) | ||
); | ||
|
||
$collector = new Collector(); | ||
|
||
$collector->collect($first); | ||
$collector->collect($second); | ||
$collector->collect($thirdForSameTest); | ||
|
||
$expected = [ | ||
$thirdForSameTest, | ||
$second, | ||
]; | ||
|
||
self::assertSame($expected, $collector->slowTests()); | ||
} | ||
|
||
} |
Oops, something went wrong.