class PluginLabelTest in Plugin 8.2
@coversDefaultClass \Drupal\plugin\Plugin\Field\FieldFormatter\PluginLabel
@group Plugin
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\plugin\Unit\Plugin\Field\FieldFormatter\PluginLabelTest
Expanded class hierarchy of PluginLabelTest
File
- tests/
src/ Unit/ Plugin/ Field/ FieldFormatter/ PluginLabelTest.php, line 20
Namespace
Drupal\Tests\plugin\Unit\Plugin\Field\FieldFormatterView source
class PluginLabelTest extends UnitTestCase {
/**
* The field definition.
*
* @var \Drupal\Core\Field\FieldDefinitionInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $fieldDefinition;
/**
* The class under test.
*
* @var \Drupal\plugin\Plugin\Field\FieldFormatter\PluginLabel
*/
protected $sut;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
$this->fieldDefinition = $this
->createMock(FieldDefinitionInterface::class);
$this->sut = new PluginLabel('plugin_label', [], $this->fieldDefinition, [], $this
->randomMachineName(), $this
->randomMachineName(), []);
}
/**
* @covers ::viewElements
*/
public function testViewElements() {
$plugin_definition_label_a = $this
->randomMachineName();
$plugin_definition_a = $this
->createMock(PluginLabelDefinitionInterface::class);
$plugin_definition_a
->expects($this
->atLeastOnce())
->method('getLabel')
->willReturn($plugin_definition_label_a);
$plugin_definition_id_b = $this
->randomMachineName();
$plugin_definition_b = $this
->createMock(PluginDefinitionInterface::class);
$plugin_definition_b
->expects($this
->atLeastOnce())
->method('getId')
->willReturn($plugin_definition_id_b);
$plugin_instance_a = $this
->createMock(PluginInspectionInterface::class);
$plugin_instance_a
->expects($this
->atLeastOnce())
->method('getPluginDefinition')
->willReturn($plugin_definition_a);
$plugin_instance_b = $this
->createMock(PluginInspectionInterface::class);
$plugin_instance_b
->expects($this
->atLeastOnce())
->method('getPluginDefinition')
->willReturn($plugin_definition_b);
$item_a = $this
->createMock(PluginCollectionItemInterface::class);
$item_a
->expects($this
->atLeastOnce())
->method('getContainedPluginInstance')
->willReturn($plugin_instance_a);
$item_b = $this
->createMock(PluginCollectionItemInterface::class);
$item_b
->expects($this
->atLeastOnce())
->method('getContainedPluginInstance')
->willReturn($plugin_instance_b);
/** @var \Drupal\plugin\Plugin\Field\FieldType\PluginCollectionItemInterface[]|\PHPUnit_Framework_MockObject_MockObject[] $items */
$items = [
$item_a,
$item_b,
];
$plugin_type = $this
->createMock(PluginTypeInterface::class);
$plugin_type
->expects($this
->atLeastOnce())
->method('ensureTypedPluginDefinition')
->willReturnArgument(0);
foreach ($items as $item) {
$item
->expects($this
->atLeastOnce())
->method('getPluginType')
->willReturn($plugin_type);
}
$iterator = new \ArrayIterator($items);
$item_list = $this
->getMockBuilder(PluginCollectionItemList::class)
->disableOriginalConstructor()
->setMethods([
'getEntity',
'getIterator',
])
->getMock();
$item_list
->expects($this
->atLeastOnce())
->method('getIterator')
->willReturn($iterator);
$expected_build = [
[
'#markup' => $plugin_definition_label_a,
],
[
'#markup' => $plugin_definition_id_b,
],
];
$langcode = 'UA';
$this
->assertSame($expected_build, $this->sut
->viewElements($item_list, $langcode));
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
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. | |
PluginLabelTest:: |
protected | property | The field definition. | |
PluginLabelTest:: |
protected | property | The class under test. | |
PluginLabelTest:: |
protected | function |
Overrides UnitTestCase:: |
|
PluginLabelTest:: |
public | function | @covers ::viewElements | |
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. |