Skip to content

Commit

Permalink
Merge pull request #26 from ergebnis/feature/time-keeper
Browse files Browse the repository at this point in the history
Enhancement: Use TimeKeeper and Collector\Collector instead of SlowTestCollector in Subscriber\TestPassedSubscriber
  • Loading branch information
ergebnis-bot authored Jan 24, 2021
2 parents 879305b + efe1215 commit 8a8344b
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 51 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ For a full diff see [`7afa59c...main`][7afa59c...main].
* Renamed `Reporter\Reporter` to `Reporter\DefaultReporter` and extracted `Reporter\Reporter` interface ([#21]), by [@localheinz]
* Renamed `Collector` to `Collector\DefaultCollector` and extracted `Collector\Collector` interface ([#24]), by [@localheinz]
* Used `TimeKeeper` instead of `SlowTestCollector` in `Subscriber\TestPreparedSubscriber` ([#25]), by [@localheinz]
* Used `TimeKeeper` and `Collector\Collector` instead of `SlowTestCollector` in `Subscriber\TestPassedSubscriber` ([#26]), by [@localheinz]

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

Expand All @@ -42,5 +43,6 @@ For a full diff see [`7afa59c...main`][7afa59c...main].
[#23]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/23
[#24]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/24
[#25]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/25
[#26]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/26

[@localheinz]: https://github.com/localheinz
34 changes: 28 additions & 6 deletions src/Subscriber/TestPassedSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,45 @@

namespace Ergebnis\PHPUnit\SlowTestDetector\Subscriber;

use Ergebnis\PHPUnit\SlowTestDetector\SlowTestCollector;
use Ergebnis\PHPUnit\SlowTestDetector\Collector;
use Ergebnis\PHPUnit\SlowTestDetector\SlowTest;
use Ergebnis\PHPUnit\SlowTestDetector\TimeKeeper;
use PHPUnit\Event;

final class TestPassedSubscriber implements Event\Test\PassedSubscriber
{
private SlowTestCollector $slowTestCollector;
private Event\Telemetry\Duration $maximumDuration;

public function __construct(SlowTestCollector $slowTestCollector)
{
$this->slowTestCollector = $slowTestCollector;
private TimeKeeper $timeKeeper;

private Collector\Collector $collector;

public function __construct(
Event\Telemetry\Duration $maximumDuration,
TimeKeeper $timeKeeper,
Collector\Collector $collector
) {
$this->maximumDuration = $maximumDuration;
$this->timeKeeper = $timeKeeper;
$this->collector = $collector;
}

public function notify(Event\Test\Passed $event): void
{
$this->slowTestCollector->testPassed(
$duration = $this->timeKeeper->stop(
$event->test(),
$event->telemetryInfo()->time()
);

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

$slowTest = SlowTest::fromTestAndDuration(
$event->test(),
$duration
);

$this->collector->collect($slowTest);
}
}
204 changes: 159 additions & 45 deletions test/Unit/Subscriber/TestPassedSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
namespace Ergebnis\PHPUnit\SlowTestDetector\Test\Unit\Subscriber;

use Ergebnis\PHPUnit\SlowTestDetector\SlowTest;
use Ergebnis\PHPUnit\SlowTestDetector\SlowTestCollector;
use Ergebnis\PHPUnit\SlowTestDetector\Subscriber\TestPassedSubscriber;
use Ergebnis\PHPUnit\SlowTestDetector\Test\Double;
use Ergebnis\PHPUnit\SlowTestDetector\Test\Fixture;
use Ergebnis\PHPUnit\SlowTestDetector\TimeKeeper;
use Ergebnis\Test\Util;
use PHPUnit\Event;
Expand All @@ -35,7 +35,7 @@ final class TestPassedSubscriberTest extends Framework\TestCase
{
use Util\Helper;

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

Expand All @@ -46,83 +46,197 @@ public function testNotifyCollectsPreparedTest(): void

$preparedTime = Event\Telemetry\HRTime::fromSecondsAndNanoseconds(
$faker->numberBetween(),
0
$faker->numberBetween(1, 999_999_999)
);

$preparedTest = self::createTest('test');
$preparedTest = new Event\Code\Test(
Fixture\ExampleTest::class,
'foo',
'foo with data set #123'
);

$passedTime = Event\Telemetry\HRTime::fromSecondsAndNanoseconds(
$preparedTime->seconds() + $maximumDuration->seconds() + 1,
0
$preparedTime->seconds() + $maximumDuration->seconds(),
$preparedTime->nanoseconds() - 1
);

$passedTest = clone $preparedTest;
$passedTest = new Event\Code\Test(
Fixture\ExampleTest::class,
'foo',
'foo with data set #123'
);

$passedTestEvent = new Event\Test\Passed(
self::createTelemetryInfo($passedTime),
$preparedTest
new Event\Telemetry\Info(
new Event\Telemetry\Snapshot(
$passedTime,
Event\Telemetry\MemoryUsage::fromBytes($faker->numberBetween()),
Event\Telemetry\MemoryUsage::fromBytes($faker->numberBetween())
),
Event\Telemetry\Duration::fromSeconds($faker->numberBetween()),
Event\Telemetry\MemoryUsage::fromBytes($faker->numberBetween()),
Event\Telemetry\Duration::fromSeconds($faker->numberBetween()),
Event\Telemetry\MemoryUsage::fromBytes($faker->numberBetween()),
),
$passedTest
);

$slowTestCollector = new SlowTestCollector(
$maximumDuration,
new TimeKeeper(),
new Double\Collector\AppendingCollector()
);
$timeKeeper = new TimeKeeper();

$slowTestCollector->testPrepared(
$timeKeeper->start(
$preparedTest,
$preparedTime
);

$subscriber = new TestPassedSubscriber($slowTestCollector);
$collector = new Double\Collector\AppendingCollector();

$subscriber->notify($passedTestEvent);
$subscriber = new TestPassedSubscriber(
$maximumDuration,
$timeKeeper,
$collector
);

$expected = [
SlowTest::fromTestAndDuration(
$passedTest,
$passedTime->duration($preparedTime)
),
];
$subscriber->notify($passedTestEvent);

self::assertEquals($expected, $slowTestCollector->slowTests());
self::assertSame([], $collector->collected());
}

private static function createTelemetryInfo(Event\Telemetry\HRTime $time): Event\Telemetry\Info
public function testNotifyDoesNotCollectSlowTestWhenDurationIsEqualToMaximumDuration(): void
{
$faker = self::faker();

return new Event\Telemetry\Info(
new Event\Telemetry\Snapshot(
$time,
$maximumDuration = Event\Telemetry\Duration::fromSeconds($faker->numberBetween(
5,
10
));

$preparedTime = Event\Telemetry\HRTime::fromSecondsAndNanoseconds(
$faker->numberBetween(),
$faker->numberBetween(1, 999_999_999)
);

$preparedTest = new Event\Code\Test(
Fixture\ExampleTest::class,
'foo',
'foo with data set #123'
);

$passedTime = Event\Telemetry\HRTime::fromSecondsAndNanoseconds(
$preparedTime->seconds() + $maximumDuration->seconds(),
$preparedTime->nanoseconds()
);

$passedTest = new Event\Code\Test(
Fixture\ExampleTest::class,
'foo',
'foo with data set #123'
);

$passedTestEvent = new Event\Test\Passed(
new Event\Telemetry\Info(
new Event\Telemetry\Snapshot(
$passedTime,
Event\Telemetry\MemoryUsage::fromBytes($faker->numberBetween()),
Event\Telemetry\MemoryUsage::fromBytes($faker->numberBetween())
),
Event\Telemetry\Duration::fromSeconds($faker->numberBetween()),
Event\Telemetry\MemoryUsage::fromBytes($faker->numberBetween()),
Event\Telemetry\Duration::fromSeconds($faker->numberBetween()),
Event\Telemetry\MemoryUsage::fromBytes($faker->numberBetween()),
Event\Telemetry\MemoryUsage::fromBytes($faker->numberBetween())
),
Event\Telemetry\Duration::fromSeconds($faker->numberBetween()),
Event\Telemetry\MemoryUsage::fromBytes($faker->numberBetween()),
Event\Telemetry\Duration::fromSeconds($faker->numberBetween()),
Event\Telemetry\MemoryUsage::fromBytes($faker->numberBetween()),
$passedTest
);

$timeKeeper = new TimeKeeper();

$timeKeeper->start(
$preparedTest,
$preparedTime
);

$collector = new Double\Collector\AppendingCollector();

$subscriber = new TestPassedSubscriber(
$maximumDuration,
$timeKeeper,
$collector
);

$subscriber->notify($passedTestEvent);

self::assertSame([], $collector->collected());
}

private static function createTest(string $methodName): Event\Code\Test
public function testNotifyCollectsSlowTestWhenDurationIsGreaterThanMaximumDuration(): void
{
$faker = self::faker();

$methodNameWithDataSet = \sprintf(
'%s with data set #%d',
$methodName,
$faker->numberBetween()
$maximumDuration = Event\Telemetry\Duration::fromSeconds($faker->numberBetween(
5,
10
));

$preparedTime = Event\Telemetry\HRTime::fromSecondsAndNanoseconds(
$faker->numberBetween(),
$faker->numberBetween(0, 999_999_998)
);

$preparedTest = new Event\Code\Test(
Fixture\ExampleTest::class,
'foo',
'foo with data set #123'
);

if ($faker->boolean) {
$methodNameWithDataSet = $methodName;
}
$passedTime = Event\Telemetry\HRTime::fromSecondsAndNanoseconds(
$preparedTime->seconds() + $maximumDuration->seconds(),
$preparedTime->nanoseconds() + 1
);

return new Event\Code\Test(
self::class,
$methodName,
$methodNameWithDataSet
$passedTest = new Event\Code\Test(
Fixture\ExampleTest::class,
'foo',
'foo with data set #123'
);

$passedTestEvent = new Event\Test\Passed(
new Event\Telemetry\Info(
new Event\Telemetry\Snapshot(
$passedTime,
Event\Telemetry\MemoryUsage::fromBytes($faker->numberBetween()),
Event\Telemetry\MemoryUsage::fromBytes($faker->numberBetween())
),
Event\Telemetry\Duration::fromSeconds($faker->numberBetween()),
Event\Telemetry\MemoryUsage::fromBytes($faker->numberBetween()),
Event\Telemetry\Duration::fromSeconds($faker->numberBetween()),
Event\Telemetry\MemoryUsage::fromBytes($faker->numberBetween()),
),
$passedTest
);

$timeKeeper = new TimeKeeper();

$timeKeeper->start(
$preparedTest,
$preparedTime
);

$collector = new Double\Collector\AppendingCollector();

$subscriber = new TestPassedSubscriber(
$maximumDuration,
$timeKeeper,
$collector
);

$subscriber->notify($passedTestEvent);

$expected = [
SlowTest::fromTestAndDuration(
$passedTest,
$passedTime->duration($preparedTime)
),
];

self::assertEquals($expected, $collector->collected());
}
}

0 comments on commit 8a8344b

Please sign in to comment.