Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
P

PHPUnit-Polyfills

> 测试质量
Open source

Set of polyfills for changed PHPUnit functionality to allow for creating PHPUnit cross-version compatible tests

186 stars0 likes2 views
WebsiteGitHub

About

Set of polyfills for changed PHPUnit functionality to allow for creating PHPUnit cross-version compatible tests

PHPUnit Polyfills

[][Packagist] [][Packagist] [Packagist]: https://packagist.org/packages/yoast/phpunit-polyfills

Set of polyfills for changed PHPUnit functionality to allow for creating PHPUnit cross-version compatible tests.

  • Requirements
  • Installation
    • Autoloading
  • Why use the PHPUnit Polyfills?
    • PHPUnit support
  • Using this library
    • Supported ways of calling the assertions
  • Features
    • Polyfill traits
    • TestCases
    • TestListener
  • Frequently Asked Questions
  • Contributing
  • License

Requirements

  • PHP 7.1 or higher.
  • PHPUnit 7.5 - 9.x, 11.x and 12.x (automatically required via Composer).

Installation

To install this package, run:

bash
composer require --dev yoast/phpunit-polyfills:"^4.0"

To update this package, run:

bash
composer update --dev yoast/phpunit-polyfills --with-dependencies

Autoloading

Make sure to:

  • Either use the Composer vendor/autoload.php file as your test bootstrap file;
  • Or require the vendor/yoast/phpunit-polyfills/phpunitpolyfills-autoload.php file in your test bootstrap.

Why use the PHPUnit Polyfills?

This library is set up to allow for creating PHPUnit cross-version compatible tests by offering a number of polyfills for functionality which was introduced, split up or renamed in PHPUnit.

Write your tests for PHPUnit 12.x and run them on PHPUnit 7.5 - 12.x

The polyfills have been setup to allow tests to be forward-compatible. What that means is, that your tests can use the assertions supported by the latest PHPUnit version, even when running on older PHPUnit versions.

This puts the burden of upgrading to use the syntax of newer PHPUnit versions at the point when you want to start running your tests on a newer version. By doing so, dropping support for an older PHPUnit version becomes as straight-forward as removing it from the version constraint in your composer.json file.

PHPUnit support

  • Releases in the 1.x series of the PHPUnit Polyfills support PHPUnit 4.8 - 9.x.
  • Releases in the 2.x series of the PHPUnit Polyfills support PHPUnit 5.7 - 10.x.
  • Releases in the 3.x series of the PHPUnit Polyfills support PHPUnit 6.4 - 11.x (but don't support running tests on PHPUnit 10).
  • Releases in the 4.x series of the PHPUnit Polyfills support PHPUnit 7.5 - 12.x (but don't support running tests on PHPUnit 10).

Please keep in mind that the PHPUnit Polyfills provide forward-compatibility. This means that features which PHPUnit no longer supports in PHPUnit 10.x, like expecting PHP deprecation notices or warnings, will not be supported in the PHPUnit Polyfills 2.x series and features not supported in PHPUnit 11.x, will not be supported in the PHPUnit Polyfills 3.x series etc.

Please refer to the PHPUnit 10 release notification/PHPUnit 10 changelog, PHPUnit 11 release notification/PHPUnit 11 changelog and/or the PHPUnit 12 release notification/PHPUnit 12 changelog to inform your decision on whether or not to upgrade (yet).

Using this library

Each of the polyfills and helpers has been setup as a trait and can be imported and used in any test file which extends the PHPUnit native TestCase class.

If the polyfill is not needed for the particular PHPUnit version on which the tests are being run, the autoloader will automatically load an empty trait with that same name, so you can safely use these traits in tests which need to be PHPUnit cross-version compatible.

php
assertContainsOnlyBool( $haystack );
        self::assertContainsNotOnlyNumeric( $haystack );
    }
}

Alternatively, you can use one of the TestCase classes provided by this library instead of using the PHPUnit native TestCase class.

In that case, all polyfills and helpers will be available whenever needed.

php
assertContainsOnlyBool( $haystack );
        self::assertContainsNotOnlyNumeric( $haystack );
    }
}

Supported ways of calling the assertions

By default, PHPUnit supports four ways of calling assertions:

  1. As a method in the TestCase class - $this->assertSomething().
  2. Statically as a method in the TestCase class - self/static/parent::assertSomething().
  3. Statically as a method of the Assert class - Assert::assertSomething().
  4. As a global function - assertSomething().

The polyfills in this library support the first two ways of calling the assertions as those are the most commonly used type of assertion calls.

For the polyfills to work, a test class is required to be a (grand-)child of the PHPUnit native TestCase class.

Features

Polyfill traits

PHPUnit :warning: The PHPUnit native implementation of these assertions is also affected by these bugs!

The shouldClosedResourceAssertionBeSkipped() helper method is therefore available cross-version.

Usage examples:

php
// Example: skipping the test completely.
if ( $this->shouldClosedResourceAssertionBeSkipped( $actual ) === true ) {
    $this->markTestSkipped('assertIs[Not]ClosedResource() cannot determine whether this resource is'
        . ' open or closed due to bugs in PHP (PHP ' . \PHP_VERSION . ').');
}

// Example: selectively skipping the assertion.
if ( self::shouldClosedResourceAssertionBeSkipped( $actual ) === false ) {
    $this->assertIsClosedResource( $actual );
}

:point_right: While this polyfill is tested extensively, testing for these kind of bugs exhaustively is hard. Please report any bugs found and include a clear code sample to reproduce the issue.

PHPUnit :information_source: In PHPUnit Polyfills 1.x and 2.x, the polyfill for this assertion had some limitations. These limitations are no longer present in PHPUnit Polyfills 3.x and the assertion now matches the PHPUnit native implementation.

PHPUnit = 11.0 |

| -------------- | --------------- | | Only one deprecation can be expected per test | Multiple deprecations can be expected per test | | The test stops running as soon as the deprecation message has been seen | The test will be executed completely, independently of the deprecation notice | | The message passed to expectUserDeprecationMessage() will be compared as a substring | The message passed to expectUserDeprecationMessage() must be an exact match | | Can expect both PHP native and user-land deprecation notices | Can only expect user-land deprecation notices, i.e. E_USER_DEPRECATED, not E_DEPRECATED |

Please keep these differences in mind when writing tests using the expectUserDeprecationMessage*() methods.

Note: on PHPUnit 9.5.x, when using the expectUserDeprecationMessage*() expectations, a "Expecting E_DEPRECATED and E_USER_DEPRECATED is deprecated and will no longer be possible in PHPUnit 10." deprecation will be shown in the test output. As long at the actual test uses the expectUserDeprecationMessage*() expectations, this depreation message can be safely ignored.

:information_source: Important: when using the expectUserDeprecationMessage*() expectation(s) in a test, the test should be annotated with a #[IgnoreDeprecations] attribute.

PHPUnit IMPORTANT: The snake_case methods should not call the PHPUnit parent, i.e. do not use parent::setUp() from within an overloaded set_up() method.

If necessary, DO call parent::set_up().

…

Option 2: Yoast\PHPUnitPolyfills\TestCases\XTestCase

This TestCase overcomes the signature mismatch by using the PHPUnit @before[Class] and @after[Class] annotations and attributes in combination with different methods names, i.e. setUpFixturesBeforeClass(), setUpFixtures(), tearDownFixtures() and tearDownFixturesAfterClass().

When using this TestCase, overloaded fixture methods need to use the @beforeClass/#[BeforeClass], @before/#[Before], @after/#[After] and @afterClass/#[AfterClass] annotations/attributes. The naming of the overloaded methods is open as long as the method names don't conflict with the PHPUnit native method names.

…

TestListener

:warning: Important :warning:

The TestListener polyfill in PHPUnit Polyfills 2.0/3.0/4.0 is not (yet) compatible with PHPUnit 10.x/11.x/12.x.

If you need the TestListener polyfill, it is recommended to stay on the PHPUnit Polyfills 1.x series for the time being and to watch and upvote the related ticket.

The below documentation is for the PHPUnit 7.x-9.x TestListener polyfill implementation.

The method signatures in the PHPUnit TestListener interface have changed a number of times across versions. Additionally, the use of the TestListener principle has been deprecated in PHPUnit 7 in favour of using the TestRunner hook interfaces.

Note: while deprecated in PHPUnit 7, the TestListener interface has not yet been removed and is still supported in PHPUnit 9.x.

It is recommended to use the TestRunner hook interfaces extensions instead.

Yoast\PHPUnitPolyfills\TestListeners\TestListenerDefaultImplementation

This TestListenerDefaultImplementation trait overcomes the signature mismatches by having multiple versions and loading the correct one depen

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHPcompatibilityphpphpunitpolyfill

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category测试质量
PricingOpen source

> Related tools

J
Jest
JavaScript 测试框架
P
Playwright
现代端到端测试框架
E
eslint-plugin-test-selectors
Enforces that data-test-id attributes are added to interactive DOM elements (JSX) to help with UI testing. JSX only.