[11.x] Make expectsChoice
assertion more intuitive with associative arrays.
#52408
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.
Currently, the
expectsChoice
test method behaves in an unusual way when passing an associative array to thechoice
method (or theselect
prompt).For example, imagine the following scenario:
To use
expectsChoice
, you would need to write the test like this:This is because the available options are retrieved from Symfony using the
getAutocompleterValues
method. Technically, this could be considered correct because with Symfony, the user must type a value to select an option, and they can either type a key or a value. However, I don't think it's very intuitive, and with theselect
prompt, it's even less intuitive because the keys are never exposed to the user.This PR addresses this by detecting the available options using the
getChoices
method instead, which allows an associative array to be used in theexpectsChoice
method like so:To avoid breaking backwards compatibility for anyone relying on the current behaviour, this only uses the
getChoice
method when an associative array is passed toexpectsChoice
, meaning that passing a list will continue to function as it currently does.Fixes laravel/prompts#158