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\Tests\Component\Plugin\Fixtures\vegetable\Broccoli;
9 use Drupal\Tests\Component\Plugin\Fixtures\vegetable\Corn;
10 use Drupal\Tests\Component\Plugin\Fixtures\vegetable\VegetableInterface;
11 use PHPUnit\Framework\TestCase;
14 * @coversDefaultClass \Drupal\Component\Plugin\Factory\DefaultFactory
17 class DefaultFactoryTest extends TestCase {
20 * Tests getPluginClass() with a valid array plugin definition.
22 * @covers ::getPluginClass
24 public function testGetPluginClassWithValidArrayPluginDefinition() {
25 $plugin_class = Corn::class;
26 $class = DefaultFactory::getPluginClass('corn', ['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 = Corn::class;
38 $plugin_definition = $this->getMockBuilder(PluginDefinitionInterface::class)->getMock();
39 $plugin_definition->expects($this->atLeastOnce())
41 ->willReturn($plugin_class);
42 $class = DefaultFactory::getPluginClass('corn', $plugin_definition);
44 $this->assertEquals($plugin_class, $class);
48 * Tests getPluginClass() with a missing class definition.
50 * @covers ::getPluginClass
52 public function testGetPluginClassWithMissingClassWithArrayPluginDefinition() {
53 if (method_exists($this, 'expectException')) {
54 $this->expectException(PluginException::class);
55 $this->expectExceptionMessage('The plugin (corn) did not specify an instance class.');
58 $this->setExpectedException(PluginException::class, 'The plugin (corn) did not specify an instance class.');
60 DefaultFactory::getPluginClass('corn', []);
64 * Tests getPluginClass() with a missing class definition.
66 * @covers ::getPluginClass
68 public function testGetPluginClassWithMissingClassWithObjectPluginDefinition() {
69 $plugin_definition = $this->getMockBuilder(PluginDefinitionInterface::class)->getMock();
70 if (method_exists($this, 'expectException')) {
71 $this->expectException(PluginException::class);
72 $this->expectExceptionMessage('The plugin (corn) did not specify an instance class.');
75 $this->setExpectedException(PluginException::class, 'The plugin (corn) did not specify an instance class.');
77 DefaultFactory::getPluginClass('corn', $plugin_definition);
81 * Tests getPluginClass() with a not existing class definition.
83 * @covers ::getPluginClass
85 public function testGetPluginClassWithNotExistingClassWithArrayPluginDefinition() {
86 if (method_exists($this, 'expectException')) {
87 $this->expectException(PluginException::class);
88 $this->expectExceptionMessage('Plugin (carrot) instance class "Drupal\Tests\Component\Plugin\Fixtures\vegetable\Carrot" does not exist.');
91 $this->setExpectedException(PluginException::class, 'Plugin (carrot) instance class "Drupal\Tests\Component\Plugin\Fixtures\vegetable\Carrot" does not exist.');
93 DefaultFactory::getPluginClass('carrot', ['class' => 'Drupal\Tests\Component\Plugin\Fixtures\vegetable\Carrot']);
97 * Tests getPluginClass() with a not existing class definition.
99 * @covers ::getPluginClass
101 public function testGetPluginClassWithNotExistingClassWithObjectPluginDefinition() {
102 $plugin_class = 'Drupal\Tests\Component\Plugin\Fixtures\vegetable\Carrot';
103 $plugin_definition = $this->getMockBuilder(PluginDefinitionInterface::class)->getMock();
104 $plugin_definition->expects($this->atLeastOnce())
106 ->willReturn($plugin_class);
107 if (method_exists($this, 'expectException')) {
108 $this->expectException(PluginException::class);
111 $this->setExpectedException(PluginException::class);
113 DefaultFactory::getPluginClass('carrot', $plugin_definition);
117 * Tests getPluginClass() with a required interface.
119 * @covers ::getPluginClass
121 public function testGetPluginClassWithInterfaceWithArrayPluginDefinition() {
122 $plugin_class = Corn::class;
123 $class = DefaultFactory::getPluginClass('corn', ['class' => $plugin_class], VegetableInterface::class);
125 $this->assertEquals($plugin_class, $class);
129 * Tests getPluginClass() with a required interface.
131 * @covers ::getPluginClass
133 public function testGetPluginClassWithInterfaceWithObjectPluginDefinition() {
134 $plugin_class = Corn::class;
135 $plugin_definition = $this->getMockBuilder(PluginDefinitionInterface::class)->getMock();
136 $plugin_definition->expects($this->atLeastOnce())
138 ->willReturn($plugin_class);
139 $class = DefaultFactory::getPluginClass('corn', $plugin_definition, VegetableInterface::class);
141 $this->assertEquals($plugin_class, $class);
145 * Tests getPluginClass() with a required interface but no implementation.
147 * @covers ::getPluginClass
149 public function testGetPluginClassWithInterfaceAndInvalidClassWithArrayPluginDefinition() {
150 if (method_exists($this, 'expectException')) {
151 $this->expectException(PluginException::class);
152 $this->expectExceptionMessage('Plugin "corn" (Drupal\Tests\Component\Plugin\Fixtures\vegetable\Broccoli) must implement interface Drupal\Tests\Component\Plugin\Fixtures\vegetable\VegetableInterface.');
155 $this->setExpectedException(PluginException::class, 'Plugin "corn" (Drupal\Tests\Component\Plugin\Fixtures\vegetable\Broccoli) must implement interface Drupal\Tests\Component\Plugin\Fixtures\vegetable\VegetableInterface.');
157 DefaultFactory::getPluginClass('corn', ['class' => Broccoli::class], VegetableInterface::class);
161 * Tests getPluginClass() with a required interface but no implementation.
163 * @covers ::getPluginClass
165 public function testGetPluginClassWithInterfaceAndInvalidClassWithObjectPluginDefinition() {
166 $plugin_class = Broccoli::class;
167 $plugin_definition = $this->getMockBuilder(PluginDefinitionInterface::class)->getMock();
168 $plugin_definition->expects($this->atLeastOnce())
170 ->willReturn($plugin_class);
171 if (method_exists($this, 'expectException')) {
172 $this->expectException(PluginException::class);
175 $this->setExpectedException(PluginException::class);
177 DefaultFactory::getPluginClass('corn', $plugin_definition, VegetableInterface::class);