Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Implement TestPreparedSubscriber #12

Merged
merged 1 commit into from
Jan 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ 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]

[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

[@localheinz]: https://github.com/localheinz
35 changes: 35 additions & 0 deletions src/Subscriber/TestPreparedSubscriber.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 TestPreparedSubscriber implements Event\Test\PreparedSubscriber
{
private SlowTestCollector $slowTestCollector;

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

public function notify(Event\Test\Prepared $event): void
{
$this->slowTestCollector->testPrepared(
$event->test(),
$event->telemetryInfo()->time()
);
}
}
123 changes: 123 additions & 0 deletions test/Unit/Subscriber/TestPreparedSubscriberTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?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\TestPreparedSubscriber;
use Ergebnis\Test\Util;
use PHPUnit\Event;
use PHPUnit\Framework;

/**
* @internal
*
* @covers \Ergebnis\PHPUnit\SlowTestCollector\Subscriber\TestPreparedSubscriber
*
* @uses \Ergebnis\PHPUnit\SlowTestCollector\SlowTest
* @uses \Ergebnis\PHPUnit\SlowTestCollector\SlowTestCollector
*/
final class TestPreparedSubscriberTest 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');

$preparedTestEvent = new Event\Test\Prepared(
self::createTelemetryInfo($preparedTime),
$preparedTest
);

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

$passedTest = clone $preparedTest;

$slowTestCollector = new SlowTestCollector($maximumDuration);

$subscriber = new TestPreparedSubscriber($slowTestCollector);

$subscriber->notify($preparedTestEvent);

self::assertSame([], $slowTestCollector->slowTests());

$slowTestCollector->testPassed(
$passedTest,
$passedTime
);

$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
);
}
}