public static function PHPUnit_Util_XML::assertValidKeys

Validate list of keys in the associative array.

@since Method available since Release 3.3.0 @author Mike Naberezny <mike@maintainable.com> @author Derek DeVries <derek@maintainable.com>

Parameters

array $hash:

array $validKeys:

Return value

array

Throws

PHPUnit_Framework_Exception

7 calls to PHPUnit_Util_XML::assertValidKeys()
PHPUnit_Util_XML::findNodes in drupal/core/vendor/phpunit/phpunit/PHPUnit/Util/XML.php
Parse out the options from the tag using DOM object tree.
Util_XMLTest::testAssertValidKeysDefaultValuesA in drupal/core/vendor/phpunit/phpunit/Tests/Util/XMLTest.php
Util_XMLTest::testAssertValidKeysDefaultValuesB in drupal/core/vendor/phpunit/phpunit/Tests/Util/XMLTest.php
Util_XMLTest::testAssertValidKeysInvalidKey in drupal/core/vendor/phpunit/phpunit/Tests/Util/XMLTest.php
Util_XMLTest::testAssertValidKeysInvalidKeys in drupal/core/vendor/phpunit/phpunit/Tests/Util/XMLTest.php

... See full list

File

drupal/core/vendor/phpunit/phpunit/PHPUnit/Util/XML.php, line 299

Class

PHPUnit_Util_XML
XML helpers.

Code

public static function assertValidKeys(array $hash, array $validKeys) {
  $valids = array();

  // Normalize validation keys so that we can use both indexed and
  // associative arrays.
  foreach ($validKeys as $key => $val) {
    is_int($key) ? $valids[$val] = NULL : ($valids[$key] = $val);
  }
  $validKeys = array_keys($valids);

  // Check for invalid keys.
  foreach ($hash as $key => $value) {
    if (!in_array($key, $validKeys)) {
      $unknown[] = $key;
    }
  }
  if (!empty($unknown)) {
    throw new PHPUnit_Framework_Exception('Unknown key(s): ' . implode(', ', $unknown));
  }

  // Add default values for any valid keys that are empty.
  foreach ($valids as $key => $value) {
    if (!isset($hash[$key])) {
      $hash[$key] = $value;
    }
  }
  return $hash;
}