diff --git a/autoload.php b/autoload.php index caedb48..7c5bb25 100644 --- a/autoload.php +++ b/autoload.php @@ -1,19 +1,3 @@ getClass(); } else { - throw new PluginException($missing_class_message); + $plugin_definition_type = is_object($plugin_definition) ? get_class($plugin_definition) : gettype($plugin_definition); + throw new PluginException(sprintf('% can only handle plugin definitions that are arrays or that implement %s, but %s given.', __CLASS__, PluginDefinitionInterface::class, $plugin_definition_type)); } if (!class_exists($class)) { diff --git a/core/tests/Drupal/Tests/Component/Plugin/DefaultFactoryTest.php b/core/tests/Drupal/Tests/Component/Plugin/DefaultFactoryTest.php index a23b690..d02deee 100644 --- a/core/tests/Drupal/Tests/Component/Plugin/DefaultFactoryTest.php +++ b/core/tests/Drupal/Tests/Component/Plugin/DefaultFactoryTest.php @@ -7,7 +7,11 @@ namespace Drupal\Tests\Component\Plugin; +use Drupal\Component\Plugin\Definition\PluginDefinitionInterface; use Drupal\Component\Plugin\Factory\DefaultFactory; +use Drupal\plugin_test\Plugin\plugin_test\fruit\Cherry; +use Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface; +use Drupal\plugin_test\Plugin\plugin_test\fruit\Kale; use Drupal\Tests\UnitTestCase; /** @@ -17,41 +21,110 @@ class DefaultFactoryTest extends UnitTestCase { /** - * Tests getPluginClass() with a valid plugin. + * Tests getPluginClass() with a valid array plugin definition. + * + * @covers ::getPluginClass */ - public function testGetPluginClassWithValidPlugin() { - $plugin_class = 'Drupal\plugin_test\Plugin\plugin_test\fruit\Cherry'; + public function testGetPluginClassWithValidArrayPluginDefinition() { + $plugin_class = Cherry::class; $class = DefaultFactory::getPluginClass('cherry', ['class' => $plugin_class]); $this->assertEquals($plugin_class, $class); } /** + * Tests getPluginClass() with a valid object plugin definition. + * + * @covers ::getPluginClass + */ + public function testGetPluginClassWithValidObjectPluginDefinition() { + $plugin_class = Cherry::class; + $plugin_definition = $this->getMock(PluginDefinitionInterface::class); + $plugin_definition->expects($this->atLeastOnce()) + ->method('getClass') + ->willReturn($plugin_class); + $class = DefaultFactory::getPluginClass('cherry', $plugin_definition); + + $this->assertEquals($plugin_class, $class); + } + + /** * Tests getPluginClass() with a missing class definition. * + * @covers ::getPluginClass + * * @expectedException \Drupal\Component\Plugin\Exception\PluginException * @expectedExceptionMessage The plugin (cherry) did not specify an instance class. */ - public function testGetPluginClassWithMissingClass() { + public function testGetPluginClassWithMissingClassWithArrayPluginDefinition() { DefaultFactory::getPluginClass('cherry', []); } /** + * Tests getPluginClass() with a missing class definition. + * + * @covers ::getPluginClass + * + * @expectedException \Drupal\Component\Plugin\Exception\PluginException + * @expectedExceptionMessage The plugin (cherry) did not specify an instance class. + */ + public function testGetPluginClassWithMissingClassWithObjectPluginDefinition() { + $plugin_definition = $this->getMock(PluginDefinitionInterface::class); + DefaultFactory::getPluginClass('cherry', $plugin_definition); + } + + /** * Tests getPluginClass() with a not existing class definition. * + * @covers ::getPluginClass + * * @expectedException \Drupal\Component\Plugin\Exception\PluginException * @expectedExceptionMessage Plugin (kiwifruit) instance class "\Drupal\plugin_test\Plugin\plugin_test\fruit\Kiwifruit" does not exist. */ - public function testGetPluginClassWithNotExistingClass() { + public function testGetPluginClassWithNotExistingClassWithArrayPluginDefinition() { DefaultFactory::getPluginClass('kiwifruit', ['class' => '\Drupal\plugin_test\Plugin\plugin_test\fruit\Kiwifruit']); } /** + * Tests getPluginClass() with a not existing class definition. + * + * @covers ::getPluginClass + * + * @expectedException \Drupal\Component\Plugin\Exception\PluginException + */ + public function testGetPluginClassWithNotExistingClassWithObjectPluginDefinition() { + $plugin_class = '\Drupal\plugin_test\Plugin\plugin_test\fruit\Kiwifruit'; + $plugin_definition = $this->getMock(PluginDefinitionInterface::class); + $plugin_definition->expects($this->atLeastOnce()) + ->method('getClass') + ->willReturn($plugin_class); + DefaultFactory::getPluginClass('kiwifruit', $plugin_definition); + } + + /** + * Tests getPluginClass() with a required interface. + * + * @covers ::getPluginClass + */ + public function testGetPluginClassWithInterfaceWithArrayPluginDefinition() { + $plugin_class = Cherry::class; + $class = DefaultFactory::getPluginClass('cherry', ['class' => $plugin_class], FruitInterface::class); + + $this->assertEquals($plugin_class, $class); + } + + /** * Tests getPluginClass() with a required interface. + * + * @covers ::getPluginClass */ - public function testGetPluginClassWithInterface() { - $plugin_class = 'Drupal\plugin_test\Plugin\plugin_test\fruit\Cherry'; - $class = DefaultFactory::getPluginClass('cherry', ['class' => $plugin_class], '\Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface'); + public function testGetPluginClassWithInterfaceWithObjectPluginDefinition() { + $plugin_class = Cherry::class; + $plugin_definition = $this->getMock(PluginDefinitionInterface::class); + $plugin_definition->expects($this->atLeastOnce()) + ->method('getClass') + ->willReturn($plugin_class); + $class = DefaultFactory::getPluginClass('cherry', $plugin_definition, FruitInterface::class); $this->assertEquals($plugin_class, $class); } @@ -59,12 +132,30 @@ public function testGetPluginClassWithInterface() { /** * Tests getPluginClass() with a required interface but no implementation. * + * @covers ::getPluginClass + * + * @expectedException \Drupal\Component\Plugin\Exception\PluginException + * @expectedExceptionMessage Plugin "cherry" (Drupal\plugin_test\Plugin\plugin_test\fruit\Kale) must implement interface Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface. + */ + public function testGetPluginClassWithInterfaceAndInvalidClassWithArrayPluginDefinition() { + $plugin_class = Kale::class; + DefaultFactory::getPluginClass('cherry', ['class' => $plugin_class, 'provider' => 'core'], FruitInterface::class); + } + + /** + * Tests getPluginClass() with a required interface but no implementation. + * + * @covers ::getPluginClass + * * @expectedException \Drupal\Component\Plugin\Exception\PluginException - * @expectedExceptionMessage Plugin "cherry" (Drupal\plugin_test\Plugin\plugin_test\fruit\Kale) must implement interface \Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface. */ - public function testGetPluginClassWithInterfaceAndInvalidClass() { - $plugin_class = 'Drupal\plugin_test\Plugin\plugin_test\fruit\Kale'; - DefaultFactory::getPluginClass('cherry', ['class' => $plugin_class, 'provider' => 'core'], '\Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface'); + public function testGetPluginClassWithInterfaceAndInvalidClassWithObjectPluginDefinition() { + $plugin_class = Kale::class; + $plugin_definition = $this->getMock(PluginDefinitionInterface::class); + $plugin_definition->expects($this->atLeastOnce()) + ->method('getClass') + ->willReturn($plugin_class); + DefaultFactory::getPluginClass('cherry', $plugin_definition, FruitInterface::class); } }