Skip to content

Commit

Permalink
Merge pull request #13 from ergebnis/feature/test-passed-subscriber
Browse files Browse the repository at this point in the history
Enhancement: Implement TestPassedSubscriber
  • Loading branch information
ergebnis-bot authored Jan 23, 2021
2 parents 084733b + 3a89ded commit 61302c2
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ For a full diff see [`7afa59c...main`][7afa59c...main].
* Added `SlowTest` ([#6]), by [@localheinz]
* Added `SlowTestCollector` ([#8]), by [@localheinz]
* Added `Subscriber\TestPreparedSubscriber` ([#12]), by [@localheinz]
* Added `Subscriber\TestPassedSubscriber` ([#13]), 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
[#12]: https://github.com/ergebnis/phpunit-slow-test-collector/pull/12
[#13]: https://github.com/ergebnis/phpunit-slow-test-collector/pull/13

[@localheinz]: https://github.com/localheinz
35 changes: 35 additions & 0 deletions src/Subscriber/TestPassedSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?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\Subscriber;

use Ergebnis\PHPUnit\SlowTestCollector\SlowTestCollector;
use PHPUnit\Event;

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

public function __construct(SlowTestCollector $slowTestCollector)
{
$this->slowTestCollector = $slowTestCollector;
}

public function notify(Event\Test\Passed $event): void
{
$this->slowTestCollector->testPassed(
$event->test(),
$event->telemetryInfo()->time()
);
}
}
121 changes: 121 additions & 0 deletions test/Unit/Subscriber/TestPassedSubscriberTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?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\Test\Unit\Subscriber;

use Ergebnis\PHPUnit\SlowTestCollector\SlowTest;
use Ergebnis\PHPUnit\SlowTestCollector\SlowTestCollector;
use Ergebnis\PHPUnit\SlowTestCollector\Subscriber\TestPassedSubscriber;
use Ergebnis\Test\Util;
use PHPUnit\Event;
use PHPUnit\Framework;

/**
* @internal
*
* @covers \Ergebnis\PHPUnit\SlowTestCollector\Subscriber\TestPassedSubscriber
*
* @uses \Ergebnis\PHPUnit\SlowTestCollector\SlowTest
* @uses \Ergebnis\PHPUnit\SlowTestCollector\SlowTestCollector
*/
final class TestPassedSubscriberTest extends Framework\TestCase
{
use Util\Helper;

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

$maximumDuration = Event\Telemetry\Duration::fromSeconds($faker->numberBetween(
5,
10
));

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

$preparedTest = self::createTest('test');

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

$passedTest = clone $preparedTest;

$passedTestEvent = new Event\Test\Passed(
self::createTelemetryInfo($passedTime),
$preparedTest
);

$slowTestCollector = new SlowTestCollector($maximumDuration);

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

$subscriber = new TestPassedSubscriber($slowTestCollector);

$subscriber->notify($passedTestEvent);

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

self::assertEquals($expected, $slowTestCollector->slowTests());
}

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

return new Event\Telemetry\Info(
new Event\Telemetry\Snapshot(
$time,
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()),
);
}

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

$methodNameWithDataSet = \sprintf(
'%s with data set #%d',
$methodName,
$faker->numberBetween()
);

if ($faker->boolean) {
$methodNameWithDataSet = $methodName;
}

return new Event\Code\Test(
self::class,
$methodName,
$methodNameWithDataSet
);
}
}

0 comments on commit 61302c2

Please sign in to comment.