Allow static validator to run before prompt's validator. #186
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.
Summary
Prompts allow to specify a validator for a prompt using
validate()closure or static$validateUsingclosure.Prompt::validate()contains amatch()with:This will call the Prompt's validator before the static validator.
Problem
When testing prompts (via PHPUnit or Pest) in consumer projects using interactive inputs, the prompt that has failed the validation will halt the test execution expecting an input.
To work around this, a test executor (PHPUnit in the example below), could override the handling of the incorrect validator to, for example, throw an exception instead of re-asking for an input, and then assert for that exception in the test:
Before this PR, the static
$validateUsingwill not be assessed if the Prompt'svalidate()closure is set, making it impossible to intercept the validation handling.After this PR is merged, the static
$validateUsingwill take precedence and will allow to intercept the validation.Changes
static::$validateUsingvalidateUsing()method to allow acceptingNULLas a callback to allow resetting the value of$validateUsing. Note that the property value already allows to have aNULL'ified callback, so the changes to this setter follow that property type definition.static::$validateUsingand then erroneously resetting the value usingfn() => nullto clear it: this was setting the value to an empty closure that would return aNULLinstead of unsetting the callback completely, so the intended "resetting" was not working correctly resulting in "bleeding" of thestatic::$validateUsinginto other tests; this was not picked up by tests until a change was made in this PR.