Expectations API
This reference lists immediate and temporal expectation types.
These signatures are the public API.
CallExpectation
Namespace: Greenlight\Expect
Checks a call’s captured outcome. Construct with Expect::calling().
Matchers in one immediate chain share one invocation.
final class CallExpectation
PHPDoc:
@template T
not()
public function not(): self
PHPDoc:
@return self<T>
because()
Sets a reason for all subsequent matchers in the chain.
Another because() call replaces the reason.
public function because(string $reason): self
PHPDoc:
@param non-empty-string $reason@return self<T>@throws ExpectationFailed
toReturn()
Checks identity with the return value. An unexpected throwable propagates.
public function toReturn(mixed $expected): self
PHPDoc:
@return self<T>@throws ExpectationFailed
returnValue()
Selects the return value without invoking the call.
public function returnValue(): ReturnValueExpectation
PHPDoc:
@return ReturnValueExpectation<T>@throws ExpectationFailed
toThrow()
Checks the thrown type, exact object, or typed callback constraint. With no constraint, matches any Throwable.
public function toThrow(
string|\Closure|\Throwable $throwable = \Throwable::class,
?string $matching = null,
?string $message = null,
): self
PHPDoc:
@template TThrowable of \Throwable@param class-string<TThrowable>|TThrowable|\Closure(TThrowable): void $throwable@return self<T>@throws ExpectationFailed
eventually()
public function eventually(): PendingEventuallyCall
PHPDoc:
@return PendingEventuallyCall<T>@throws ExpectationFailed
consistently()
public function consistently(): PendingConsistentlyCall
PHPDoc:
@return PendingConsistentlyCall<T>@throws ExpectationFailed
ConsistentlyExpectation
Namespace: Greenlight\Expect
Checks each probe value for a fixed period and fails on the first mismatch.
Use Expect::calling(...)->returnValue()->consistently() and for() to create this object.
final class ConsistentlyExpectation extends TemporalExpectation
PHPDoc:
@template T@extends TemporalExpectation<T>
toBe()
Passes when the subject and expected value are identical (===).
public function toBe(mixed $expected): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toEqual()
Passes when the subject and expected value satisfy the rules for deep
equality in the Expectation class description.
public function toEqual(mixed $expected): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed@throws \InvalidArgumentException when the comparison follows a cyclic array
toEqualCanonicalizing()
Uses the toEqual() rules but ignores list-element order. Canonicalization
recurses through array values. It does not inspect object properties.
Thus, lists in object properties keep their order. Associative arrays
keep their keys.
public function toEqualCanonicalizing(mixed $expected): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed@throws \InvalidArgumentException when an array selected for comparison or ordering contains a cycle
toBeOneOf()
Passes when the subject is identical (===) to one of the options.
public function toBeOneOf(mixed ...$options): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeIn()
Passes when the haystack contains the subject by identity (===). This
matcher is the reverse of toContain(). The check consumes a Traversable
haystack.
public function toBeIn(iterable $haystack): Expectation
PHPDoc:
@param iterable<mixed> $haystack@return Expectation<T>@throws ExpectationFailed
toBeInstanceOf()
public function toBeInstanceOf(string $class): Expectation
PHPDoc:
@param class-string $class@return Expectation<T>@throws ExpectationFailed
toBeTrue()
public function toBeTrue(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeFalse()
public function toBeFalse(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeNull()
public function toBeNull(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeArray()
public function toBeArray(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeString()
public function toBeString(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeInt()
public function toBeInt(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeFloat()
public function toBeFloat(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeBool()
public function toBeBool(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeCallable()
public function toBeCallable(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeIterable()
public function toBeIterable(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toContain()
For a string subject, checks for a string needle. For an iterable subject, checks for the value by identity (===). Iteration stops at the first match or the end of the subject.
public function toContain(mixed $needle): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toHaveCount()
Accepts an array, Countable, or Traversable subject. Uses count()
for arrays and Countable objects. Otherwise, consumes the iterator.
public function toHaveCount(int $count): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeEmpty()
Passes when the subject is an empty string or contains no elements.
Accepts a string, array, Countable, or Traversable subject. Uses
count() for arrays and Countable objects. For other Traversable
objects, consumes the iterator.
public function toBeEmpty(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toHaveLength()
For a valid UTF-8 string, measures the number of code points. For other
strings, measures the number of bytes. Array and Countable subjects use
count().
public function toHaveLength(int $length): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toHaveKey()
The subject must be an array or an ArrayAccess implementation. The
matcher uses array_key_exists() for arrays and offsetExists() for
ArrayAccess.
public function toHaveKey(int|string $key): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toContainSubset()
Each subset key must exist in the subject with an equal value. Equality
uses the toEqual() rules. A nested array is also a subset. The
related nested subject array can contain extra keys. The failure
identifies the first different key by its dot-separated path.
public function toContainSubset(array $subset): Expectation
PHPDoc:
@param array<array-key, mixed> $subset@return Expectation<T>@throws ExpectationFailed
toBeGreaterThan()
public function toBeGreaterThan(int|float $bound): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeGreaterThanOrEqual()
public function toBeGreaterThanOrEqual(int|float $bound): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeLessThan()
public function toBeLessThan(int|float $bound): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeLessThanOrEqual()
public function toBeLessThanOrEqual(int|float $bound): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeWithin()
Passes when the absolute difference between the numeric subject and
$of is not more than $delta. Use a finite tolerance of zero or more.
public function toBeWithin(float $delta, float $of): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toMatch()
public function toMatch(string $pattern): Expectation
PHPDoc:
@return Expectation<T>@throws \InvalidArgumentException when the pattern is not a valid regular expression@throws ExpectationFailed
toStartWith()
public function toStartWith(string $prefix): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toEndWith()
public function toEndWith(string $suffix): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeJson()
The subject must be a string. The matcher passes when the string contains valid JSON.
public function toBeJson(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toMatchJson()
The subject must be a string that contains valid JSON. The matcher decodes the subject and expected JSON. It then applies deep equality to the results. Object-key order has no effect. Invalid subject JSON causes an expectation failure. Invalid expected JSON causes a usage error.
public function toMatchJson(string $expected): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
not()
Negates the next matcher for every value returned by the probe.
final public function not(): static
because()
Sets a reason for all subsequent matchers in the chain.
Another because() call replaces the reason.
If the matcher fails, the failure message ends with “because” and the reason. An empty reason causes a usage failure.
final public function because(string $reason): static
PHPDoc:
@param non-empty-string $reason@throws ExpectationFailed
__call()
Runs a native or configured extension matcher against each probe value.
final public function __call(string $name, array $arguments): Expectation
PHPDoc:
@param array<array-key, mixed> $arguments@return Expectation<T>@throws \BadMethodCallException if no native or registered extension matcher has the requested name@throws ExpectationFailed
EventuallyExpectation
Namespace: Greenlight\Expect
Polls the probe until its matcher passes or the deadline expires.
Use Expect::calling(...)->returnValue()->eventually() and within() to create this object.
final class EventuallyExpectation extends TemporalExpectation
PHPDoc:
@template T@extends TemporalExpectation<T>
toBe()
Passes when the subject and expected value are identical (===).
public function toBe(mixed $expected): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toEqual()
Passes when the subject and expected value satisfy the rules for deep
equality in the Expectation class description.
public function toEqual(mixed $expected): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed@throws \InvalidArgumentException when the comparison follows a cyclic array
toEqualCanonicalizing()
Uses the toEqual() rules but ignores list-element order. Canonicalization
recurses through array values. It does not inspect object properties.
Thus, lists in object properties keep their order. Associative arrays
keep their keys.
public function toEqualCanonicalizing(mixed $expected): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed@throws \InvalidArgumentException when an array selected for comparison or ordering contains a cycle
toBeOneOf()
Passes when the subject is identical (===) to one of the options.
public function toBeOneOf(mixed ...$options): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeIn()
Passes when the haystack contains the subject by identity (===). This
matcher is the reverse of toContain(). The check consumes a Traversable
haystack.
public function toBeIn(iterable $haystack): Expectation
PHPDoc:
@param iterable<mixed> $haystack@return Expectation<T>@throws ExpectationFailed
toBeInstanceOf()
public function toBeInstanceOf(string $class): Expectation
PHPDoc:
@param class-string $class@return Expectation<T>@throws ExpectationFailed
toBeTrue()
public function toBeTrue(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeFalse()
public function toBeFalse(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeNull()
public function toBeNull(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeArray()
public function toBeArray(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeString()
public function toBeString(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeInt()
public function toBeInt(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeFloat()
public function toBeFloat(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeBool()
public function toBeBool(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeCallable()
public function toBeCallable(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeIterable()
public function toBeIterable(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toContain()
For a string subject, checks for a string needle. For an iterable subject, checks for the value by identity (===). Iteration stops at the first match or the end of the subject.
public function toContain(mixed $needle): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toHaveCount()
Accepts an array, Countable, or Traversable subject. Uses count()
for arrays and Countable objects. Otherwise, consumes the iterator.
public function toHaveCount(int $count): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeEmpty()
Passes when the subject is an empty string or contains no elements.
Accepts a string, array, Countable, or Traversable subject. Uses
count() for arrays and Countable objects. For other Traversable
objects, consumes the iterator.
public function toBeEmpty(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toHaveLength()
For a valid UTF-8 string, measures the number of code points. For other
strings, measures the number of bytes. Array and Countable subjects use
count().
public function toHaveLength(int $length): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toHaveKey()
The subject must be an array or an ArrayAccess implementation. The
matcher uses array_key_exists() for arrays and offsetExists() for
ArrayAccess.
public function toHaveKey(int|string $key): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toContainSubset()
Each subset key must exist in the subject with an equal value. Equality
uses the toEqual() rules. A nested array is also a subset. The
related nested subject array can contain extra keys. The failure
identifies the first different key by its dot-separated path.
public function toContainSubset(array $subset): Expectation
PHPDoc:
@param array<array-key, mixed> $subset@return Expectation<T>@throws ExpectationFailed
toBeGreaterThan()
public function toBeGreaterThan(int|float $bound): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeGreaterThanOrEqual()
public function toBeGreaterThanOrEqual(int|float $bound): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeLessThan()
public function toBeLessThan(int|float $bound): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeLessThanOrEqual()
public function toBeLessThanOrEqual(int|float $bound): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeWithin()
Passes when the absolute difference between the numeric subject and
$of is not more than $delta. Use a finite tolerance of zero or more.
public function toBeWithin(float $delta, float $of): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toMatch()
public function toMatch(string $pattern): Expectation
PHPDoc:
@return Expectation<T>@throws \InvalidArgumentException when the pattern is not a valid regular expression@throws ExpectationFailed
toStartWith()
public function toStartWith(string $prefix): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toEndWith()
public function toEndWith(string $suffix): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeJson()
The subject must be a string. The matcher passes when the string contains valid JSON.
public function toBeJson(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toMatchJson()
The subject must be a string that contains valid JSON. The matcher decodes the subject and expected JSON. It then applies deep equality to the results. Object-key order has no effect. Invalid subject JSON causes an expectation failure. Invalid expected JSON causes a usage error.
public function toMatchJson(string $expected): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
not()
Negates the next matcher for every value returned by the probe.
final public function not(): static
because()
Sets a reason for all subsequent matchers in the chain.
Another because() call replaces the reason.
If the matcher fails, the failure message ends with “because” and the reason. An empty reason causes a usage failure.
final public function because(string $reason): static
PHPDoc:
@param non-empty-string $reason@throws ExpectationFailed
__call()
Runs a native or configured extension matcher against each probe value.
final public function __call(string $name, array $arguments): Expectation
PHPDoc:
@param array<array-key, mixed> $arguments@return Expectation<T>@throws \BadMethodCallException if no native or registered extension matcher has the requested name@throws ExpectationFailed
Expect
Namespace: Greenlight\Expect
Creates immediate and temporal expectations.
The worker loads the configured expectation extensions before test execution.
Each expectation chain uses a snapshot of those extensions.
The runner also loads Greenlight\expect($value) for value expectations.
Use Greenlight\expect()->calling($call) to select a call explicitly.
Import the function for short expectation calls.
final class Expect
value()
public static function value(mixed $value): Expectation
PHPDoc:
@template T@param T $value@return Expectation<T>
calling()
Selects a call without executing it.
public static function calling(callable $call): CallExpectation
PHPDoc:
@template T@param callable(): T $call@return CallExpectation<T>
Expectation
Namespace: Greenlight\Expect
Checks one value. Callable values are never invoked.
Use Expect::value() to create an expectation.
toEqual() uses these rules for deep equality:
-
Integers and floats use numeric value. Thus,
1equals1.0.NANdoes not equal a value, even itself. -
Other scalar values and
nulluse strict equality. Thus,'1'does not equal1. -
Arrays are equal when they contain the same keys and recursively equal values. Key order has no effect. Cyclic arrays are unsupported. When the comparison follows a cycle, it raises
InvalidArgumentException. -
Enum cases, closures, and resources use identity.
-
DateTimeInterfaceinstances are equal at the same instant and microsecond. The timezone has no effect. -
Other objects are equal when they have the same class and recursively equal properties. This rule includes private and inherited properties. The comparison safely processes cyclic object structures.
class Expectation
PHPDoc:
@template T
toBe()
Passes when the subject and expected value are identical (===).
public function toBe(mixed $expected): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toEqual()
Passes when the subject and expected value satisfy the rules for deep
equality in the Expectation class description.
public function toEqual(mixed $expected): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed@throws \InvalidArgumentException when the comparison follows a cyclic array
toEqualCanonicalizing()
Uses the toEqual() rules but ignores list-element order. Canonicalization
recurses through array values. It does not inspect object properties.
Thus, lists in object properties keep their order. Associative arrays
keep their keys.
public function toEqualCanonicalizing(mixed $expected): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed@throws \InvalidArgumentException when an array selected for comparison or ordering contains a cycle
toBeOneOf()
Passes when the subject is identical (===) to one of the options.
public function toBeOneOf(mixed ...$options): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeIn()
Passes when the haystack contains the subject by identity (===). This
matcher is the reverse of toContain(). The check consumes a Traversable
haystack.
public function toBeIn(iterable $haystack): Expectation
PHPDoc:
@param iterable<mixed> $haystack@return Expectation<T>@throws ExpectationFailed
toBeInstanceOf()
public function toBeInstanceOf(string $class): Expectation
PHPDoc:
@param class-string $class@return Expectation<T>@throws ExpectationFailed
toBeTrue()
public function toBeTrue(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeFalse()
public function toBeFalse(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeNull()
public function toBeNull(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeArray()
public function toBeArray(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeString()
public function toBeString(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeInt()
public function toBeInt(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeFloat()
public function toBeFloat(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeBool()
public function toBeBool(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeCallable()
public function toBeCallable(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeIterable()
public function toBeIterable(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toContain()
For a string subject, checks for a string needle. For an iterable subject, checks for the value by identity (===). Iteration stops at the first match or the end of the subject.
public function toContain(mixed $needle): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toHaveCount()
Accepts an array, Countable, or Traversable subject. Uses count()
for arrays and Countable objects. Otherwise, consumes the iterator.
public function toHaveCount(int $count): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeEmpty()
Passes when the subject is an empty string or contains no elements.
Accepts a string, array, Countable, or Traversable subject. Uses
count() for arrays and Countable objects. For other Traversable
objects, consumes the iterator.
public function toBeEmpty(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toHaveLength()
For a valid UTF-8 string, measures the number of code points. For other
strings, measures the number of bytes. Array and Countable subjects use
count().
public function toHaveLength(int $length): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toHaveKey()
The subject must be an array or an ArrayAccess implementation. The
matcher uses array_key_exists() for arrays and offsetExists() for
ArrayAccess.
public function toHaveKey(int|string $key): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toContainSubset()
Each subset key must exist in the subject with an equal value. Equality
uses the toEqual() rules. A nested array is also a subset. The
related nested subject array can contain extra keys. The failure
identifies the first different key by its dot-separated path.
public function toContainSubset(array $subset): Expectation
PHPDoc:
@param array<array-key, mixed> $subset@return Expectation<T>@throws ExpectationFailed
toBeGreaterThan()
public function toBeGreaterThan(int|float $bound): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeGreaterThanOrEqual()
public function toBeGreaterThanOrEqual(int|float $bound): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeLessThan()
public function toBeLessThan(int|float $bound): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeLessThanOrEqual()
public function toBeLessThanOrEqual(int|float $bound): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeWithin()
Passes when the absolute difference between the numeric subject and
$of is not more than $delta. Use a finite tolerance of zero or more.
public function toBeWithin(float $delta, float $of): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toMatch()
public function toMatch(string $pattern): Expectation
PHPDoc:
@return Expectation<T>@throws \InvalidArgumentException when the pattern is not a valid regular expression@throws ExpectationFailed
toStartWith()
public function toStartWith(string $prefix): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toEndWith()
public function toEndWith(string $suffix): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeJson()
The subject must be a string. The matcher passes when the string contains valid JSON.
public function toBeJson(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toMatchJson()
The subject must be a string that contains valid JSON. The matcher decodes the subject and expected JSON. It then applies deep equality to the results. Object-key order has no effect. Invalid subject JSON causes an expectation failure. Invalid expected JSON causes a usage error.
public function toMatchJson(string $expected): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
not()
public function not(): static
because()
Sets a reason for all subsequent matchers in the chain.
Another because() call replaces the reason.
public function because(string $reason): static
PHPDoc:
@param non-empty-string $reason@throws ExpectationFailed
__call()
public function __call(string $name, array $arguments): Expectation
PHPDoc:
@param array<array-key, mixed> $arguments@return Expectation<T>@throws \BadMethodCallException@throws ExpectationFailed
ExpectationBuilder
Namespace: Greenlight\Expect
Selects an explicit call subject after Greenlight\expect() with no argument.
final readonly class ExpectationBuilder
calling()
Selects a call for later execution.
public function calling(callable $call): CallExpectation
PHPDoc:
@template T@param callable(): T $call@return CallExpectation<T>
ExpectationExtension
Namespace: Greenlight\Expect
Supplies extension matchers through Expectation::__call().
interface ExpectationExtension extends Plugin
matchers()
Maps each expectation-chain matcher name to its predicate.
Names must not match public native expectation methods. The comparison ignores letter case. Rename a matcher that has a conflicting name.
The predicate receives the subject and then the matcher arguments. Native parameter types declare the arguments. The predicate must return true for the expectation to hold. All other results fail it. Each matcher can use narrower parameters. Thus, one closure signature cannot describe all matchers.
public function matchers(): array;
PHPDoc:
@return array<non-empty-string, \Closure>
ExpectationFailed
Namespace: Greenlight\Expect
Identifies one or more failed expectations.
Contains structured FailureDetail values. The runner uses them to report
the expected value, actual value, and call site. It does not have to parse
the message.
final class ExpectationFailed extends \Exception
$details
public readonly array $details
fromDetail()
public static function fromDetail(FailureDetail $detail): self
fromDetails()
public static function fromDetails(array $details): self
PHPDoc:
@param non-empty-list<FailureDetail> $details
detail()
Returns the first failure. Use $details to read all failures, including
multiple unmet mock expectations.
public function detail(): FailureDetail
Fail
Namespace: Greenlight\Expect
final class Fail
because()
public static function because(string $reason): never
PHPDoc:
@throws ExpectationFailed
PendingConsistently
Namespace: Greenlight\Expect
Collects poll options until for() sets the duration.
Use Expect::calling(...)->returnValue()->consistently() to create this object.
final class PendingConsistently
PHPDoc:
@template T
not()
public function not(): self
PHPDoc:
@return self<T>
because()
public function because(string $reason): self
PHPDoc:
@param non-empty-string $reason@return self<T>@throws ExpectationFailed
pollEvery()
public function pollEvery(float $seconds): self
PHPDoc:
@return self<T>@throws \InvalidArgumentException if the interval is not finite or is less than 0.001 seconds
for()
public function for(float $seconds): ConsistentlyExpectation
PHPDoc:
@throws ExpectationFailed@return ConsistentlyExpectation<T>@throws \InvalidArgumentException if the duration is not finite or is not positive
PendingConsistentlyCall
Namespace: Greenlight\Expect
Collects time controls for a call expectation.
final readonly class PendingConsistentlyCall
PHPDoc:
@template T
not()
public function not(): self
PHPDoc:
@return self<T>
because()
public function because(string $reason): self
PHPDoc:
@param non-empty-string $reason@return self<T>@throws ExpectationFailed
pollEvery()
public function pollEvery(float $seconds): self
PHPDoc:
@return self<T>
for()
public function for(float $seconds): TemporalCallExpectation
PHPDoc:
@return TemporalCallExpectation<T>@throws ExpectationFailed
PendingEventually
Namespace: Greenlight\Expect
Collects poll options until within() sets the deadline.
Use Expect::calling(...)->returnValue()->eventually() to create this object.
final class PendingEventually
PHPDoc:
@template T
not()
public function not(): self
PHPDoc:
@return self<T>
because()
public function because(string $reason): self
PHPDoc:
@param non-empty-string $reason@return self<T>@throws ExpectationFailed
pollEvery()
public function pollEvery(float $seconds): self
PHPDoc:
@return self<T>@throws \InvalidArgumentException if the interval is not finite or is less than 0.001 seconds
retryOnException()
public function retryOnException(string ...$types): self
PHPDoc:
@param class-string<\Exception> ...$types@return self<T>@throws \InvalidArgumentException if a type does not extend Exception
within()
public function within(float $seconds): EventuallyExpectation
PHPDoc:
@throws ExpectationFailed@return EventuallyExpectation<T>@throws \InvalidArgumentException if the duration is not finite or is not positive
PendingEventuallyCall
Namespace: Greenlight\Expect
Collects time controls for a call expectation.
final readonly class PendingEventuallyCall
PHPDoc:
@template T
not()
public function not(): self
PHPDoc:
@return self<T>
because()
public function because(string $reason): self
PHPDoc:
@param non-empty-string $reason@return self<T>@throws ExpectationFailed
pollEvery()
public function pollEvery(float $seconds): self
PHPDoc:
@return self<T>
within()
public function within(float $seconds): TemporalCallExpectation
PHPDoc:
@return TemporalCallExpectation<T>@throws ExpectationFailed
ReturnValueExpectation
Namespace: Greenlight\Expect
Checks a call’s return value immediately or over time. A temporal matcher invokes the original call once per observation.
final class ReturnValueExpectation extends Expectation
PHPDoc:
@template T@extends Expectation<T>
toBe()
Passes when the subject and expected value are identical (===).
public function toBe(mixed $expected): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toEqual()
Passes when the subject and expected value satisfy the rules for deep
equality in the Expectation class description.
public function toEqual(mixed $expected): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed@throws \InvalidArgumentException when the comparison follows a cyclic array
toEqualCanonicalizing()
Uses the toEqual() rules but ignores list-element order. Canonicalization
recurses through array values. It does not inspect object properties.
Thus, lists in object properties keep their order. Associative arrays
keep their keys.
public function toEqualCanonicalizing(mixed $expected): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed@throws \InvalidArgumentException when an array selected for comparison or ordering contains a cycle
toBeOneOf()
Passes when the subject is identical (===) to one of the options.
public function toBeOneOf(mixed ...$options): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeIn()
Passes when the haystack contains the subject by identity (===). This
matcher is the reverse of toContain(). The check consumes a Traversable
haystack.
public function toBeIn(iterable $haystack): Expectation
PHPDoc:
@param iterable<mixed> $haystack@return Expectation<T>@throws ExpectationFailed
toBeInstanceOf()
public function toBeInstanceOf(string $class): Expectation
PHPDoc:
@param class-string $class@return Expectation<T>@throws ExpectationFailed
toBeTrue()
public function toBeTrue(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeFalse()
public function toBeFalse(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeNull()
public function toBeNull(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeArray()
public function toBeArray(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeString()
public function toBeString(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeInt()
public function toBeInt(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeFloat()
public function toBeFloat(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeBool()
public function toBeBool(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeCallable()
public function toBeCallable(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeIterable()
public function toBeIterable(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toContain()
For a string subject, checks for a string needle. For an iterable subject, checks for the value by identity (===). Iteration stops at the first match or the end of the subject.
public function toContain(mixed $needle): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toHaveCount()
Accepts an array, Countable, or Traversable subject. Uses count()
for arrays and Countable objects. Otherwise, consumes the iterator.
public function toHaveCount(int $count): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeEmpty()
Passes when the subject is an empty string or contains no elements.
Accepts a string, array, Countable, or Traversable subject. Uses
count() for arrays and Countable objects. For other Traversable
objects, consumes the iterator.
public function toBeEmpty(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toHaveLength()
For a valid UTF-8 string, measures the number of code points. For other
strings, measures the number of bytes. Array and Countable subjects use
count().
public function toHaveLength(int $length): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toHaveKey()
The subject must be an array or an ArrayAccess implementation. The
matcher uses array_key_exists() for arrays and offsetExists() for
ArrayAccess.
public function toHaveKey(int|string $key): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toContainSubset()
Each subset key must exist in the subject with an equal value. Equality
uses the toEqual() rules. A nested array is also a subset. The
related nested subject array can contain extra keys. The failure
identifies the first different key by its dot-separated path.
public function toContainSubset(array $subset): Expectation
PHPDoc:
@param array<array-key, mixed> $subset@return Expectation<T>@throws ExpectationFailed
toBeGreaterThan()
public function toBeGreaterThan(int|float $bound): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeGreaterThanOrEqual()
public function toBeGreaterThanOrEqual(int|float $bound): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeLessThan()
public function toBeLessThan(int|float $bound): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeLessThanOrEqual()
public function toBeLessThanOrEqual(int|float $bound): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeWithin()
Passes when the absolute difference between the numeric subject and
$of is not more than $delta. Use a finite tolerance of zero or more.
public function toBeWithin(float $delta, float $of): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toMatch()
public function toMatch(string $pattern): Expectation
PHPDoc:
@return Expectation<T>@throws \InvalidArgumentException when the pattern is not a valid regular expression@throws ExpectationFailed
toStartWith()
public function toStartWith(string $prefix): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toEndWith()
public function toEndWith(string $suffix): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeJson()
The subject must be a string. The matcher passes when the string contains valid JSON.
public function toBeJson(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toMatchJson()
The subject must be a string that contains valid JSON. The matcher decodes the subject and expected JSON. It then applies deep equality to the results. Object-key order has no effect. Invalid subject JSON causes an expectation failure. Invalid expected JSON causes a usage error.
public function toMatchJson(string $expected): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
not()
public function not(): static
because()
Sets a reason for all subsequent matchers in the chain.
Another because() call replaces the reason.
public function because(string $reason): static
PHPDoc:
@param non-empty-string $reason@throws ExpectationFailed
__call()
public function __call(string $name, array $arguments): Expectation
PHPDoc:
@param array<array-key, mixed> $arguments@return Expectation<T>@throws \BadMethodCallException@throws ExpectationFailed
eventually()
public function eventually(): PendingEventually
PHPDoc:
@return PendingEventually<T>@throws ExpectationFailed
consistently()
public function consistently(): PendingConsistently
PHPDoc:
@return PendingConsistently<T>@throws ExpectationFailed
TemporalCallExpectation
Namespace: Greenlight\Expect
Checks a call outcome on each poll and retains the last matched outcome.
final readonly class TemporalCallExpectation
PHPDoc:
@template T
not()
public function not(): self
PHPDoc:
@return self<T>
because()
public function because(string $reason): self
PHPDoc:
@param non-empty-string $reason@return self<T>@throws ExpectationFailed
toReturn()
public function toReturn(mixed $expected): CallExpectation
PHPDoc:
@return CallExpectation<T>@throws ExpectationFailed
toThrow()
public function toThrow(
string|\Closure|\Throwable $throwable = \Throwable::class,
?string $matching = null,
?string $message = null,
): CallExpectation
PHPDoc:
@template TThrowable of \Throwable@param class-string<TThrowable>|TThrowable|\Closure(TThrowable): void $throwable@return CallExpectation<T>@throws ExpectationFailed
TemporalExpectation
Namespace: Greenlight\Expect
Contains the matcher dispatch and probe operations for eventual and consistent expectations.
abstract class TemporalExpectation
PHPDoc:
@template T
toBe()
Passes when the subject and expected value are identical (===).
public function toBe(mixed $expected): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toEqual()
Passes when the subject and expected value satisfy the rules for deep
equality in the Expectation class description.
public function toEqual(mixed $expected): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed@throws \InvalidArgumentException when the comparison follows a cyclic array
toEqualCanonicalizing()
Uses the toEqual() rules but ignores list-element order. Canonicalization
recurses through array values. It does not inspect object properties.
Thus, lists in object properties keep their order. Associative arrays
keep their keys.
public function toEqualCanonicalizing(mixed $expected): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed@throws \InvalidArgumentException when an array selected for comparison or ordering contains a cycle
toBeOneOf()
Passes when the subject is identical (===) to one of the options.
public function toBeOneOf(mixed ...$options): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeIn()
Passes when the haystack contains the subject by identity (===). This
matcher is the reverse of toContain(). The check consumes a Traversable
haystack.
public function toBeIn(iterable $haystack): Expectation
PHPDoc:
@param iterable<mixed> $haystack@return Expectation<T>@throws ExpectationFailed
toBeInstanceOf()
public function toBeInstanceOf(string $class): Expectation
PHPDoc:
@param class-string $class@return Expectation<T>@throws ExpectationFailed
toBeTrue()
public function toBeTrue(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeFalse()
public function toBeFalse(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeNull()
public function toBeNull(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeArray()
public function toBeArray(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeString()
public function toBeString(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeInt()
public function toBeInt(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeFloat()
public function toBeFloat(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeBool()
public function toBeBool(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeCallable()
public function toBeCallable(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeIterable()
public function toBeIterable(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toContain()
For a string subject, checks for a string needle. For an iterable subject, checks for the value by identity (===). Iteration stops at the first match or the end of the subject.
public function toContain(mixed $needle): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toHaveCount()
Accepts an array, Countable, or Traversable subject. Uses count()
for arrays and Countable objects. Otherwise, consumes the iterator.
public function toHaveCount(int $count): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeEmpty()
Passes when the subject is an empty string or contains no elements.
Accepts a string, array, Countable, or Traversable subject. Uses
count() for arrays and Countable objects. For other Traversable
objects, consumes the iterator.
public function toBeEmpty(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toHaveLength()
For a valid UTF-8 string, measures the number of code points. For other
strings, measures the number of bytes. Array and Countable subjects use
count().
public function toHaveLength(int $length): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toHaveKey()
The subject must be an array or an ArrayAccess implementation. The
matcher uses array_key_exists() for arrays and offsetExists() for
ArrayAccess.
public function toHaveKey(int|string $key): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toContainSubset()
Each subset key must exist in the subject with an equal value. Equality
uses the toEqual() rules. A nested array is also a subset. The
related nested subject array can contain extra keys. The failure
identifies the first different key by its dot-separated path.
public function toContainSubset(array $subset): Expectation
PHPDoc:
@param array<array-key, mixed> $subset@return Expectation<T>@throws ExpectationFailed
toBeGreaterThan()
public function toBeGreaterThan(int|float $bound): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeGreaterThanOrEqual()
public function toBeGreaterThanOrEqual(int|float $bound): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeLessThan()
public function toBeLessThan(int|float $bound): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeLessThanOrEqual()
public function toBeLessThanOrEqual(int|float $bound): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeWithin()
Passes when the absolute difference between the numeric subject and
$of is not more than $delta. Use a finite tolerance of zero or more.
public function toBeWithin(float $delta, float $of): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toMatch()
public function toMatch(string $pattern): Expectation
PHPDoc:
@return Expectation<T>@throws \InvalidArgumentException when the pattern is not a valid regular expression@throws ExpectationFailed
toStartWith()
public function toStartWith(string $prefix): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toEndWith()
public function toEndWith(string $suffix): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toBeJson()
The subject must be a string. The matcher passes when the string contains valid JSON.
public function toBeJson(): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
toMatchJson()
The subject must be a string that contains valid JSON. The matcher decodes the subject and expected JSON. It then applies deep equality to the results. Object-key order has no effect. Invalid subject JSON causes an expectation failure. Invalid expected JSON causes a usage error.
public function toMatchJson(string $expected): Expectation
PHPDoc:
@return Expectation<T>@throws ExpectationFailed
not()
Negates the next matcher for every value returned by the probe.
final public function not(): static
because()
Sets a reason for all subsequent matchers in the chain.
Another because() call replaces the reason.
If the matcher fails, the failure message ends with “because” and the reason. An empty reason causes a usage failure.
final public function because(string $reason): static
PHPDoc:
@param non-empty-string $reason@throws ExpectationFailed
__call()
Runs a native or configured extension matcher against each probe value.
final public function __call(string $name, array $arguments): Expectation
PHPDoc:
@param array<array-key, mixed> $arguments@return Expectation<T>@throws \BadMethodCallException if no native or registered extension matcher has the requested name@throws ExpectationFailed