Better respect PHP native array key handling for assertArrayIs*ToArrayOnlyConsideringListOfKeys()#5716
Merged
sebastianbergmann merged 1 commit intosebastianbergmann:11.0from Feb 29, 2024
Conversation
…spect PHP native array key handling Related to sebastianbergmann/phpunit 5600 As things are, arrays in PHP can have either integer or string keys. Depending on the key input, PHP does some type juggling magic though, like auto-converting purely integer string keys to integers and flooring floating point keys to integers. While experienced devs will know this pitfall, less experienced devs (who also write tests) may not be as aware and may provide the keys in `$keysToBeConsidered` the same way as the original array was defined, not realizing that the type of some of the keys will have auto-magically been changed by PHP. The code in the new `assertArrayIs*ToArrayOnlyConsideringListOfKeys()` assertions, with its use of strict `in_array()` [did not respect the key juggling PHP does](https://3v4l.org/FdReu), while [the code for the `assertArrayIs*ToArrayIgnoringListOfKeys` assertions did](https://3v4l.org/AfHoc) (as `unset()` - and `isset()` for that matter - will do the same type juggling for the array keys). This commit adjusts the code for the `assertArrayIs*ToArrayOnlyConsideringListOfKeys()` assertions to handle arrays keys passed in `$keysToBeConsidered` consistently in the same way PHP itself would do. Includes tests. Includes tests for the same for the `assertArrayIs*ToArrayIgnoringListOfKeys` assertions which were not affected by this bug.
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## 11.0 #5716 +/- ##
=========================================
Coverage 89.96% 89.96%
Complexity 6465 6465
=========================================
Files 683 683
Lines 19596 19600 +4
=========================================
+ Hits 17630 17634 +4
Misses 1966 1966 ☔ View full report in Codecov by Sentry. |
assertArrayIs*ToArrayOnlyConsideringListOfKeys()
Contributor
Author
|
Thanks for accepting this fix @sebastianbergmann ! Just in case anyone is wondering why this could be an issue, here's an example usecase where this could come into play with these assertions: Think an integration test where the code under test inserts some data into a database and the tests wants to verify that the data was correctly added. In the test:
By default (without an abstraction layer), most databases will return text strings, even for ids, so without this fix that comparison would have failed. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related to #5600
As things are, arrays in PHP can have either integer or string keys. Depending on the key input, PHP does some type juggling magic though, like auto-converting purely integer string keys to integers and flooring floating point keys to integers.
While experienced devs will know this pitfall, less experienced devs (who also write tests) may not be as aware and may provide the keys in
$keysToBeConsideredthe same way as the original array was defined, not realizing that the type of some of the keys will have auto-magically been changed by PHP.The code in the new
assertArrayIs*ToArrayOnlyConsideringListOfKeys()assertions, with its use of strictin_array()did not respect the key juggling PHP does, while the code for theassertArrayIs*ToArrayIgnoringListOfKeysassertions did (asunset()- andisset()for that matter - will do the same type juggling for the array keys).This commit adjusts the code for the
assertArrayIs*ToArrayOnlyConsideringListOfKeys()assertions to handle arrays keys passed in$keysToBeConsideredconsistently in the same way PHP itself would do.Includes tests.
Includes tests for the same for the
assertArrayIs*ToArrayIgnoringListOfKeysassertions which were not affected by this bug.