Set of polyfills for changed PHPUnit functionality to allow for creating PHPUnit cross-version compatible tests
Set of polyfills for changed PHPUnit functionality to allow for creating PHPUnit cross-version compatible tests
[][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.
To install this package, run:
composer require --dev yoast/phpunit-polyfills:"^4.0"To update this package, run:
composer update --dev yoast/phpunit-polyfills --with-dependenciesMake sure to:
vendor/autoload.php file as your test bootstrap file;vendor/yoast/phpunit-polyfills/phpunitpolyfills-autoload.php file in your test bootstrap.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.
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.
1.x series of the PHPUnit Polyfills support PHPUnit 4.8 - 9.x.2.x series of the PHPUnit Polyfills support PHPUnit 5.7 - 10.x.3.x series of the PHPUnit Polyfills support PHPUnit 6.4 - 11.x (but don't support running tests on PHPUnit 10).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).
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.
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.
assertContainsOnlyBool( $haystack );
self::assertContainsNotOnlyNumeric( $haystack );
}
}By default, PHPUnit supports four ways of calling assertions:
TestCase class - $this->assertSomething().TestCase class - self/static/parent::assertSomething().Assert class - Assert::assertSomething().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.
The shouldClosedResourceAssertionBeSkipped() helper method is therefore available cross-version.
Usage examples:
// 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.
| -------------- | --------------- |
| 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.
parent::setUp() from within an overloaded set_up() method.If necessary, DO call
parent::set_up().
…Yoast\PHPUnitPolyfills\TestCases\XTestCaseThis 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.
…: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\TestListenerDefaultImplementationThis TestListenerDefaultImplementation trait overcomes the signature mismatches by having multiple versions and loading the correct one depen
No open issues yet, or sync has not completed.