3 namespace Drupal\Tests\Core\Plugin;
5 use Drupal\Component\Plugin\Definition\PluginDefinitionInterface;
6 use Drupal\Component\Plugin\DependentPluginInterface;
7 use Drupal\Component\Plugin\PluginInspectionInterface;
8 use Drupal\Core\Plugin\Definition\DependentPluginDefinitionInterface;
9 use Drupal\Core\Plugin\PluginDependencyTrait;
10 use Drupal\Tests\UnitTestCase;
11 use Prophecy\Prophecy\ProphecyInterface;
14 * @coversDefaultClass \Drupal\Core\Plugin\PluginDependencyTrait
17 class PluginDependencyTraitTest extends UnitTestCase {
20 * @covers ::getPluginDependencies
22 * @dataProvider providerTestPluginDependencies
24 public function testGetPluginDependencies(ProphecyInterface $plugin, $definition, array $expected) {
25 $test_class = new TestPluginDependency();
27 $plugin->getPluginDefinition()->willReturn($definition);
29 $actual = $test_class->getPluginDependencies($plugin->reveal());
30 $this->assertEquals($expected, $actual);
31 $this->assertEmpty($test_class->getDependencies());
35 * @covers ::calculatePluginDependencies
37 * @dataProvider providerTestPluginDependencies
39 * @param \Prophecy\Prophecy\ProphecyInterface $plugin
40 * A prophecy of a plugin instance.
41 * @param mixed $definition
42 * A plugin definition.
43 * @param array $expected
44 * The expected dependencies.
46 public function testCalculatePluginDependencies(ProphecyInterface $plugin, $definition, array $expected) {
47 $test_class = new TestPluginDependency();
49 $plugin->getPluginDefinition()->willReturn($definition);
51 $test_class->calculatePluginDependencies($plugin->reveal());
52 $this->assertEquals($expected, $test_class->getDependencies());
56 * Provides test data for plugin dependencies.
58 public function providerTestPluginDependencies() {
61 $plugin = $this->prophesize(PluginInspectionInterface::class);
63 $dependent_plugin = $this->prophesize(PluginInspectionInterface::class)->willImplement(DependentPluginInterface::class);
64 $dependent_plugin->calculateDependencies()->willReturn([
65 'module' => ['test_module2'],
68 $data['dependent_plugin'] = [
70 ['provider' => 'test_module1'],
79 $data['array_with_config_dependencies'] = [
82 'provider' => 'test_module1',
83 'config_dependencies' => [
84 'module' => ['test_module2'],
95 $definition = $this->prophesize(PluginDefinitionInterface::class);
96 $definition->getProvider()->willReturn('test_module1');
97 $data['object_definition'] = [
99 $definition->reveal(),
107 $dependent_definition = $this->prophesize(PluginDefinitionInterface::class)->willImplement(DependentPluginDefinitionInterface::class);
108 $dependent_definition->getProvider()->willReturn('test_module1');
109 $dependent_definition->getConfigDependencies()->willReturn(['module' => ['test_module2']]);
110 $data['dependent_object_definition'] = [
112 $dependent_definition->reveal(),
125 class TestPluginDependency {
127 use PluginDependencyTrait {
128 calculatePluginDependencies as public;
129 getPluginDependencies as public;
135 public function getDependencies() {
136 return $this->dependencies;