Skip to content

Commit 1cb0438

Browse files
authored
Merge pull request #576 from ergebnis/feature/test-description
Enhancement: Implement `TestDescription`
2 parents dd37d9c + fede7ed commit 1cb0438

File tree

12 files changed

+237
-3
lines changed

12 files changed

+237
-3
lines changed

psalm-baseline.xml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<files psalm-version="5.23.1@8471a896ccea3526b26d082f4461eeea467f10a4">
2+
<files psalm-version="5.24.0@462c80e31c34e58cc4f750c656be3927e80e550e">
33
<file src="src/Collector/Collector.php">
44
<MissingReturnType>
55
<code><![CDATA[collect]]></code>
@@ -755,6 +755,11 @@
755755
<code><![CDATA[testNotLessThanOrEqualToEndReturnsException]]></code>
756756
</MissingReturnType>
757757
</file>
758+
<file src="test/Unit/Exception/InvalidTestDescriptionTest.php">
759+
<MissingReturnType>
760+
<code><![CDATA[testBlankOrEmptyReturnsException]]></code>
761+
</MissingReturnType>
762+
</file>
758763
<file src="test/Unit/Exception/InvalidTestIdentifierTest.php">
759764
<MissingReturnType>
760765
<code><![CDATA[testBlankOrEmptyReturnsException]]></code>
@@ -797,6 +802,21 @@
797802
<code><![CDATA[testCreateReturnsSlowTest]]></code>
798803
</MissingReturnType>
799804
</file>
805+
<file src="test/Unit/TestDescriptionTest.php">
806+
<InternalClass>
807+
<code><![CDATA[TestDescription::fromString($value)]]></code>
808+
<code><![CDATA[TestDescription::fromString($value)]]></code>
809+
</InternalClass>
810+
<InternalMethod>
811+
<code><![CDATA[TestDescription::fromString($value)]]></code>
812+
<code><![CDATA[TestDescription::fromString($value)]]></code>
813+
<code><![CDATA[toString]]></code>
814+
</InternalMethod>
815+
<MissingReturnType>
816+
<code><![CDATA[testFromStringRejectsInvalidValue]]></code>
817+
<code><![CDATA[testFromStringReturnsTestDescription]]></code>
818+
</MissingReturnType>
819+
</file>
800820
<file src="test/Unit/TestIdentifierTest.php">
801821
<MissingReturnType>
802822
<code><![CDATA[testFromStringRejectsInvalidValue]]></code>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Copyright (c) 2021-2024 Andreas Möller
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE.md file that was distributed with this source code.
10+
*
11+
* @see https://github.com/ergebnis/phpunit-slow-test-detector
12+
*/
13+
14+
namespace Ergebnis\PHPUnit\SlowTestDetector\Exception;
15+
16+
/**
17+
* @internal
18+
*/
19+
final class InvalidTestDescription extends \InvalidArgumentException
20+
{
21+
public static function blankOrEmpty(): self
22+
{
23+
return new self('Value cannot be blank or empty.');
24+
}
25+
}

src/Extension.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,15 @@ public function endTest(
175175
$test->getName()
176176
));
177177

178+
$testDescription = TestDescription::fromString(\sprintf(
179+
'%s::%s',
180+
\get_class($test),
181+
$test->getName()
182+
));
183+
178184
$slowTest = SlowTest::create(
179185
$testIdentifier,
186+
$testDescription,
180187
$duration,
181188
$maximumDuration
182189
);
@@ -314,9 +321,11 @@ public function executeAfterTest(
314321
}
315322

316323
$testIdentifier = TestIdentifier::fromString($test);
324+
$testDescription = TestDescription::fromString($test);
317325

318326
$slowTest = SlowTest::create(
319327
$testIdentifier,
328+
$testDescription,
320329
$duration,
321330
$maximumDuration
322331
);

src/Reporter/DefaultReporter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,10 @@ static function (Duration $maximumDuration, SlowTest $slowTest): Duration {
159159
)
160160
);
161161

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

164164
return <<<TXT
165-
{$formattedNumber}. {$formattedDuration} {$formattedMaximumDuration} {$testName}
165+
{$formattedNumber}. {$formattedDuration} {$formattedMaximumDuration} {$testDescription}
166166
TXT;
167167
}, \range(1, \count($slowTestsToReport)), $slowTestsToReport);
168168

src/SlowTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ final class SlowTest
2323
*/
2424
private $testIdentifier;
2525

26+
/**
27+
* @var TestDescription
28+
*/
29+
private $testDescription;
30+
2631
/**
2732
* @var Duration
2833
*/
@@ -35,21 +40,25 @@ final class SlowTest
3540

3641
private function __construct(
3742
TestIdentifier $testIdentifier,
43+
TestDescription $testDescription,
3844
Duration $duration,
3945
Duration $maximumDuration
4046
) {
4147
$this->testIdentifier = $testIdentifier;
48+
$this->testDescription = $testDescription;
4249
$this->duration = $duration;
4350
$this->maximumDuration = $maximumDuration;
4451
}
4552

4653
public static function create(
4754
TestIdentifier $testIdentifier,
55+
TestDescription $testDescription,
4856
Duration $duration,
4957
Duration $maximumDuration
5058
): self {
5159
return new self(
5260
$testIdentifier,
61+
$testDescription,
5362
$duration,
5463
$maximumDuration
5564
);
@@ -60,6 +69,11 @@ public function testIdentifier(): TestIdentifier
6069
return $this->testIdentifier;
6170
}
6271

72+
public function testDescription(): TestDescription
73+
{
74+
return $this->testDescription;
75+
}
76+
6377
public function duration(): Duration
6478
{
6579
return $this->duration;

src/Subscriber/Test/FinishedSubscriber.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Ergebnis\PHPUnit\SlowTestDetector\Duration;
1919
use Ergebnis\PHPUnit\SlowTestDetector\PhaseIdentifier;
2020
use Ergebnis\PHPUnit\SlowTestDetector\SlowTest;
21+
use Ergebnis\PHPUnit\SlowTestDetector\TestDescription;
2122
use Ergebnis\PHPUnit\SlowTestDetector\TestIdentifier;
2223
use Ergebnis\PHPUnit\SlowTestDetector\Time;
2324
use Ergebnis\PHPUnit\SlowTestDetector\TimeKeeper;
@@ -82,6 +83,7 @@ public function notify(Event\Test\Finished $event): void
8283

8384
$slowTest = SlowTest::create(
8485
TestIdentifier::fromString($event->test()->id()),
86+
TestDescription::fromString($event->test()->id()),
8587
$duration,
8688
$maximumDuration
8789
);

src/TestDescription.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Copyright (c) 2021-2024 Andreas Möller
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE.md file that was distributed with this source code.
10+
*
11+
* @see https://github.com/ergebnis/phpunit-slow-test-detector
12+
*/
13+
14+
namespace Ergebnis\PHPUnit\SlowTestDetector;
15+
16+
/**
17+
* @internal
18+
*/
19+
final class TestDescription
20+
{
21+
/**
22+
* @var string
23+
*/
24+
private $value;
25+
26+
private function __construct(string $value)
27+
{
28+
$this->value = $value;
29+
}
30+
31+
/**
32+
* @throws Exception\InvalidTestDescription
33+
*/
34+
public static function fromString(string $value): self
35+
{
36+
if ('' === \trim($value)) {
37+
throw Exception\InvalidTestDescription::blankOrEmpty();
38+
}
39+
40+
return new self($value);
41+
}
42+
43+
public function toString(): string
44+
{
45+
return $this->value;
46+
}
47+
}

test/Unit/Collector/DefaultCollectorTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Ergebnis\PHPUnit\SlowTestDetector\Duration;
1818
use Ergebnis\PHPUnit\SlowTestDetector\SlowTest;
1919
use Ergebnis\PHPUnit\SlowTestDetector\Test;
20+
use Ergebnis\PHPUnit\SlowTestDetector\TestDescription;
2021
use Ergebnis\PHPUnit\SlowTestDetector\TestIdentifier;
2122
use PHPUnit\Framework;
2223

@@ -25,6 +26,7 @@
2526
*
2627
* @uses \Ergebnis\PHPUnit\SlowTestDetector\Duration
2728
* @uses \Ergebnis\PHPUnit\SlowTestDetector\SlowTest
29+
* @uses \Ergebnis\PHPUnit\SlowTestDetector\TestDescription
2830
* @uses \Ergebnis\PHPUnit\SlowTestDetector\TestIdentifier
2931
*/
3032
final class DefaultCollectorTest extends Framework\TestCase
@@ -37,12 +39,14 @@ public function testCollectCollectsSlowTests()
3739

3840
$one = SlowTest::create(
3941
TestIdentifier::fromString($faker->word()),
42+
TestDescription::fromString($faker->word()),
4043
Duration::fromMilliseconds($faker->numberBetween(0)),
4144
Duration::fromMilliseconds($faker->numberBetween(0))
4245
);
4346

4447
$two = SlowTest::create(
4548
TestIdentifier::fromString($faker->word()),
49+
TestDescription::fromString($faker->word()),
4650
Duration::fromMilliseconds($faker->numberBetween(0)),
4751
Duration::fromMilliseconds($faker->numberBetween(0))
4852
);
@@ -66,12 +70,14 @@ public function testCollectCollectsSlowerTestWithSameTestIdentifier()
6670

6771
$one = SlowTest::create(
6872
TestIdentifier::fromString($faker->word()),
73+
TestDescription::fromString($faker->word()),
6974
Duration::fromMilliseconds($faker->numberBetween(0)),
7075
Duration::fromMilliseconds($faker->numberBetween(0, 999999999 - 1))
7176
);
7277

7378
$two = SlowTest::create(
7479
$one->testIdentifier(),
80+
TestDescription::fromString($faker->word()),
7581
Duration::fromSecondsAndNanoseconds(
7682
$one->duration()->seconds(),
7783
$one->duration()->nanoseconds() + 1
@@ -97,12 +103,14 @@ public function testCollectDoesNotCollectFasterTestWithSameTestIdentifier()
97103

98104
$one = SlowTest::create(
99105
TestIdentifier::fromString($faker->word()),
106+
TestDescription::fromString($faker->word()),
100107
Duration::fromMilliseconds($faker->numberBetween(0)),
101108
Duration::fromMilliseconds($faker->numberBetween(1, 999999999))
102109
);
103110

104111
$two = SlowTest::create(
105112
$one->testIdentifier(),
113+
TestDescription::fromString($faker->word()),
106114
Duration::fromSecondsAndNanoseconds(
107115
$one->duration()->seconds(),
108116
$one->duration()->nanoseconds() - 1
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Copyright (c) 2021-2024 Andreas Möller
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE.md file that was distributed with this source code.
10+
*
11+
* @see https://github.com/ergebnis/phpunit-slow-test-detector
12+
*/
13+
14+
namespace Ergebnis\PHPUnit\SlowTestDetector\Test\Unit\Exception;
15+
16+
use Ergebnis\PHPUnit\SlowTestDetector\Exception;
17+
use PHPUnit\Framework;
18+
19+
/**
20+
* @covers \Ergebnis\PHPUnit\SlowTestDetector\Exception\InvalidTestDescription
21+
*/
22+
final class InvalidTestDescriptionTest extends Framework\TestCase
23+
{
24+
public function testBlankOrEmptyReturnsException()
25+
{
26+
$exception = Exception\InvalidTestDescription::blankOrEmpty();
27+
28+
self::assertSame('Value cannot be blank or empty.', $exception->getMessage());
29+
}
30+
}

0 commit comments

Comments
 (0)