Same name and namespace in other branches
  1. 7.x modules/field/tests/field_test.module \field_test_memorize()
  2. 8.9.x core/modules/field/tests/modules/field_test/field_test.module \field_test_memorize()
  3. 9 core/modules/field/tests/modules/field_test/field_test.module \field_test_memorize()

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
FieldStorageConfig::create($field_definition)
  ->save();

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

// make sure hook_field_storage_config_create() is invoked correctly
assertEquals(1, count($mem['field_test_field_storage_config_create']));
assertEquals(array(
  $field,
), $mem['field_test_field_storage_config_create'][0]);

Parameters

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

$value: A value to store for $key.

Return value

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

5 calls to field_test_memorize()
BulkDeleteTest::testPurgeField in core/modules/field/tests/src/Kernel/BulkDeleteTest.php
Tests purging fields.
BulkDeleteTest::testPurgeFieldStorage in core/modules/field/tests/src/Kernel/BulkDeleteTest.php
Tests purging field storages.
FieldStorageCrudTest::testCreate in core/modules/field/tests/src/Kernel/FieldStorageCrudTest.php
Tests the creation of a field storage.
field_test_field_storage_config_create in core/modules/field/tests/modules/field_test/field_test.module
Memorize calls to field_test_field_storage_config_create().
field_test_query_efq_metadata_test_alter in core/modules/field/tests/modules/field_test/field_test.module
Implements hook_query_TAG_alter() for tag 'efq_metadata_test'.

File

core/modules/field/tests/modules/field_test/field_test.module, line 67
Helper module for the Field API tests.

Code

function field_test_memorize($key = NULL, $value = NULL) {
  static $memorize;
  if (!isset($key)) {
    $return = $memorize;
    $memorize = [];
    return $return;
  }
  if (is_array($memorize)) {
    $memorize[$key][] = $value;
  }
}