VOOZH about

URL: https://deepwiki.com/hypervel/prompts/10.1-testing-with-mocked-terminal-io

⇱ Testing with Mocked Terminal I/O | hypervel/prompts | DeepWiki


Loading...
Last indexed: 7 February 2026 (2e2181)
Menu

Testing with Mocked Terminal I/O

Purpose and Scope

This document describes the testing infrastructure provided by the FakesInputOutput trait for writing automated tests for prompts without requiring actual terminal interaction. The trait allows tests to simulate user keyboard input and capture terminal output in a controlled environment using mocked I/O.

For information about keyboard input processing, see Key Handling and Input Processing. For information about the terminal control traits, see Terminal Control - Cursor and Erase.

Overview

Testing interactive terminal applications presents unique challenges since they rely on real-time user input and terminal output. The FakesInputOutput trait solves this by:

  1. Mocking the Terminal class to intercept I/O operations
  2. Queuing simulated key presses to be returned sequentially
  3. Buffering all output for inspection
  4. Providing assertion methods for verifying output content

This trait is designed to be used in prompt classes (those extending the base Prompt class) and enables full integration testing of interactive prompt behavior.

Sources: src/Concerns/FakesInputOutput.php1-114

Architecture: Faked Terminal I/O System


Sources: src/Concerns/FakesInputOutput.php20-44

The FakesInputOutput Trait

The FakesInputOutput trait is located at src/Concerns/FakesInputOutput.php and provides all necessary methods for testing prompts in isolation. It is designed to be used within prompt classes or test helper classes.

Key Dependencies

DependencyPurpose
MockeryCreates mock instances of the Terminal class
PHPUnit\Framework\AssertProvides assertion methods for output verification
BufferedConsoleOutputCaptures all output written to the console
TerminalThe class being mocked to control I/O

Sources: src/Concerns/FakesInputOutput.php7-11

Setting Up Fake Terminal I/O

The fake() Method

The primary entry point for testing is the static fake() method, which configures the entire testing environment.


What it does:

  1. Forces interactive mode via static::interactive()
  2. Creates a Mockery mock of the Terminal class
  3. Configures default expectations for terminal methods
  4. Queues the provided key presses to be returned sequentially
  5. Replaces the prompt's terminal with the mock
  6. Sets up a BufferedConsoleOutput to capture output

Sources: src/Concerns/FakesInputOutput.php20-44

Mocked Terminal Methods

The mock terminal is configured with the following default expectations:

MethodDefault Behavior
write()No-op (accepts all writes)
exit()No-op
setTty()No-op
restoreTty()No-op
cols()Returns 80
lines()Returns 24
initDimensions()No-op
read()Returns queued key presses in sequence

Sources: src/Concerns/FakesInputOutput.php27-38

Terminal Dimensions

The mock terminal simulates a standard 80x24 terminal. These dimensions are returned by cols() and lines() respectively, allowing prompts to calculate layout and wrapping behavior consistently during tests.

Sources: src/Concerns/FakesInputOutput.php31-32

Simulating User Input

Key Press Queue

The fake() method accepts an array of key presses that will be returned sequentially when the prompt calls Terminal::read(). Each element in the array represents a single key press.


The fakeKeyPresses() Method


This method iterates through the key array and invokes a callable for each key. The default implementation configures Mockery expectations:


This ensures that each call to Terminal::read() returns the next key in the queue.

Sources: src/Concerns/FakesInputOutput.php56-61

Custom Key Press Handling

The fakeKeyPresses() method can be overridden to implement custom key press simulation logic. This is useful for advanced testing scenarios where you need to emit key presses using a different mechanism.

Sources: src/Concerns/FakesInputOutput.php47-55

Capturing and Inspecting Output

BufferedConsoleOutput

When fake() is called, the prompt's output is replaced with a BufferedConsoleOutput instance. This class captures all output written to the console, including ANSI escape sequences for colors and cursor control.

Sources: src/Concerns/FakesInputOutput.php43

The content() Method


Returns the complete buffered output including all ANSI escape sequences. This is useful when you need to verify the exact output including formatting codes.

Throws: RuntimeException if called before fake() has been invoked.

Sources: src/Concerns/FakesInputOutput.php98-105

The strippedContent() Method


Returns the buffered output with all ANSI escape sequences removed. This uses a regular expression to strip escape codes in the pattern \e[[0-9;?]*[A-Za-z].

This method is particularly useful for assertions that focus on the textual content without caring about formatting or cursor positioning.

Sources: src/Concerns/FakesInputOutput.php110-113

Assertion Methods

The FakesInputOutput trait provides four assertion methods that delegate to PHPUnit's Assert class:

assertOutputContains()


Asserts that the raw output (including ANSI codes) contains the specified string.

Sources: src/Concerns/FakesInputOutput.php66-69

assertOutputDoesntContain()


Asserts that the raw output does not contain the specified string.

Sources: src/Concerns/FakesInputOutput.php74-77

assertStrippedOutputContains()


Asserts that the stripped output (ANSI codes removed) contains the specified string. Use this when you want to verify text content without worrying about formatting.

Sources: src/Concerns/FakesInputOutput.php82-85

assertStrippedOutputDoesntContain()


Asserts that the stripped output does not contain the specified string.

Sources: src/Concerns/FakesInputOutput.php90-93

Testing Workflow


Sources: src/Concerns/FakesInputOutput.php20-113

Usage Patterns

Basic Pattern

  1. Call PromptClass::fake([key_presses]) with the simulated input
  2. Invoke the prompt normally
  3. Use assertion methods to verify output
  4. Access the returned value to verify behavior

Testing Text Input


Testing Selection Prompts


Inspecting Raw vs Stripped Output


ANSI Escape Sequence Handling

Regular Expression Pattern

The strippedContent() method uses the following pattern to remove ANSI escape sequences:

/\e\[[0-9;?]*[A-Za-z]/

This pattern matches:

  • \e[ - The escape sequence introducer
  • [0-9;?]* - Zero or more digits, semicolons, or question marks (parameters)
  • [A-Za-z] - A single letter (the command)

This covers most common ANSI escape sequences including:

  • Color codes (e.g., \e<FileRef file-url="https://github.com/hypervel/prompts/blob/2e218156/31m for red)\n- Cursor positioning (e.g., \\e[10;5H)\n- Text styling (e.g., \\e[1m for bold)\n- Erasing operations (e.g., \\e[2J for clear screen)\n\nSources#LNaN-LNaN" NaN file-path="31mfor red)\n- Cursor positioning (e.g.,\e[10;5H)\n- Text styling (e.g., \e[1mfor bold)\n- Erasing operations (e.g.,\e[2J` for clear screen)\n\nSources">Hii

Error Handling

RuntimeException on Invalid State

The content() method throws a RuntimeException if accessed before fake() has been called:

Prompt must be faked before accessing content.

This ensures tests fail fast if the testing infrastructure is not properly initialized.

Sources: src/Concerns/FakesInputOutput.php100-102

Method Reference Table

MethodReturn TypePurpose
fake(array $keys)voidSet up mocked terminal with queued key presses
fakeKeyPresses(array $keys, callable $callable)voidConfigure key press simulation (overridable)
content()stringGet raw buffered output with ANSI codes
strippedContent()stringGet buffered output without ANSI codes
assertOutputContains(string $string)voidAssert raw output contains string
assertOutputDoesntContain(string $string)voidAssert raw output doesn't contain string
assertStrippedOutputContains(string $string)voidAssert stripped output contains string
assertStrippedOutputDoesntContain(string $string)voidAssert stripped output doesn't contain string

Sources: src/Concerns/FakesInputOutput.php20-113

Integration with Mockery

The trait relies heavily on Mockery for creating and configuring mock objects. Key Mockery features used:

FeatureUsage
Mockery::mock(Terminal::class)Creates a mock Terminal instance
shouldReceive($method)Defines method expectations
byDefault()Marks expectation as optional
andReturn($value)Specifies return value
once()Expects method to be called exactly once

The mock must be properly configured before the prompt's render loop begins, as the prompt will immediately start calling read() and other terminal methods.

Sources: src/Concerns/FakesInputOutput.php25-38

Refresh this wiki

On this page