function ViewsDataTest::testCacheCallsWithSameTableMultipleTimes

Same name and namespace in other branches
  1. 9 core/modules/views/tests/src/Unit/ViewsDataTest.php \Drupal\Tests\views\Unit\ViewsDataTest::testCacheCallsWithSameTableMultipleTimes()
  2. 8.9.x core/modules/views/tests/src/Unit/ViewsDataTest.php \Drupal\Tests\views\Unit\ViewsDataTest::testCacheCallsWithSameTableMultipleTimes()
  3. 11.x core/modules/views/tests/src/Unit/ViewsDataTest.php \Drupal\Tests\views\Unit\ViewsDataTest::testCacheCallsWithSameTableMultipleTimes()

Tests the cache backend behavior with requesting the same table multiple.

File

core/modules/views/tests/src/Unit/ViewsDataTest.php, line 370

Class

ViewsDataTest
@coversDefaultClass <a href="/api/drupal/core%21modules%21views%21src%21ViewsData.php/class/ViewsData/10" title="Class to manage and lazy load cached views data." class="local">\Drupal\views\ViewsData</a> @group views

Namespace

Drupal\Tests\views\Unit

Code

public function testCacheCallsWithSameTableMultipleTimes() : void {
    $expected_views_data = $this->viewsDataWithProvider();
    $this->setupMockedModuleHandler();
    $gets = [
        'views_data:views_test_data:en',
        'views_data:en',
    ];
    $this->cacheBackend
        ->expects($this->exactly(count($gets)))
        ->method('get')
        ->with($this->callback(function (string $key) use (&$gets) : bool {
        return $key === array_shift($gets);
    }));
    $sets = [
        'views_data:en',
        $expected_views_data,
        'views_data:views_test_data:en',
        $expected_views_data['views_test_data'],
    ];
    $this->cacheBackend
        ->expects($this->exactly(count($sets) / 2))
        ->method('set')
        ->with($this->callback(function (string $key) use (&$sets) : bool {
        return $key === array_shift($sets);
    }), $this->callback(function (array $data) use (&$sets) : bool {
        return $data === array_shift($sets);
    }));
    // Request the same table 5 times. The caches are empty at this point, so
    // what will happen is that it will first check for a cache entry for the
    // given table, get a cache miss, then try the cache entry for all tables,
    // which does not exist yet either. As a result, it rebuilds the information
    // and writes a cache entry for all tables and the requested table.
    $table_name = 'views_test_data';
    for ($i = 0; $i < 5; $i++) {
        $views_data = $this->viewsData
            ->get($table_name);
        $this->assertSame($expected_views_data['views_test_data'], $views_data);
    }
}

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