Skip to content

WhenBuilder

Rasuvaeff\Understudy\WhenBuilder

ClassPackage: rasuvaeff/understudySourceVersion: v0.4.1

Type parameters:

  • TReturn of mixed = mixed

Configures what a stubbed call does.

TReturn is the return type of the specified method. Plain Psalm and PHPStan cannot infer it from the closure on their own — understudy-psalm fills it in, and until then the parameter stays mixed, which is why the template is declared here rather than added later: a published signature cannot grow one without changing its contract.

Constructor

php
__construct(
    Expectation\Expectation $expectation,
)
ParameterTypeDefaultDescription
$expectationExpectation\Expectationrequired

Methods

returns()

php
returns(\TReturn $values): static

Returns each value in turn across successive calls, then keeps returning the last one.

throws()

php
throws(Throwable $error): static

answers()

php
answers(callable $answer): static

then()

php
then(): static

Opens the next link of the chain, so the verb that follows describes what the next call does:

php
when(fn () => $breaker->call($operation))
    ->returns('ok')
    ->then()->throws(new ConnectionLost());

Once the chain runs out, its last link keeps answering.

times()

php
times(int<0, max> $minimum, int<0, max>|null $maximum = NULL): static

Makes the call count part of what verifyAll() checks. Without it a stub is permission, not a claim.

With one argument the count is exact; with two it is a range, and a null maximum means no upper bound.