class CacheTest

Same name in this branch
  1. 8.9.x core/modules/views/tests/src/Kernel/Plugin/CacheTest.php \Drupal\Tests\views\Kernel\Plugin\CacheTest
Same name in other branches
  1. 9 core/modules/views/tests/src/Kernel/Plugin/CacheTest.php \Drupal\Tests\views\Kernel\Plugin\CacheTest
  2. 9 core/tests/Drupal/Tests/Core/Cache/CacheTest.php \Drupal\Tests\Core\Cache\CacheTest
  3. 10 core/modules/views/tests/src/Kernel/Plugin/CacheTest.php \Drupal\Tests\views\Kernel\Plugin\CacheTest
  4. 10 core/tests/Drupal/Tests/Core/Cache/CacheTest.php \Drupal\Tests\Core\Cache\CacheTest
  5. 11.x core/modules/views/tests/src/Kernel/Plugin/CacheTest.php \Drupal\Tests\views\Kernel\Plugin\CacheTest
  6. 11.x core/tests/Drupal/Tests/Core/Cache/CacheTest.php \Drupal\Tests\Core\Cache\CacheTest

@coversDefaultClass \Drupal\Core\Cache\Cache @group Cache

Hierarchy

Expanded class hierarchy of CacheTest

File

core/tests/Drupal/Tests/Core/Cache/CacheTest.php, line 12

Namespace

Drupal\Tests\Core\Cache
View source
class CacheTest extends UnitTestCase {
    
    /**
     * Provides a list of cache tags arrays.
     *
     * @return array
     */
    public function validateTagsProvider() {
        return [
            [
                [],
                FALSE,
            ],
            [
                [
                    'foo',
                ],
                FALSE,
            ],
            [
                [
                    'foo',
                    'bar',
                ],
                FALSE,
            ],
            [
                [
                    'foo',
                    'bar',
                    'llama:2001988',
                    'baz',
                    'llama:14031991',
                ],
                FALSE,
            ],
            // Invalid.
[
                [
                    FALSE,
                ],
                'Cache tags must be strings, boolean given.',
            ],
            [
                [
                    TRUE,
                ],
                'Cache tags must be strings, boolean given.',
            ],
            [
                [
                    'foo',
                    FALSE,
                ],
                'Cache tags must be strings, boolean given.',
            ],
            [
                [
                    NULL,
                ],
                'Cache tags must be strings, NULL given.',
            ],
            [
                [
                    'foo',
                    NULL,
                ],
                'Cache tags must be strings, NULL given.',
            ],
            [
                [
                    1337,
                ],
                'Cache tags must be strings, integer given.',
            ],
            [
                [
                    'foo',
                    1337,
                ],
                'Cache tags must be strings, integer given.',
            ],
            [
                [
                    3.14,
                ],
                'Cache tags must be strings, double given.',
            ],
            [
                [
                    'foo',
                    3.14,
                ],
                'Cache tags must be strings, double given.',
            ],
            [
                [
                    [],
                ],
                'Cache tags must be strings, array given.',
            ],
            [
                [
                    'foo',
                    [],
                ],
                'Cache tags must be strings, array given.',
            ],
            [
                [
                    'foo',
                    [
                        'bar',
                    ],
                ],
                'Cache tags must be strings, array given.',
            ],
            [
                [
                    new \stdClass(),
                ],
                'Cache tags must be strings, object given.',
            ],
            [
                [
                    'foo',
                    new \stdClass(),
                ],
                'Cache tags must be strings, object given.',
            ],
        ];
    }
    
    /**
     * @covers ::validateTags
     *
     * @dataProvider validateTagsProvider
     * @expectedDeprecation Drupal\Core\Cache\Cache::validateTags() is deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use assert(\Drupal\Component\Assertion\Inspector::assertAllStrings($tags)) instead.
     * @group legacy
     */
    public function testValidateTags(array $tags, $expected_exception_message) {
        if ($expected_exception_message !== FALSE) {
            $this->expectException('LogicException');
            $this->expectExceptionMessage($expected_exception_message);
        }
        // If it doesn't throw an exception, validateTags() returns NULL.
        $this->assertNull(Cache::validateTags($tags));
    }
    
    /**
     * Provides a list of pairs of cache tags arrays to be merged.
     *
     * @return array
     */
    public function mergeTagsProvider() {
        return [
            [
                [],
                [],
                [],
            ],
            [
                [
                    'bar',
                ],
                [
                    'foo',
                ],
                [
                    'bar',
                    'foo',
                ],
            ],
            [
                [
                    'foo',
                ],
                [
                    'bar',
                ],
                [
                    'bar',
                    'foo',
                ],
            ],
            [
                [
                    'foo',
                ],
                [
                    'bar',
                    'foo',
                ],
                [
                    'bar',
                    'foo',
                ],
            ],
            [
                [
                    'foo',
                ],
                [
                    'foo',
                    'bar',
                ],
                [
                    'bar',
                    'foo',
                ],
            ],
            [
                [
                    'bar',
                    'foo',
                ],
                [
                    'foo',
                    'bar',
                ],
                [
                    'bar',
                    'foo',
                ],
            ],
            [
                [
                    'foo',
                    'bar',
                ],
                [
                    'foo',
                    'bar',
                ],
                [
                    'bar',
                    'foo',
                ],
            ],
        ];
    }
    
    /**
     * @covers ::mergeTags
     *
     * @dataProvider mergeTagsProvider
     */
    public function testMergeTags(array $a, array $b, array $expected) {
        $this->assertEquals($expected, Cache::mergeTags($a, $b));
    }
    
    /**
     * Provides a list of pairs of cache tags arrays to be merged.
     *
     * @return array
     */
    public function mergeMaxAgesProvider() {
        return [
            [
                Cache::PERMANENT,
                Cache::PERMANENT,
                Cache::PERMANENT,
            ],
            [
                60,
                60,
                60,
            ],
            [
                0,
                0,
                0,
            ],
            [
                60,
                0,
                0,
            ],
            [
                0,
                60,
                0,
            ],
            [
                Cache::PERMANENT,
                0,
                0,
            ],
            [
                0,
                Cache::PERMANENT,
                0,
            ],
            [
                Cache::PERMANENT,
                60,
                60,
            ],
            [
                60,
                Cache::PERMANENT,
                60,
            ],
        ];
    }
    
    /**
     * @covers ::mergeMaxAges
     *
     * @dataProvider mergeMaxAgesProvider
     */
    public function testMergeMaxAges($a, $b, $expected) {
        $this->assertSame($expected, Cache::mergeMaxAges($a, $b));
    }
    
    /**
     * Provides a list of pairs of (prefix, suffixes) to build cache tags from.
     *
     * @return array
     */
    public function buildTagsProvider() {
        return [
            [
                'node',
                [
                    1,
                ],
                [
                    'node:1',
                ],
            ],
            [
                'node',
                [
                    1,
                    2,
                    3,
                ],
                [
                    'node:1',
                    'node:2',
                    'node:3',
                ],
            ],
            [
                'node',
                [
                    3,
                    2,
                    1,
                ],
                [
                    'node:3',
                    'node:2',
                    'node:1',
                ],
            ],
            [
                'node',
                [
                    NULL,
                ],
                [
                    'node:',
                ],
            ],
            [
                'node',
                [
                    TRUE,
                    FALSE,
                ],
                [
                    'node:1',
                    'node:',
                ],
            ],
            [
                'node',
                [
                    'a',
                    'z',
                    'b',
                ],
                [
                    'node:a',
                    'node:z',
                    'node:b',
                ],
            ],
            // No suffixes, no cache tags.
[
                '',
                [],
                [],
            ],
            [
                'node',
                [],
                [],
            ],
            // Only suffix values matter, not keys: verify that expectation.
[
                'node',
                [
                    5 => 145,
                    4545 => 3,
                ],
                [
                    'node:145',
                    'node:3',
                ],
            ],
            [
                'node',
                [
                    5 => TRUE,
                ],
                [
                    'node:1',
                ],
            ],
            [
                'node',
                [
                    5 => NULL,
                ],
                [
                    'node:',
                ],
            ],
            [
                'node',
                [
                    'a' => NULL,
                ],
                [
                    'node:',
                ],
            ],
            [
                'node',
                [
                    'a' => TRUE,
                ],
                [
                    'node:1',
                ],
            ],
            // Test the $glue parameter.
[
                'config:system.menu',
                [
                    'menu_name',
                ],
                [
                    'config:system.menu.menu_name',
                ],
                '.',
            ],
        ];
    }
    
    /**
     * @covers ::buildTags
     *
     * @dataProvider buildTagsProvider
     */
    public function testBuildTags($prefix, array $suffixes, array $expected, $glue = ':') {
        $this->assertEquals($expected, Cache::buildTags($prefix, $suffixes, $glue));
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overrides
CacheTest::buildTagsProvider public function Provides a list of pairs of (prefix, suffixes) to build cache tags from.
CacheTest::mergeMaxAgesProvider public function Provides a list of pairs of cache tags arrays to be merged.
CacheTest::mergeTagsProvider public function Provides a list of pairs of cache tags arrays to be merged.
CacheTest::testBuildTags public function @covers ::buildTags
CacheTest::testMergeMaxAges public function @covers ::mergeMaxAges
CacheTest::testMergeTags public function @covers ::mergeTags
CacheTest::testValidateTags public function @covers ::validateTags
CacheTest::validateTagsProvider public function Provides a list of cache tags arrays.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 340

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