Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests: PHPUnit cross-version compatibility #690

Conversation

jrfnl
Copy link
Contributor

@jrfnl jrfnl commented Aug 14, 2021

This PR makes the test suite cross-version compatible with PHPUnit 5.x-9.x, which then allows the tests to be run on the full range of supported PHP versions, including PHP 8.1 (beta).

Note: this doesn't mean the tests will pass, but test failures will/should be addressed in separate PRs.

As this test suite uses a limited set of the PHPUnit functionality, not that much needed to be done and where necessary, I've just added a trait to an individual test file.

If in the future, you'd want the full range of polyfills to be available at all times to all tests, I'd recommend to add a generic SimplePieTestCase which either extends one of the Polyfill test cases or includes all the Polyfills traits. The actual test classes can then all extend from that TestCase. Or alternatively, all test classes could extend one of the Polyfill test cases instead of the PHPUnit base TestCase.

Fixes #663

Commit Details

Tests: make config cross-version compatible

PHPUnit config file:

  • Add backupGlobals="false".
    The default value for this setting changed in PHPUnit 6 from true to false. By explicitly setting it to false, the behaviour for the tests will be consistent PHPUnit cross-version.
  • Remove the blacklist element. This element hasn't been supported since PHPUnit 5.0.
  • Point to the correct directory for the source code to allow for recording code coverage.
  • Make sure that all src files are taken into consideration when calculating code coverage.
  • Add XSD schema reference.
    Note: the config as-is will now validate for PHPUnit 4.4-9.2. For PHPUnit 9.3, the code coverage terminology has changed, though the "old" configuration is still supported.
    If needs be (PHPUnit 10), the config can be updated on the fly by using --migrate-configuration.

Other:

  • Ignore a locally overloaded PHPUnit config file using the standard phpunit.xml file name.

Composer: add dependency on the PHPUnit Polyfills package

  • Adds a dev dependency to the yoast/phpunit-polyfills package.
  • As that package already requires and manages the installable versions for PHPUnit, remove this as an explicit requirement from require-dev in favour of letting the PHPUnit Polyfills package manage the versions.
    This will update the supported PHPUnit versions from ~5.4.3 || ~6.5 to ^5.7.21 || ^6.0 || ^7.0 || ^8.0 || ^9.0.
    Note: the supported versions for PHPUnit 5.x are very specific and restrictive as it specifically targets the versions in which the "forward compatible" alias files for the namespaced classes are available.

This new package adds the following features:

  • Polyfills for various PHPUnit cross-version changes.
  • Basic test case and test listener.

Refs:

Includes

  • Adding the PHPUnit cache file, which is automatically created in PHPUnit 8 and 9, to the .gitignore file.
  • Adding a require_once statement to load the PHPUnit Polyfills autoload file from the test bootstrap.

Tests: switch out @ExpectedException annotations

These annotations are no longer supported in recent PHPUnit versions.

For PHP native errors/warnings/notices, the TestCase::expectError(), TestCase::expectWarning(), TestCase::expectNotice() and TestCase::expectDeprecation() methods (and variants) should be used.

For (custom) exceptions, the TestCase::expectException() method (and variants) should be used.

This also removes the need for the setUpBeforeClass() method in the IRITest, which only created an alias for a PHPUnit class.

The new methods and their variants are polyfilled via the PHPUnit Polyfill repo via the included trait.

EncodingTest: rename a custom assertion

The method assertEquals() is a PHPUnit native method and if it is overloaded, the method signature is expected to be the same as the method signature in PHPUnit itself.

However, the method signature for this method has changed a couple of times across PHPUnit versions.

The simplest way to get round this is to rename the custom assertion to not overlap with a PHPUnit native method name.

@jrfnl
Copy link
Contributor Author

jrfnl commented Aug 14, 2021

P.S.: I have a commit ready to address the test failure on PHP 8+, but it depends on this PR.

I can either pull it after this PR has been merged or add it to this PR, even though it is not part of the scope of this PR (PHPUnit cross-version compat).

jrfnl added 4 commits August 14, 2021 04:51
PHPUnit config file:

* Add `backupGlobals="false"`.
    The default value for this setting changed in PHPUnit 6 from `true` to `false`. By explicitly setting it to `false`, the behaviour for the tests will be consistent PHPUnit cross-version.
* Remove the `blacklist` element. This element hasn't been supported since PHPUnit 5.0.
* Point to the correct directory for the source code to allow for recording code coverage.
* Make sure that all src files are taken into consideration when calculating code coverage.
* Add XSD schema reference.
    Note: the config as-is will now validate for PHPUnit 4.4-9.2. For PHPUnit 9.3, the code coverage terminology has changed, though the "old" configuration is still supported.
    If needs be (PHPUnit 10), the config can be updated on the fly by using `--migrate-configuration`.

Other:
* Ignore a locally overloaded PHPUnit config file using the standard `phpunit.xml` file name.
* Adds a dev dependency to the `yoast/phpunit-polyfills` package.
* As that package already requires and manages the installable versions for PHPUnit, remove this as an explicit requirement from `require-dev` in favour of letting the PHPUnit Polyfills package manage the versions.
    This will update the supported PHPUnit versions from `~5.4.3 || ~6.5` to `^5.7.21 || ^6.0 || ^7.0 || ^8.0 || ^9.0`.
    Note: the supported versions for PHPUnit 5.x are very specific and restrictive as it specifically targets the versions in which the "forward compatible" alias files for the namespaced classes are available.

This new package adds the following features:
* Polyfills for various PHPUnit cross-version changes.
* Basic test case and test listener.

Refs:
* https://github.com/Yoast/PHPUnit-Polyfills/

Includes
* Adding the PHPUnit cache file, which is automatically created in PHPUnit 8 and 9, to the `.gitignore` file.
* Adding a `require_once` statement to load the PHPUnit Polyfills autoload file from the test bootstrap.
These annotations are no longer supported in recent PHPUnit versions.

For PHP native errors/warnings/notices, the `TestCase::expectError()`, `TestCase::expectWarning()`, `TestCase::expectNotice()` and `TestCase::expectDeprecation()` methods (and variants) should be used.

For (custom) exceptions, the `TestCase::expectException()` method (and variants) should be used.

This also removes the need for the `setUpBeforeClass()` method in the `IRITest`, which only created an alias for a PHPUnit class.

The new methods and their variants are polyfilled via the PHPUnit Polyfill repo via the included trait.
The method `assertEquals()` is a PHPUnit native method and if it is overloaded, the method signature is expected to be the same as the method signature in PHPUnit itself.

However, the method signature for this method has changed a couple of times across PHPUnit versions.

The simplest way to get round this is to rename the custom assertion to not overlap with a PHPUnit native method name.
@jrfnl jrfnl force-pushed the feature/tests-phpunit-cross-version-compatibility branch from b58b7aa to 4f66ea8 Compare August 14, 2021 02:51
@mblaney mblaney merged commit edbd0fb into simplepie:master Aug 15, 2021
@mblaney
Copy link
Member

mblaney commented Aug 15, 2021

looks good happy to merge the next PR separately

@jrfnl jrfnl deleted the feature/tests-phpunit-cross-version-compatibility branch August 15, 2021 10:09
@jrfnl
Copy link
Contributor Author

jrfnl commented Aug 15, 2021

@mblaney I've opened PR #691 to address that last test failure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make testsuite PHPUnit cross-version compatible
2 participants