class BlocacheMetadataUnitTest in Blocache (Block Cache Control) 8
Tests for BlocacheMetadata class.
@group blocache
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\blocache\Unit\BlocacheMetadataUnitTest uses BlockCreationTrait
Expanded class hierarchy of BlocacheMetadataUnitTest
File
- tests/
src/ Unit/ BlocacheMetadataUnitTest.php, line 15
Namespace
Drupal\Tests\blocache\UnitView source
class BlocacheMetadataUnitTest extends UnitTestCase {
use BlockCreationTrait;
/**
* The blocache.metadata service.
*
* @var \Drupal\blocache\BlocacheMetadata
*/
protected $blocacheMetadata;
/**
* The block entity.
*
* @var \Drupal\block\BlockInterface
*/
protected $block;
/**
* Cache metadata for the tests.
*
* @var array
*/
protected $metadata;
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$this->blocacheMetadata = new BlocacheMetadata();
$plugin = $this
->getMockBuilder('Drupal\\Core\\Block\\BlockBase')
->disableOriginalConstructor()
->getMock();
$plugin
->expects($this
->any())
->method('getMachineNameSuggestion')
->will($this
->returnValue($this
->randomMachineName(8)));
$block = $this
->getMockBuilder('Drupal\\block\\Entity\\Block')
->disableOriginalConstructor()
->getMock();
$block
->expects($this
->any())
->method('getPlugin')
->will($this
->returnValue($plugin));
$this->block = $block;
$this->blocacheMetadata
->setBlock($this->block);
$this->metadata = [
BlocacheMetadata::METADATA_MAX_AGE => 600,
BlocacheMetadata::METADATA_CONTEXTS => [
'user',
'language',
],
BlocacheMetadata::METADATA_TAGS => [
'user:1',
'language:en',
],
];
}
/**
* @covers Drupal\blocache\BlocacheMetadata::setBlock
*/
public function testSetBlock() {
$this
->assertInstanceOf(BlockInterface::class, $this->blocacheMetadata
->getBlock());
}
/**
* @covers Drupal\blocache\BlocacheMetadata::setOverrides
*/
public function testSetOverrides() {
$max_age = $this->metadata[BlocacheMetadata::METADATA_MAX_AGE];
$contexts = $this->metadata[BlocacheMetadata::METADATA_CONTEXTS];
$tags = $this->metadata[BlocacheMetadata::METADATA_TAGS];
$this
->assertEquals(TRUE, $this->blocacheMetadata
->setOverrides($max_age, $contexts, $tags));
$this
->assertEquals(TRUE, $this->blocacheMetadata
->isOverridden());
$this
->assertEquals($this->metadata, $this->blocacheMetadata
->getOverrides());
}
/**
* @covers Drupal\blocache\BlocacheMetadata::unsetOverrides
*/
public function testUnsetOverrides() {
$max_age = $this->metadata[BlocacheMetadata::METADATA_MAX_AGE];
$contexts = $this->metadata[BlocacheMetadata::METADATA_CONTEXTS];
$tags = $this->metadata[BlocacheMetadata::METADATA_TAGS];
$this
->assertEquals(TRUE, $this->blocacheMetadata
->setOverrides($max_age, $contexts, $tags));
$this
->assertEquals(TRUE, $this->blocacheMetadata
->unsetOverrides());
$this
->assertEquals([], $this->blocacheMetadata
->getOverrides());
}
/**
* {@inheritdoc}
*/
public function tearDown() {
unset($this->blocacheMetadata);
unset($this->block);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BlocacheMetadataUnitTest:: |
protected | property | The blocache.metadata service. | |
BlocacheMetadataUnitTest:: |
protected | property | The block entity. | |
BlocacheMetadataUnitTest:: |
protected | property | Cache metadata for the tests. | |
BlocacheMetadataUnitTest:: |
public | function |
Overrides UnitTestCase:: |
|
BlocacheMetadataUnitTest:: |
public | function | ||
BlocacheMetadataUnitTest:: |
public | function | @covers Drupal\blocache\BlocacheMetadata::setBlock | |
BlocacheMetadataUnitTest:: |
public | function | @covers Drupal\blocache\BlocacheMetadata::setOverrides | |
BlocacheMetadataUnitTest:: |
public | function | @covers Drupal\blocache\BlocacheMetadata::unsetOverrides | |
BlockCreationTrait:: |
protected | function | Creates a block instance based on default settings. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |