Skip to content

Core\Test\Summary::class

$summary->class(): string

Summary::class() returns the name of the test class, with its namespace. For a class CartTest in the namespace Shop\Tests, the result is Shop\Tests\CartTest.

You get a Summary as the parameter of a public static method with #[AfterClass]. The test runner creates it after the last test of the class and passes it to that method. You cannot create a Summary yourself, and your own class cannot extend it.

Use class() to label a report about the whole class, such as a heading or a file name.

The examples below show a heading for each test class, the class name without its namespace, and one report file for each class.

Return value

Returns string. The class name, with its namespace.

Examples

A heading for each test class

<?nvs
// Shows an `#[AfterClass]` method that prints a heading with the class name and
// its counts. A long test log is then easy to read class by class.
use Core\Test;
use Core\Test\AfterClass;
use Core\Test\Summary;
class Heading
{
public static function of(string $class, int $passed, int $failed): string
{
return '== ' . $class . ': ' . $passed . ' passed, ' . $failed . ' failed ==';
}
}
class Basket
{
public static function count(array<string> $items): uint
{
return Core\Arr::count($items);
}
}
class BasketTest
{
#[Test]
public function itCountsTheItems(): void
{
Test::assertSame(Basket::count(['tea', 'milk']), 2);
}
// `class()` returns the name of this test class, here `BasketTest`.
#[AfterClass]
public static function printHeading(Summary $summary): void
{
echo Heading::of($summary->class(), $summary->passed(), $summary->failed()), "\n";
}
}
// This prints `== BasketTest: 1 passed, 0 failed ==`.
echo Heading::of('BasketTest', 1, 0), "\n";
Output
== BasketTest: 1 passed, 0 failed ==

The class name without its namespace

<?nvs
// Shows how to remove the namespace from the class name of a `Summary`. The
// short name is easier to read in a narrow terminal.
namespace Shop\Tests;
use Core\Arr;
use Core\Str;
use Core\Test;
use Core\Test\AfterClass;
use Core\Test\Summary;
class ShortName
{
// The last part after a backslash is the class name alone.
public static function of(string $class): string
{
return Arr::last(Str::split($class, "\\")) ?? $class;
}
}
class CouponTest
{
#[Test]
public function itAcceptsUpperCase(): void
{
Test::assertSame(Str::upper('summer10'), 'SUMMER10');
}
// `class()` returns `Shop\Tests\CouponTest` here.
#[AfterClass]
public static function printResult(Summary $summary): void
{
echo ShortName::of($summary->class()), ': ', $summary->failed(), " failed\n";
}
}
// This prints `CouponTest`.
echo ShortName::of("Shop\\Tests\\CouponTest"), "\n";
Output
CouponTest

One report file for each class

<?nvs
// Shows an `#[AfterClass]` method that writes a small report file for its class.
// A build server can keep these files and show them next to the build.
namespace Shop\Tests;
use Core\IO;
use Core\Path;
use Core\Str;
use Core\Test;
use Core\Test\AfterClass;
use Core\Test\Summary;
class Report
{
// A backslash is not allowed in a file name on every system, so it becomes `-`.
public static function fileFor(string $class): string
{
return 'reports/' . Str::replace($class, "\\", '-') . '.txt';
}
public static function text(int $passed, int $failed, int $skipped): string
{
return 'passed: ' . $passed . "\nfailed: " . $failed . "\nskipped: " . $skipped . "\n";
}
}
class RefundTest
{
#[Test]
public function itRefundsTheFullPrice(): void
{
Test::assertSame(2490 - 2490, 0);
}
#[AfterClass]
public static function writeReport(Summary $summary): void
{
string $text = Report::text($summary->passed(), $summary->failed(), $summary->skipped());
IO::write(Path::fromWorkingDirectory(Report::fileFor($summary->class())), $text);
}
}
// This prints `reports/Shop-Tests-RefundTest.txt`.
echo Report::fileFor("Shop\\Tests\\RefundTest"), "\n";
Output
reports/Shop-Tests-RefundTest.txt