Browse documentation

Expectations API

This reference lists immediate and temporal expectation types.

These signatures are the public API.

ConsistentlyExpectation

Namespace: Greenlight\Expect

Checks each probe value for a fixed period and fails on the first mismatch.

final class ConsistentlyExpectation extends TemporalExpectation

View source

PHPDoc:

This type does not declare public members.

EventuallyExpectation

Namespace: Greenlight\Expect

Polls the probe until its matcher passes or the deadline expires.

final class EventuallyExpectation extends TemporalExpectation

View source

PHPDoc:

This type does not declare public members.

Expect

Namespace: Greenlight\Expect

Extension matchers are worker-local state. install() stores the configured ExpectationExtension list when the worker starts. Each chain from that() uses a snapshot of this list. The runner changes the list before test execution starts. Before install() runs, that() uses no extensions.

final class Expect

View source

that()

public static function that(mixed $value): Expectation

PHPDoc:

View source

eventually()

Polls the probe until its matcher passes or the deadline expires.

public static function eventually(callable $probe): PendingEventually

PHPDoc:

View source

consistently()

Polls the probe for a fixed period and fails on the first mismatch.

public static function consistently(callable $probe): PendingConsistently

PHPDoc:

View source

Expectation

Namespace: Greenlight\Expect

A fluent matcher chain for one subject value.

Use Expect::that() to create an instance.

A failed matcher throws ExpectationFailed immediately.

toEqual() uses these rules for deep equality:

final class Expectation

View source

PHPDoc:

__call()

Dispatches extension matchers. If an ExpectationExtension provides the requested matcher, this method gives it the subject and arguments.

An extension cannot replace a native matcher. PHP calls the native method directly.

public function __call(string $name, array $arguments): self

PHPDoc:

View source

not()

Inverts the next matcher in the chain. That matcher consumes the inversion. Negation does not apply to subject type checks. A matcher fails if it cannot process the subject type.

public function not(): self

PHPDoc:

View source

because()

Sets a reason for the next matcher in the chain. The next matcher consumes the reason.

If the matcher fails, the failure message ends with “because” and the reason. An empty reason causes a usage failure.

public function because(string $reason): self

PHPDoc:

View source

and()

Sets a new subject for the chain. The chain does not apply not() and because() modifiers to the new subject.

public function and(mixed $value): self

PHPDoc:

View source

toBe()

Passes when the subject and expected value are identical (===).

public function toBe(mixed $expected): self

PHPDoc:

View source

toEqual()

Passes when the subject and expected value satisfy the rules for deep equality on this class.

public function toEqual(mixed $expected): self

PHPDoc:

View source

toEqualCanonicalizing()

Uses the toEqual() rules but ignores list-element order at all levels. Associative arrays keep their keys.

public function toEqualCanonicalizing(mixed $expected): self

PHPDoc:

View source

toBeOneOf()

Passes when the subject is identical (===) to one of the options.

public function toBeOneOf(mixed ...$options): self

PHPDoc:

View source

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): self

PHPDoc:

View source

toBeInstanceOf()

public function toBeInstanceOf(string $class): self

PHPDoc:

View source

toBeTrue()

public function toBeTrue(): self

PHPDoc:

View source

toBeFalse()

public function toBeFalse(): self

PHPDoc:

View source

toBeNull()

public function toBeNull(): self

PHPDoc:

View source

toBeArray()

public function toBeArray(): self

PHPDoc:

View source

toBeString()

public function toBeString(): self

PHPDoc:

View source

toBeInt()

public function toBeInt(): self

PHPDoc:

View source

toBeFloat()

public function toBeFloat(): self

PHPDoc:

View source

toBeBool()

public function toBeBool(): self

PHPDoc:

View source

toBeCallable()

public function toBeCallable(): self

PHPDoc:

View source

toBeIterable()

public function toBeIterable(): self

PHPDoc:

View source

toContain()

For a string subject, checks for a string needle. For an iterable subject, checks for the value by identity (===). The check consumes a Traversable subject.

public function toContain(mixed $needle): self

PHPDoc:

View source

toHaveCount()

The subject must be Countable or Traversable. The count consumes a Traversable subject.

public function toHaveCount(int $count): self

PHPDoc:

View source

toBeEmpty()

Passes when the subject is an empty string or contains no elements. The subject must be a string, array, Countable, or iterable. The check consumes a Traversable subject.

public function toBeEmpty(): self

PHPDoc:

View source

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): self

PHPDoc:

View source

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): self

PHPDoc:

View source

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): self

PHPDoc:

View source

toBeGreaterThan()

public function toBeGreaterThan(int|float $bound): self

PHPDoc:

View source

toBeGreaterThanOrEqual()

public function toBeGreaterThanOrEqual(int|float $bound): self

PHPDoc:

View source

toBeLessThan()

public function toBeLessThan(int|float $bound): self

PHPDoc:

View source

toBeLessThanOrEqual()

public function toBeLessThanOrEqual(int|float $bound): self

PHPDoc:

View source

toBeWithin()

Passes when the absolute difference between the numeric subject and $of is not more than $delta.

public function toBeWithin(float $delta, float $of): self

PHPDoc:

View source

toMatch()

public function toMatch(string $pattern): self

PHPDoc:

View source

toStartWith()

public function toStartWith(string $prefix): self

PHPDoc:

View source

toEndWith()

public function toEndWith(string $suffix): self

PHPDoc:

View source

toBeJson()

The subject must be a string. The matcher passes when the string contains valid JSON.

public function toBeJson(): self

PHPDoc:

View source

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): self

PHPDoc:

View source

toThrow()

The subject must be callable. The matcher calls it with no arguments. It passes when the subject throws an instance of the specified class. The message must satisfy the optional regular expression or exact-text constraint.

With not(), a throwable that does not satisfy both conditions makes the matcher pass.

public function toThrow(string $throwable, ?string $matching = null, ?string $message = null): self

PHPDoc:

View source

ExpectationExtension

Namespace: Greenlight\Expect

Supplies extension matchers through Expectation::__call().

interface ExpectationExtension extends Plugin

View source

matchers()

Maps each expectation-chain matcher name to its predicate.

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:

View source

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

View source

fromDetail()

public static function fromDetail(FailureDetail $detail): self

View source

fromDetails()

public static function fromDetails(array $details): self

PHPDoc:

View source

detail()

Returns the first failure. In the default mode, this is the only failure.

public function detail(): FailureDetail

View source

Fail

Namespace: Greenlight\Expect

final class Fail

View source

because()

public static function because(string $reason): never

PHPDoc:

View source

PendingConsistently

Namespace: Greenlight\Expect

Collects poll options until for() sets the duration.

final class PendingConsistently

View source

PHPDoc:

pollEvery()

public function pollEvery(float $seconds): self

PHPDoc:

View source

for()

public function for(float $seconds): ConsistentlyExpectation

PHPDoc:

View source

PendingEventually

Namespace: Greenlight\Expect

Collects poll options until within() sets the deadline.

final class PendingEventually

View source

PHPDoc:

pollEvery()

public function pollEvery(float $seconds): self

PHPDoc:

View source

retryOnException()

public function retryOnException(string ...$types): self

PHPDoc:

View source

within()

public function within(float $seconds): EventuallyExpectation

PHPDoc:

View source