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

Webfonts: change class properties from static to instance members #39361

Conversation

zaguiini
Copy link
Contributor

@zaguiini zaguiini commented Mar 10, 2022

Part of #39332.

What?

Having static variables in the WP_Webfonts class makes the class share state between instances. This is not needed in a lot of cases and @jeyip and I couldn't figure out why it was done like that.

Why?

That are several problems with that approach, including shared state issues. The tests become aware of each other and order starts to matter. Other instances of WP_Webfonts might appear in the codebase, and that might lead to confusing and unexpected behavior. It's even impossible to test features of the wp_webfonts_* API in the current state of things.

I wonder why we decided to take this route 🤔

How?

By moving $webfonts and $providers variables to instance members, whenever someone creates a new instance of WP_Webfonts, they'll start with a fresh copy.

We're also moving from static to global inside wp_webfonts so the instance can be accessed and reset before running a new test case.

Testing Instructions

Nothing should change at the implementation level. You should notice the test's expectations are a little different, but the tests are still passing.

@zaguiini
Copy link
Contributor Author

@aristath please review that as soon as possible as it's blocking us from writing new tests to #39327. Thanks!

@aristath
Copy link
Member

I'm on the fence about this one.
I understand why we want to do it... But introducing a new singleton class just for the sake of tests - with no other value to the implementation itself - seems a bit weird.
The wp_webfonts() function with the use of a static $instance var does pretty much the same thing a singleton does, so in real-life there would never be more than 1 instances of the class.

What if instead of introducing a new _WP_Webfonts_Singleton object, we just added a new reset_instance() method in the WP_Webfonts class?
Or we could make the $webfonts and $providers public instead of private

Both solutions above would allow us to reset the class props during tests, without the addition of a new singleton class 🤔

@zaguiini zaguiini force-pushed the fix/move-wp-webfonts-properties-to-instance-instead-of-static-members branch from 9078b01 to b2acc64 Compare March 11, 2022 20:00
@zaguiini
Copy link
Contributor Author

@aristath,

I moved from static to global and now we can intercept the $wp_webfonts global variable before each test runs. That's how wp_styles() is doing and it's the norm in the codebase. Hope it looks good to you.

@zaguiini zaguiini changed the title Webfonts: Change class properties from static to instance members Mar 11, 2022
Copy link
Member

@aristath aristath left a comment

Choose a reason for hiding this comment

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

LGTM! 👍

@zaguiini
Copy link
Contributor Author

Test failures are not related to the changes proposed by the PR. End to End Tests are failing regularly on trunk.

@jeyip jeyip merged commit 4dc5505 into trunk Mar 14, 2022
@jeyip jeyip deleted the fix/move-wp-webfonts-properties-to-instance-instead-of-static-members branch March 14, 2022 17:57
@github-actions github-actions bot added this to the Gutenberg 12.9 milestone Mar 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants