Search by

lekoala / kaly-tests

lekoala

Dependency-driven testing for PHP: compose real components, isolate with explicit resources, select from the workspace, explore without test debt.

Package info

github.com/lekoala/kaly-tests

pkg:composer/lekoala/kaly-tests

Statistics

Installs: 5

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

0.4.0 2026-12-02 08:00 UTC

README

Dependency-driven testing for PHP.

Compose the smallest useful environment, isolate mutable state, select tests for the changes in your workspace, and explore application behavior without turning every verification into a permanent test.

Kaly Tests is experimental (0.x). The public surface described in Public API and compatibility is the only compatibility promise, and it may still change between minor releases.

Why Kaly Tests?

The cost of a test is not just its assertions. It is also the environment it boots, the state it prepares, the dependencies it hides, and the maintenance it introduces.

Kaly Tests treats test definitions as PHP data and resource requirements as part of a test's signature. Expensive preparation can be shared within a worker while mutable scenario state remains isolated. A convention-based selector can choose candidate files before loading test definitions.

  • Compose: use real components; replace external boundaries when necessary.
  • Isolate: declare resources explicitly; share preparation, not mutable test state.
  • Select: use workspace changes and project conventions to limit local feedback loops.
  • Explore: use the same resource composition for temporary verification outside the runner.

Alongside your existing runner

Kaly Tests is not an all-or-nothing replacement for PHPUnit, Pest or Crucible. Keep the runner your CI already trusts, and add a resource composition next to it to compose isolated application environments, investigate real behavior and run focused checks. When an exploration reveals a regression worth protecting, keep the resulting test in whichever runner best fits your project.

The reusable resource composition is the foundation; the runner and Runtime::invoke() are two ways to use it.

Project Natural adoption
New project Kaly Tests as the main runner
Existing project Progressive adoption next to the historical runner
Agent-driven application Composition and Runtime to explore real services

What it looks like

The runnable example is a small invitation domain. The classes live in examples/src, the tests and composition in examples/invitations. They use the Kaly\Tests\Examples namespace because they ship with this repository; an application uses its own namespace and the same shape.

1. Test a real service

A test file returns definitions. This one exercises a real domain rule and needs no environment:

<?php

declare(strict_types=1);

use Kaly\Tests\Examples\Invitation;

use function Kaly\Tests\expect;
use function Kaly\Tests\test;

return [
    test('an invitation expires at its deadline', function (): void {
        $invitation = new Invitation(
            'inv-1',
            'ada@example.com',
            new DateTimeImmutable('2026-01-01T00:00:00+00:00'),
        );

        expect($invitation->isExpiredAt(new DateTimeImmutable('2026-01-01T00:00:00+00:00')))->toBeTrue();
    }),
];

examples/invitations/expiry.test.php contains the full version.

2. Compose the environment

An invitation service needs a clock and a repository. The test asks for the service and does not build it:

use Kaly\Tests\Examples\InvitationService;

return [
    test('an expired invitation cannot be accepted', function (InvitationService $invitations): void {
        $invitation = $invitations->create(
            'ada@example.com',
            expiresAt: new DateTimeImmutable('2026-01-01T00:00:00+00:00'),
        );

        expect($invitations->accept($invitation->id))->toBeFalse();
    }),
];

The composition in examples/invitations/resources.php provides it:

use Kaly\Tests\Examples\Clock;
use Kaly\Tests\Examples\FixedClock;
use Kaly\Tests\Examples\InvitationRepository;
use Kaly\Tests\Examples\InvitationService;
use Kaly\Tests\Resource\ResourceDefinitions;
use Kaly\Tests\Resource\Scope;

return ResourceDefinitions::create()
    ->factory(
        Clock::class,
        static fn (): Clock => new FixedClock(new DateTimeImmutable('2026-01-02T12:00:00+00:00')),
        scope: Scope::Worker,
    )
    ->factory(
        InvitationRepository::class,
        static fn (): InvitationRepository => new InvitationRepository(),
    )
    ->factory(
        InvitationService::class,
        static fn (InvitationRepository $repository, Clock $clock): InvitationService => new InvitationService(
            $repository,
            $clock,
        ),
    );

The test expresses the behaviour; the composition provides the environment.

3. Isolate mutable state

Clock is Worker scope: it is immutable and shared by the cases of a worker. InvitationRepository and InvitationService are Test scope: every case gets a fresh repository. The isolation is visible in a test:

test('each scenario starts with an empty invitation store', function (InvitationService $invitations): void {
    expect($invitations->all())->toBe([]);
}),

A setUp/tearDown pair usually hides this. Here the scope is declared next to the factory, and the test body stays about invitations.

4. Select the tests for a change

examples/invitations/selection.php maps the example sources to its tests:

use Kaly\Tests\Selection\SelectionConfig;

return SelectionConfig::create()
    ->tests('examples/invitations')
    ->map('examples/src', 'examples/invitations');
php bin/kaly-tests --tests=examples/invitations --changed --plan
php bin/kaly-tests --tests=examples/invitations --changed

In an application, map source directories or exact files to test directories and declare module dependencies. --changed is a conservative local aid; run the full suite before integration.

5. Explore outside the runner

The same composition is available through Runtime:

$runtime = new Runtime($resources);

$expired = $runtime->invoke(static function (InvitationService $invitations): array {
    $invitations->create(
        'ada@example.com',
        expiresAt: new DateTimeImmutable('2026-01-01T00:00:00+00:00'),
    );

    return $invitations->findExpired();
});

Run php examples/exploration/invitations.php. This is a temporary probe with no test file and no report; it uses the same clock and repository lifecycle.

6. From exploration to regression

A probe is evidence about one input and environment, not proof about all behavior. When it reveals a real defect, fix it and protect the invariant in the runner your project already trusts; the regression may stay in PHPUnit, Pest or Crucible. Reuse the composition, not the probe:

Explore with Runtime -> Discover a defect -> Fix the application -> Protect it in the permanent runner

7. Use more workers

php bin/kaly-tests --tests=examples/invitations --workers=4

Definitions are scheduled dynamically in plan order. Test resources are rebuilt per case, Worker resources stay per process, and the report keeps the same case ids and statuses as a sequential run. The default remains --workers=1.

Local workflow

composer install
php bin/kaly-tests --tests=examples/invitations
php bin/kaly-tests --tests=examples/invitations --workers=4
php bin/kaly-tests --tests=examples/invitations --changed --plan
php bin/kaly-tests --tests=examples/invitations --changed

These commands run from this development checkout. In an installed application, use vendor/bin/kaly-tests and replace examples/invitations with tests. Run the full suite before integration; a green changed-only run is not proof that unselected code is unaffected.

Documentation

Scope

Kaly Tests is not a PHPUnit compatibility layer, a mocking framework, a coverage engine, a database framework or a static analyzer. The 0.2 line is experimental. Portable validation evidence is an evaluation spike rather than a release commitment. Coverage, native mocking, PHPUnit compatibility, sharding and a plugin system are not part of 0.2.

Requirements

PHP 8.3 or later, Composer, and Git for workspace selection. Kaly DI is used for the invocation contract. External services are needed only by resources that request them.

The package is published as GitHub tags; Packagist is not configured yet. Install a tagged release through a Composer VCS repository, or use a checkout through a path repository. See Getting started.

Examples

The examples/ directory contains runnable examples for a development checkout: the invitation domain used above, the basic and resource examples, the exploration script, and the PostgreSQL composition. Its support classes are loaded through autoload-dev, so they are not executable from an installed dependency.

Contributing

Development notes are kept in CONTRIBUTING.md and AGENTS.md in the development repository. The historical spike lives in a separate repository; this repository contains the product, not its entire research history. docs/design.md records the exact source commit and the evidence carried over from it.