Skip to content

Core\Test::assertMatchesInline

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

Core\Test::assertMatchesInline() checks a value against a text you write in the test. This text is called a snapshot. The check turns the value into text with Core\Debug::render, and then compares the two texts. They must be equal character by character.

This is useful for a value with many parts, such as an array or an object. You write the whole expected result once, and you see it in the test.

When the texts are different, the check throws a Core\Test\Failure. The message shows the text of the value and the snapshot. An empty snapshot "" is also a failure, so you can copy the text from the message. A secret property is shown as [redacted], so its value is never in a snapshot.

The examples below show a check of simple values, the message of a failed check, and a test of an object with a secret property.

Parameters

  • $actualmixedThe value to render. A secret property inside it is shown redacted, so a snapshot never puts a secret into your source code.
  • $expectedstringThe text you expect the rendering to be, written in the test. nvs test --update writes it here for you. It changes only this string in the file.

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 rendering is different from $expected. The failure message shows both. The failure is recorded before it is thrown, so a catch does not remove it from the test's result.

Examples

Check simple values

<?nvs
// Shows how to check simple values against the text `Core\Debug::render` prints for them.
// The text of a number has its type and its value.
Core\Test::assertMatchesInline(42, 'int(42)');
// The text of a string has its length and the string in quotes.
// The quotes inside the snapshot are written as `\"`.
Core\Test::assertMatchesInline('ready', "string(5) \"ready\"");
// The text of an array has one line for each entry. `\n` is a line break.
Core\Test::assertMatchesInline([10, 20], "array(2) [\n 0 => int(10)\n 1 => int(20)\n]");
// Every check passed, so this prints the line below.
echo "all three snapshots match\n";
Output
all three snapshots match

Read a failure

<?nvs
// Shows what happens when the snapshot is wrong, and how to start from an empty one.
// The value is 7, but the snapshot says 8. The check throws a `Core\Test\Failure`.
// The message shows the text of the value and the snapshot.
try {
Core\Test::assertMatchesInline(7, 'int(8)');
} catch (Core\Test\Failure $failure) {
echo $failure->message, "\n";
}
// You can start with an empty snapshot. The check fails, and the message shows
// the text to copy into the test.
try {
Core\Test::assertMatchesInline(['red', 'blue'], '');
} 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::assertMatchesInline(true, 'bool(false)', { message: 'the feature flag is off' });
} catch (Core\Test\Failure $failure) {
echo $failure->message, "\n";
}
Output
Core\Test::assertMatchesInline failed: the rendering is "int(7)", and the snapshot holds "int(8)"
Core\Test::assertMatchesInline failed: the rendering is "array(2) [\n 0 => string(3) \"red\"\n 1 => string(4) \"blue\"\n]", and the snapshot holds ""
the feature flag is off: Core\Test::assertMatchesInline failed: the rendering is "bool(true)", and the snapshot holds "bool(false)"

Test a user record

<?nvs
// Shows a test of an object that a function builds. The password is a `secret` property.
final class User
{
public function constructor(
public string $email,
public secret string $password,
public bool $active,
)
{}
}
class Signup
{
// Builds a new user. The email is stored in lower case.
public static function register(string $email, string $password): User
{
return new User(Core\Str::lower($email), $password, false);
}
}
User $user = Signup::register('Ana@Example.com', 'correct horse');
// A `secret` property is shown as `[redacted]`. The password is not in the snapshot.
// A `$` inside a double-quoted string is written as `\$`.
Core\Test::assertMatchesInline(
$user,
"User#1 (3) {\n \$email => string(15) \"ana@example.com\"\n \$password => [redacted]\n \$active => bool(false)\n}",
);
echo "the new user matches the snapshot\n";
Output
the new user matches the snapshot