class CacheTest
Same name in this branch
- main core/modules/views/tests/src/Kernel/Plugin/CacheTest.php \Drupal\Tests\views\Kernel\Plugin\CacheTest
Same name and namespace in other branches
- 11.x core/modules/views/tests/src/Kernel/Plugin/CacheTest.php \Drupal\Tests\views\Kernel\Plugin\CacheTest
- 11.x core/tests/Drupal/Tests/Core/Cache/CacheTest.php \Drupal\Tests\Core\Cache\CacheTest
- 10 core/modules/views/tests/src/Kernel/Plugin/CacheTest.php \Drupal\Tests\views\Kernel\Plugin\CacheTest
- 10 core/tests/Drupal/Tests/Core/Cache/CacheTest.php \Drupal\Tests\Core\Cache\CacheTest
- 9 core/modules/views/tests/src/Kernel/Plugin/CacheTest.php \Drupal\Tests\views\Kernel\Plugin\CacheTest
- 9 core/tests/Drupal/Tests/Core/Cache/CacheTest.php \Drupal\Tests\Core\Cache\CacheTest
- 8.9.x core/modules/views/tests/src/Kernel/Plugin/CacheTest.php \Drupal\Tests\views\Kernel\Plugin\CacheTest
- 8.9.x core/tests/Drupal/Tests/Core/Cache/CacheTest.php \Drupal\Tests\Core\Cache\CacheTest
Tests Drupal\Core\Cache\Cache.
Attributes
#[CoversClass(Cache::class)]
#[Group('Cache')]
Hierarchy
- class \Drupal\Tests\UnitTestCase uses \Drupal\Tests\DrupalTestCaseTrait, \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Drupal\TestTools\Extension\DeprecationBridge\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait extends \PHPUnit\Framework\TestCase
- class \Drupal\Tests\Core\Cache\CacheTest extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of CacheTest
File
-
core/
tests/ Drupal/ Tests/ Core/ Cache/ CacheTest.php, line 19
Namespace
Drupal\Tests\Core\CacheView source
class CacheTest extends UnitTestCase {
/**
* Provides a list of cache tags arrays.
*
* @return array
* An array of cache tags arrays.
*/
public function validateTagsProvider() : array {
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.',
],
];
}
/**
* Provides a list of pairs of cache tags arrays to be merged.
*
* @return array
* An array of pairs of cache tags arrays to be merged.
*/
public static function mergeTagsProvider() : array {
return [
[
[],
[],
[],
],
[
[
'bar',
'foo',
],
[
'bar',
],
[
'foo',
],
],
[
[
'foo',
'bar',
],
[
'foo',
],
[
'bar',
],
],
[
[
'foo',
'bar',
],
[
'foo',
],
[
'bar',
'foo',
],
],
[
[
'foo',
'bar',
],
[
'foo',
],
[
'foo',
'bar',
],
],
[
[
'bar',
'foo',
],
[
'bar',
'foo',
],
[
'foo',
'bar',
],
],
[
[
'foo',
'bar',
],
[
'foo',
'bar',
],
[
'foo',
'bar',
],
],
[
[
'bar',
'foo',
'llama',
],
[
'bar',
'foo',
],
[
'foo',
'bar',
],
[
'llama',
'foo',
],
],
];
}
/**
* Tests merge tags.
*/
public function testMergeTags(array $expected, ...$cache_tags) : void {
$this->assertEqualsCanonicalizing($expected, Cache::mergeTags(...$cache_tags));
}
/**
* Provides a list of pairs of cache tags arrays to be merged.
*
* @return array
* An array of pairs of cache tags arrays to be merged.
*/
public static function mergeMaxAgesProvider() : array {
return [
[
Cache::PERMANENT,
Cache::PERMANENT,
Cache::PERMANENT,
],
[
60,
60,
60,
],
[
0,
0,
0,
],
[
0,
60,
0,
],
[
0,
0,
60,
],
[
0,
Cache::PERMANENT,
0,
],
[
0,
0,
Cache::PERMANENT,
],
[
60,
Cache::PERMANENT,
60,
],
[
60,
60,
Cache::PERMANENT,
],
[
Cache::PERMANENT,
Cache::PERMANENT,
Cache::PERMANENT,
Cache::PERMANENT,
],
[
30,
60,
60,
30,
],
[
0,
0,
0,
0,
],
[
60,
Cache::PERMANENT,
60,
Cache::PERMANENT,
],
[
60,
60,
Cache::PERMANENT,
Cache::PERMANENT,
],
[
60,
Cache::PERMANENT,
Cache::PERMANENT,
60,
],
];
}
/**
* Tests merge max ages.
*/
public function testMergeMaxAges($expected, ...$max_ages) : void {
$this->assertSame($expected, Cache::mergeMaxAges(...$max_ages));
}
/**
* Provides a list of pairs of cache contexts arrays to be merged.
*
* @return array
* An array of pairs of cache contexts arrays to be merged.
*/
public static function mergeCacheContextsProvide() : array {
return [
[
[],
[],
[],
],
[
[
'foo',
],
[],
[
'foo',
],
],
[
[
'foo',
],
[
'foo',
],
[],
],
[
[
'bar',
'foo',
],
[
'foo',
],
[
'bar',
],
],
[
[
'bar',
'foo',
],
[
'bar',
],
[
'foo',
],
],
[
[
'foo.bar',
'foo.foo',
],
[
'foo.foo',
],
[
'foo.bar',
],
],
[
[
'foo.bar',
'foo.foo',
],
[
'foo.bar',
],
[
'foo.foo',
],
],
[
[
'bar',
'foo',
'llama',
],
[],
[
'foo',
'bar',
'llama',
],
],
[
[
'bar',
'foo',
'llama',
],
[
'foo',
'bar',
'llama',
],
[],
],
[
[
'bar',
'foo',
'llama',
],
[
'foo',
],
[
'foo',
'bar',
'llama',
],
],
[
[
'bar',
'foo',
'llama',
],
[
'foo',
'bar',
'llama',
],
[
'foo',
],
],
];
}
/**
* Tests merge cache contexts.
*
* @legacy-covers ::mergeContexts
*/
public function testMergeCacheContexts(array $expected, ...$contexts) : void {
$cache_contexts_manager = $this->prophesize(CacheContextsManager::class);
$cache_contexts_manager->assertValidTokens(Argument::any())
->willReturn(TRUE);
$container = $this->prophesize(Container::class);
$container->get('cache_contexts_manager')
->willReturn($cache_contexts_manager->reveal());
\Drupal::setContainer($container->reveal());
$this->assertEqualsCanonicalizing($expected, Cache::mergeContexts(...$contexts));
}
/**
* Provides a list of pairs of (prefix, suffixes) to build cache tags from.
*
* @return array
* An array of pairs of (prefix, suffixes) to build cache tags from.
*/
public static function buildTagsProvider() : array {
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',
],
'.',
],
];
}
/**
* Tests build tags.
*/
public function testBuildTags($prefix, array $suffixes, array $expected, $glue = ':') : void {
$this->assertEquals($expected, Cache::buildTags($prefix, $suffixes, $glue));
}
}
Members
| Title Sort descending | Deprecated | Modifiers | Object type | Summary |
|---|---|---|---|---|
| CacheTest::buildTagsProvider | public static | function | Provides a list of pairs of (prefix, suffixes) to build cache tags from. | |
| CacheTest::mergeCacheContextsProvide | public static | function | Provides a list of pairs of cache contexts arrays to be merged. | |
| CacheTest::mergeMaxAgesProvider | public static | function | Provides a list of pairs of cache tags arrays to be merged. | |
| CacheTest::mergeTagsProvider | public static | function | Provides a list of pairs of cache tags arrays to be merged. | |
| CacheTest::testBuildTags | public | function | Tests build tags. | |
| CacheTest::testMergeCacheContexts | public | function | Tests merge cache contexts. | |
| CacheTest::testMergeMaxAges | public | function | Tests merge max ages. | |
| CacheTest::testMergeTags | public | function | Tests merge tags. | |
| CacheTest::validateTagsProvider | public | function | Provides a list of cache tags arrays. | |
| DrupalTestCaseTrait::checkErrorHandlerOnTearDown | public | function | Checks the test error handler after test execution. | |
| ExpectDeprecationTrait::expectDeprecation | Deprecated | public | function | Adds an expected deprecation. |
| ExpectDeprecationTrait::regularExpressionForFormatDescription | private | function | ||
| RandomGeneratorTrait::getRandomGenerator | protected | function | Gets the random generator for the utility methods. | |
| RandomGeneratorTrait::randomMachineName | protected | function | Generates a unique random string containing letters and numbers. | |
| RandomGeneratorTrait::randomObject | public | function | Generates a random PHP object. | |
| RandomGeneratorTrait::randomString | public | function | Generates a pseudo-random string of ASCII characters of codes 32 to 126. | |
| UnitTestCase::$root | protected | property | The app root. | |
| 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::getContainerWithCacheTagsInvalidator | protected | function | Sets up a container with a cache tags invalidator. | |
| UnitTestCase::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | |
| UnitTestCase::setDebugDumpHandler | public static | function | Registers the dumper CLI handler when the DebugDump extension is enabled. | |
| UnitTestCase::setUp | protected | function | ||
| UnitTestCase::setupMockIterator | protected | function | Set up a traversable class mock to return specific items when iterated. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.