f76fc672fe53649d0e12e40a3ad862e89d8b9958
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Plugin / PluginDependencyTraitTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Plugin;
4
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;
12
13 /**
14  * @coversDefaultClass \Drupal\Core\Plugin\PluginDependencyTrait
15  * @group Plugin
16  */
17 class PluginDependencyTraitTest extends UnitTestCase {
18
19   /**
20    * @covers ::getPluginDependencies
21    *
22    * @dataProvider providerTestPluginDependencies
23    */
24   public function testGetPluginDependencies(ProphecyInterface $plugin, $definition, array $expected) {
25     $test_class = new TestPluginDependency();
26
27     $plugin->getPluginDefinition()->willReturn($definition);
28
29     $actual = $test_class->getPluginDependencies($plugin->reveal());
30     $this->assertEquals($expected, $actual);
31     $this->assertEmpty($test_class->getDependencies());
32   }
33
34   /**
35    * @covers ::calculatePluginDependencies
36    *
37    * @dataProvider providerTestPluginDependencies
38    *
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.
45    */
46   public function testCalculatePluginDependencies(ProphecyInterface $plugin, $definition, array $expected) {
47     $test_class = new TestPluginDependency();
48
49     $plugin->getPluginDefinition()->willReturn($definition);
50
51     $test_class->calculatePluginDependencies($plugin->reveal());
52     $this->assertEquals($expected, $test_class->getDependencies());
53   }
54
55   /**
56    * Provides test data for plugin dependencies.
57    */
58   public function providerTestPluginDependencies() {
59     $data = [];
60
61     $plugin = $this->prophesize(PluginInspectionInterface::class);
62
63     $dependent_plugin = $this->prophesize(PluginInspectionInterface::class)->willImplement(DependentPluginInterface::class);
64     $dependent_plugin->calculateDependencies()->willReturn([
65       'module' => ['test_module2'],
66     ]);
67
68     $data['dependent_plugin'] = [
69       $dependent_plugin,
70       ['provider' => 'test_module1'],
71       [
72         'module' => [
73           'test_module1',
74           'test_module2',
75         ],
76       ],
77     ];
78
79     $data['array_with_config_dependencies'] = [
80       $plugin,
81       [
82         'provider' => 'test_module1',
83         'config_dependencies' => [
84           'module' => ['test_module2'],
85         ],
86       ],
87       [
88         'module' => [
89           'test_module1',
90           'test_module2',
91         ],
92       ],
93     ];
94
95     $definition = $this->prophesize(PluginDefinitionInterface::class);
96     $definition->getProvider()->willReturn('test_module1');
97     $data['object_definition'] = [
98       $plugin,
99       $definition->reveal(),
100       [
101         'module' => [
102           'test_module1',
103         ],
104       ],
105     ];
106
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'] = [
111       $plugin,
112       $dependent_definition->reveal(),
113       [
114         'module' => [
115           'test_module1',
116           'test_module2',
117         ],
118       ],
119     ];
120     return $data;
121   }
122
123 }
124
125 class TestPluginDependency {
126
127   use PluginDependencyTrait {
128     calculatePluginDependencies as public;
129     getPluginDependencies as public;
130   }
131
132   /**
133    * @return array[]
134    */
135   public function getDependencies() {
136     return $this->dependencies;
137   }
138
139 }