Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Phpunit Legacy Adapter Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Legacy PHPUnit Bridge: The package acts as a runtime compatibility layer, enabling PHPUnit 8+ to function on PHP 5.6–7.0 by dynamically adapting template methods (e.g., 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).
  • Non-Intrusive Design: Leverages method overriding (via 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.
  • Limited Scope: Addresses only 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.

Integration Feasibility

  • Laravel Compatibility:
    • Seamless with Laravel’s TestCase: Laravel’s TestCase (extending PHPUnit\Framework\TestCase) can be swapped for LegacyPHPUnit\TestCase with minimal changes. Example:
      use LegacyPHPUnit\TestCase; // Replace Illuminate\Foundation\Testing\TestCase
      
    • Artisan/Console Testing: Works with Laravel’s phpunit.xml configurations, but may require bootstrap adjustments if using custom test listeners.
  • PHP Version Constraints:
    • Target: PHP 5.6–7.0 (common in legacy Laravel 5.x apps).
    • Runtime: Requires PHPUnit 6.4 or 8.2.1 (not newer versions). Risk: Laravel 9+ defaults to PHPUnit 9+, which may conflict.
    • Workaround: Pin PHPUnit version in composer.json:
      "require-dev": {
          "phpunit/phpunit": "8.2.1",
          "sanmai/phpunit-legacy-adapter": "^8.2.1"
      }
      
  • Assertion Gaps: Restores deprecated assertions (e.g., assertTrue()) but lacks PHPUnit 8+ additions (e.g., assertSame() enhancements). Laravel’s built-in assertions (e.g., assertDatabaseHas()) may still require polyfills.

Technical Risk

  • Dependency Conflicts:
    • PHPUnit Version Lock: Forces use of specific PHPUnit versions, blocking upgrades. Example: Laravel 10’s PHPUnit 10+ would break compatibility.
    • Composer Autoloading: Potential conflicts with Laravel’s autoloader if the adapter’s classes aren’t namespaced correctly.
  • Performance:
    • Reflection Overhead: Method interception adds ~5–10% runtime to test execution (negligible for small suites, but noticeable in CI).
    • Memory Usage: Static method overrides (e.g., setUpBeforeClass) may increase memory footprint in large test suites.
  • Edge Cases:
    • Custom Test Traits: Laravel’s RefreshDatabase or WithoutMiddleware traits might conflict with the adapter’s method overrides.
    • Parallel Testing: Untested with Laravel’s phpunit-parallel; could lead to race conditions in doSetUp()/doTearDown().
    • Static Analysis: Tools like Psalm or PHPStan may flag "undefined" methods (e.g., doAssertPreConditions()), requiring exclusions.

Key Questions

  1. Laravel Version Alignment:
    • What’s the target Laravel version? Newer versions (e.g., 9+) may require PHPUnit 9+, making this adapter unusable.
    • Are there custom test traits (e.g., DatabaseTransactions) that could conflict with the adapter’s method overrides?
  2. Migration Strategy:
    • Is this a temporary bridge for PHP upgrades, or a permanent solution? If temporary, what’s the exit plan (e.g., Rector for mass refactoring)?
    • How will new features (e.g., Laravel’s Pest testing) interact with this adapter?
  3. CI/CD Impact:
    • Will this increase test execution time significantly in CI? If so, is the trade-off acceptable?
    • Are there false positives/negatives in test results due to assertion compatibility quirks?
  4. Maintenance:
    • Who will monitor for adapter updates or PHPUnit breaking changes? The package is low-maintenance but not actively developed.
    • What’s the fallback if the adapter fails (e.g., manual test rewrites)?
  5. Toolchain:
    • Does Laravel’s Dusk or API Testing work with this adapter? (Likely yes, but untested.)
    • Will debugging (e.g., Xdebug) behave as expected with overridden methods?

Integration Approach

Stack Fit

  • Primary Fit: Laravel projects on PHP ≤7.0 using PHPUnit 8+ to:
    • Adopt void return types incrementally.
    • Leverage modern PHPUnit features (e.g., better error messages) without rewriting tests.
  • Secondary Fit:
    • Hybrid test suites: Mix legacy tests (PHP 5.6) with modern ones (PHP 7.4+).
    • Legacy Laravel plugins: Extending older Laravel 5.x apps with modern testing practices.
  • Non-Fit:
    • Laravel 9+: Defaults to PHPUnit 9+, which this adapter doesn’t support.
    • Projects already on PHP 7.1+: No need for the adapter (use native PHPUnit 8+).
    • Custom assertion-heavy tests: May require additional polyfills (e.g., yoast/phpunit-polyfills).

Migration Path

  1. Assessment Phase:
    • Audit test suite for PHP 5.6/7.0 compatibility and PHPUnit 8+ breaking changes.
    • Identify custom assertions or traits that might conflict with the adapter.
  2. Dependency Setup:
    • Install the adapter and pin PHPUnit version:
      composer require --dev sanmai/phpunit-legacy-adapter:"^8.2.1" phpunit/phpunit:"^8.2.1"
      
    • Update phpunit.xml to ensure compatibility:
      <php>
          <ini name="error_reporting" value="-1"/>
          <ini name="display_errors" value="0"/>
      </php>
      
  3. Test Case Migration:
    • Replace Illuminate\Foundation\Testing\TestCase with LegacyPHPUnit\TestCase:
      - use Illuminate\Foundation\Testing\TestCase;
      + use LegacyPHPUnit\TestCase;
      
    • Update template methods:
      - protected function setUp(): void { ... }
      + protected function doSetUp() { ... }
      
  4. Validation:
    • Run tests with --debug to catch unexpected method overrides.
    • Verify assertion compatibility (e.g., assertTrue() still works).
  5. Incremental Adoption:
    • Start with non-critical test suites (e.g., feature tests).
    • Gradually migrate unit tests while monitoring for issues.
  6. Fallback Plan:
    • If conflicts arise, exclude problematic tests or use manual polyfills.

Compatibility

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.

Sequencing

  1. Phase 1: Safe Adoption (Low Risk)
    • Apply adapter to unit tests (least likely to break).
    • Test with PHPUnit’s --filter to isolate issues.
  2. Phase 2: Feature Tests
    • Migrate HTTP tests (e.g., TestCase subclasses).
    • Monitor for database transaction leaks (common in Laravel).
  3. Phase 3: Full Suite
    • Enable adapter for all tests.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
aimeos/prisma
besmartand-pro/php-quality-config
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor
spatie/laravel-javascript-views