Skip to content

Core\Test::assertSame

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

Core\Test::assertSame() checks that a result is exactly the value you expect. You pass the result first and the expected value second. Both must have the same type.

Two numbers, strings or arrays are the same when their values are the same. Two objects are the same only when they are one object. Two objects with equal properties are not the same. Use Core\Test::assertEquals to compare them by their properties.

When the values are not the same, 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 two checks that pass, the message of a failed check, and a test that a cache returns one object every time.

Parameters

  • $actualTThe value under test.
  • $expectedTThe value $actual must be identical to. 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 identical. The failure is recorded before it is thrown, so a catch does not remove it from the test's result.

Examples

Compare simple values

<?nvs
// Shows how to check that a result is exactly the value you expect.
// The first value is the result, the second is the value you expect.
// Both are 15, so the check passes and nothing is thrown.
int $total = 4 + 5 + 6;
Core\Test::assertSame($total, 15);
// Strings are the same when they have the same characters.
string $name = Core\Str::upper('ada');
Core\Test::assertSame($name, 'ADA');
echo "both checks passed\n";
Output
both checks passed

Read a failure

<?nvs
// Shows what happens when the values are not the same, and how to add your own message.
final class Point
{
public function constructor(public int $x, public int $y)
{}
}
// "10" and "010" are different strings, so the check throws a `Core\Test\Failure`.
// The message shows both values.
try {
Core\Test::assertSame('010', '10');
} catch (Core\Test\Failure $failure) {
echo $failure->message, "\n";
}
// Two objects are the same only when they are one object.
// These two have equal properties, but they are two objects, so the check fails.
try {
Core\Test::assertSame(new Point(1, 2), new Point(1, 2));
} 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::assertSame(3, 4, { message: 'the cart has three items' });
} catch (Core\Test\Failure $failure) {
echo $failure->message, "\n";
}
Output
Core\Test::assertSame failed: `$actual` is "010", `$expected` is "10"
Core\Test::assertSame failed: `$actual` is a `Point`, `$expected` is a `Point`
the cart has three items: Core\Test::assertSame failed: `$actual` is 3, `$expected` is 4

Test that a cache reuses an object

<?nvs
// Shows a test that a cache returns the same object every time.
final class Settings
{
public function constructor(public string $language)
{}
}
final class Cache
{
private static ?Settings $settings = null;
public static int $loads = 0;
// Loads the settings once. Every later call returns the same object.
public static function settings(): Settings
{
?Settings $cached = self::$settings;
if ($cached != null) {
return $cached;
}
self::$loads = self::$loads + 1;
Settings $loaded = new Settings('en');
self::$settings = $loaded;
return $loaded;
}
}
// Both calls return one object, so the check passes.
Core\Test::assertSame(Cache::settings(), Cache::settings());
// The settings were loaded only once.
Core\Test::assertSame(Cache::$loads, 1);
echo 'loads: ', Cache::$loads, "\n";
Output
loads: 1