Skip to content

Support for replacing placeholders in @testdox text with data provider values #3196

@sebastianbergmann

Description

@sebastianbergmann

By default, PHPUnit's TestDox result printer uses a test method's name:

<?php declare(strict_types=1);

use PHPUnit\Framework\TestCase;

final class RouterTest extends TestCase
{
    public function testRoutesRequest(): void
    {
        // ...
    }
}
Router
 ✔ Routes request

In the example above, the method name testRoutesRequest is displayed as Routes request. This is done by

  • removing the test prefix,
  • converting the remaining string to lowercase,
  • converting the first character to uppercase,
  • and inserting a space at camel-case word boundaries in the original string

This also works when snake-case (test_routes_request) is used instead.

Alternatively, PHPUnit's TestDox result printer case use a string provided using the @testdox annotation:

<?php declare(strict_types=1);

use PHPUnit\Framework\TestCase;

final class RouterTest extends TestCase
{
    /**
     * @testdox Routes request
     */
    public function testWithIndescriptiveName(): void
    {
        // ...
    }
}
Router
 ✔ Routes request

Things get "interesting" when we bring data providers into the mix:

<?php declare(strict_types=1);

use PHPUnit\Framework\TestCase;

final class RouterTest extends TestCase
{
    /**
     * @dataProvider routesProvider
     */
    public function testRoutesRequest(string $url, string $handler): void
    {
        // ...
    }

    public function routesProvider()
    {
        return [
            '/foo/bar' => [
                '/foo/bar',
                FooBarHandler::class,
                // ...
            ]
        ];
    }
}
Router
 ✔ Routes request data set "/foo/bar"

This is not nice to read.

This would be nice to read:

Router
 ✔ Routes /foo/bar to FooBarHandler

To make this possible, I propose the support for placeholders in @testdox strings that are replaced with values that come from a data provider:

    /**
     * @dataProvider routesProvider
     * @testdox      Routes $url to $handler
     */
    public function testRoutesRequest(string $url, string $handler): void
    {
        // ...
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    type/enhancementA new idea that should be implemented

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions