Skip to content

About

Testo plugins for thesis packages: tests in the event loop of thesis/async, real or virtual time, PHP errors as failures.

Topics

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

20 Commits

Folders and files

Repository files navigation

Thesis Testo Plugin

PHP Release License PHPStan Coverage

A read-only split of packages/testo-plugin from the thesis-php/thesis monorepo: issues and pull requests go there.

Report a bug · Request a feature · Changelog · Contributing

Plugins for Testo to test code built on thesis packages.

Installation

composer require --dev thesis/testo-plugin

Requires 64-bit PHP 8.6+.

The event loop

A wait of thesis/async outside a run of its event loop throws LogicException. EventLoopPlugin and the #[EventLoop] attribute run a test as the body of a Thesis\Async\group(), on the real clock, so the test itself can wait. They are wired as the ones of virtual time below.

Virtual time

VirtualTimePlugin and the #[VirtualTime] attribute run a test in its own Thesis\Async\virtualTime() of thesis/async: the clock jumps to the next timer whenever nothing can run, so delay(hours(1)) is over at once and durations can be asserted exactly. How the virtual clock behaves is described in the README of thesis/async.

The fibers that are ready in one tick run in an order shuffled by a seed, a random one for every test: a test that counts on one order fails sooner or later, as it would on a loaded machine. A test that fails prints its seed, and THESIS_SEED replays it:

Seed 2242033608576963125: replay with THESIS_SEED=2242033608576963125.

Every test of a suite, with the plugin in testo.php:

use Testo\Application\Config\ApplicationConfig;
use Testo\Application\Config\SuiteConfig;
use Thesis\TestoPlugin\VirtualTimePlugin;

return new ApplicationConfig(
    suites: [
        new SuiteConfig(name: 'unit', location: [__DIR__ . '/tests'], plugins: [new VirtualTimePlugin()]),
    ],
);

Or only some, with the attribute on a test class or a test:

use Testo\Assert;
use Testo\Test;
use Thesis\Async\Instant;
use Thesis\TestoPlugin\VirtualTime;
use function Thesis\Async\delay;
use function Thesis\Time\hours;

#[Test]
final class HeartbeatTest
{
    #[VirtualTime]
    public function waitsAnHour(): void
    {
        $start = Instant::now();

        delay(hours(1));

        Assert::equals(Instant::now()->since($start), hours(1));
    }
}

The lifecycle hooks of a test (#[BeforeTest], #[AfterTest]) run on its clock, and every data set gets a clock of its own. A test that has the attribute in a suite with the plugin is not wrapped twice.

PHP errors

PHP only prints a warning, a notice or a deprecation, and the test passes. PhpErrorsPlugin throws one as ErrorException where it happens, in the test and in the tasks it starts alike, so the test fails and the trace points at the cause. An error suppressed by @ is left alone, and a handler that the test sets itself sees the errors first.

Every suite, with the plugin in testo.php:

use Testo\Application\Config\ApplicationConfig;
use Testo\Application\Config\SuiteConfig;
use Thesis\TestoPlugin\PhpErrorsPlugin;

return new ApplicationConfig(
    suites: [
        new SuiteConfig(name: 'unit', location: [__DIR__ . '/tests']),
    ],
    plugins: [new PhpErrorsPlugin()],
);

About

Testo plugins for thesis packages: tests in the event loop of thesis/async, real or virtual time, PHP errors as failures.

Topics

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages