function ViewsDataTest::testCacheCallsWithSameTableMultipleTimes

Same name 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. 10 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 \Drupal\views\ViewsData @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.