Skip to content

Core\Test::assertEquals

Core\Test::assertEquals(
    T $actual,
    T $expected,
    {message?: string},
): void

Core\Test::assertEquals() checks that a value is equal to the value you expect. The first argument is the result of your code, and the second is the expected value. Both must have the same type.

Numbers, strings, null and arrays are equal when they are the same value. Two objects are equal when compareTo returns 0, so the class must implement Comparable. An object without compareTo throws an error. Use Core\Test::assertEqualsDeep to compare its properties instead.

When the values are different, the check throws a Core\Test\Failure. The message shows both values. The option message adds your own text in front of it.

The examples below show a check of a function's result, the message of a failed check, and a test of a money object.

Parameters

  • $actualTThe value under test.
  • $expectedTThe value $actual must equal. T is the type of $actual, so $expected must have the same type. Otherwise the call does not compile.

Options

  • messagestringoptionalYour own text, added at the start of the failure message. The default is no text.

Return value

Returns void. Nothing. The check is recorded as passed.

Errors

  • Core\Test\FailureThe two values are not equal. The failure is recorded before it is thrown, so a catch does not remove it from the test's result.
  • RuntimeErrorBoth are objects and $actual's class declares no compareTo.

Examples

Check a result

<?nvs
// Shows how to check that a function returns the value you expect.
class Prices
{
public static function withShipping(int $cents): int
{
if ($cents >= 5000) {
return $cents;
}
return $cents + 495;
}
}
// The first value is the result of your code. The second value is what you expect.
// The check passes, so nothing is thrown.
Core\Test::assertEquals(Prices::withShipping(1000), 1495);
echo "1000 cents with shipping is 1495 cents\n";
// Arrays are equal when they have the same keys and values in the same order.
Core\Test::assertEquals([Prices::withShipping(500), Prices::withShipping(6000)], [995, 6000]);
echo "shipping is free from 5000 cents\n";
Output
1000 cents with shipping is 1495 cents
shipping is free from 5000 cents

Read a failure

<?nvs
// Shows what happens when a check fails, and how to add your own message.
int $itemsInCart = 2;
// The values are different, so the check throws a `Core\Test\Failure`.
// The message shows both values.
try {
Core\Test::assertEquals($itemsInCart, 3);
} catch (Core\Test\Failure $failure) {
echo $failure->message, "\n";
}
// The option `message` adds your own text at the start of the message.
try {
Core\Test::assertEquals($itemsInCart, 3, { message: 'the cart has three items after checkout' });
} catch (Core\Test\Failure $failure) {
echo $failure->message, "\n";
}
Output
Core\Test::assertEquals failed: `$actual` is 2, `$expected` is 3
the cart has three items after checkout: Core\Test::assertEquals failed: `$actual` is 2, `$expected` is 3

Compare two money objects

<?nvs
// Shows how to test a value object. Two objects are equal when their `compareTo` method returns 0.
class Money implements Comparable
{
public function constructor(public int $cents, public string $currency)
{}
// Money in different currencies is never equal, so this returns 1 for it.
public function compareTo(self $other): int
{
if ($this->currency != $other->currency) {
return 1;
}
return $this->cents <=> $other->cents;
}
}
class Invoice
{
public static function total(array<Money> $lines): Money
{
int $cents = 0;
foreach ($lines as Money $line) {
$cents = $cents + $line->cents;
}
return new Money($cents, 'EUR');
}
}
Money $total = Invoice::total([new Money(1999, 'EUR'), new Money(501, 'EUR')]);
// These are two different objects. `compareTo` returns 0, so the check passes.
Core\Test::assertEquals($total, new Money(2500, 'EUR'));
echo "the invoice total is 25.00 EUR\n";
// The amount is the same, but the currency is not. The check fails.
try {
Core\Test::assertEquals($total, new Money(2500, 'USD'));
} catch (Core\Test\Failure $failure) {
echo "not equal to 25.00 USD\n";
}
Output
the invoice total is 25.00 EUR
not equal to 25.00 USD