TestInputReader
in package
implements
IInputReader
Test double for IInputReader that returns pre-programmed responses.
This allows CLI commands to be unit tested without requiring actual user input or complex process isolation.
Example usage:
$reader = new TestInputReader();
$reader->addResponse( 'yes' );
$reader->addResponse( 'John Doe' );
$reader->addResponse( '2' ); // Select option index 2
$command->setInputReader( $reader );
$command->execute();
// Verify prompts were shown
$prompts = $reader->getPromptHistory();
$this->assertCount( 3, $prompts );
Table of Contents
Interfaces
- IInputReader
- Interface for reading user input in CLI commands.
Properties
- $currentIndex : int
- Current position in the responses queue.
- $promptHistory : array<string|int, string>
- History of all prompts that were shown.
- $responses : array<string|int, string>
- Queue of responses to return.
Methods
- addResponse() : self
- Add a response to the queue.
- addResponses() : self
- Add multiple responses at once.
- choice() : string
- Prompt user to select from a list of options.
- confirm() : bool
- Ask user for yes/no confirmation.
- getPromptHistory() : array<string|int, string>
- Get the history of all prompts that were displayed.
- getRemainingResponseCount() : int
- Get the number of responses remaining in the queue.
- hasMoreResponses() : bool
- Check if there are responses remaining in the queue.
- prompt() : string
- Prompt user for input and return their response.
- reset() : void
- Reset the input reader to initial state.
- secret() : string
- Prompt for sensitive input without echoing to console.
Properties
$currentIndex
Current position in the responses queue.
private
int
$currentIndex
= 0
$promptHistory
History of all prompts that were shown.
private
array<string|int, string>
$promptHistory
= []
$responses
Queue of responses to return.
private
array<string|int, string>
$responses
= []
Methods
addResponse()
Add a response to the queue.
public
addResponse(string $response) : self
Responses are returned in the order they are added.
Parameters
- $response : string
-
The response to return when next prompted
Return values
self —For method chaining
addResponses()
Add multiple responses at once.
public
addResponses(array<string|int, string> $responses) : self
Parameters
- $responses : array<string|int, string>
-
Array of responses
Return values
self —For method chaining
choice()
Prompt user to select from a list of options.
public
choice(string $message, array<string|int, mixed> $options[, string|null $default = null ]) : string
Parameters
- $message : string
-
The prompt message
- $options : array<string|int, mixed>
-
Available options
- $default : string|null = null
-
Default option (will be marked with *)
Tags
Return values
string —The selected option
confirm()
Ask user for yes/no confirmation.
public
confirm(string $message[, bool $default = false ]) : bool
Parameters
- $message : string
-
The confirmation message
- $default : bool = false
-
Default value if user just presses enter
Tags
Return values
bool —True if user confirms, false otherwise
getPromptHistory()
Get the history of all prompts that were displayed.
public
getPromptHistory() : array<string|int, string>
Useful for asserting that the correct prompts were shown to the user.
Return values
array<string|int, string>getRemainingResponseCount()
Get the number of responses remaining in the queue.
public
getRemainingResponseCount() : int
Return values
int —Number of responses not yet consumed
hasMoreResponses()
Check if there are responses remaining in the queue.
public
hasMoreResponses() : bool
Return values
bool —True if more responses are available, false otherwise
prompt()
Prompt user for input and return their response.
public
prompt(string $message) : string
Parameters
- $message : string
-
The prompt message to display
Tags
Return values
string —The user's response (trimmed)
reset()
Reset the input reader to initial state.
public
reset() : void
Clears all responses and prompt history.
secret()
Prompt for sensitive input without echoing to console.
public
secret(string $message) : string
Parameters
- $message : string
-
The prompt message
Tags
Return values
string —The user's input (trimmed)