class CacheableMetadataTest
Same name in other branches
- 9 core/tests/Drupal/Tests/Core/Cache/CacheableMetadataTest.php \Drupal\Tests\Core\Cache\CacheableMetadataTest
- 8.9.x core/tests/Drupal/Tests/Core/Cache/CacheableMetadataTest.php \Drupal\Tests\Core\Cache\CacheableMetadataTest
- 11.x core/tests/Drupal/Tests/Core/Cache/CacheableMetadataTest.php \Drupal\Tests\Core\Cache\CacheableMetadataTest
@coversDefaultClass \Drupal\Core\Cache\CacheableMetadata @group Cache
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait
- class \Drupal\Tests\Core\Cache\CacheableMetadataTest extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of CacheableMetadataTest
File
-
core/
tests/ Drupal/ Tests/ Core/ Cache/ CacheableMetadataTest.php, line 17
Namespace
Drupal\Tests\Core\CacheView source
class CacheableMetadataTest extends UnitTestCase {
/**
* @covers ::merge
* @dataProvider providerTestMerge
*
* This only tests at a high level, because it reuses existing logic. Detailed
* tests exist for the existing logic:
*
* @see \Drupal\Tests\Core\Cache\CacheTest::testMergeTags()
* @see \Drupal\Tests\Core\Cache\CacheTest::testMergeMaxAges()
* @see \Drupal\Tests\Core\Cache\CacheContextsTest
*/
public function testMerge(CacheableMetadata $a, CacheableMetadata $b, CacheableMetadata $expected) : void {
$cache_contexts_manager = $this->getMockBuilder('Drupal\\Core\\Cache\\Context\\CacheContextsManager')
->disableOriginalConstructor()
->getMock();
$cache_contexts_manager->method('assertValidTokens')
->willReturn(TRUE);
$container = new ContainerBuilder();
$container->set('cache_contexts_manager', $cache_contexts_manager);
\Drupal::setContainer($container);
$this->assertEqualsCanonicalizing($expected, $a->merge($b));
}
/**
* @covers ::addCacheableDependency
* @dataProvider providerTestMerge
*
* This only tests at a high level, because it reuses existing logic. Detailed
* tests exist for the existing logic:
*
* @see \Drupal\Tests\Core\Cache\CacheTest::testMergeTags()
* @see \Drupal\Tests\Core\Cache\CacheTest::testMergeMaxAges()
* @see \Drupal\Tests\Core\Cache\CacheContextsTest
*/
public function testAddCacheableDependency(CacheableMetadata $a, CacheableMetadata $b, CacheableMetadata $expected) : void {
$cache_contexts_manager = $this->getMockBuilder('Drupal\\Core\\Cache\\Context\\CacheContextsManager')
->disableOriginalConstructor()
->getMock();
$cache_contexts_manager->method('assertValidTokens')
->willReturn(TRUE);
$container = new ContainerBuilder();
$container->set('cache_contexts_manager', $cache_contexts_manager);
\Drupal::setContainer($container);
$this->assertEqualsCanonicalizing($expected, $a->addCacheableDependency($b));
}
/**
* Provides test data for testMerge().
*
* @return array
*/
public static function providerTestMerge() {
return [
// All empty.
[
new CacheableMetadata(),
new CacheableMetadata(),
new CacheableMetadata(),
],
// Cache contexts.
[
(new CacheableMetadata())->setCacheContexts([
'foo',
]),
(new CacheableMetadata())->setCacheContexts([
'bar',
]),
(new CacheableMetadata())->setCacheContexts([
'bar',
'foo',
]),
],
// Cache tags.
[
(new CacheableMetadata())->setCacheTags([
'foo',
]),
(new CacheableMetadata())->setCacheTags([
'bar',
]),
(new CacheableMetadata())->setCacheTags([
'foo',
'bar',
]),
],
// Cache max-ages.
[
(new CacheableMetadata())->setCacheMaxAge(60),
(new CacheableMetadata())->setCacheMaxAge(Cache::PERMANENT),
(new CacheableMetadata())->setCacheMaxAge(60),
],
];
}
/**
* This delegates to Cache::mergeTags(), so just a basic test.
*
* @covers ::addCacheTags
*/
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));
}
}
/**
* Tests valid and invalid values as max age.
*
* @covers ::setCacheMaxAge
* @dataProvider providerSetCacheMaxAge
*/
public function testSetCacheMaxAge($data, $expect_exception) : void {
$metadata = new CacheableMetadata();
if ($expect_exception) {
$this->expectException('\\InvalidArgumentException');
}
$metadata->setCacheMaxAge($data);
$this->assertEquals($data, $metadata->getCacheMaxAge());
}
/**
* Data provider for testSetCacheMaxAge.
*/
public static function providerSetCacheMaxAge() {
return [
[
0,
FALSE,
],
[
'http',
TRUE,
],
[
'0',
TRUE,
],
[
new \stdClass(),
TRUE,
],
[
300,
FALSE,
],
[
[],
TRUE,
],
[
8.0,
TRUE,
],
];
}
/**
* @covers ::createFromRenderArray
* @dataProvider providerTestCreateFromRenderArray
*/
public function testCreateFromRenderArray(array $render_array, CacheableMetadata $expected) : void {
$this->assertEquals($expected, CacheableMetadata::createFromRenderArray($render_array));
}
/**
* Provides test data for createFromRenderArray().
*
* @return array
*/
public static function providerTestCreateFromRenderArray() {
$data = [];
$empty_metadata = new CacheableMetadata();
$nonempty_metadata = new CacheableMetadata();
$nonempty_metadata->setCacheContexts([
'qux',
])
->setCacheTags([
'foo:bar',
]);
$empty_render_array = [];
$nonempty_render_array = [
'#cache' => [
'contexts' => [
'qux',
],
'tags' => [
'foo:bar',
],
'max-age' => Cache::PERMANENT,
],
];
$data[] = [
$empty_render_array,
$empty_metadata,
];
$data[] = [
$nonempty_render_array,
$nonempty_metadata,
];
return $data;
}
/**
* @covers ::createFromObject
* @dataProvider providerTestCreateFromObject
*/
public function testCreateFromObject($object, CacheableMetadata $expected) : void {
$this->assertEquals($expected, CacheableMetadata::createFromObject($object));
}
/**
* Provides test data for createFromObject().
*
* @return array
*/
public static function providerTestCreateFromObject() {
$data = [];
$empty_metadata = new CacheableMetadata();
$nonempty_metadata = new CacheableMetadata();
$nonempty_metadata->setCacheContexts([
'qux',
])
->setCacheTags([
'foo:bar',
])
->setCacheMaxAge(600);
$uncacheable_metadata = new CacheableMetadata();
$uncacheable_metadata->setCacheMaxAge(0);
$empty_cacheable_object = new TestCacheableDependency([], [], Cache::PERMANENT);
$nonempty_cacheable_object = new TestCacheableDependency([
'qux',
], [
'foo:bar',
], 600);
$uncacheable_object = new \stdClass();
$data[] = [
$empty_cacheable_object,
$empty_metadata,
];
$data[] = [
$nonempty_cacheable_object,
$nonempty_metadata,
];
$data[] = [
$uncacheable_object,
$uncacheable_metadata,
];
return $data;
}
}
Members
Title Sort descending | Deprecated | Modifiers | Object type | Summary | Overrides |
---|---|---|---|---|---|
CacheableMetadataTest::providerSetCacheMaxAge | public static | function | Data provider for testSetCacheMaxAge. | ||
CacheableMetadataTest::providerTestCreateFromObject | public static | function | Provides test data for createFromObject(). | ||
CacheableMetadataTest::providerTestCreateFromRenderArray | public static | function | Provides test data for createFromRenderArray(). | ||
CacheableMetadataTest::providerTestMerge | public static | function | Provides test data for testMerge(). | ||
CacheableMetadataTest::testAddCacheableDependency | public | function | @covers ::addCacheableDependency @dataProvider providerTestMerge |
||
CacheableMetadataTest::testAddCacheTags | public | function | This delegates to Cache::mergeTags(), so just a basic test. | ||
CacheableMetadataTest::testCreateFromObject | public | function | @covers ::createFromObject @dataProvider providerTestCreateFromObject |
||
CacheableMetadataTest::testCreateFromRenderArray | public | function | @covers ::createFromRenderArray @dataProvider providerTestCreateFromRenderArray |
||
CacheableMetadataTest::testMerge | public | function | @covers ::merge @dataProvider providerTestMerge |
||
CacheableMetadataTest::testSetCacheMaxAge | public | function | Tests valid and invalid values as max age. | ||
PhpUnitWarnings::$deprecationWarnings | private static | property | Deprecation warnings from PHPUnit to raise with @trigger_error(). | ||
PhpUnitWarnings::addWarning | public | function | Converts PHPUnit deprecation warnings to E_USER_DEPRECATED. | ||
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. | ||
RandomGeneratorTrait::randomStringValidate | Deprecated | public | function | Callback for random string validation. | |
UnitTestCase::$root | protected | property | The app root. | 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::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | ||
UnitTestCase::setUp | protected | function | 357 | ||
UnitTestCase::setUpBeforeClass | public static | function | |||
UnitTestCase::__get | public | function |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.