sanmai/phpunit-legacy-adapter
Compatibility adapter for running legacy PHPUnit test suites on newer PHPUnit versions. Helps bridge API changes, keep older tests passing, and smooth migrations without rewriting everything. Suitable for maintaining long-lived PHP projects with outdated test setups.
Installation:
composer require --dev sanmai/phpunit-legacy-adapter:"^6.4 || ^8.2.1"
Ensure compatibility with your PHPUnit version (6.x or 8.x).
Configuration:
Add the adapter to your phpunit.xml (or phpunit.xml.dist) under <php>:
<php>
<autoload>
<classmap suffix=".php" />
</autoload>
<server name="PHPUNIT_LEGACY_ADAPTER" value="1" />
</php>
First Use Case: Run tests with PHPUnit 8+ on PHP 7.0/5.6:
./vendor/bin/phpunit
The adapter automatically patches setUp(), tearDown(), and other template methods to work without void return type declarations.
Legacy Test Migration:
setUp() without void in legacy tests while adding it to new tests.CI/CD Pipeline:
# .github/workflows/tests.yml
jobs:
test-legacy:
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v4
- run: docker run --rm -v $(pwd):/app composer:php7.0 install
- run: docker run --rm -v $(pwd):/app composer:php7.0 run test
test-modern:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: composer install
- run: composer test
Hybrid Assertions:
assertEquals) with modern assertions (e.g., assertSame) in the same test suite.Test Suite Isolation:
// Legacy test (PHP 7.0/5.6)
class LegacyTest extends \PHPUnit\Framework\TestCase {
public function setUp() { ... } // No void return
}
// Modern test (PHP 8.x)
class ModernTest extends \PHPUnit\Framework\TestCase {
public function setUp(): void { ... } // Explicit void
}
Assertion Conflicts:
assertTrue() in favor of assertThat(). The adapter does not restore deprecated assertions—only template methods.assertThat() or manually add legacy assertions via assertTrue() in a trait.PHP Version Mismatch:
Static Analysis Tools:
setUp() without void as an error, even with the adapter.<!-- phpstan.neon -->
parameters:
excludePaths:
- tests/Legacy/
Autoloading Issues:
composer.json under autoload-dev:
"autoload-dev": {
"psr-4": {
"Sanmai\\LegacyAdapter\\": "vendor/sanmai/phpunit-legacy-adapter/src"
}
}
Verify Adapter Activation:
public function testAdapterLoaded() {
$this->assertTrue(class_exists(\Sanmai\LegacyAdapter\LegacyAdapter::class),
"Adapter not loaded!");
}
Log Template Method Calls:
public function setUp() {
error_log("Legacy setUp() called!");
parent::setUp(); // If using PHPUnit 8+
}
Isolate Legacy Tests:
@group legacy and filter tests:
./vendor/bin/phpunit --group legacy
Custom Template Methods:
setUpBeforeClass):
// src/LegacyAdapter/Extensions.php
namespace Sanmai\LegacyAdapter;
class Extensions {
public static function patchMethods() {
LegacyAdapter::patchMethod('setUpBeforeClass');
}
}
Extensions::patchMethods() in a bootstrap file.Assertion Backporting:
assertInternalType) by extending the adapter’s Assert class.PHPUnit Event Hooks:
$this->addListener(new \Sanmai\LegacyAdapter\Event\LegacyTestListener());
How can I help you explore Laravel packages today?