function BulkDeleteTest::checkHooksInvocations

Same name in other branches
  1. 8.9.x core/modules/field/tests/src/Kernel/BulkDeleteTest.php \Drupal\Tests\field\Kernel\BulkDeleteTest::checkHooksInvocations()
  2. 10 core/modules/field/tests/src/Kernel/BulkDeleteTest.php \Drupal\Tests\field\Kernel\BulkDeleteTest::checkHooksInvocations()
  3. 11.x core/modules/field/tests/src/Kernel/BulkDeleteTest.php \Drupal\Tests\field\Kernel\BulkDeleteTest::checkHooksInvocations()

Tests that the expected hooks have been invoked on the expected entities.

Parameters

$expected_hooks: An array keyed by hook name, with one entry per expected invocation. Each entry is the value of the "$entity" parameter the hook is expected to have been passed.

$actual_hooks: The array of actual hook invocations recorded by field_test_memorize().

2 calls to BulkDeleteTest::checkHooksInvocations()
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.

File

core/modules/field/tests/src/Kernel/BulkDeleteTest.php, line 62

Class

BulkDeleteTest
Bulk delete storages and fields, and clean up afterwards.

Namespace

Drupal\Tests\field\Kernel

Code

public function checkHooksInvocations($expected_hooks, $actual_hooks) {
    foreach ($expected_hooks as $hook => $invocations) {
        $actual_invocations = $actual_hooks[$hook];
        // Check that the number of invocations is correct.
        $this->assertSameSize($invocations, $actual_invocations, "{$hook}() was called the expected number of times.");
        // Check that the hook was called for each expected argument.
        foreach ($invocations as $argument) {
            $found = FALSE;
            foreach ($actual_invocations as $actual_arguments) {
                // The argument we are looking for is either an array of entities as
                // the second argument or a single entity object as the first.
                if ($argument instanceof EntityInterface && $actual_arguments[0]->id() == $argument->id()) {
                    $found = TRUE;
                    break;
                }
                elseif (is_array($argument) && count($actual_arguments[1]) == count($argument) && count(array_diff_key($actual_arguments[1], $argument)) == 0) {
                    $found = TRUE;
                    break;
                }
            }
            $this->assertTrue($found, "{$hook}() was called on expected argument");
        }
    }
}

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