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

PHPUnit Legacy Versions Adapter

As you're here, you are probably well aware that PHPUnit 8+ requires common template methods like setUp() or tearDown() to have a void return type declaration, which methods naturally break anything below PHP 7.1.

Although it is not a big deal to automatically update your code to use these return type declaration with help from the likes of PHP-CS-Fixer or Rector, it might become a problem if, for whatever unfortunate (but, hopefully, lucrative) reasons, you have to ensure your code is working under PHP 7.0 or PHP 5.6, all the while wanting using the best world can give you in the more-less recent versions of PHPUnit.

In this case, you'll have two problems. One, newer versions of PHPUnit do not have old assertions, but you can find a way around this, and another, as mentioned, newer versions of PHPUnit require void return type declarations for the convenient template methods, and then you're stuck because rewriting tests to work without these template methods is a major pain and might be impossible even. And then this small library comes to save your day!

composer require --dev sanmai/phpunit-legacy-adapter:"^6.4 || ^8.2.1"

How to use

First, update your tests to extend from \LegacyPHPUnit\TestCase instead of \PHPUnit\Framework\TestCase:

- class MyTest extends \PHPUnit\Framework\TestCase
+ class MyTest extends \LegacyPHPUnit\TestCase

Then, where you had to use setUp(): void template method, use doSetUp() method, omitting all any any return types in a fully backward-compatible way.

- protected function setUp(): void
+ protected function doSetUp()

There are similar replacements for most other template method:

- public static function setUpBeforeClass(): void
+ public static function doSetUpBeforeClass()
- public static function tearDownAfterClass(): void
+ public static function doTearDownAfterClass()
- protected function setUp(): void
+ protected function doSetUp()
- protected function tearDown(): void
+ protected function doTearDown()
- protected function assertPreConditions(): void
+ protected function doAssertPreConditions()
- protected function assertPostConditions(): void
+ protected function doAssertPostConditions()

Reference

Method Replacement
setUpBeforeClass(): void doSetUpBeforeClass()
tearDownAfterClass(): void doTearDownAfterClass()
setUp(): void doSetUp()
tearDown(): void doTearDown()
assertPreConditions(): void doAssertPreConditions()
assertPostConditions(): void doAssertPostConditions()

Supported versions

Future versions will likely follow the same pattern.

What this library does not do

Although this library solves the most annoying part of the problem, there are other parts the library was not designed to cover. For example:

  • Some versions of PHPUnit allow assertContains to be used with strings, while other do not.
  • In some versions one method is called expectExceptionMessageRegExp, while in others the same method is called expectExceptionMessageMatches.
  • And so on and on.

There are polyfills for these changed methods (see below), but it should not be a big deal to write an ad hoc polyfill just for the methods you need. E.g.:

    public function __call($method, $args)
    {
        if ($method === 'assertStringContainsString') {
            $this->assertContains(...$args);
        }
        
        if ($method === 'assertIsBool') {
            $this->assertTrue(\is_bool($args[0]));
        }
        
        if ($method === 'expectExceptionMessageRegExp') {
            $this->expectExceptionMessageMatches(...$args);
        }
        
        throw new \InvalidArgumentException();
    }

If there are several modular (and not) multi-version polyfills for these, and other methods:

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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport