28a48f6c1324700e1cfd1deea8bc66a887da84f1
[yaffs-website] / web / core / tests / Drupal / Tests / Component / Plugin / DefaultFactoryTest.php
1 <?php
2
3 namespace Drupal\Tests\Component\Plugin;
4
5 use Drupal\Component\Plugin\Definition\PluginDefinitionInterface;
6 use Drupal\Component\Plugin\Exception\PluginException;
7 use Drupal\Component\Plugin\Factory\DefaultFactory;
8 use Drupal\plugin_test\Plugin\plugin_test\fruit\Cherry;
9 use Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface;
10 use Drupal\plugin_test\Plugin\plugin_test\fruit\Kale;
11 use Drupal\Tests\UnitTestCase;
12
13 /**
14  * @coversDefaultClass \Drupal\Component\Plugin\Factory\DefaultFactory
15  * @group Plugin
16  */
17 class DefaultFactoryTest extends UnitTestCase {
18
19   /**
20    * Tests getPluginClass() with a valid array plugin definition.
21    *
22    * @covers ::getPluginClass
23    */
24   public function testGetPluginClassWithValidArrayPluginDefinition() {
25     $plugin_class = Cherry::class;
26     $class = DefaultFactory::getPluginClass('cherry', ['class' => $plugin_class]);
27
28     $this->assertEquals($plugin_class, $class);
29   }
30
31   /**
32    * Tests getPluginClass() with a valid object plugin definition.
33    *
34    * @covers ::getPluginClass
35    */
36   public function testGetPluginClassWithValidObjectPluginDefinition() {
37     $plugin_class = Cherry::class;
38     $plugin_definition = $this->getMock(PluginDefinitionInterface::class);
39     $plugin_definition->expects($this->atLeastOnce())
40       ->method('getClass')
41       ->willReturn($plugin_class);
42     $class = DefaultFactory::getPluginClass('cherry', $plugin_definition);
43
44     $this->assertEquals($plugin_class, $class);
45   }
46
47   /**
48    * Tests getPluginClass() with a missing class definition.
49    *
50    * @covers ::getPluginClass
51    */
52   public function testGetPluginClassWithMissingClassWithArrayPluginDefinition() {
53     $this->setExpectedException(PluginException::class, 'The plugin (cherry) did not specify an instance class.');
54     DefaultFactory::getPluginClass('cherry', []);
55   }
56
57   /**
58    * Tests getPluginClass() with a missing class definition.
59    *
60    * @covers ::getPluginClass
61    */
62   public function testGetPluginClassWithMissingClassWithObjectPluginDefinition() {
63     $plugin_definition = $this->getMock(PluginDefinitionInterface::class);
64     $this->setExpectedException(PluginException::class, 'The plugin (cherry) did not specify an instance class.');
65     DefaultFactory::getPluginClass('cherry', $plugin_definition);
66   }
67
68   /**
69    * Tests getPluginClass() with a not existing class definition.
70    *
71    * @covers ::getPluginClass
72    */
73   public function testGetPluginClassWithNotExistingClassWithArrayPluginDefinition() {
74     $this->setExpectedException(PluginException::class, 'Plugin (kiwifruit) instance class "\Drupal\plugin_test\Plugin\plugin_test\fruit\Kiwifruit" does not exist.');
75     DefaultFactory::getPluginClass('kiwifruit', ['class' => '\Drupal\plugin_test\Plugin\plugin_test\fruit\Kiwifruit']);
76   }
77
78   /**
79    * Tests getPluginClass() with a not existing class definition.
80    *
81    * @covers ::getPluginClass
82    */
83   public function testGetPluginClassWithNotExistingClassWithObjectPluginDefinition() {
84     $plugin_class = '\Drupal\plugin_test\Plugin\plugin_test\fruit\Kiwifruit';
85     $plugin_definition = $this->getMock(PluginDefinitionInterface::class);
86     $plugin_definition->expects($this->atLeastOnce())
87       ->method('getClass')
88       ->willReturn($plugin_class);
89     $this->setExpectedException(PluginException::class);
90     DefaultFactory::getPluginClass('kiwifruit', $plugin_definition);
91   }
92
93   /**
94    * Tests getPluginClass() with a required interface.
95    *
96    * @covers ::getPluginClass
97    */
98   public function testGetPluginClassWithInterfaceWithArrayPluginDefinition() {
99     $plugin_class = Cherry::class;
100     $class = DefaultFactory::getPluginClass('cherry', ['class' => $plugin_class], FruitInterface::class);
101
102     $this->assertEquals($plugin_class, $class);
103   }
104
105   /**
106    * Tests getPluginClass() with a required interface.
107    *
108    * @covers ::getPluginClass
109    */
110   public function testGetPluginClassWithInterfaceWithObjectPluginDefinition() {
111     $plugin_class = Cherry::class;
112     $plugin_definition = $this->getMock(PluginDefinitionInterface::class);
113     $plugin_definition->expects($this->atLeastOnce())
114       ->method('getClass')
115       ->willReturn($plugin_class);
116     $class = DefaultFactory::getPluginClass('cherry', $plugin_definition, FruitInterface::class);
117
118     $this->assertEquals($plugin_class, $class);
119   }
120
121   /**
122    * Tests getPluginClass() with a required interface but no implementation.
123    *
124    * @covers ::getPluginClass
125    */
126   public function testGetPluginClassWithInterfaceAndInvalidClassWithArrayPluginDefinition() {
127     $plugin_class = Kale::class;
128     $this->setExpectedException(PluginException::class, 'Plugin "cherry" (Drupal\plugin_test\Plugin\plugin_test\fruit\Kale) must implement interface Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface.');
129     DefaultFactory::getPluginClass('cherry', ['class' => $plugin_class, 'provider' => 'core'], FruitInterface::class);
130   }
131
132   /**
133    * Tests getPluginClass() with a required interface but no implementation.
134    *
135    * @covers ::getPluginClass
136    */
137   public function testGetPluginClassWithInterfaceAndInvalidClassWithObjectPluginDefinition() {
138     $plugin_class = Kale::class;
139     $plugin_definition = $this->getMock(PluginDefinitionInterface::class);
140     $plugin_definition->expects($this->atLeastOnce())
141       ->method('getClass')
142       ->willReturn($plugin_class);
143     $this->setExpectedException(PluginException::class);
144     DefaultFactory::getPluginClass('cherry', $plugin_definition, FruitInterface::class);
145   }
146
147 }