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.
setUp() → doSetUp()). This aligns with Laravel’s backward-compatibility-first philosophy, particularly for projects constrained by legacy PHP versions (e.g., older Laravel 5.x applications).LegacyPHPUnit\TestCase) without modifying test logic, making it ideal for large codebases where test refactoring is costly. Complements Laravel’s test-first approach by allowing incremental modernization.void return type enforcement and basic assertion compatibility, not broader PHPUnit 8+ features (e.g., XML configuration, new assertions). Requires supplementary tools (e.g., phpunit-polyfills) for full migration.TestCase (extending PHPUnit\Framework\TestCase) can be swapped for LegacyPHPUnit\TestCase with minimal changes. Example:
use LegacyPHPUnit\TestCase; // Replace Illuminate\Foundation\Testing\TestCase
phpunit.xml configurations, but may require bootstrap adjustments if using custom test listeners.composer.json:
"require-dev": {
"phpunit/phpunit": "8.2.1",
"sanmai/phpunit-legacy-adapter": "^8.2.1"
}
assertTrue()) but lacks PHPUnit 8+ additions (e.g., assertSame() enhancements). Laravel’s built-in assertions (e.g., assertDatabaseHas()) may still require polyfills.setUpBeforeClass) may increase memory footprint in large test suites.RefreshDatabase or WithoutMiddleware traits might conflict with the adapter’s method overrides.phpunit-parallel; could lead to race conditions in doSetUp()/doTearDown().doAssertPreConditions()), requiring exclusions.DatabaseTransactions) that could conflict with the adapter’s method overrides?Pest testing) interact with this adapter?void return types incrementally.yoast/phpunit-polyfills).composer require --dev sanmai/phpunit-legacy-adapter:"^8.2.1" phpunit/phpunit:"^8.2.1"
phpunit.xml to ensure compatibility:
<php>
<ini name="error_reporting" value="-1"/>
<ini name="display_errors" value="0"/>
</php>
Illuminate\Foundation\Testing\TestCase with LegacyPHPUnit\TestCase:
- use Illuminate\Foundation\Testing\TestCase;
+ use LegacyPHPUnit\TestCase;
- protected function setUp(): void { ... }
+ protected function doSetUp() { ... }
--debug to catch unexpected method overrides.assertTrue() still works).| Component | Compatibility | Notes |
|---|---|---|
| Laravel TestCase | ✅ High (direct replacement) | Works if no custom traits override template methods. |
| Artisan Commands | ✅ High | No direct impact; tests run as usual. |
| Database Transactions | ⚠️ Medium | May conflict if using setUpBeforeClass/tearDownAfterClass. |
| Dusk/Browser Testing | ✅ High | No known conflicts; uses same test runner. |
| Pest Framework | ❌ Low | Pest may not inherit from LegacyPHPUnit\TestCase; requires custom setup. |
| PHPUnit Parallel | ⚠️ Low | Untested; potential race conditions in static methods. |
| Static Analyzers | ⚠️ Medium | Psalm/PHPStan may flag "undefined" methods; requires exclusions. |
--filter to isolate issues.TestCase subclasses).How can I help you explore Laravel packages today?