Skip to content

Commit

Permalink
Merge pull request #576 from ergebnis/feature/test-description
Browse files Browse the repository at this point in the history
Enhancement: Implement `TestDescription`
  • Loading branch information
localheinz committed Jun 16, 2024
2 parents dd37d9c + fede7ed commit 1cb0438
Show file tree
Hide file tree
Showing 12 changed files with 237 additions and 3 deletions.
22 changes: 21 additions & 1 deletion psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.23.1@8471a896ccea3526b26d082f4461eeea467f10a4">
<files psalm-version="5.24.0@462c80e31c34e58cc4f750c656be3927e80e550e">
<file src="src/Collector/Collector.php">
<MissingReturnType>
<code><![CDATA[collect]]></code>
Expand Down Expand Up @@ -755,6 +755,11 @@
<code><![CDATA[testNotLessThanOrEqualToEndReturnsException]]></code>
</MissingReturnType>
</file>
<file src="test/Unit/Exception/InvalidTestDescriptionTest.php">
<MissingReturnType>
<code><![CDATA[testBlankOrEmptyReturnsException]]></code>
</MissingReturnType>
</file>
<file src="test/Unit/Exception/InvalidTestIdentifierTest.php">
<MissingReturnType>
<code><![CDATA[testBlankOrEmptyReturnsException]]></code>
Expand Down Expand Up @@ -797,6 +802,21 @@
<code><![CDATA[testCreateReturnsSlowTest]]></code>
</MissingReturnType>
</file>
<file src="test/Unit/TestDescriptionTest.php">
<InternalClass>
<code><![CDATA[TestDescription::fromString($value)]]></code>
<code><![CDATA[TestDescription::fromString($value)]]></code>
</InternalClass>
<InternalMethod>
<code><![CDATA[TestDescription::fromString($value)]]></code>
<code><![CDATA[TestDescription::fromString($value)]]></code>
<code><![CDATA[toString]]></code>
</InternalMethod>
<MissingReturnType>
<code><![CDATA[testFromStringRejectsInvalidValue]]></code>
<code><![CDATA[testFromStringReturnsTestDescription]]></code>
</MissingReturnType>
</file>
<file src="test/Unit/TestIdentifierTest.php">
<MissingReturnType>
<code><![CDATA[testFromStringRejectsInvalidValue]]></code>
Expand Down
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\InvalidTestDescription
*/
public static function fromString(string $value): self
{
if ('' === \trim($value)) {
throw Exception\InvalidTestDescription::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()
{
$exception = Exception\InvalidTestDescription::blankOrEmpty();

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

0 comments on commit 1cb0438

Please sign in to comment.