Core\Test::assertContains
Core\Test::assertContains() checks that an array contains a value. The first argument is the array, and the second is the value you look for. The value must have the type of the array's entries. The position of the entry does not matter.
The check uses the same comparison as Core\Arr::contains. Numbers, strings and null match when they are the same value. An object matches only when it is the same object. Another object with the same properties does not match.
When no entry matches, the check throws a Core\Test\Failure. The message shows the number of entries and the value you looked for. The option message adds your own text in front of it.
The examples below show a successful check, the message of a failed check, and a test of a list of error messages.
Parameters
$actualarray<T>The array under test.$expectedTThe entry the array must contain. It is compared by identity.int,uint,floatanddecimalvalues compare as numbers, so1finds1.0.
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\FailureNo entry of the array is identical to$expected. The failure is recorded before it is thrown, so acatchdoes not remove it from the test's result.
Examples
Check that a value is in a list
<?nvs// Shows how to check that an array contains a value.
class Roles{ public static function of(string $user): array<string> { if ($user == 'ana') { return ['reader', 'editor', 'admin']; } return ['reader']; }}
// The first value is the array. The second value is the entry you look for.// "editor" is in the array, so the check passes and nothing is thrown.Core\Test::assertContains(Roles::of('ana'), 'editor');echo "ana is an editor\n";
// The position of the entry does not matter.Core\Test::assertContains(Roles::of('li'), 'reader');echo "li is a reader\n";ana is an editorli is a readerRead a failure
<?nvs// Shows what happens when the value is not in the array.
array<string> $sizes = ['S', 'M', 'L'];
// "XL" is not in the array, so the check throws a `Core\Test\Failure`.// The message shows the number of entries and the value you looked for.try { Core\Test::assertContains($sizes, 'XL');} catch (Core\Test\Failure $failure) { echo $failure->message, "\n";}
// The option `message` adds your own text at the start of the message.array<int> $ids = [1, 2, 3];try { Core\Test::assertContains($ids, 4, { message: 'order 4 is in the export' });} catch (Core\Test\Failure $failure) { echo $failure->message, "\n";}Core\Test::assertContains failed: `$actual` holds 3 entries and none is `$expected`, which is "XL"order 4 is in the export: Core\Test::assertContains failed: `$actual` holds 3 entries and none is `$expected`, which is 4Test a list of error messages
<?nvs// Shows a test of a form check that returns a list of error messages.
class SignupForm{ public static function errors(string $email, string $password): array<string> { array<string> $errors = []; if (!Core\Str::contains($email, '@')) { $errors[] = 'the email address has no @'; } if (Core\Str::length($password) < 8) { $errors[] = 'the password is shorter than 8 characters'; } return $errors; }}
// Both fields are wrong, so both messages are in the list.array<string> $errors = SignupForm::errors('ana.example.com', 'secret');Core\Test::assertContains($errors, 'the email address has no @');Core\Test::assertContains($errors, 'the password is shorter than 8 characters');echo "both errors are reported\n";
// A correct form gives an empty list.Core\Test::assertCount(SignupForm::errors('ana@example.com', 'correct horse'), 0);echo "a correct form has no errors\n";both errors are reporteda correct form has no errors