Skip to content

Enhancement: Extract TestIdentifier #356

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

Merged
merged 1 commit into from
Nov 2, 2023
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 @@ -14,6 +14,7 @@ For a full diff see [`2.3.2...main`][2.3.2...main].
- Merged `MaximumDuration` into `Duration` ([#352]), by [@localheinz]
- Renamed `MaximumCount` to `Count` ([#353]), by [@localheinz]
- Extracted `Time` ([#354]), by [@localheinz]
- Extracted `TestIdentifier` ([#355]), by [@localheinz]

### Fixed

Expand Down Expand Up @@ -180,5 +181,6 @@ For a full diff see [`7afa59c...1.0.0`][7afa59c...1.0.0].
[#352]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/352
[#353]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/353
[#354]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/354
[#355]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/355

[@localheinz]: https://github.com/localheinz
80 changes: 80 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,86 @@
<code>provideDurationAndFormattedDuration</code>
</PossiblyUnusedMethod>
</file>
<file src="test/Unit/SlowTestTest.php">
<InternalClass>
<code><![CDATA[Duration::fromMilliseconds($faker->numberBetween(0))]]></code>
<code><![CDATA[Duration::fromMilliseconds($faker->numberBetween(0))]]></code>
<code>SlowTest::fromTestIdentifierDurationAndMaximumDuration(
$testIdentifier,
$duration,
$maximumDuration,
)</code>
<code><![CDATA[TestIdentifier::fromString($faker->word())]]></code>
</InternalClass>
<InternalMethod>
<code><![CDATA[Duration::fromMilliseconds($faker->numberBetween(0))]]></code>
<code><![CDATA[Duration::fromMilliseconds($faker->numberBetween(0))]]></code>
<code>SlowTest::fromTestIdentifierDurationAndMaximumDuration(
$testIdentifier,
$duration,
$maximumDuration,
)</code>
<code><![CDATA[TestIdentifier::fromString($faker->word())]]></code>
<code>duration</code>
<code>maximumDuration</code>
<code>testIdentifier</code>
</InternalMethod>
</file>
<file src="test/Unit/TestIdentifierTest.php">
<InternalClass>
<code>TestIdentifier::fromString($value)</code>
<code>TestIdentifier::fromString($value)</code>
</InternalClass>
<InternalMethod>
<code>TestIdentifier::fromString($value)</code>
<code>TestIdentifier::fromString($value)</code>
<code>tostring</code>
</InternalMethod>
</file>
<file src="test/Unit/TimeKeeperTest.php">
<InternalClass>
<code><![CDATA[TestIdentifier::fromString($faker->word())]]></code>
<code><![CDATA[TestIdentifier::fromString($faker->word())]]></code>
<code><![CDATA[Time::fromSecondsAndNanoseconds(
$faker->numberBetween($startedTime->seconds() + 1),
$faker->numberBetween($startedTime->nanoseconds() + 1, 999_999_999),
)]]></code>
<code><![CDATA[Time::fromSecondsAndNanoseconds(
$faker->numberBetween(0),
$faker->numberBetween(0, 999_999_999 - 1),
)]]></code>
<code><![CDATA[Time::fromSecondsAndNanoseconds(
$faker->numberBetween(0),
$faker->numberBetween(0, 999_999_999),
)]]></code>
<code>new TimeKeeper()</code>
<code>new TimeKeeper()</code>
</InternalClass>
<InternalMethod>
<code><![CDATA[TestIdentifier::fromString($faker->word())]]></code>
<code><![CDATA[TestIdentifier::fromString($faker->word())]]></code>
<code><![CDATA[Time::fromSecondsAndNanoseconds(
$faker->numberBetween($startedTime->seconds() + 1),
$faker->numberBetween($startedTime->nanoseconds() + 1, 999_999_999),
)]]></code>
<code><![CDATA[Time::fromSecondsAndNanoseconds(
$faker->numberBetween(0),
$faker->numberBetween(0, 999_999_999 - 1),
)]]></code>
<code><![CDATA[Time::fromSecondsAndNanoseconds(
$faker->numberBetween(0),
$faker->numberBetween(0, 999_999_999),
)]]></code>
<code>duration</code>
<code>nanoseconds</code>
<code>nanoseconds</code>
<code>seconds</code>
<code>seconds</code>
<code>start</code>
<code>stop</code>
<code>stop</code>
</InternalMethod>
</file>
<file src="test/Unit/TimeTest.php">
<PossiblyUnusedMethod>
<code>provideStartEndAndDuration</code>
Expand Down
2 changes: 1 addition & 1 deletion src/Collector/DefaultCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ final class DefaultCollector implements Collector

public function collect(SlowTest $slowTest): void
{
$key = $slowTest->test()->id();
$key = $slowTest->testIdentifier()->toString();

if (\array_key_exists($key, $this->slowTests)) {
$previousSlowTest = $this->slowTests[$key];
Expand Down
25 changes: 25 additions & 0 deletions src/Exception/InvalidTestIdentifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2021-2023 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\Exception;

/**
* @internal
*/
final class InvalidTestIdentifier extends \InvalidArgumentException
{
public static function blankOrEmpty(): self
{
return new self('Value can not be blank or empty.');
}
}
2 changes: 1 addition & 1 deletion src/Reporter/DefaultReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ static function (Duration $maximumDuration, SlowTest $slowTest): Duration {
),
);

$testName = $slowTest->test()->id();
$testName = $slowTest->testIdentifier()->toString();

return <<<TXT
{$formattedNumber}. {$formattedDuration} {$formattedMaximumDuration} {$testName}
Expand Down
14 changes: 6 additions & 8 deletions src/SlowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,33 @@

namespace Ergebnis\PHPUnit\SlowTestDetector;

use PHPUnit\Event;

/**
* @internal
*/
final class SlowTest
{
private function __construct(
private readonly Event\Code\Test $test,
private readonly TestIdentifier $testIdentifier,
private readonly Duration $duration,
private readonly Duration $maximumDuration,
) {
}

public static function fromTestDurationAndMaximumDuration(
Event\Code\Test $test,
public static function fromTestIdentifierDurationAndMaximumDuration(
TestIdentifier $testIdentifier,
Duration $duration,
Duration $maximumDuration,
): self {
return new self(
$test,
$testIdentifier,
$duration,
$maximumDuration,
);
}

public function test(): Event\Code\Test
public function testIdentifier(): TestIdentifier
{
return $this->test;
return $this->testIdentifier;
}

public function duration(): Duration
Expand Down
9 changes: 6 additions & 3 deletions src/Subscriber/TestPassedSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Ergebnis\PHPUnit\SlowTestDetector\Collector;
use Ergebnis\PHPUnit\SlowTestDetector\Duration;
use Ergebnis\PHPUnit\SlowTestDetector\SlowTest;
use Ergebnis\PHPUnit\SlowTestDetector\TestIdentifier;
use Ergebnis\PHPUnit\SlowTestDetector\Time;
use Ergebnis\PHPUnit\SlowTestDetector\TimeKeeper;
use PHPUnit\Event;
Expand All @@ -35,10 +36,12 @@ public function __construct(

public function notify(Event\Test\Passed $event): void
{
$testIdentifier = TestIdentifier::fromString($event->test()->id());

$time = $event->telemetryInfo()->time();

$duration = $this->timeKeeper->stop(
$event->test(),
$testIdentifier,
Time::fromSecondsAndNanoseconds(
$time->seconds(),
$time->nanoseconds(),
Expand All @@ -51,8 +54,8 @@ public function notify(Event\Test\Passed $event): void
return;
}

$slowTest = SlowTest::fromTestDurationAndMaximumDuration(
$event->test(),
$slowTest = SlowTest::fromTestIdentifierDurationAndMaximumDuration(
$testIdentifier,
$duration,
$maximumDuration,
);
Expand Down
3 changes: 2 additions & 1 deletion src/Subscriber/TestPreparedSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Ergebnis\PHPUnit\SlowTestDetector\Subscriber;

use Ergebnis\PHPUnit\SlowTestDetector\TestIdentifier;
use Ergebnis\PHPUnit\SlowTestDetector\Time;
use Ergebnis\PHPUnit\SlowTestDetector\TimeKeeper;
use PHPUnit\Event;
Expand All @@ -31,7 +32,7 @@ public function notify(Event\Test\Prepared $event): void
$time = $event->telemetryInfo()->time();

$this->timeKeeper->start(
$event->test(),
TestIdentifier::fromString($event->test()->id()),
Time::fromSecondsAndNanoseconds(
$time->seconds(),
$time->nanoseconds(),
Expand Down
41 changes: 41 additions & 0 deletions src/TestIdentifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2021-2023 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;

/**
* @internal
*/
final class TestIdentifier
{
private function __construct(private readonly string $value)
{
}

/**
* @throws Exception\InvalidTestIdentifier
*/
public static function fromString(string $value): self
{
if ('' === \trim($value)) {
throw Exception\InvalidTestIdentifier::blankOrEmpty();
}

return new self($value);
}

public function toString(): string
{
return $this->value;
}
}
10 changes: 4 additions & 6 deletions src/TimeKeeper.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

namespace Ergebnis\PHPUnit\SlowTestDetector;

use PHPUnit\Event;

/**
* @internal
*/
Expand All @@ -26,19 +24,19 @@ final class TimeKeeper
private array $startedTimes = [];

public function start(
Event\Code\Test $test,
TestIdentifier $testIdentifier,
Time $startedTime,
): void {
$key = $test->id();
$key = $testIdentifier->toString();

$this->startedTimes[$key] = $startedTime;
}

public function stop(
Event\Code\Test $test,
TestIdentifier $testIdentifier,
Time $stoppedTime,
): Duration {
$key = $test->id();
$key = $testIdentifier->toString();

if (!\array_key_exists($key, $this->startedTimes)) {
return Duration::fromSecondsAndNanoseconds(
Expand Down
31 changes: 31 additions & 0 deletions test/Unit/Exception/InvalidTestIdentifierTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2021-2023 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\Exception;

use Ergebnis\PHPUnit\SlowTestDetector\Exception;
use Ergebnis\PHPUnit\SlowTestDetector\Test;
use PHPUnit\Framework;

#[Framework\Attributes\CoversClass(Exception\InvalidTestIdentifier::class)]
final class InvalidTestIdentifierTest extends Framework\TestCase
{
use Test\Util\Helper;

public function testBlankOrEmptyReturnsException(): void
{
$exception = Exception\InvalidTestIdentifier::blankOrEmpty();

self::assertSame('Value can not be blank or empty.', $exception->getMessage());
}
}
45 changes: 45 additions & 0 deletions test/Unit/SlowTestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2021-2023 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
*/

use Ergebnis\PHPUnit\SlowTestDetector\Duration;
use Ergebnis\PHPUnit\SlowTestDetector\SlowTest;
use Ergebnis\PHPUnit\SlowTestDetector\Test;
use Ergebnis\PHPUnit\SlowTestDetector\TestIdentifier;
use PHPUnit\Framework;

#[Framework\Attributes\CoversClass(SlowTest::class)]
#[Framework\Attributes\UsesClass(Duration::class)]
#[Framework\Attributes\UsesClass(TestIdentifier::class)]
final class SlowTestTest extends Framework\TestCase
{
use Test\Util\Helper;

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

$testIdentifier = TestIdentifier::fromString($faker->word());
$duration = Duration::fromMilliseconds($faker->numberBetween(0));
$maximumDuration = Duration::fromMilliseconds($faker->numberBetween(0));

$slowTest = SlowTest::fromTestIdentifierDurationAndMaximumDuration(
$testIdentifier,
$duration,
$maximumDuration,
);

self::assertSame($testIdentifier, $slowTest->testIdentifier());
self::assertSame($duration, $slowTest->duration());
self::assertSame($maximumDuration, $slowTest->maximumDuration());
}
}
Loading