function CacheableMetadataTest::testAddCacheTags

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Cache/CacheableMetadataTest.php \Drupal\Tests\Core\Cache\CacheableMetadataTest::testAddCacheTags()
  2. 8.9.x core/tests/Drupal/Tests/Core/Cache/CacheableMetadataTest.php \Drupal\Tests\Core\Cache\CacheableMetadataTest::testAddCacheTags()
  3. 10 core/tests/Drupal/Tests/Core/Cache/CacheableMetadataTest.php \Drupal\Tests\Core\Cache\CacheableMetadataTest::testAddCacheTags()

This delegates to Cache::mergeTags(), so just a basic test.

@covers ::addCacheTags

File

core/tests/Drupal/Tests/Core/Cache/CacheableMetadataTest.php, line 93

Class

CacheableMetadataTest
@coversDefaultClass <a href="/api/drupal/core%21lib%21Drupal%21Core%21Cache%21CacheableMetadata.php/class/CacheableMetadata/11.x" title="Defines a generic class for passing cacheability metadata." class="local">\Drupal\Core\Cache\CacheableMetadata</a> @group Cache

Namespace

Drupal\Tests\Core\Cache

Code

public function testAddCacheTags() : void {
    $metadata = new CacheableMetadata();
    $add_expected = [
        [
            [],
            [],
        ],
        [
            [
                'foo:bar',
            ],
            [
                'foo:bar',
            ],
        ],
        [
            [
                'foo:baz',
            ],
            [
                'foo:bar',
                'foo:baz',
            ],
        ],
        [
            [
                'axx:first',
                'foo:baz',
            ],
            [
                'foo:bar',
                'foo:baz',
                'axx:first',
            ],
        ],
        [
            [],
            [
                'foo:bar',
                'foo:baz',
                'axx:first',
            ],
        ],
        [
            [
                'axx:first',
            ],
            [
                'foo:bar',
                'foo:baz',
                'axx:first',
            ],
        ],
    ];
    foreach ($add_expected as $row => $data) {
        [
            $add,
            $expected,
        ] = $data;
        $metadata->addCacheTags($add);
        $this->assertEquals($expected, $metadata->getCacheTags(), sprintf("Dataset in %d row failed on validation.", $row + 1));
    }
}

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