#38266 closed task (blessed) (fixed)
Tests: Use assertSame() when the type of the value in the assertion is important
Reported by: | johnbillion | Owned by: | SergeyBiryukov |
---|---|---|---|
Milestone: | 5.7 | Priority: | normal |
Severity: | normal | Version: | |
Component: | Build/Test Tools | Keywords: | needs-unit-tests php8 |
Focuses: | Cc: |
Description
Several tests use assertEquals()
on a falsey value where the type is important to the accuracy of the test.
For example, in Tests_Cache::test_miss()
, a value is tested against an expected NULL
value using assertEquals()
which will produce a false positive if another falsey value such as 0
, false
, or an empty string is passed. These means these such assertions are actually buggy.
Using assertSame()
instead of assertEquals()
will cause the type to be checked in addition to its value.
Change History (36)
#3
@
5 years ago
I wrote a package to detect usage of assertEquals()
on falsey values: https://github.com/johnbillion/falsey-assertequals-detector. If someone wants to pick this up, that package could be used to start addressing these cases in core.
This ticket was mentioned in Slack in #core-multisite by johnbillion. View the logs.
5 years ago
#5
@
4 years ago
Noting here that this will become all the more important with PHP 8 introducing stricter type check exceptions. Documenting via the tests what the actual type of a expected return is + strict checking the type by using assertSame()
, will be extremely helpful to detect more PHP 8 related problems.
#6
@
4 years ago
- Milestone changed from Future Release to 5.6
- Owner set to SergeyBiryukov
- Status changed from new to accepted
Wanted to create a ticket for this, but found this one instead and would like to pick it up :)
In the same way that it is considered best practice per the coding standards to use strict comparisons, I think it would be beneficial to switch most of the tests to use assertSame()
instead of assertEquals()
where possible.
This ticket was mentioned in Slack in #core by sergey. View the logs.
4 years ago
This ticket was mentioned in Slack in #core by hellofromtonya. View the logs.
4 years ago
#16
@
4 years ago
Tests_Cache::test_miss()
was added in [1/tests]. Object caching was introduced in [3011]. As far as I can tell,WP_Object_Cache::get()
always returnedfalse
, notnull
for cache misses, so the test was never correct.Tests_Cache::test_add_get_null()
was added in [280/tests] and adjusted in [327/tests]. At the time,null
was indeed converted to an empty string due to this piece inWP_Object_Cache::set()
, added in [3021] and adjusted in [6247]:if (NULL === $data) $data = '';
This was later removed in [20089], but the test was never updated.
#19
follow-up:
↓ 31
@
4 years ago
The commits here so far have reduced the number of assertEquals()
instances from 8378 matches in 431 files (WordPress 5.5) to 572 matches in 121 files.
The remaining instances fall into three groups:
- Ones that use delta comparison for floats, dates, etc. These should be switched to assertEqualsWithDelta() once PHPUnit 7.5 is the minimum supported version. Or, perhaps
assertEqualsWithDelta()
should be polyfilled for older PHPUnit versions. - Ones that legitimately use
assertEquals()
for comparing objects. Trying to switch these toassertSame()
would result in "Failed asserting that two variables reference the same object" error. - Ones that should be switched to
assertSame()
, but would currently result in a failure when doing so, due to data type mismatches, mostly strings vs. integers, or integers vs. floats. These need further investigation:- In some cases, the tests should be adjusted to use the correct data type.
- In other cases, this points to minor bugs in core, e.g. using
ceil()
for values that are documented as integers, but without explicitly casting toint
, results in them being of typefloat
instead. This affects some properties likemax_num_pages
,max_pages
,total_pages
in various classes, or functions likeget_comment_pages_count()
,wp_embed_defaults()
,get_oembed_response_data()
.
This ticket was mentioned in Slack in #core by hellofromtonya. View the logs.
4 years ago
#29
@
4 years ago
- Milestone changed from 5.6 to 5.7
Punting this ticket to 5.7 as today is 5.6 RC2 (though this one could easily be a close and then reopen new for 5.7).
If any maintainer or committer feels this can be resolved in time, or wishes to assume ownership during a specific cycle, feel free to update the milestone accordingly.
#31
in reply to:
↑ 19
@
4 years ago
Replying to SergeyBiryukov:
In other cases, this points to minor bugs in core, e.g. using
ceil()
for values that are documented as integers, but without explicitly casting toint
, results in them being of typefloat
instead. This affects some properties likemax_num_pages
,max_pages
,total_pages
in various classes, or functions likeget_comment_pages_count()
,wp_embed_defaults()
,get_oembed_response_data()
.
Related: #46346
#32
follow-up:
↓ 34
@
4 years ago
- Resolution set to fixed
- Status changed from accepted to closed
This feels like an ongoing ticket. There's more than 700 instances of assertEquals
in use and quite a few of those are still checking falsey values.
Closing as fixed for 5.7, I'll open a follow-up for 5.8.
#34
in reply to:
↑ 32
@
4 years ago
Replying to johnbillion:
There's more than 700 instances of
assertEquals
in use and quite a few of those are still checking falsey values.
If we ignore assertEqualsWithDelta()
and search specifically for assertEquals()
, there are currently 653 matches in 128 files. This has gone up a bit since WordPress 5.6: comment:19 (572 matches in 121 files), so apparently some newly introduced tests use assertEquals()
instead of assertSame()
. Some of those were corrected in [49126] and [49547], but more were added in 5.7.
Let's continue on #52482 though :)
Looks like two of the assertions in
Tests_Cache
are incorrect.test_miss()
-false
is returned from a cache miss, notnull
.test_add_get_null()
- anull
value is retained, not converted to an empty string.These will need more investigation.