Skip to content

Towards a better plugin infrastructure #3002

@sebastianbergmann

Description

@sebastianbergmann

It should be easier to extend PHPUnit's test runner. More importantly, it should be possible to do so without abusing the TestListener interface.

In a first step, two hooks should be implemented: one that is triggered before the first test is executed and one that is triggered after the last test is executed. This is simple enough to implement and has been requested a couple of times over the years.

Another possible hook could be test filtering and ordering for #2660.

I currently envision the following:

namespace PHPUnit\Runner;

interface Hook
{
}
namespace PHPUnit\Runner;

interface BeforeFirstTestHook extends Hook
{
    public function executeBeforeFirstTest(): void;
}
namespace PHPUnit\Runner;

interface AfterLastTestHook extends Hook
{
    public function executeAfterLastTest(): void;
}

Following the Interface Segregation Principle, the idea here is to avoid one big interface and instead have one interface per extension point at which extensions can hook into PHPUnit's test runner.

Here is an example of an extension that uses the two hooks mentioned above:

namespace vendor;

use PHPUnit\Runner\AfterLastTestHook;
use PHPUnit\Runner\BeforeFirstTestHook;

final class MyExtension implements BeforeFirstTestHook, AfterLastTestHook
{
    public function executeAfterLastTest(): void
    {
        // ...
    }

    public function executeBeforeFirstTest(): void
    {
        // ...
    }
}

Here is how you configure PHPUnit's test runner to use that extension:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/7.1/phpunit.xsd">
    <extensions>
        <extension class="vendor\MyExtension"/>
    </extensions>
</phpunit>

Metadata

Metadata

Labels

type/enhancementA new idea that should be implemented

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions