Browse documentation

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

View source

any()

This matcher accepts all values in its position.

public static function any(): Any

View source

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

View source

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

View source

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

View source

captor()

This matcher accepts all values. It records the value when Greenlight selects the related expectation for the call.

public static function captor(): ArgumentCaptor

View source

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

View source

matches()

public function matches(mixed $value): bool

View source

describe()

public function describe(): string

View source

values()

public function values(): array

PHPDoc:

View source

value()

public function value(): mixed

View source

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

View source

matches()

public function matches(mixed $value): bool;

View source

describe()

public function describe(): string;

View source

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

View source

__construct()

public function __construct(?string $proxyDirectory = null)

PHPDoc:

View source

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:

View source

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:

View source

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:

View source

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:

View source

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

View source

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

View source

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

View source

any()

Returns the with() wildcard that accepts all values in its position.

public static function any(): Any

View source

expects()

public function expects(string $method): MethodExpectation

PHPDoc:

View source