Repository navigation
Tests: Use strict assertions in the style engine CSS rules store tests. - #83777
haritpanchal wants to merge 1 commit into
Conversation
The store's get_store() returns the stored instance rather than a copy, so assertSame() asserts the identity the registry actually guarantees. The loose assertions could not tell the singleton apart from a clone of it. See https://core.trac.wordpress.org/ticket/64895
🤖 PR meta 🤖🎉 PropsIf you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Updated as activity occurs, without notifying anyone named here. Add the 🏷️ LabelsThis pull request needs exactly one label indicating its type, and has 0.
Read more about Type labels in Gutenberg. If you cannot add labels yourself, a reviewer can do it for you. |
|
This has been waiting a while, so flagging it for review — @lancewillett, you reviewed the One CI note: "Check for a Core backport changelog entry" is failing. The workflow runs on Trac ticket: https://core.trac.wordpress.org/ticket/64895 |
What?
See https://core.trac.wordpress.org/ticket/64895 (Gutenberg follow-up requested in
comment 89)
Converts the three
assertEquals()calls inphpunit/style-engine/class-wp-style-engine-css-rules-store-test.phptoassertSame(). This isthe style engine group of the Gutenberg inventory on that ticket, and it leaves no
assertEquals()calls in the file.Why?
assertEquals()compares by value, so it cannot tell the store's singleton instance apart froma copy of it. That matters here specifically, because returning the same instance is the whole
contract of
WP_Style_Engine_CSS_Rules_Store::get_store(): block supports accumulate rules bycalling it repeatedly, and if it ever handed back a copy, rules added by one caller would be
silently dropped. The existing assertions could not have caught that.
Trac #64895 is the ongoing effort to replace loose assertions with type-strict ones, and its
description asks for the same changes upstream in Gutenberg to ease merging tests between the
two repositories.
How?
WP_Style_Engine_CSS_Rules_Storeis a static registry.get_store( $name )lazily creates andreturns
static::$stores[ $name ],get_stores()returns that same array, andremove_all_stores()assignsarray().test_should_get_all_existing_stores()andtest_should_remove_all_stores()build theirexpected array out of the very objects
get_store()just returned. Because those are the storedinstances rather than copies,
assertSame()asserts identity, which is what the registryactually guarantees.
The final assertion in
test_should_remove_all_stores()comparesarray()againstget_stores()after the reset, so the strict form additionally pins that the return value is areal array rather than merely something falsey.
Two things were checked rather than assumed, since a strict array comparison is sensitive to
both:
==ignores order and===does not, sothe ordering is genuinely asserted here. Confirmed by swapping the two keys in the expected
array:
assertSame()fails, and the same swap underassertEquals()passes.survived into
get_stores(). The class'stear_down()callsremove_all_stores(), and thetests were additionally run in two different randomized orders to confirm no order dependence.
The identity gain was confirmed the same way, by running each mutation under both assertions so
the result isolates what the conversion buys:
assertSame()assertEquals()clone $burrito_storeNo other assertions are touched.
Testing Instructions
This is a test-only change, so the verification is that the suite still passes with the stricter
assertions in place.
OK (8 tests, 14 assertions)OK (8 tests, 14 assertions)OK (8 tests, 14 assertions)To see that the assertions actually got stricter, change the expected value in
test_should_get_all_existing_stores()from$burrito_storetoclone $burrito_store. The testnow fails, and reverting that one assertion to
assertEquals()makes it pass again.Coding standards on the changed file:
Testing Instructions for Keyboard
Not applicable. This PR changes PHPUnit test assertions only and has no user interface.
Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5
Used for: Checking each assertion against the store implementation it exercises, and confirming
by mutation what the strict form adds. I reviewed the changes and take responsibility for the
final code.