Updated to Drupal 8.5. Core Media not yet in use.
[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 ::calculatePluginDependencies
21    *
22    * @dataProvider providerTestCalculatePluginDependencies
23    *
24    * @param \Prophecy\Prophecy\ProphecyInterface $plugin
25    *   A prophecy of a plugin instance.
26    * @param mixed $definition
27    *   A plugin definition.
28    * @param array $expected
29    *   The expected dependencies.
30    */
31   public function testCalculatePluginDependencies(ProphecyInterface $plugin, $definition, array $expected) {
32     $test_class = new TestPluginDependency();
33
34     $plugin->getPluginDefinition()->willReturn($definition);
35
36     $test_class->calculatePluginDependencies($plugin->reveal());
37     $this->assertEquals($expected, $test_class->getDependencies());
38   }
39
40   /**
41    * Provides test data for ::testCalculatePluginDependencies().
42    */
43   public function providerTestCalculatePluginDependencies() {
44     $data = [];
45
46     $plugin = $this->prophesize(PluginInspectionInterface::class);
47
48     $dependent_plugin = $this->prophesize(PluginInspectionInterface::class)->willImplement(DependentPluginInterface::class);
49     $dependent_plugin->calculateDependencies()->willReturn([
50       'module' => ['test_module2'],
51     ]);
52
53     $data['dependent_plugin'] = [
54       $dependent_plugin,
55       ['provider' => 'test_module1'],
56       [
57         'module' => [
58           'test_module1',
59           'test_module2',
60         ],
61       ],
62     ];
63
64     $data['array_with_config_dependencies'] = [
65       $plugin,
66       [
67         'provider' => 'test_module1',
68         'config_dependencies' => [
69           'module' => ['test_module2'],
70         ],
71       ],
72       [
73         'module' => [
74           'test_module1',
75           'test_module2',
76         ],
77       ],
78     ];
79
80     $definition = $this->prophesize(PluginDefinitionInterface::class);
81     $definition->getProvider()->willReturn('test_module1');
82     $data['object_definition'] = [
83       $plugin,
84       $definition->reveal(),
85       [
86         'module' => [
87           'test_module1',
88         ],
89       ],
90     ];
91
92     $dependent_definition = $this->prophesize(PluginDefinitionInterface::class)->willImplement(DependentPluginDefinitionInterface::class);
93     $dependent_definition->getProvider()->willReturn('test_module1');
94     $dependent_definition->getConfigDependencies()->willReturn(['module' => ['test_module2']]);
95     $data['dependent_object_definition'] = [
96       $plugin,
97       $dependent_definition->reveal(),
98       [
99         'module' => [
100           'test_module1',
101           'test_module2',
102         ],
103       ],
104     ];
105     return $data;
106   }
107
108 }
109
110 class TestPluginDependency {
111
112   use PluginDependencyTrait {
113     calculatePluginDependencies as public;
114   }
115
116   /**
117    * @return array[]
118    */
119   public function getDependencies() {
120     return $this->dependencies;
121   }
122
123 }