FakeRandom
in package
implements
IRandom
Fake random implementation for testing.
Provides predictable, deterministic "random" values for testing. Can be seeded with specific values or use a predictable sequence.
Table of Contents
Interfaces
- IRandom
- Interface for random generation abstraction.
Properties
- $byteSequence : array<string|int, mixed>
- $intSequence : array<string|int, mixed>
- $seed : int
- $uniqueIdSequence : array<string|int, mixed>
Methods
- bytes() : string
- Generate cryptographically secure random bytes
- float() : float
- Generate random float between 0 and 1
- int() : int
- Generate random integer in range (inclusive)
- setByteSequence() : void
- Set sequence of bytes to return from bytes() method
- setIntSequence() : void
- Set sequence of integers to return from int() method
- setSeed() : void
- Set seed for predictable generation
- setUniqueIdSequence() : void
- Set sequence of unique IDs to return from uniqueId() method
- shuffle() : array<string|int, mixed>
- Shuffle an array randomly
- string() : string
- Generate random string using specified charset
- uniqueId() : string
- Generate unique identifier
- stringFromCharset() : string
- Generate string from character set using predictable pattern
Properties
$byteSequence
private
array<string|int, mixed>
$byteSequence
= []
$intSequence
private
array<string|int, mixed>
$intSequence
= []
$seed
private
int
$seed
= 0
$uniqueIdSequence
private
array<string|int, mixed>
$uniqueIdSequence
= []
Methods
bytes()
Generate cryptographically secure random bytes
public
bytes(int $length) : string
Parameters
- $length : int
-
Number of bytes to generate
Tags
Return values
string —Random bytes
float()
Generate random float between 0 and 1
public
float() : float
Tags
Return values
float —Random float between 0.0 and 1.0
int()
Generate random integer in range (inclusive)
public
int(int $min, int $max) : int
Parameters
- $min : int
-
Minimum value (inclusive)
- $max : int
-
Maximum value (inclusive)
Tags
Return values
int —Random integer
setByteSequence()
Set sequence of bytes to return from bytes() method
public
setByteSequence(array<string|int, mixed> $sequence) : void
Parameters
- $sequence : array<string|int, mixed>
-
Array of byte strings
setIntSequence()
Set sequence of integers to return from int() method
public
setIntSequence(array<string|int, mixed> $sequence) : void
Parameters
- $sequence : array<string|int, mixed>
-
Array of integers
setSeed()
Set seed for predictable generation
public
setSeed(int $seed) : void
Parameters
- $seed : int
-
Seed value
setUniqueIdSequence()
Set sequence of unique IDs to return from uniqueId() method
public
setUniqueIdSequence(array<string|int, mixed> $sequence) : void
Parameters
- $sequence : array<string|int, mixed>
-
Array of unique ID strings
shuffle()
Shuffle an array randomly
public
shuffle(array<string|int, mixed> $array) : array<string|int, mixed>
Parameters
- $array : array<string|int, mixed>
-
Array to shuffle
Tags
Return values
array<string|int, mixed> —Shuffled array
string()
Generate random string using specified charset
public
string(int $length[, string $charset = 'hex' ]) : string
Parameters
- $length : int
-
Length of the string
- $charset : string = 'hex'
-
Character set ('hex', 'base64', 'alphanumeric', 'alpha', 'numeric')
Tags
Return values
string —Random string
uniqueId()
Generate unique identifier
public
uniqueId([string $prefix = '' ]) : string
Parameters
- $prefix : string = ''
-
Prefix for the unique ID
Tags
Return values
string —Unique identifier
stringFromCharset()
Generate string from character set using predictable pattern
private
stringFromCharset(int $length, string $chars) : string
Parameters
- $length : int
-
Length of string
- $chars : string
-
Available characters
Return values
string —Predictable string