Greenlight

Documentation

API reference

On this page

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(): ArgumentMatcher

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

PHPDoc:

View source

intersection()

This matcher accepts values that have every specified type.

public static function intersection(string $first, string $second, string ...$rest): ArgumentMatcher

PHPDoc:

View source

union()

This matcher accepts values that have one or more specified types.

public static function union(string $first, string $second, string ...$rest): ArgumentMatcher

PHPDoc:

View source

predicate()

This matcher accepts the value when the closure returns true. A declared parameter type rejects incompatible values before the closure runs. The description identifies the constraint in failure messages.

public static function predicate(\Closure $predicate, string $description = 'predicate'): ArgumentMatcher

PHPDoc:

View source

equals()

This matcher uses the same deep equality as Expectation::toEqual(). Use it when with() must compare by value instead of identity.

public static function equals(mixed $value): ArgumentMatcher

PHPDoc:

View source

allOf()

This matcher accepts a value when all its matchers accept the value. Greenlight checks the matchers in argument order and stops after a failure.

public static function allOf(
    ArgumentMatcher $first,
    ArgumentMatcher $second,
    ArgumentMatcher ...$rest,
): ArgumentMatcher

PHPDoc:

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

PHPDoc:

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

PHPDoc:

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

PHPDoc:

matches()

public function matches(mixed $value): bool;

View source

describe()

public function describe(): string;

View source

Doubles

Namespace: Greenlight\Doubles

Creates mocks, stubs, and spies. For intercepted methods, mocks fail on calls without a planned expectation. Each return value needs a configured result. Stubs cause an error for intercepted calls. Spies record intercepted 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 classes that are neither final nor readonly. Class constructors do not run. Final methods keep their original implementation. Doubles does not support partial mocks or static interception.

Greenlight disposes injected factories after each test attempt. If you construct a factory directly, call dispose() to verify its mocks.

final class Doubles implements Disposable

View source

__construct()

public function __construct(?string $proxyDirectory = null)

PHPDoc:

View source

mock()

Creates a strict double. Disposal checks each planned expectation. An intercepted call without an expectation fails the test immediately.

public function mock(string $type, ?\Closure $plan = null): object

PHPDoc:

View source

stub()

Creates a double that satisfies the specified type. Intercepted calls 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 intercepted calls and their arguments. An intercepted 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.

public function dispose(): void

PHPDoc:

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.

InvalidDoubleUsage

Namespace: Greenlight\Doubles

Identifies incorrect use of the doubles API. Examples include an unsupported type or a method that Doubles cannot intercept. Other examples are a prohibited interaction or a return value without a configured result.

These conditions are errors in the test code, not expectation failures. Thus, Greenlight reports the test as an error.

final class InvalidDoubleUsage extends \LogicException

View source

stubWasCalled()

public static function stubWasCalled(string $type, string $method): self

View source

returnNotConfigured()

public static function returnNotConfigured(string $type, string $method): self

View source

spyCannotAnswer()

public static function spyCannotAnswer(string $type, string $method): self

View source

noSuchMethod()

public static function noSuchMethod(string $type, string $method): self

View source

noSuchRecordedMethod()

public static function noSuchRecordedMethod(string $type, string $method): self

View source

staticMethod()

public static function staticMethod(string $type, string $method): self

View source

neverMethodRequiresThrow()

public static function neverMethodRequiresThrow(string $type, string $method): self

View source

methodNotPublic()

public static function methodNotPublic(string $type, string $method): self

View source

finalMethod()

public static function finalMethod(string $type, string $method): self

View source

unsupportedReflectionType()

public static function unsupportedReflectionType(string $typeClass): self

View source

parentTypeWithoutParent()

public static function parentTypeWithoutParent(string $context): self

View source

unsupportedNestedReflectionType()

public static function unsupportedNestedReflectionType(string $typeClass): self

View source

cannotDoubleEnum()

public static function cannotDoubleEnum(string $type): self

View source

cannotDoubleReadonly()

public static function cannotDoubleReadonly(string $type): self

View source

cannotDoubleFinal()

public static function cannotDoubleFinal(string $type): self

View source

cannotDoubleTrait()

public static function cannotDoubleTrait(string $type): self

View source

notDoubleable()

public static function notDoubleable(string $type): self

View source

attachHandlerCollision()

public static function attachHandlerCollision(string $class): self

View source

handlerPropertyCollision()

public static function handlerPropertyCollision(string $class): self

View source

defaultValueNotReproducible()

public static function defaultValueNotReproducible(string $parameter, string $class, string $method): self

View source

defaultConstantUnresolvable()

public static function defaultConstantUnresolvable(string $parameter): self

View source

objectDefaultNotReproducible()

public static function objectDefaultNotReproducible(string $parameter, string $class, string $method): self

View source

objectDefaultSourceUnavailable()

public static function objectDefaultSourceUnavailable(string $parameter, string $class, string $method): self

View source

objectDefaultScopeUnavailable()

public static function objectDefaultScopeUnavailable(string $parameter, string $class, string $method): self

View source

proxyDirectoryNotCreated()

public static function proxyDirectoryNotCreated(string $directory, ?string $reason = null): self

View source

proxyFileNotWritten()

public static function proxyFileNotWritten(string $file, \Throwable $cause): self

View source

proxyFileNotLoaded()

public static function proxyFileNotLoaded(string $file, ?\Throwable $cause = null): self

View source

workingDirectoryUnresolved()

public static function workingDirectoryUnresolved(): self

View source

foreignDouble()

public static function foreignDouble(string $class): self

View source

invalidTimes()

public static function invalidTimes(int $count): self

View source

invalidAtLeast()

public static function invalidAtLeast(int $count): self

View source

tooFewPlannedArguments()

public static function tooFewPlannedArguments(
    string $selector,
    string $type,
    string $method,
    int $actual,
    int $required,
): self

PHPDoc:

View source

tooManyPlannedArguments()

public static function tooManyPlannedArguments(
    string $selector,
    string $type,
    string $method,
    int $actual,
    int $maximum,
): self

PHPDoc:

View source

incompatiblePlannedArgumentMatcher()

public static function incompatiblePlannedArgumentMatcher(
    string $selector,
    string $type,
    string $method,
    int $position,
    string $matcherType,
    string $parameter,
    string $parameterType,
): self

PHPDoc:

View source

tooFewCallArguments()

public static function tooFewCallArguments(string $type, string $method, int $actual, int $required): self

PHPDoc:

View source

tooManyCallArguments()

public static function tooManyCallArguments(string $type, string $method, int $actual, int $maximum): self

PHPDoc:

View source

conflictingAnswers()

public static function conflictingAnswers(string $method): self

View source

emptySequence()

public static function emptySequence(string $method): self

View source

sequenceExhausted()

public static function sequenceExhausted(string $method, int $count): self

View source

nothingCaptured()

public static function nothingCaptured(): self

View source

invalidCaptorPosition()

public static function invalidCaptorPosition(int $position): self

View source

invalidArgumentType()

public static function invalidArgumentType(): self

View source

invalidArgumentTypeCombination()

public static function invalidArgumentTypeCombination(string $factory): self

View source

compositeArgumentCaptor()

public static function compositeArgumentCaptor(): self

View source

MethodExpectation

Namespace: Greenlight\Doubles

Defines one planned call pattern for a method of a double. The plan specifies the accepted arguments, cardinality, and result.

MockPlan::expects() creates this object. Its fluent plan methods are the public interface. The call handler and verifier use members that have the @internal tag.

Bare argument values use strict comparison (===). Use Argument::equals() to apply deep equality.

final class MethodExpectation

View source

PHPDoc:

$method

public readonly string $method;

View source

withNoArguments()

public function withNoArguments(): self

PHPDoc:

View source

with()

public function with(mixed $first, mixed ...$rest): self

PHPDoc:

View source

once()

public function once(): self

View source

times()

public function times(int $count): self

PHPDoc:

View source

atLeast()

public function atLeast(int $count): self

PHPDoc:

View source

never()

public function never(): self

View source

andReturns()

public function andReturns(mixed $value): self

PHPDoc:

View source

andReturnsSequence()

Each accepted call consumes the next value. A call after the last value causes an error in the test code.

public function andReturnsSequence(mixed ...$values): self

PHPDoc:

View source

andReturnsUsing()

The closure receives the call arguments. The call returns the value from the closure.

public function andReturnsUsing(\Closure $answer): self

PHPDoc:

View source

andThrows()

public function andThrows(\Throwable $throwable): self

PHPDoc:

View source

captureArgument()

Records the argument at $position each time Greenlight selects this expectation for a call. The method returns the captor and ends the fluent chain. Before you call this method, configure the cardinality. If the doubled method returns a value, configure its result first.

public function captureArgument(int $position = 0): ArgumentCaptor

PHPDoc:

View source

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

PHPDoc:

any()

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

public static function any(): ArgumentMatcher

View source

expects()

public function expects(string $method): MethodExpectation

PHPDoc:

View source