Core\Test::assertNeverCalled
Core\Test::assertNeverCalled() checks that your code did not call a method on a test double. It is the opposite of Core\Test::assertCalled, and it reads the same list of calls.
You write the method as Mailer::send, and the compiler checks the name. One call to the method is enough to fail. The check then throws a Core\Test\Failure. The message gives the number of calls and the arguments of the first one. An object that is not a double throws a LogicError.
Use it for code that must not do something: a dry run that must not delete files, or a failed payment that must not send a confirmation.
The examples below show a dry run, the message of a failed check, and an order that must not send an email.
Parameters
$doubleobjectThe double or partial double to check.$methodmethodThe method, written asMailer::purge. It is checked the same way as inassertCalled.
Options
messagestringoptionalYour own text, added at the start of the failure message, as on every other check.
Return value
Returns void. Nothing. One recorded call is enough to throw an error.
Errors
Core\Test\FailureThe method was called. The message shows the first recorded call and the total number of calls.LogicError$doubleis not a double. This is the same error as inassertCalled.
Examples
A dry run deletes nothing
<?nvs// Shows how to check that your code did not call a method.
interface Files{ public function list(): array<string>; public function delete(string $path): void;}
class Cleanup{ // With `$dryRun` set, this only prints what it would delete. public static function run(Files $files, bool $dryRun): void { foreach ($files->list() as string $path) { if ($dryRun) { echo 'would delete ', $path, "\n"; } else { $files->delete($path); } } }}
Files $files = Core\Test::double<Files>({ list: fn(): array<string> => ['cache/a.tmp', 'cache/b.tmp'], delete: fn(string $path): void => {},});
Cleanup::run($files, true);
// The check passes, because `delete` was not called.Core\Test::assertNeverCalled($files, Files::delete);echo "nothing was deleted\n";would delete cache/a.tmpwould delete cache/b.tmpnothing was deletedWhat a failure says
<?nvs// Shows the error that `Core\Test::assertNeverCalled` throws when the method was called.
interface Cache{ public function clear(string $key): void;}
Cache $cache = Core\Test::double<Cache>({ clear: fn(string $key): void => {},});
$cache->clear('users');$cache->clear('orders');
// The check throws a `Core\Test\Failure`. The message gives the number of calls// and the arguments of the first call.try { Core\Test::assertNeverCalled($cache, Cache::clear);} catch (Core\Test\Failure $failure) { echo $failure->message, "\n";}
// `message` adds your own text at the start of the error message.try { Core\Test::assertNeverCalled($cache, Cache::clear, { message: 'a read must not clear the cache' });} catch (Core\Test\Failure $failure) { echo $failure->message, "\n";}Core\Test::assertNeverCalled failed: `clear` was called 2 time(s), the first as clear("users")a read must not clear the cache: Core\Test::assertNeverCalled failed: `clear` was called 2 time(s), the first as clear("users")No email for a declined order
<?nvs// Shows how to test that a declined payment sends no order confirmation.
interface Payments{ public function charge(int $cents): bool;}
interface Mailer{ public function confirm(string $orderId): void;}
class Orders{ public static function place(Payments $payments, Mailer $mailer, string $orderId, int $cents): string { if (!$payments->charge($cents)) { return 'declined'; } $mailer->confirm($orderId); return 'placed'; }}
// This payment double declines every charge.Payments $payments = Core\Test::double<Payments>({ charge: fn(int $cents): bool => false,});Mailer $mailer = Core\Test::double<Mailer>({ confirm: fn(string $orderId): void => {},});
// This prints `declined`.echo Orders::place($payments, $mailer, 'order-1042', 2500), "\n";
// The customer must not get a confirmation for an order that was not paid.Core\Test::assertCalled($payments, Payments::charge, { times: 1 });Core\Test::assertNeverCalled($mailer, Mailer::confirm);echo "no confirmation was sent\n";declinedno confirmation was sent