Skip to content

Commit

Permalink
Merge pull request #8 from ergebnis/feature/slow-test-collector
Browse files Browse the repository at this point in the history
Enhancement: Implement SlowTestCollector
  • Loading branch information
ergebnis-bot authored Jan 23, 2021
2 parents 26f9fb1 + 9f559a9 commit 690b006
Show file tree
Hide file tree
Showing 3 changed files with 397 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ For a full diff see [`7afa59c...main`][7afa59c...main].
### Added

* Added `SlowTest` ([#6]), by [@localheinz]
* Added `SlowTestCollector` ([#8]), by [@localheinz]

[7afa59c...main]: https://github.com/ergebnis/phpunit-slow-test-collector/compare/7afa59c...main

[#6]: https://github.com/ergebnis/phpunit-slow-test-collector/pull/6
[#8]: https://github.com/ergebnis/phpunit-slow-test-collector/pull/8

[@localheinz]: https://github.com/localheinz
78 changes: 78 additions & 0 deletions src/SlowTestCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?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-collector
*/

namespace Ergebnis\PHPUnit\SlowTestCollector;

use PHPUnit\Event;

final class SlowTestCollector
{
private Event\Telemetry\Duration $maximumDuration;

/**
* @var array<string, SlowTest>
*/
private array $slowTests = [];

public function __construct(Event\Telemetry\Duration $maximumDuration)
{
$this->maximumDuration = $maximumDuration;
}

public function collect(SlowTest $slowTest): void
{
$duration = $slowTest->duration();

if (!$duration->isGreaterThan($this->maximumDuration)) {
return;
}

$test = $slowTest->test();

$key = \sprintf(
'%s::%s',
$test->className(),
$test->methodNameWithDataSet(),
);

if (\array_key_exists($key, $this->slowTests)) {
$previousSlowTest = $this->slowTests[$key];

if (!$duration->isGreaterThan($previousSlowTest->duration())) {
return;
}

$this->slowTests[$key] = $slowTest;

return;
}

$this->slowTests[$key] = $slowTest;
}

public function maximumDuration(): Event\Telemetry\Duration
{
return $this->maximumDuration;
}

/**
* @phpstan-return list<SlowTest>
* @psalm-return list<SlowTest>
*
* @return array<int, SlowTest>
*/
public function slowTests(): array
{
return \array_values($this->slowTests);
}
}
Loading

0 comments on commit 690b006

Please sign in to comment.