Array Assertions
Array assertions validate arrays, collections, and array-accessible objects.
Available Assertions
Section titled “Available Assertions”isCountable()
Section titled “isCountable()”Assert that a value is countable.
use Cline\Assert\Assertions\Assertion;
Assertion::isCountable($array);Assertion::isCountable($collection, 'Value must be countable');count()
Section titled “count()”Assert exact number of elements.
Assertion::count($array, 5);Assertion::count($items, 3, 'Must have exactly 3 items');minCount()
Section titled “minCount()”Assert minimum number of elements.
Assertion::minCount($array, 1);Assertion::minCount($options, 2, 'Must provide at least 2 options');maxCount()
Section titled “maxCount()”Assert maximum number of elements.
Assertion::maxCount($array, 10);Assertion::maxCount($tags, 5, 'Maximum 5 tags allowed');keyExists()
Section titled “keyExists()”Assert that a key exists.
Assertion::keyExists($array, 'name');Assertion::keyExists($config, 'database', 'Database configuration is required');keyNotExists()
Section titled “keyNotExists()”Assert that a key does NOT exist.
Assertion::keyNotExists($array, 'legacy_field');Assertion::keyNotExists($data, 'password', 'Password should not be included');notEmptyKey()
Section titled “notEmptyKey()”Assert that a key exists and its value is not empty.
Assertion::notEmptyKey($data, 'name');Assertion::notEmptyKey($form, 'email', 'Email field cannot be empty');uniqueValues()
Section titled “uniqueValues()”Assert all values are unique.
Assertion::uniqueValues($array);Assertion::uniqueValues($ids, 'All IDs must be unique');inArray()
Section titled “inArray()”Assert that a value exists in an array.
Assertion::inArray($status, ['draft', 'published', 'archived']);Assertion::inArray($role, $validRoles, 'Invalid role selected');notInArray()
Section titled “notInArray()”Assert that a value does NOT exist in an array.
Assertion::notInArray($username, $bannedNames);Assertion::notInArray($value, $blacklist, 'This value is not allowed');choice()
Section titled “choice()”Alias for inArray().
Assertion::choice($color, ['red', 'green', 'blue']);eqArraySubset()
Section titled “eqArraySubset()”Assert that an array contains a subset.
$expected = ['name' => 'John', 'active' => true];Assertion::eqArraySubset($user, $expected);Chaining Array Assertions
Section titled “Chaining Array Assertions”use Cline\Assert\Assert;
Assert::that($array) ->isArray() ->notEmpty() ->minCount(1) ->maxCount(10);
Assert::that($status) ->string() ->inArray(['pending', 'approved', 'rejected']);Common Patterns
Section titled “Common Patterns”Required Array Keys
Section titled “Required Array Keys”Assert::that($config) ->isArray() ->keyExists('host') ->keyExists('port') ->keyExists('database');Form Validation
Section titled “Form Validation”Assert::lazy() ->that($form, 'form')->isArray() ->that($form, 'form')->keyExists('name') ->that($form, 'form')->notEmptyKey('name') ->verifyNow();Collection Size Validation
Section titled “Collection Size Validation”Assert::that($items) ->isArray() ->minCount(1, 'At least one item is required') ->maxCount(100, 'Maximum 100 items allowed');Validating All Elements
Section titled “Validating All Elements”Use the all() modifier to validate every element:
Assert::thatAll($tags)->string();Assert::thatAll($quantities) ->integer() ->greaterThan(0);Assert::thatAll($emailList)->email();Nested Arrays
Section titled “Nested Arrays”Assert::that($data) ->isArray() ->keyExists('user') ->keyExists('settings');
Assert::that($data['user']) ->isArray() ->notEmptyKey('id') ->notEmptyKey('name');Next Steps
Section titled “Next Steps”- Type Assertions - Type checking
- Null and Empty Assertions - Empty array checks
- Lazy Assertions - Validate multiple conditions