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

Better respect PHP native array key handling for assertArrayIs*ToArrayOnlyConsideringListOfKeys() #5716

Commits on Feb 29, 2024

  1. assertArrayIs*ToArrayOnlyConsideringListOfKeys(): bug fix - better re…

    …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.
    jrfnl committed Feb 29, 2024
    Configuration menu
    Copy the full SHA
    754f2ec View commit details
    Browse the repository at this point in the history