Core\Test::assertCalled
Core\Test::assertCalled(
object $double,
method $method,
{times?: uint, with?: array<mixed>, message?: string},
): voidCore\Test::assertCalled() checks that your code called a method on a test double. You make the double with Core\Test::double or Core\Test::partial. The double keeps a list of every call it gets, and this check reads that list after your code has run.
You write the method as Mailer::send. The compiler checks this name, so a renamed method breaks the test at compile time. The option times gives the exact number of calls. Without it, one call or more passes. The option with gives the arguments that one of the calls must have.
When the check fails, it throws a Core\Test\Failure. The message lists every call the double got. An object that is not a double throws a LogicError.
The examples below show a check that an email was sent, a check of the number of calls, and a test that a checkout charges the customer once.
Parameters
$doubleobjectThe double or partial double to check.$methodmethodThe method, written asMailer::send. The compiler checks this reference, so renaming the method updates or breaks the test.
Options
timesuintoptionalThe exact number of calls you expect. If you leave it out, any number above zero passes.witharray<mixed>optionalThe arguments that one of the recorded calls must have. They are compared the wayCore\Test::assertEqualscompares. If you leave it out, the arguments are not checked.messagestringoptionalYour own text, added at the start of the failure message, as on every other check.
Return value
Returns void. Nothing. The call throws an error if the method was not called, or was called a different number of times or with different arguments.
Errors
Core\Test\FailureThe record does not contain the call that the options describe. The message names the method, what you expected, and every call in the record.LogicError$doubleis an ordinary object.Core\Test::doubleorCore\Test::partialdid not create it, so it has no record of calls.
Examples
Check that an email was sent
<?nvs// Shows how to check that your code called a method on a test double.
interface Mailer{ public function send(string $to): bool;}
class Signup{ public static function register(Mailer $mailer, string $email): void { $mailer->send($email); }}
// `Core\Test::double` builds a `Mailer` for the test. It does not send anything.Mailer $mailer = Core\Test::double<Mailer>({ send: fn(string $to): bool => true,});
Signup::register($mailer, 'ada@example.com');
// `Mailer::send` names the method. The check passes if it was called at least once.Core\Test::assertCalled($mailer, Mailer::send);
// `with` checks the arguments of the call as well.Core\Test::assertCalled($mailer, Mailer::send, { with: ['ada@example.com'] });echo "the welcome email was sent\n";the welcome email was sentCount the calls
<?nvs// Shows how to check how many times a method was called.
interface Logger{ public function write(string $line): void;}
Logger $logger = Core\Test::double<Logger>({ write: fn(string $line): void => {},});
array<string> $lines = ['start', 'working', 'done'];foreach ($lines as string $line) { $logger->write($line);}
// `times` is the exact number of calls. This check passes.Core\Test::assertCalled($logger, Logger::write, { times: 3 });echo "3 calls\n";
// The number is wrong here, so the check throws a `Core\Test\Failure`.// The message lists every call that was made.try { Core\Test::assertCalled($logger, Logger::write, { times: 2 });} catch (Core\Test\Failure $failure) { echo $failure->message, "\n";}3 callsCore\Test::assertCalled failed: expected 2 call(s) to `write`, found 3 — the record holds write("start"), write("working"), write("done")A checkout that charges once
<?nvs// Shows how to test that a checkout charges the customer exactly once, with the right amount.
interface Payments{ public function charge(string $customer, int $cents): bool;}
class Checkout{ public static function pay(Payments $payments, string $customer, array<int> $prices): bool { int $total = 0; foreach ($prices as int $price) { $total = $total + $price; } return $payments->charge($customer, $total); }}
// The test double accepts every charge. No real payment happens.Payments $payments = Core\Test::double<Payments>({ charge: fn(string $customer, int $cents): bool => true,});
echo Checkout::pay($payments, 'customer-7', [1200, 350, 450]) ? 'paid' : 'declined', "\n";
// One call, for the customer, with the sum of the three prices.Core\Test::assertCalled($payments, Payments::charge, { times: 1, with: ['customer-7', 2000],});echo "charged once, 2000 cents\n";paidcharged once, 2000 cents