function Inspector::assertAllHaveKey

Same name in other branches
  1. 9 core/lib/Drupal/Component/Assertion/Inspector.php \Drupal\Component\Assertion\Inspector::assertAllHaveKey()
  2. 8.9.x core/lib/Drupal/Component/Assertion/Inspector.php \Drupal\Component\Assertion\Inspector::assertAllHaveKey()
  3. 10 core/lib/Drupal/Component/Assertion/Inspector.php \Drupal\Component\Assertion\Inspector::assertAllHaveKey()

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.

assert(Inspector::assertAllHaveKey($arrayToTest, "type", "theme path", "function", "template", "variables", "render element", "preprocess functions"));

Note: If a method requires certain keys to be present it will usually be specific about the data types for the values of those keys. Therefore it will be best to write a specific test for it. Such tests are either bound to the object that uses them, or are collected into one assertion set for the package.

Parameters

mixed $traversable: Variable to be examined.

string ...: Keys to be searched for.

Return value

bool TRUE if $traversable can be traversed and all members have all keys.

1 call to Inspector::assertAllHaveKey()
InspectorTest::testAssertAllHaveKey in core/tests/Drupal/Tests/Component/Assertion/InspectorTest.php
Tests asserting all members have specified keys.

File

core/lib/Drupal/Component/Assertion/Inspector.php, line 193

Class

Inspector
Generic inspections for the assert() statement.

Namespace

Drupal\Component\Assertion

Code

public static function assertAllHaveKey($traversable) {
    $args = func_get_args();
    unset($args[0]);
    if (is_iterable($traversable)) {
        foreach ($traversable as $member) {
            foreach ($args as $key) {
                if (!array_key_exists($key, $member)) {
                    return FALSE;
                }
            }
        }
        return TRUE;
    }
    return FALSE;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.