PluginHierarchyTraitTest.php in Plugin 8.2
File
tests/src/Unit/PluginHierarchyTraitTest.php
View source
<?php
namespace Drupal\Tests\plugin\Unit;
use Drupal\plugin\PluginDefinition\PluginHierarchyDefinitionInterface;
use Drupal\plugin\PluginDefinition\PluginLabelDefinitionInterface;
use Drupal\plugin\PluginDiscovery\TypedDiscoveryInterface;
use Drupal\plugin\PluginHierarchyTrait;
use Drupal\Tests\UnitTestCase;
class PluginHierarchyTraitTest extends UnitTestCase {
protected $sut;
protected function setUp() : void {
parent::setUp();
$this->sut = $this
->getMockForTrait(PluginHierarchyTrait::class);
}
public function testBuildPluginHierarchy() {
$plugin_definition_id_a = $this
->randomMachineName();
$plugin_definition_label_a = 'foo';
$plugin_definition_a = $this
->createMock(PluginLabelDefinitionInterface::class);
$plugin_definition_a
->expects($this
->any())
->method('getLabel')
->willReturn($plugin_definition_label_a);
$plugin_definition_id_b = $this
->randomMachineName();
$plugin_definition_label_b = 'Bar';
$plugin_definition_b = $this
->createMock(PluginLabelDefinitionInterface::class);
$plugin_definition_b
->expects($this
->any())
->method('getLabel')
->willReturn($plugin_definition_label_b);
$plugin_definition_id_c = $this
->randomMachineName();
$plugin_definition_c = $this
->createMock(PluginHierarchyDefinitionInterface::class);
$plugin_definition_c
->expects($this
->any())
->method('getId')
->willReturn($plugin_definition_id_c);
$plugin_definition_c
->expects($this
->any())
->method('getParentId')
->willReturn($plugin_definition_id_a);
$plugin_discovery = $this
->createMock(TypedDiscoveryInterface::class);
$plugin_discovery
->expects($this
->any())
->method('getDefinitions')
->willReturn([
$plugin_definition_id_a => $plugin_definition_a,
$plugin_definition_id_b => $plugin_definition_b,
$plugin_definition_id_c => $plugin_definition_c,
]);
$method = new \ReflectionMethod($this->sut, 'buildPluginHierarchy');
$method
->setAccessible(TRUE);
$hierarchy = @$method
->invokeArgs($this->sut, array(
$plugin_discovery,
));
$expected = [
$plugin_definition_id_b => [],
$plugin_definition_id_a => [
$plugin_definition_id_c => [],
],
];
$this
->assertSame($expected, $hierarchy);
}
}