Search by

adachsoft / phpunit-runner-lib

Arkadiusz Adach

Library for running PHPUnit via CLI and parsing results into structured DTOs.

Package info

gitlab.com/a.adach/phpunit-runner-lib

Issues

pkg:composer/adachsoft/phpunit-runner-lib

Statistics

Installs: 11

Dependents: 1

Suggesters: 0

Stars: 0

v0.2.0 2026-09-22 06:22 UTC

This package is not auto-updated.

Last update: 2026-09-22 04:22:54 UTC


README

A PHP library for running PHPUnit through the command line and converting test output into structured results.

Features

  • Execute PHPUnit with a configurable working directory and binary path.
  • Run a complete test suite or target a specific path.
  • Support PHPUnit configuration files, filters, groups, coverage, and timeouts.
  • Capture standard output, error output, exit codes, and a human-readable summary.
  • Parse failures, errors, warnings, notices, and deprecations into file issues.
  • Work safely with relative test paths.
  • Discover available logical PHP versions and run PHPUnit with a selected version.
  • Keep PHP interpreter paths internal while exposing version metadata through DTOs.

Requirements

  • PHP 8.3 or newer.
  • PHPUnit available in the configured project.

Installation

composer require adachsoft/phpunit-runner-lib

Basic usage

<?php

declare(strict_types=1);

use AdachSoft\CommandExecutorLib\SimpleCommandExecutor;
use AdachSoft\PhpUnitRunnerLib\Config\PhpUnitRunnerConfigDto;
use AdachSoft\PhpUnitRunnerLib\Dto\RunPhpUnitRequestDto;
use AdachSoft\PhpUnitRunnerLib\Parser\PhpUnitOutputParser;
use AdachSoft\PhpUnitRunnerLib\PhpUnitRunner;

$projectRoot = dirname(__DIR__);

$runner = new PhpUnitRunner(
    new PhpUnitRunnerConfigDto(
        basePath: $projectRoot,
        phpUnitPath: 'vendor/bin/phpunit',
        defaultTimeout: 60,
    ),
    new SimpleCommandExecutor(),
    new PhpUnitOutputParser(),
);

$result = $runner->run(
    new RunPhpUnitRequestDto(
        path: 'tests',
        coverage: false,
    ),
);

if (!$result->success) {
    foreach ($result->fileIssues->all() as $issue) {
        printf("%s:%s [%s] %s\n", $issue->file, $issue->line ?? '-', $issue->type, $issue->message);
    }
}

printf("Exit code: %d\n%s\n", $result->exitCode, $result->summary);

Selecting a PHP version

Use PhpUnitRunnerFactory when the runner must discover and select a logical PHP version through the system alternatives configuration:

<?php

declare(strict_types=1);

use AdachSoft\CommandExecutorLib\SimpleCommandExecutor;
use AdachSoft\PhpUnitRunnerLib\Config\PhpUnitRunnerConfigDto;
use AdachSoft\PhpUnitRunnerLib\Dto\RunPhpUnitRequestDto;
use AdachSoft\PhpUnitRunnerLib\PhpUnitRunnerFactory;

$runner = PhpUnitRunnerFactory::create(
    new PhpUnitRunnerConfigDto(
        basePath: dirname(__DIR__),
        phpUnitPath: 'vendor/bin/phpunit',
    ),
    new SimpleCommandExecutor(),
);

foreach ($runner->listAvailableVersions()->all() as $version) {
    printf("%s%s\n", $version->version, $version->isCurrent ? ' (current)' : '');
}

$result = $runner->run(
    new RunPhpUnitRequestDto(
        path: 'tests',
        phpVersion: '8.3',
        coverage: false,
    ),
);

The selected version is validated against the available alternatives before execution. Interpreter paths are resolved internally and are not exposed by the public DTOs.

Configuration

PhpUnitRunnerConfigDto accepts the project base path, the PHPUnit binary path, an optional default timeout, and flags controlling PHPUnit notices, warnings, and deprecations.

RunPhpUnitRequestDto can target a path and can provide a PHP version, PHPUnit XML configuration, filter, group, coverage flag, and per-run timeout. The runner returns a RunPhpUnitResultDto containing the raw output, exit code, summary, success state, and a FileIssueCollection.

See the examples for focused usage patterns.

License

MIT