diff --git a/core/tests/Drupal/Tests/Component/Plugin/DefaultFactoryTest.php b/core/tests/Drupal/Tests/Component/Plugin/DefaultFactoryTest.php index 0d12fac..ed53798 100644 --- a/core/tests/Drupal/Tests/Component/Plugin/DefaultFactoryTest.php +++ b/core/tests/Drupal/Tests/Component/Plugin/DefaultFactoryTest.php @@ -7,7 +7,74 @@ namespace Drupal\Tests\Component\Plugin; -class DefaultFactoryTest { +use Drupal\Component\Plugin\Factory\DefaultFactory; +use Drupal\Tests\UnitTestCase; + +/** + * @coversDefaultClass \Drupal\Component\Plugin\DefaultFactory + */ +class DefaultFactoryTest extends UnitTestCase { + + /** + * Tests getPluginClass() with a valid plugin. + * + * @covers ::getPluginClass + */ + public function testGetPluginClassWithValidPlugin() { + $plugin_class = 'Drupal\plugin_test\Plugin\plugin_test\fruit\Cherry'; + $class = DefaultFactory::getPluginClass('cherry', ['class' => $plugin_class]); + + $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() { + DefaultFactory::getPluginClass('cherry', []); + } + + /** + * 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() { + DefaultFactory::getPluginClass('kiwifruit', ['class' => '\Drupal\plugin_test\Plugin\plugin_test\fruit\Kiwifruit']); + } + + /** + * 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'); + + $this->assertEquals($plugin_class, $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) in core should 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'); + } } diff --git a/core/tests/Drupal/Tests/Core/Plugin/TestPluginManager.php b/core/tests/Drupal/Tests/Core/Plugin/TestPluginManager.php index 407a916..c1c2d7f 100644 --- a/core/tests/Drupal/Tests/Core/Plugin/TestPluginManager.php +++ b/core/tests/Drupal/Tests/Core/Plugin/TestPluginManager.php @@ -38,7 +38,7 @@ public function __construct(\Traversable $namespaces, array $definitions, Module // discovery implementation, but StaticDiscovery lets us add some simple // mock plugins for unit testing. $this->discovery = new StaticDiscovery(); - $this->factory = new DefaultFactory($this->discovery); + $this->factory = new DefaultFactory($this->discovery, $interface); // Add the static definitions. foreach ($definitions as $key => $definition) { @@ -50,7 +50,6 @@ public function __construct(\Traversable $namespaces, array $definitions, Module if ($alter_hook) { $this->alterInfo($alter_hook); } - $this->interface = $interface; } }