function FileManagedTestBase::assertFileHookCalled

Same name in other branches
  1. 9 core/modules/file/tests/src/Functional/FileManagedTestBase.php \Drupal\Tests\file\Functional\FileManagedTestBase::assertFileHookCalled()
  2. 8.9.x core/modules/file/src/Tests/FileManagedTestBase.php \Drupal\file\Tests\FileManagedTestBase::assertFileHookCalled()
  3. 8.9.x core/modules/file/tests/src/Functional/FileManagedTestBase.php \Drupal\Tests\file\Functional\FileManagedTestBase::assertFileHookCalled()
  4. 10 core/modules/file/tests/src/Functional/FileManagedTestBase.php \Drupal\Tests\file\Functional\FileManagedTestBase::assertFileHookCalled()

Assert that a hook_file_* hook was called a certain number of times.

Parameters

string $hook: String with the hook name; for instance, 'load', 'save', 'insert', etc.

int $expected_count: Optional integer count.

string|null $message: Optional translated string message.

File

core/modules/file/tests/src/Functional/FileManagedTestBase.php, line 73

Class

FileManagedTestBase
Provides a base class for testing files with the file_test module.

Namespace

Drupal\Tests\file\Functional

Code

public function assertFileHookCalled($hook, $expected_count = 1, $message = NULL) {
    $actual_count = count(file_test_get_calls($hook));
    if (!isset($message)) {
        if ($actual_count == $expected_count) {
            $message = new FormattableMarkup('hook_file_@name was called correctly.', [
                '@name' => $hook,
            ]);
        }
        elseif ($expected_count == 0) {
            $message = \Drupal::translation()->formatPlural($actual_count, 'hook_file_@name was not expected to be called but was actually called once.', 'hook_file_@name was not expected to be called but was actually called @count times.', [
                '@name' => $hook,
                '@count' => $actual_count,
            ]);
        }
        else {
            $message = new FormattableMarkup('hook_file_@name was expected to be called %expected times but was called %actual times.', [
                '@name' => $hook,
                '%expected' => $expected_count,
                '%actual' => $actual_count,
            ]);
        }
    }
    $this->assertEquals($expected_count, $actual_count, $message);
}

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