diff --git a/core/lib/Drupal/Component/Plugin/Factory/DefaultFactory.php b/core/lib/Drupal/Component/Plugin/Factory/DefaultFactory.php index 7b8bfcc..ebada0a 100644 --- a/core/lib/Drupal/Component/Plugin/Factory/DefaultFactory.php +++ b/core/lib/Drupal/Component/Plugin/Factory/DefaultFactory.php @@ -35,7 +35,7 @@ class DefaultFactory implements FactoryInterface { * * @var string */ - protected static $interface; + protected $interface; /** * Constructs a Drupal\Component\Plugin\Factory\DefaultFactory object. @@ -50,8 +50,8 @@ public function __construct(DiscoveryInterface $discovery) { * @param string * The interface the plugin should have. */ - public static function setInterface($interface) { - static::$interface = $interface; + public function setInterface($interface) { + $this->interface = $interface; } /** @@ -60,6 +60,16 @@ public static function setInterface($interface) { public function createInstance($plugin_id, array $configuration) { $plugin_definition = $this->discovery->getDefinition($plugin_id); $plugin_class = static::getPluginClass($plugin_id, $plugin_definition); + + // Validate the interface of the class. + if (isset($this->interface)) { + $interfaces = class_implements($plugin_class); + if (!isset($interfaces[$this->interface])) { + throw new PluginException(sprintf('Plugin (%s) instance class "%s" has to implement interface %s', $plugin_id, $plugin_class, $this->interface)); + } + } + + return new $plugin_class($configuration, $plugin_id, $plugin_definition); } @@ -85,14 +95,6 @@ public static function getPluginClass($plugin_id, array $plugin_definition = NUL throw new PluginException(sprintf('Plugin (%s) instance class "%s" does not exist.', $plugin_id, $class)); } - // Validate the interface of the class. - if (isset(static::$interface)) { - $interfaces = class_implements($class); - if (!isset($interfaces[static::$interface])) { - throw new PluginException(sprintf('Plugin (%s) instance class "%s" has to implement interface %s', $plugin_id, $class, static::$interface)); - } - } - return $class; } } diff --git a/core/tests/Drupal/Tests/Component/Plugin/Factory/DefaultPluginFactoryTest.php b/core/tests/Drupal/Tests/Component/Plugin/Factory/DefaultPluginFactoryTest.php index b90a4e7..f3096b9 100644 --- a/core/tests/Drupal/Tests/Component/Plugin/Factory/DefaultPluginFactoryTest.php +++ b/core/tests/Drupal/Tests/Component/Plugin/Factory/DefaultPluginFactoryTest.php @@ -17,6 +17,20 @@ */ class DefaultPluginFactoryTest extends UnitTestCase { + /** + * The mocked plugin discovery. + * + * @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface|\PHPUnit_Framework_MockObject_MockObject + */ + protected $discovery; + + /** + * The tested default factory. + * + * @var \Drupal\Component\Plugin\Factory\DefaultFactory + */ + protected $defaultFactory; + public static function getInfo() { return array( 'name' => 'Default plugin factory', @@ -26,16 +40,29 @@ public static function getInfo() { } /** + * {@inheritdoc} + */ + protected function setUp() { + $this->discovery = $this->getMock('Drupal\Component\Plugin\Discovery\DiscoveryInterface'); + $this->defaultFactory = new DefaultFactory($this->discovery); + } + + /** * Tests the getPluginClass method. * * @see \Drupal\Component\Plugin\DefaultFactory::getPluginClass * @dataProvider providerTestGetPluginClass */ public function testGetPluginClass($plugin_id, $plugin_definition) { - DefaultFactory::setInterface('Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface'); - $instance = DefaultFactory::getPluginClass($plugin_id, $plugin_definition); + $this->discovery->expects($this->once()) + ->method('getDefinition') + ->with($plugin_id) + ->will($this->returnValue($plugin_definition)); + $this->defaultFactory->setInterface('Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface'); + $instance = $this->defaultFactory->createInstance($plugin_id, array()); - $this->assertEquals($plugin_definition['class'], $instance); + $this->assertInstanceOf($plugin_definition['class'], $instance); + $this->assertInstanceOf('Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface', $instance); } /** @@ -61,8 +88,12 @@ public function providerTestGetPluginClass() { * @dataProvider providerTestGetPluginClassWithInvalidInput */ public function testGetPluginClassWithInvalidInput($plugin_id, $plugin_definition) { - DefaultFactory::setInterface('Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface'); - DefaultFactory::getPluginClass($plugin_id, $plugin_definition); + $this->discovery->expects($this->once()) + ->method('getDefinition') + ->with($plugin_id) + ->will($this->returnValue($plugin_definition)); + $this->defaultFactory->setInterface('Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface'); + $this->defaultFactory->createInstance($plugin_id, array()); } /**