Skip to content
New issue

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

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

Already on GitHub? # to your account

Fix PHP 8 unit test GHA job; remove PHPUnit dependency and instead use PHPUnit polyfills package #6525

Merged
merged 15 commits into from
Aug 12, 2021

Conversation

pierlon
Copy link
Contributor

@pierlon pierlon commented Aug 10, 2021

Summary

Note: Each commit in this PR represents an individual set of changes and so it's recommended to go through each commit instead of viewing all the files that changed.

This PR fixes the PHP 8 unit test GHA, along with some other changes that will allow us to easily adopt newer versions of PHPUnit when the time comes.

Changes

  • Replace deprecated PHPUnit assertions with their successors. This will allow us to easily switch to newer versions of PHPUnit in the future without hassle. (d846856, d1438ad, c828932, bf34961, cce7f32)

  • Remove AssertContainsCompatibility trait as it is now unnecessary (0ec6eaf)

  • Remove the phpunit/phpunit Composer package and use yoast/wp-test-utils to manage it instead. This allows us to remove the hard PHPUnit 5.7 requirement and delegate that responsibility to wp-test-utils which will install the most recent compatible version of PHPUnit depending on the active PHP version (47a1e6b)

  • Remove no longer necessary composer.json patch used to run PHPUnit tests on PHP 8 (25ae209)

  • Update all tests to use the Yoast\WPTestUtils\WPIntegration\TestCase instead of WP_UnitTestCase. This will still allow us to use all the WP Core test utilities as it extends WP_UnitTestCase and will load the correct PHPUnit polyfills when necessary (ebfd6e9)

  • Patch the phpunit/phpunit-mock-objects package to prevent PHP warnings from occurring on PHP 8, which without it would cause some tests to fail (ffdcc78)

Checklist

  • My code is tested and passes existing tests.
  • My code follows the Engineering Guidelines (updates are often made to the guidelines, check it out periodically).

@pierlon pierlon force-pushed the enhancement/phpunit-polyfills branch from 15ce05f to ebfd6e9 Compare August 12, 2021 01:02
@pierlon pierlon requested a review from westonruter August 12, 2021 05:08
@pierlon pierlon changed the title Improve PHPUnit polyfills Fix PHP 8 unit test GHA job; remove PHPUnit dependency and instead use PHPUnit polyfills package Aug 12, 2021
@pierlon pierlon marked this pull request as ready for review August 12, 2021 05:10
@pierlon pierlon requested a review from schlessera August 12, 2021 05:10
@pierlon pierlon added this to the v2.2 milestone Aug 12, 2021
@pierlon pierlon added dependencies Pull requests that update a dependency file Infrastructure Changes impacting testing infrastructure or build tooling Testing Issues related with Unit, E2E, Smoke, and other testing requirements/needs labels Aug 12, 2021
Copy link
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing. 😍

@@ -16,7 +16,6 @@ public function setUp() {
$this->instance = $this->injector->make( PairedUrl::class );
}

/** @covers ::__construct() */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this just not do anything?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When running PHPUnit with coverage it complained that __construct() didn't exist in the class, so in short yes.

@@ -2037,7 +2036,7 @@ public function get_external_stylesheet_data() {
],
'expected_styles' => [],
'expected_errors' => [ AMP_Style_Sanitizer::STYLESHEET_FETCH_ERROR ],
'cached_data' => new CachedResponse(
'expected_cached_response' => new CachedResponse(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does TestCase map associative arrays to named positional args?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In PHP 8 it does.

@@ -125,7 +126,7 @@ public function get_data_for_test_amp_get_customizer_url() {
Option::THEME_SUPPORT => AMP_Theme_Support::READER_MODE_SLUG,
Option::READER_THEME => ReaderThemes::DEFAULT_READER_THEME,
],
function () {
'assert' => function () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this causing an error before or is it just a bug? It's strange that there's an associative array mixed with a numerical index.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In PHP 8 it maps to a named parameter.

@github-actions
Copy link
Contributor

github-actions bot commented Aug 12, 2021

Plugin builds for 0c545ac are ready 🛎️!

@codecov
Copy link

codecov bot commented Aug 12, 2021

Codecov Report

Merging #6525 (b9c7a40) into develop (8898527) will decrease coverage by 0.03%.
The diff coverage is n/a.

❗ Current head b9c7a40 differs from pull request most recent head 0c545ac. Consider uploading reports for the commit 0c545ac to get more accurate results
Impacted file tree graph

@@              Coverage Diff              @@
##             develop    #6525      +/-   ##
=============================================
- Coverage      75.52%   75.49%   -0.04%     
  Complexity      5983     5983              
=============================================
  Files            190      190              
  Lines          18013    18013              
=============================================
- Hits           13605    13599       -6     
- Misses          4408     4414       +6     
Flag Coverage Δ
php 75.49% <ø> (-0.04%) ⬇️
unit 75.49% <ø> (-0.04%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/Infrastructure/Injector/SimpleInjector.php 88.88% <ø> (ø)
src/Optimizer/Transformer/DetermineHeroImages.php 87.03% <0.00%> (-11.12%) ⬇️

@westonruter westonruter merged commit 313ef69 into develop Aug 12, 2021
@westonruter westonruter deleted the enhancement/phpunit-polyfills branch August 12, 2021 05:34
@pierlon pierlon mentioned this pull request Aug 12, 2021
2 tasks
@schlessera
Copy link
Collaborator

I'd recommend using a plugin-specific TestCase class, instead of coupling every single test file against an external dependency.

See https://github.com/ampproject/amp-toolbox-php/blob/main/tests/src/TestCase.php for a simple example.

This decouples the tests from whatever compatibility layer is needed.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
dependencies Pull requests that update a dependency file Infrastructure Changes impacting testing infrastructure or build tooling Testing Issues related with Unit, E2E, Smoke, and other testing requirements/needs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants