Skip to content

Add Prompter::canPrompt() and withPromptFallback() - #83

Merged
imanimanyara merged 1 commit into
mainfrom
feat/prompter-can-prompt
Oct 13, 2026
Merged

imanimanyara merged 1 commit into
mainfrom
feat/prompter-can-prompt

Conversation

@imanimanyara

Copy link
Copy Markdown
Member

What

  • Prompter::canPrompt(?InputInterface $input = null): bool says whether a prompt asked now can reach a person:
    • false under a non-interactive input (--no-interaction, or no terminal on stdin);
    • true when prompts are answered through Laravel's question-helper fallback (Windows, the test harness);
    • otherwise whether stdin is a terminal.
  • InteractsWithConsole::withPromptFallback() turns on that fallback for a test that switches the app out of the testing environment and still answers a command's prompts with expectsQuestion().

Why

Without a terminal, laravel/prompts returns each prompt's default instead of asking, so a prompt with no usable default fails with "Required.". The only way for a package to check first was to reach into Laravel\Prompts\SelectPrompt::shouldFallback() itself. laranail/env-kit does exactly that today, without declaring laravel/prompts. This gives it, and anything else, the check through console's own API.

Tests

  • tests/Prompter/PrompterCanPromptTest.php: each branch, including a fresh subprocess with stdin on /dev/null (returns false). The prompts fallback statics are snapshotted and restored around each case. Mutating the method (dropping the input check, returning true for the TTY branch) turns 4 of the 5 cases red.
  • InteractsWithConsoleTest: withPromptFallback() sets the flag.

Local: pest 573 passed, phpstan clean, rector dry-run clean, pint (package-tools config) passed.

Release

laranail/env-kit's refactor (separate PR) requires laranail/console ^0.1.7 for this method, so it waits on a release that carries this change. The CHANGELOG entry is under [Unreleased].

A package that runs a loop of prompts needs to know first whether any of
them can reach a person: without a terminal laravel/prompts returns each
prompt's default instead of asking, and a prompt with no usable default
fails with "Required.". The only way to ask was to reach into
Laravel\Prompts\SelectPrompt::shouldFallback() and stream_isatty(STDIN),
which is what laranail/env-kit does today.

canPrompt() answers it in one place: false under a non-interactive input,
true when prompts go through Laravel's question-helper fallback, and
otherwise whether stdin is a terminal.

withPromptFallback() is the test-side counterpart, for a test that moves
the app out of the testing environment and still answers a command's
prompts with expectsQuestion().
Copilot AI balanced review requested due to automatic review settings October 13, 2026 14:08

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@imanimanyara
imanimanyara merged commit 1eefe04 into main Oct 13, 2026
14 checks passed
@imanimanyara
imanimanyara deleted the feat/prompter-can-prompt branch October 13, 2026 14:44
@imanimanyara imanimanyara mentioned this pull request Oct 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants