Skip to content

Functions

The free functions understudy declares in src/functions.php, autoloaded through composer's files entry. They are the primary surface: every example in this guide opens with one. Each has a collision-free static twin on Understudy — see Using Pest for when that matters.

FunctionReturnsSummary
expect()ExpectBuilderDeclares a call the code under test is expected to make: exactly once unless times() says…
expectSequence()voidArms a protocol before the code under test runs: a call that breaks the order fails at that call,…
verify()voidAsserts, after the fact, how many times a call was made.
when()WhenBuilderStubs a call: allowed any number of times, including none.

expect()

Rasuvaeff\Understudy\expect() · Source

php
expect(callable $call): ExpectBuilder

Declares a call the code under test is expected to make: exactly once unless times() says otherwise, checked by Understudy::verifyAll().

php
expect(fn () => $repository->save($book));

Pest declares a global expect() of its own. In a Pest suite either import this one under another name — use function Rasuvaeff\Understudy\expect as expectCall; — or call Understudy::expect(), which cannot collide.

ParameterTypeDescription
$callcallable

expectSequence()

Rasuvaeff\Understudy\expectSequence() · Source

php
expectSequence(callable ...$calls): void

Arms a protocol before the code under test runs: a call that breaks the order fails at that call, not in teardown.

php
expectSequence(
    fn () => $repo->begin(),
    fn () => $repo->save($book),
    fn () => $repo->commit(),
);
ParameterTypeDescription
...$callscallable

verify()

Rasuvaeff\Understudy\verify() · Source

php
verify(callable $call, int<0, max>|null $times = NULL, int<0, max>|null $minimum = NULL, int<0, max>|null $maximum = NULL, bool $never = false): void

Asserts, after the fact, how many times a call was made.

php
verify(fn () => $repository->recordView($book), times: 2);
ParameterTypeDescription
$callcallable
$times = NULLint<0, max>|null
$minimum = NULLint<0, max>|null
$maximum = NULLint<0, max>|null
$never = falsebool

when()

Rasuvaeff\Understudy\when() · Source

php
when(callable $call): WhenBuilder

Stubs a call: allowed any number of times, including none.

php
when(fn () => $repository->find(123))->returns($book);
ParameterTypeDescription
$callcallable