Doubles API
This reference lists double factories, argument matchers, captors, and mock plans.
These signatures are the public API.
Argument
Namespace: Greenlight\Doubles
final class Argument
any()
This matcher accepts all values in its position.
public static function any(): Any
type()
This matcher accepts instances of the specified class or interface.
It also accepts values when get_debug_type() returns $type.
public static function type(string $type): ArgumentMatcher
predicate()
This matcher accepts the value when the closure returns true. The description identifies the constraint in failure messages.
public static function predicate(\Closure $predicate, string $description = 'predicate'): ArgumentMatcher
equals()
This matcher uses the same deep equality as a value in with().
This form states the comparison explicitly.
public static function equals(mixed $value): ArgumentMatcher
captor()
This matcher accepts all values. It records the value when Greenlight selects the related expectation for the call.
public static function captor(): ArgumentCaptor
ArgumentCaptor
Namespace: Greenlight\Doubles
A captor records the argument in its position. It records the argument when Greenlight selects the related expectation for the call.
matches() always accepts the value and does not record it. Only the
expectation selected for the call can record an argument. Thus, checks of
candidate expectations cannot add values to a captor.
final class ArgumentCaptor implements ArgumentMatcher
matches()
public function matches(mixed $value): bool
describe()
public function describe(): string
values()
public function values(): array
PHPDoc:
@return list<mixed>
value()
public function value(): mixed
ArgumentMatcher
Namespace: Greenlight\Doubles
An argument constraint for one position in with().
matches() determines if the matcher accepts a value in its position.
Candidate expectations can receive checks for calls that they do not
answer. Thus, matches() must not cause side effects. describe() identifies
the constraint in failure messages.
Use the Argument factories to get matchers.
interface ArgumentMatcher
matches()
public function matches(mixed $value): bool;
describe()
public function describe(): string;
Doubles
Namespace: Greenlight\Doubles
Mocks are strict. A call without a planned expectation fails the test immediately. Each return value needs a configured result. Stubs cause an error for all interactions. Spies record calls to methods without a return value.
A verification failure throws one ExpectationFailed. It contains one
FailureDetail for each unmet expectation. Thus, the reporter shows it in
the same format as an Expect failure.
Doubles supports interfaces and non-final classes. Class constructors do
not run. Doubles does not support partial mocks or static interception.
final class Doubles implements Disposable
__construct()
public function __construct(?string $proxyDirectory = null)
PHPDoc:
@param string|null $proxyDirectory Directory for generated proxy classes. An empty string is invalid. The default is a project directory in the system temporary directory. A hash of the current working directory identifies it.
mock()
Creates a strict double. Verification checks each planned expectation at test end. A call without an expectation fails the test immediately.
public function mock(string $type, ?\Closure $plan = null): object
PHPDoc:
@template T of object@param class-string<T> $type@param \Closure(MockPlan): void|null $plan@return T
stub()
Creates an inert double that satisfies the specified type. All interactions cause a test error. Use a mock with explicit expectations when a collaborator must supply results.
public function stub(string $type): object
PHPDoc:
@template T of object@param class-string<T> $type@return T
spy()
Creates a spy that records each call and its arguments. A call to a
method that returns a value causes a test error. Use callsTo() to get the
calls. Use Expect to check them.
public function spy(string $type): object
PHPDoc:
@template T of object@param class-string<T> $type@return T
callsTo()
Gets the calls to one method of a double from this factory. The result uses call order. Each entry contains the arguments for one call. The method must exist on the doubled type.
public function callsTo(object $double, string $method): array
PHPDoc:
@return list<list<mixed>>
dispose()
Verifies mocks and clears their state when the test scope closes.
One ExpectationFailed contains the details for all unmet expectations.
[\Override]
public function dispose(): void
Fake
Namespace: Greenlight\Doubles
Identifies a manual in-memory test implementation.
The interface does not change behavior. It lets reporters and tools identify the object as an intentional fake, not production code under test.
interface Fake
This type does not declare public members.
MockPlan
Namespace: Greenlight\Doubles
Defines the mock plan that Doubles::mock() supplies to its closure.
Use the fluent methods to declare call patterns. The default cardinality is at least one call. The verifier checks each declared pattern when the test scope closes.
final readonly class MockPlan
any()
Returns the with() wildcard that accepts all values in its position.
public static function any(): Any
expects()
public function expects(string $method): MethodExpectation
PHPDoc:
@param non-empty-string $method