Skip to content

Commit

Permalink
Enhancement: Implement TestDescription
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Jun 16, 2024
1 parent dd37d9c commit a118f92
Show file tree
Hide file tree
Showing 11 changed files with 216 additions and 2 deletions.
25 changes: 25 additions & 0 deletions src/Exception/InvalidTestDescription.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2021-2024 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 InvalidTestDescription extends \InvalidArgumentException
{
public static function blankOrEmpty(): self
{
return new self('Value cannot be blank or empty.');
}
}
9 changes: 9 additions & 0 deletions src/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,15 @@ public function endTest(
$test->getName()
));

$testDescription = TestDescription::fromString(\sprintf(
'%s::%s',
\get_class($test),
$test->getName()
));

$slowTest = SlowTest::create(
$testIdentifier,
$testDescription,
$duration,
$maximumDuration
);
Expand Down Expand Up @@ -314,9 +321,11 @@ public function executeAfterTest(
}

$testIdentifier = TestIdentifier::fromString($test);
$testDescription = TestDescription::fromString($test);

$slowTest = SlowTest::create(
$testIdentifier,
$testDescription,
$duration,
$maximumDuration
);
Expand Down
4 changes: 2 additions & 2 deletions src/Reporter/DefaultReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,10 @@ static function (Duration $maximumDuration, SlowTest $slowTest): Duration {
)
);

$testName = $slowTest->testIdentifier()->toString();
$testDescription = $slowTest->testDescription()->toString();

return <<<TXT
{$formattedNumber}. {$formattedDuration} {$formattedMaximumDuration} {$testName}
{$formattedNumber}. {$formattedDuration} {$formattedMaximumDuration} {$testDescription}
TXT;
}, \range(1, \count($slowTestsToReport)), $slowTestsToReport);

Expand Down
14 changes: 14 additions & 0 deletions src/SlowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ final class SlowTest
*/
private $testIdentifier;

/**
* @var TestDescription
*/
private $testDescription;

/**
* @var Duration
*/
Expand All @@ -35,21 +40,25 @@ final class SlowTest

private function __construct(
TestIdentifier $testIdentifier,
TestDescription $testDescription,
Duration $duration,
Duration $maximumDuration
) {
$this->testIdentifier = $testIdentifier;
$this->testDescription = $testDescription;
$this->duration = $duration;
$this->maximumDuration = $maximumDuration;
}

public static function create(
TestIdentifier $testIdentifier,
TestDescription $testDescription,
Duration $duration,
Duration $maximumDuration
): self {
return new self(
$testIdentifier,
$testDescription,
$duration,
$maximumDuration
);
Expand All @@ -60,6 +69,11 @@ public function testIdentifier(): TestIdentifier
return $this->testIdentifier;
}

public function testDescription(): TestDescription
{
return $this->testDescription;
}

public function duration(): Duration
{
return $this->duration;
Expand Down
2 changes: 2 additions & 0 deletions src/Subscriber/Test/FinishedSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Ergebnis\PHPUnit\SlowTestDetector\Duration;
use Ergebnis\PHPUnit\SlowTestDetector\PhaseIdentifier;
use Ergebnis\PHPUnit\SlowTestDetector\SlowTest;
use Ergebnis\PHPUnit\SlowTestDetector\TestDescription;
use Ergebnis\PHPUnit\SlowTestDetector\TestIdentifier;
use Ergebnis\PHPUnit\SlowTestDetector\Time;
use Ergebnis\PHPUnit\SlowTestDetector\TimeKeeper;
Expand Down Expand Up @@ -82,6 +83,7 @@ public function notify(Event\Test\Finished $event): void

$slowTest = SlowTest::create(
TestIdentifier::fromString($event->test()->id()),
TestDescription::fromString($event->test()->id()),
$duration,
$maximumDuration
);
Expand Down
47 changes: 47 additions & 0 deletions src/TestDescription.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2021-2024 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 TestDescription
{
/**
* @var string
*/
private $value;

private function __construct(string $value)
{
$this->value = $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;
}
}
8 changes: 8 additions & 0 deletions test/Unit/Collector/DefaultCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Ergebnis\PHPUnit\SlowTestDetector\Duration;
use Ergebnis\PHPUnit\SlowTestDetector\SlowTest;
use Ergebnis\PHPUnit\SlowTestDetector\Test;
use Ergebnis\PHPUnit\SlowTestDetector\TestDescription;
use Ergebnis\PHPUnit\SlowTestDetector\TestIdentifier;
use PHPUnit\Framework;

Expand All @@ -25,6 +26,7 @@
*
* @uses \Ergebnis\PHPUnit\SlowTestDetector\Duration
* @uses \Ergebnis\PHPUnit\SlowTestDetector\SlowTest
* @uses \Ergebnis\PHPUnit\SlowTestDetector\TestDescription
* @uses \Ergebnis\PHPUnit\SlowTestDetector\TestIdentifier
*/
final class DefaultCollectorTest extends Framework\TestCase
Expand All @@ -37,12 +39,14 @@ public function testCollectCollectsSlowTests()

$one = SlowTest::create(
TestIdentifier::fromString($faker->word()),
TestDescription::fromString($faker->word()),
Duration::fromMilliseconds($faker->numberBetween(0)),
Duration::fromMilliseconds($faker->numberBetween(0))
);

$two = SlowTest::create(
TestIdentifier::fromString($faker->word()),
TestDescription::fromString($faker->word()),
Duration::fromMilliseconds($faker->numberBetween(0)),
Duration::fromMilliseconds($faker->numberBetween(0))
);
Expand All @@ -66,12 +70,14 @@ public function testCollectCollectsSlowerTestWithSameTestIdentifier()

$one = SlowTest::create(
TestIdentifier::fromString($faker->word()),
TestDescription::fromString($faker->word()),
Duration::fromMilliseconds($faker->numberBetween(0)),
Duration::fromMilliseconds($faker->numberBetween(0, 999999999 - 1))
);

$two = SlowTest::create(
$one->testIdentifier(),
TestDescription::fromString($faker->word()),
Duration::fromSecondsAndNanoseconds(
$one->duration()->seconds(),
$one->duration()->nanoseconds() + 1
Expand All @@ -97,12 +103,14 @@ public function testCollectDoesNotCollectFasterTestWithSameTestIdentifier()

$one = SlowTest::create(
TestIdentifier::fromString($faker->word()),
TestDescription::fromString($faker->word()),
Duration::fromMilliseconds($faker->numberBetween(0)),
Duration::fromMilliseconds($faker->numberBetween(1, 999999999))
);

$two = SlowTest::create(
$one->testIdentifier(),
TestDescription::fromString($faker->word()),
Duration::fromSecondsAndNanoseconds(
$one->duration()->seconds(),
$one->duration()->nanoseconds() - 1
Expand Down
30 changes: 30 additions & 0 deletions test/Unit/Exception/InvalidTestDescriptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2021-2024 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 PHPUnit\Framework;

/**
* @covers \Ergebnis\PHPUnit\SlowTestDetector\Exception\InvalidTestDescription
*/
final class InvalidTestDescriptionTest extends Framework\TestCase
{
public function testBlankOrEmptyReturnsException()

Check failure on line 24 in test/Unit/Exception/InvalidTestDescriptionTest.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (7.4, locked)

MissingReturnType

test/Unit/Exception/InvalidTestDescriptionTest.php:24:21: MissingReturnType: Method Ergebnis\PHPUnit\SlowTestDetector\Test\Unit\Exception\InvalidTestDescriptionTest::testBlankOrEmptyReturnsException does not have a return type, expecting void (see https://psalm.dev/050)
{
$exception = Exception\InvalidTestDescription::blankOrEmpty();

self::assertSame('Value cannot be blank or empty.', $exception->getMessage());
}
}
Loading

0 comments on commit a118f92

Please sign in to comment.