3 namespace Drupal\Tests\Component\Plugin;
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;
14 * @coversDefaultClass \Drupal\Component\Plugin\Factory\DefaultFactory
17 class DefaultFactoryTest extends UnitTestCase {
20 * Tests getPluginClass() with a valid array plugin definition.
22 * @covers ::getPluginClass
24 public function testGetPluginClassWithValidArrayPluginDefinition() {
25 $plugin_class = Cherry::class;
26 $class = DefaultFactory::getPluginClass('cherry', ['class' => $plugin_class]);
28 $this->assertEquals($plugin_class, $class);
32 * Tests getPluginClass() with a valid object plugin definition.
34 * @covers ::getPluginClass
36 public function testGetPluginClassWithValidObjectPluginDefinition() {
37 $plugin_class = Cherry::class;
38 $plugin_definition = $this->getMock(PluginDefinitionInterface::class);
39 $plugin_definition->expects($this->atLeastOnce())
41 ->willReturn($plugin_class);
42 $class = DefaultFactory::getPluginClass('cherry', $plugin_definition);
44 $this->assertEquals($plugin_class, $class);
48 * Tests getPluginClass() with a missing class definition.
50 * @covers ::getPluginClass
52 public function testGetPluginClassWithMissingClassWithArrayPluginDefinition() {
53 $this->setExpectedException(PluginException::class, 'The plugin (cherry) did not specify an instance class.');
54 DefaultFactory::getPluginClass('cherry', []);
58 * Tests getPluginClass() with a missing class definition.
60 * @covers ::getPluginClass
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);
69 * Tests getPluginClass() with a not existing class definition.
71 * @covers ::getPluginClass
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']);
79 * Tests getPluginClass() with a not existing class definition.
81 * @covers ::getPluginClass
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())
88 ->willReturn($plugin_class);
89 $this->setExpectedException(PluginException::class);
90 DefaultFactory::getPluginClass('kiwifruit', $plugin_definition);
94 * Tests getPluginClass() with a required interface.
96 * @covers ::getPluginClass
98 public function testGetPluginClassWithInterfaceWithArrayPluginDefinition() {
99 $plugin_class = Cherry::class;
100 $class = DefaultFactory::getPluginClass('cherry', ['class' => $plugin_class], FruitInterface::class);
102 $this->assertEquals($plugin_class, $class);
106 * Tests getPluginClass() with a required interface.
108 * @covers ::getPluginClass
110 public function testGetPluginClassWithInterfaceWithObjectPluginDefinition() {
111 $plugin_class = Cherry::class;
112 $plugin_definition = $this->getMock(PluginDefinitionInterface::class);
113 $plugin_definition->expects($this->atLeastOnce())
115 ->willReturn($plugin_class);
116 $class = DefaultFactory::getPluginClass('cherry', $plugin_definition, FruitInterface::class);
118 $this->assertEquals($plugin_class, $class);
122 * Tests getPluginClass() with a required interface but no implementation.
124 * @covers ::getPluginClass
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);
133 * Tests getPluginClass() with a required interface but no implementation.
135 * @covers ::getPluginClass
137 public function testGetPluginClassWithInterfaceAndInvalidClassWithObjectPluginDefinition() {
138 $plugin_class = Kale::class;
139 $plugin_definition = $this->getMock(PluginDefinitionInterface::class);
140 $plugin_definition->expects($this->atLeastOnce())
142 ->willReturn($plugin_class);
143 $this->setExpectedException(PluginException::class);
144 DefaultFactory::getPluginClass('cherry', $plugin_definition, FruitInterface::class);