Inspector
class Inspector (View source)
Generic inspections for the assert() statement.
This is a static function collection for inspecting variable contents. All functions in this collection check a variable against an assertion about its structure.
Example call:
Methods
Asserts argument can be traversed with foreach.
Asserts callback returns TRUE for each member of a traversable.
Asserts that all members are strings.
Asserts all members are strings or objects with magic __toString() method.
Asserts argument is a string or an object castable to a string.
Asserts that all members are arrays.
Asserts that the array is strict.
Asserts all members are strict arrays.
Asserts all given keys exist in every member array.
Asserts that all members are integer values.
Asserts that all members are float values.
Asserts that all members are callable.
Asserts that all members are not empty.
Asserts all members are numeric data types or strings castable to such.
Asserts that all members are strings that contain the specified string.
Asserts that all members are strings matching a regular expression.
Asserts that all members are objects.
Details
static bool
assertTraversable(mixed $traversable)
Asserts argument can be traversed with foreach.
static bool
assertAll(callable $callable, mixed $traversable)
Asserts callback returns TRUE for each member of a traversable.
This is less memory intensive than using array_filter() to build a second array and then comparing the arrays. Many of the other functions in this collection alias this function passing a specific callback to make the code more readable.
static bool
assertAllStrings(mixed $traversable)
Asserts that all members are strings.
Use this only if it is vital that the members not be objects, otherwise test with ::assertAllStringable().
static bool
assertAllStringable(mixed $traversable)
Asserts all members are strings or objects with magic __toString() method.
static bool
assertStringable(mixed $string)
Asserts argument is a string or an object castable to a string.
Use this instead of is_string() alone unless the argument being an object in any way will cause a problem.
static bool
assertAllArrays(mixed $traversable)
Asserts that all members are arrays.
static bool
assertStrictArray(mixed $array)
Asserts that the array is strict.
What PHP calls arrays are more formally called maps in most other programming languages. A map is a datatype that associates values to keys. The term 'strict array' here refers to a 0-indexed array in the classic sense found in programming languages other than PHP.
static bool
assertAllStrictArrays(mixed $traversable)
Asserts all members are strict arrays.
static bool
assertAllHaveKey(mixed $traversable)
Asserts all given keys exist in every member array.
Drupal has several data structure arrays that require certain keys be set. You can overload this function to specify a list of required keys. All of the keys must be set for this method to return TRUE.
As an example, this assertion tests for the keys of a theme registry.
static bool
assertAllIntegers(mixed $traversable)
Asserts that all members are integer values.
static bool
assertAllFloat(mixed $traversable)
Asserts that all members are float values.
static bool
assertAllCallable(mixed $traversable)
Asserts that all members are callable.
static bool
assertAllNotEmpty(mixed $traversable)
Asserts that all members are not empty.
static bool
assertAllNumeric(mixed $traversable)
Asserts all members are numeric data types or strings castable to such.
static bool
assertAllMatch(string $pattern, mixed $traversable, bool $case_sensitive = FALSE)
Asserts that all members are strings that contain the specified string.
This runs faster than the regular expression equivalent.
static bool
assertAllRegularExpressionMatch(string $pattern, mixed $traversable)
Asserts that all members are strings matching a regular expression.
static bool
assertAllObjects(mixed $traversable)
Asserts that all members are objects.
When testing if a collection is composed of objects those objects should be given a common interface to implement and the test should be written to search for just that interface. While this method will allow tests for just object status or for multiple classes and interfaces this was done to allow tests to be written for existing code without altering it. Only use this method in that manner when testing code from third party vendors.
Here are some examples: