jasonmccreary / phpunit-tia
Test Impact Analysis for PHPUnit
Requires
- php: ^8.4
- ext-json: *
- symfony/process: ^7.0|^8.0
Requires (Dev)
- laravel/pint: ^1.30
- phpunit/phpunit: ^13.2.6
Conflicts
- phpunit/phpunit: <13.2.6
README
This is a port of Pest's new TIA Engine. Test Impact Analysis (TIA) greatly improves test suite performance by only running tests which relate to impacted (changed) files. This plugin brings the same performance improvements to PHPUnit.
Installation
This plugin requires PHPUnit 13 and PHP 8.4, as well as a code coverage driver (pcov or Xdebug in coverage mode) to record new coverage. If you are not running PHPUnit 13, you may use Shift to automate the upgrade.
composer require --dev jasonmccreary/phpunit-tia
Next, register the extension in your PHPUnit configuration:
<extensions> <bootstrap class="JMac\Testing\PhpUnit\Tia\Extension"> <parameter name="storage" value="global"/> </bootstrap> </extensions>
Usage
To enable TIA, add the trait to your base TestCase:
use JMac\Testing\PhpUnit\Tia\Traits\RunWithTia; abstract class TestCase extends \PHPUnit\Framework\TestCase { use RunWithTia; }
This will activate TIA for every phpunit invocation. Since third-party plugins can not change the PHPUnit test runner, TIA marks unimpacted tests as skipped (S) to achieve faster replay speeds.
Note: if your TestCase already declares setUp(), you will need to
alias and call the trait's setUp explicitly:
abstract class TestCase extends \PHPUnit\Framework\TestCase { use RunWithTia { RunWithTia::setUp as tiaSetUp; } protected function setUp(): void { $this->tiaSetUp(); // ...your own setUp logic } }
To bypass TIA, you may pass an environment variable at runtime:
PHPUNIT_TIA=0 phpunit ...
While a baseline will be established automatically, you may pass an environment variable to rebuild:
PHPUNIT_TIA_FRESH=1 phpunit ...
Note: running tests with the --fail-on-skipped or --display-skipped option will automatically bypass TIA's speed boost. You will need to drop these options to take full advantage of TIA.
CI Workflows
To use TIA in CI, your baseline graph must persist between runs. See our own GitHub Action workflow for an example. At a high level, your workflow needs to:
- Check out with full git history (
fetch-depth: 0), since TIA diffs against a baseline commit - Re-attach
HEADto the real branch name, since a detachedHEADcollapses baselines across branches - Cache TIA's storage directory (
~/.phpunit-tiaforglobalstorage, or the configured path forlocal) keyed per-branch/runner, and save it after every run
Note: TIA is intended to reduce the feedback loop during development. As such, an ideal workflow is using TIA in local environments and running the full test suite in CI environments.
Contributing
You may contribute by opening a Pull Request with your changes. All PRs should target main, include tests to verify your change, and pass the GitHub Action workflows.