Core\Test::assertCount
Core\Test::assertCount() checks that an array has the number of entries you expect. The first argument is the array, and the second is the number. The number cannot be negative.
The check works only on arrays. To check the length of a string, compare Core\Str::length with the number in Core\Test::assertEquals. That way the reader sees that you count characters.
When the number is different, the check throws a Core\Test\Failure. The message shows the number of entries and the number you expected. 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 list split into pages.
Parameters
$actualarray<T>The array under test.$expecteduintThe number of entries the array must have.
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 array has a different number of entries. The failure is recorded before it is thrown, so acatchdoes not remove it from the test's result.
Examples
Check how many results
<?nvs// Shows how to check that a function returns the number of entries you expect.
class Tags{ public static function fromText(string $text): array<string> { return Core\Str::split($text, ','); }}
// The first value is the array. The second value is the number of entries you expect.// The array has three entries, so the check passes and nothing is thrown.Core\Test::assertCount(Tags::fromText('red,green,blue'), 3);echo "three tags\n";
// An empty array has 0 entries.array<string> $none = [];Core\Test::assertCount($none, 0);echo "no tags\n";three tagsno tagsRead a failure
<?nvs// Shows what happens when the count is wrong, and how to add your own message.
array<string> $recipients = ['ana@example.com', 'li@example.com'];
// The array has 2 entries, not 3, so the check throws a `Core\Test\Failure`.// The message shows both numbers.try { Core\Test::assertCount($recipients, 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::assertCount($recipients, 3, { message: 'every team member gets the report' });} catch (Core\Test\Failure $failure) { echo $failure->message, "\n";}Core\Test::assertCount failed: `$actual` holds 2 entries, `$expected` is 3every team member gets the report: Core\Test::assertCount failed: `$actual` holds 2 entries, `$expected` is 3Test a page of results
<?nvs// Shows a test of a function that splits a list of products into pages.
class Pages{ // Returns the products on page `$page`. The first page is 1. public static function of(array<string> $products, int $page, int $perPage): array<string> { return Core\Arr::slice($products, ($page - 1) * $perPage, $perPage); }}
array<string> $products = ['lamp', 'desk', 'chair', 'shelf', 'rug', 'mirror', 'clock'];
// Seven products with three per page give pages of 3, 3 and 1 products.Core\Test::assertCount(Pages::of($products, 1, 3), 3);Core\Test::assertCount(Pages::of($products, 2, 3), 3);Core\Test::assertCount(Pages::of($products, 3, 3), 1);
// A page after the last one is empty.Core\Test::assertCount(Pages::of($products, 4, 3), 0);echo "every page has the right size\n";every page has the right size