VOOZH about

URL: https://deepwiki.com/hypervel/process/6.4-fake-process-sequences

⇱ Fake Process Sequences | hypervel/process | DeepWiki


Loading...
Menu

Fake Process Sequences

Purpose and Scope

FakeProcessSequence enables stateful testing scenarios by returning different results on successive invocations of the same command. This class allows tests to simulate processes that exhibit different behavior across multiple executions, such as retry scenarios, progressive state changes, or time-dependent operations.

For basic fake process results that don't change across invocations, see Fake Process Results. For complex single-execution fakes with detailed configuration, see Fake Process Descriptions. For general information about the faking system, see Faking Overview.

Sources: src/FakeProcessSequence.php1-93


Overview

FakeProcessSequence maintains an ordered array of process results or descriptions that are returned sequentially on each invocation. When used as a fake handler in Factory::fake(), the sequence automatically shifts to the next result each time the command is executed.


Sources: src/FakeProcessSequence.php10-27 src/FakeProcessSequence.php81-92


Creating Sequences

Sequences are created by instantiating FakeProcessSequence with optional initial processes and a failWhenEmpty flag:

Constructor ParameterTypeDefaultDescription
$processesarray[]Initial array of process results to add to the sequence
$failWhenEmptybooltrueWhether to throw OutOfBoundsException when sequence is exhausted

Sources: src/FakeProcessSequence.php23-27


Adding Results to Sequences

The push() method adds results to the end of the sequence. It accepts multiple input types and normalizes them to FakeProcessDescription or ProcessResultContract instances:

Input TypeNormalized ToDescription
stringFakeProcessResultCreates result with given string as output
arrayFakeProcessResultCreates result with array elements as output lines
FakeProcessDescriptionFakeProcessDescriptionUsed directly without conversion
ProcessResultContractProcessResultContractUsed directly without conversion

Push Method Flow


Example usage:


Sources: src/FakeProcessSequence.php32-37 src/FakeProcessSequence.php53-58


Empty Sequence Handling

FakeProcessSequence provides three strategies for handling invocations after all results have been consumed:

Fail on Empty (Default)

When failWhenEmpty is true (default), the sequence throws OutOfBoundsException when invoked after exhaustion:


Custom Empty Response

The whenEmpty() method sets a fallback result and disables failure on empty:


Silent Empty Response

The dontFailWhenEmpty() method returns an empty success result when exhausted:


Empty Handling State Diagram


Sources: src/FakeProcessSequence.php42-48 src/FakeProcessSequence.php63-66


Invocation Behavior

FakeProcessSequence implements the __invoke() magic method, allowing instances to be used as callable handlers in Factory::fake(). Each invocation shifts the next result from the array:

Invocation Flow


Sources: src/FakeProcessSequence.php81-92


Integration with Factory

FakeProcessSequence is used as a handler value in Factory::fake() method. When the Factory encounters a sequence handler, it invokes the sequence for each matching process execution:


Sources: src/FakeProcessSequence.php81-92


Use Cases

Retry Logic Testing

Test code that retries failing processes until success:


Progressive State Changes

Simulate processes that affect system state over time:


Degradation Scenarios

Test behavior when a command gradually degrades:


Exhaustion Detection

Test that code handles sequence exhaustion appropriately:


Sources: src/FakeProcessSequence.php1-93


Method Reference

MethodParametersReturnsDescription
__construct()array $processes = []
bool $failWhenEmpty = true
-Creates new sequence with optional initial processes
push()string|array|FakeProcessDescription|ProcessResultContract $processstaticAdds a result to the end of the sequence
whenEmpty()string|array|FakeProcessDescription|ProcessResultContract $processstaticSets fallback result for empty sequence
dontFailWhenEmpty()-staticMakes empty sequence return empty success result
isEmpty()-boolChecks if all results have been consumed
__invoke()-FakeProcessDescription|ProcessResultContractReturns next result in sequence

Internal Methods

MethodParametersReturnsDescription
toProcessResult()string|array|FakeProcessDescription|ProcessResultContract $processFakeProcessDescription|ProcessResultContractNormalizes input to FakeProcessResult or passes through objects

Sources: src/FakeProcessSequence.php1-93


Class Properties

PropertyTypeVisibilityDefaultDescription
$processesarray<int, mixed>protected[]Array of remaining results in the sequence
$failWhenEmptyboolprotectedtrueWhether to throw exception when sequence is empty
$emptyProcessFakeProcessDescription|ProcessResultContract|nullprotectednullFallback result returned when sequence is empty

Sources: src/FakeProcessSequence.php14-26