Skip to content

Core\Test::double

Core\Test::double<T>(object $answers): TCore\Test::double<T>(
    object $answers,
): T

Core\Test::double() builds a test double: an object that you use in a test in place of a real one. You write the interface in angle brackets, Core\Test::double<Mailer>(...), and give one callable for each method of that interface. The result is a Mailer. You can pass it to any code that takes a Mailer.

When your code calls a method on the double, the callable for that method runs and its result is the return value. The compiler checks the callables. A missing method, an extra method or a callable with the wrong parameter types does not compile. You may leave out a method that has a body in the interface. Then that body runs. If you give a callable for it, your callable runs.

The double also keeps a list of its calls. Core\Test::assertCalled and Core\Test::assertNeverCalled check this list.

The examples below show a fixed clock, two doubles that test both results of a payment, and a report tested with a fake order store.

Parameters

  • $answersobjectOne field per method of T, with the same name as the method. Each field has the function that runs when that method is called. A field for a method that T does not declare does not compile. A method of T that has no field does not compile either. The exception is a method that has a body in the interface. Without a field, that body runs. With a field, the field's function runs.

Return value

Returns T. The double, typed as T. It records every call made to it, and Core\Test::assertCalled reads that record later. A backtrace through the double shows only classes that you wrote.

Errors

  • LogicErrorT declares more methods than one double supports. This limit is fixed, and it is far above the size of the interfaces a test usually replaces.

Changelog

VersionChange
0.0.6A double can replace an interface method that has a body with a function. The double then runs that function.

Examples

A fixed clock

<?nvs
// Shows how to replace a clock with a test double, so that the time is always the same.
interface Clock
{
public function hour(): int;
}
class Greeting
{
public static function forNow(Clock $clock): string
{
return $clock->hour() < 12 ? 'Good morning' : 'Good afternoon';
}
}
// This double is a `Clock`. Its `hour` method always returns 9.
Clock $morning = Core\Test::double<Clock>({
hour: fn(): int => 9,
});
// This prints `Good morning`.
echo Greeting::forNow($morning), "\n";
Output
Good morning

Test both results

<?nvs
// Shows how two doubles of one interface test both results of a payment.
interface Payments
{
public function charge(int $cents): bool;
}
class Checkout
{
public static function pay(Payments $payments, int $cents): string
{
return $payments->charge($cents) ? 'paid' : 'declined';
}
}
// One double accepts every charge, and the other declines every charge.
Payments $accepting = Core\Test::double<Payments>({
charge: fn(int $cents): bool => true,
});
Payments $declining = Core\Test::double<Payments>({
charge: fn(int $cents): bool => false,
});
// This prints `paid` and then `declined`.
echo Checkout::pay($accepting, 1500), "\n";
echo Checkout::pay($declining, 1500), "\n";
Output
paid
declined

A report with a fake order store

<?nvs
// Shows how to test a report without a database, with a double of the order store.
interface Orders
{
public function countFor(string $customer): int;
public function totalFor(string $customer): int;
}
class Report
{
public static function line(Orders $orders, string $customer): string
{
int $count = $orders->countFor($customer);
if ($count == 0) {
return $customer . ': no orders';
}
return $customer . ': ' . $count . ' orders, ' . $orders->totalFor($customer) . ' cents';
}
}
// The anonymous functions use their argument, so each customer gets different numbers.
Orders $orders = Core\Test::double<Orders>({
countFor: fn(string $customer): int => $customer == 'ada' ? 3 : 0,
totalFor: fn(string $customer): int => $customer == 'ada' ? 4200 : 0,
});
// This prints `ada: 3 orders, 4200 cents` and `bob: no orders`.
echo Report::line($orders, 'ada'), "\n";
echo Report::line($orders, 'bob'), "\n";
// The report asked for the total once, only for the customer with orders.
Core\Test::assertCalled($orders, Orders::totalFor, { times: 1, with: ['ada'] });
echo "the total was read once\n";
Output
ada: 3 orders, 4200 cents
bob: no orders
the total was read once