You are here

function field_test_memorize in SimpleTest 7

Store and retrieve keyed data for later verification by unit tests.

This function is a simple in-memory key-value store with the distinction that it stores all values for a given key instead of just the most recently set value. field_test module hooks call this function to record their arguments, keyed by hook name. The unit tests later call this function to verify that the correct hooks were called and were passed the correct arguments.

This function ignores all calls until the first time it is called with $key of NULL. Each time it is called with $key of NULL, it erases all previously stored data from its internal cache, but also returns the previously stored data to the caller. A typical usage scenario is:


  // calls to field_test_memorize() here are ignored

  // turn on memorization
  field_test_memorize();

  // call some Field API functions that invoke field_test hooks
  $field = field_create_field(...);

  // retrieve and reset the memorized hook call data
  $mem = field_test_memorize();

  // make sure hook_field_create_field() is invoked correctly
  assertEqual(count($mem['field_test_field_create_field']), 1);
  assertEqual($mem['field_test_field_create_field'][0], array($field));

Parameters

$key: The key under which to store to $value, or NULL as described above.

$value: A value to store for $key.

Return value

An array mapping each $key to an array of each $value passed in for that key.

4 calls to field_test_memorize()
field_test_field_create_field in tests/field_test.module
Memorize calls to hook_field_create_field().
field_test_field_delete in tests/field_test.module
Memorize calls to hook_field_delete().
field_test_field_insert in tests/field_test.module
Memorize calls to hook_field_insert().
field_test_field_update in tests/field_test.module
Memorize calls to hook_field_update().

File

tests/field_test.module, line 1195

Code

function field_test_memorize($key = NULL, $value = NULL) {
  $memorize =& drupal_static(__FUNCTION__, NULL);
  if (is_null($key)) {
    $return = $memorize;
    $memorize = array();
    return $return;
  }
  if (is_array($memorize)) {
    $memorize[$key][] = $value;
  }
}