Skip to content

Commit

Permalink
Merge branch '11.4' into 11.5
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Nov 28, 2024
2 parents 4806a79 + 8a3471d commit a571204
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 37 deletions.
8 changes: 8 additions & 0 deletions src/Logging/TeamCity/TeamCityLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ public function testPrepared(Prepared $event): void
public function testMarkedIncomplete(MarkedIncomplete $event): void
{
if ($this->time === null) {
// @codeCoverageIgnoreStart
$this->time = $event->telemetryInfo()->time();
// @codeCoverageIgnoreEnd
}

$this->writeMessage(
Expand Down Expand Up @@ -205,7 +207,9 @@ public function testErrored(Errored $event): void
public function testFailed(Failed $event): void
{
if ($this->time === null) {
// @codeCoverageIgnoreStart
$this->time = $event->telemetryInfo()->time();
// @codeCoverageIgnoreEnd
}

$parameters = [
Expand All @@ -230,7 +234,9 @@ public function testFailed(Failed $event): void
public function testConsideredRisky(ConsideredRisky $event): void
{
if ($this->time === null) {
// @codeCoverageIgnoreStart
$this->time = $event->telemetryInfo()->time();
// @codeCoverageIgnoreEnd
}

$this->writeMessage(
Expand Down Expand Up @@ -327,7 +333,9 @@ private function writeMessage(string $eventName, array $parameters = []): void
private function duration(Event $event): int
{
if ($this->time === null) {
// @codeCoverageIgnoreStart
return 0;
// @codeCoverageIgnoreEnd
}

return (int) round($event->telemetryInfo()->time()->duration($this->time)->asFloat() * 1000);
Expand Down
40 changes: 25 additions & 15 deletions src/Logging/TestDox/NamePrettifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,21 +246,7 @@ private function mapTestMethodParameterNamesToProvidedDataValues(TestCase $test,
$value = $providedDataValues[$i++] ?? null;

if (is_object($value)) {
$reflector = new ReflectionObject($value);

if ($reflector->isEnum()) {
$enumReflector = new ReflectionEnum($value);

if ($enumReflector->isBacked()) {
$value = $value->value;
} else {
$value = $value->name;
}
} elseif ($reflector->hasMethod('__toString')) {
$value = (string) $value;
} else {
$value = $value::class;
}
$value = $this->objectToString($value);
}

if (!is_scalar($value)) {
Expand Down Expand Up @@ -295,4 +281,28 @@ private function mapTestMethodParameterNamesToProvidedDataValues(TestCase $test,

return $providedData;
}

/**
* @return non-empty-string
*/
private function objectToString(object $value): string
{
$reflector = new ReflectionObject($value);

if ($reflector->isEnum()) {
$enumReflector = new ReflectionEnum($value);

if ($enumReflector->isBacked()) {
return $value->value;
}

return $value->name;
}

if ($reflector->hasMethod('__toString')) {
return $value->__toString();
}

return $value::class;
}
}
18 changes: 18 additions & 0 deletions tests/_files/TestDoxAttributeOnTestClassTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\TestFixture\TestDox;

use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;

#[TestDox('Custom Title')]
final class TestDoxAttributeOnTestClassTest extends TestCase
{
}
74 changes: 52 additions & 22 deletions tests/unit/Logging/TestDox/NamePrettifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,73 @@
namespace PHPUnit\Logging\TestDox;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Small;
use PHPUnit\Framework\TestCase;
use PHPUnit\TestFixture\TestDox\TestDoxAttributeOnTestClassTest;

#[CoversClass(NamePrettifier::class)]
#[Group('testdox')]
#[Small]
final class NamePrettifierTest extends TestCase
{
public function testTitleHasSensibleDefaults(): void
/**
* @return non-empty-list<array{0: non-empty-string, 1: non-empty-string}>
*/
public static function classNameProvider(): array
{
$this->assertEquals('Foo', (new NamePrettifier)->prettifyTestClassName('FooTest'));
$this->assertEquals('Foo', (new NamePrettifier)->prettifyTestClassName('TestFoo'));
$this->assertEquals('Foo', (new NamePrettifier)->prettifyTestClassName('TestFooTest'));
$this->assertEquals('Foo (Test\Foo)', (new NamePrettifier)->prettifyTestClassName('Test\FooTest'));
$this->assertEquals('Foo (Tests\Foo)', (new NamePrettifier)->prettifyTestClassName('Tests\FooTest'));
$this->assertEquals('Unnamed Tests', (new NamePrettifier)->prettifyTestClassName('TestTest'));
$this->assertEquals('Système Testé', (new NamePrettifier)->prettifyTestClassName('SystèmeTestéTest'));
$this->assertEquals('Expression Évaluée', (new NamePrettifier)->prettifyTestClassName('ExpressionÉvaluéeTest'));
return [
['Foo', 'FooTest'],
['Foo', 'TestFoo'],
['Foo', 'TestsFoo'],
['Foo', 'TestFooTest'],
['Foo (Test\Foo)', 'Test\FooTest'],
['Foo (Tests\Foo)', 'Tests\FooTest'],
['Unnamed Tests', 'TestTest'],
['Système Testé', 'SystèmeTestéTest'],
['Expression Évaluée', 'ExpressionÉvaluéeTest'],
['Custom Title', TestDoxAttributeOnTestClassTest::class],
];
}

public function testTestNameIsConvertedToASentence(): void
/**
* @return non-empty-list<array{0: non-empty-string, 1: non-empty-string}>
*/
public static function methodNameProvider(): array
{
$this->assertEquals('', (new NamePrettifier)->prettifyTestMethodName(''));
$this->assertEquals('This is a test', (new NamePrettifier)->prettifyTestMethodName('testThisIsATest'));
$this->assertEquals('This is a test', (new NamePrettifier)->prettifyTestMethodName('testThisIsATest2'));
$this->assertEquals('This is a test', (new NamePrettifier)->prettifyTestMethodName('this_is_a_test'));
$this->assertEquals('This is a test', (new NamePrettifier)->prettifyTestMethodName('test_this_is_a_test'));
$this->assertEquals('Foo for bar is 0', (new NamePrettifier)->prettifyTestMethodName('testFooForBarIs0'));
$this->assertEquals('Foo for baz is 1', (new NamePrettifier)->prettifyTestMethodName('testFooForBazIs1'));
$this->assertEquals('This has a 123 in its name', (new NamePrettifier)->prettifyTestMethodName('testThisHasA123InItsName'));
$this->assertEquals('', (new NamePrettifier)->prettifyTestMethodName('test'));
return [
['', ''],
['', 'test'],
['This is a test', 'testThisIsATest'],
['This is a test', 'testThisIsATest2'],
['This is a test', 'this_is_a_test'],
['This is a test', 'test_this_is_a_test'],
['Foo for bar is 0', 'testFooForBarIs0'],
['Foo for baz is 1', 'testFooForBazIs1'],
['This has a 123 in its name', 'testThisHasA123InItsName'],
['Sets redirect header on 301', 'testSetsRedirectHeaderOn301'],
['Sets redirect header on 302', 'testSetsRedirectHeaderOn302'],
];
}

public function testTestNameIsNotGroupedWhenNotInSequence(): void
/**
* @param non-empty-string $expected
* @param non-empty-string $className
*/
#[DataProvider('classNameProvider')]
public function testNameOfTestClassCanBePrettified(string $expected, string $className): void
{
$this->assertEquals('Sets redirect header on 301', (new NamePrettifier)->prettifyTestMethodName('testSetsRedirectHeaderOn301'));
$this->assertEquals('Sets redirect header on 302', (new NamePrettifier)->prettifyTestMethodName('testSetsRedirectHeaderOn302'));
$this->assertSame($expected, (new NamePrettifier)->prettifyTestClassName($className));
}

/**
* @param non-empty-string $expected
* @param non-empty-string $methodName
*/
#[DataProvider('methodNameProvider')]
public function testNameOfTestMethodCanBePrettified(string $expected, string $methodName): void
{
$this->assertSame($expected, (new NamePrettifier)->prettifyTestMethodName($methodName));
}
}

0 comments on commit a571204

Please sign in to comment.