You are here

public static function Inspector::assertAllHaveKey in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 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('Drupal\\Component\\Assertion\\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 212
Contains \Drupal\Component\Assertion\Inspector.

Class

Inspector
Generic inspections for the assert() statement.

Namespace

Drupal\Component\Assertion

Code

public static function assertAllHaveKey() {
  $args = func_get_args();
  $traversable = array_shift($args);
  if (static::assertTraversable($traversable)) {
    foreach ($traversable as $member) {
      foreach ($args as $key) {
        if (!array_key_exists($key, $member)) {
          return FALSE;
        }
      }
    }
    return TRUE;
  }
  return FALSE;
}