Commit 56554f5d authored by alexpott's avatar alexpott

Issue #2625554 by Xano, googletorp, longwave, borisson_, dawehner,...

Issue #2625554 by Xano, googletorp, longwave, borisson_, dawehner, tim.plunkett, alexpott, twistor: getInstance() fatals on almost all plugin managers
parent 12d8100f
......@@ -29,7 +29,7 @@ abstract class PluginManagerBase implements PluginManagerInterface {
/**
* The object that returns the preconfigured plugin instance appropriate for a particular runtime condition.
*
* @var \Drupal\Component\Plugin\Mapper\MapperInterface
* @var \Drupal\Component\Plugin\Mapper\MapperInterface|null
*/
protected $mapper;
......@@ -104,6 +104,9 @@ protected function handlePluginNotFound($plugin_id, array $configuration) {
* {@inheritdoc}
*/
public function getInstance(array $options) {
if (!$this->mapper) {
throw new \BadMethodCallException(sprintf('%s does not support this method unless %s::$mapper is set.', static::class, static::class));
}
return $this->mapper->getInstance($options);
}
......
......@@ -3,6 +3,8 @@
namespace Drupal\Tests\Component\Plugin;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Component\Plugin\Mapper\MapperInterface;
use Drupal\Component\Plugin\PluginManagerBase;
use PHPUnit\Framework\TestCase;
/**
......@@ -90,4 +92,43 @@ public function testCreateInstanceFallback() {
$this->assertEquals($configuration_array, $fallback_result['configuration']);
}
/**
* @covers ::getInstance
*/
public function testGetInstance() {
$options = [
'foo' => 'F00',
'bar' => 'bAr',
];
$instance = new \stdClass();
$mapper = $this->prophesize(MapperInterface::class);
$mapper->getInstance($options)
->shouldBeCalledTimes(1)
->willReturn($instance);
$manager = new StubPluginManagerBaseWithMapper($mapper->reveal());
$this->assertEquals($instance, $manager->getInstance($options));
}
/**
* @covers ::getInstance
*/
public function testGetInstanceWithoutMapperShouldThrowException() {
$options = [
'foo' => 'F00',
'bar' => 'bAr',
];
/** @var \Drupal\Component\Plugin\PluginManagerBase $manager */
$manager = $this->getMockBuilder(PluginManagerBase::class)
->getMockForAbstractClass();
// Set the expected exception thrown by ::getInstance.
if (method_exists($this, 'expectException')) {
$this->expectException(\BadMethodCallException::class);
$this->expectExceptionMessage(sprintf('%s does not support this method unless %s::$mapper is set.', get_class($manager), get_class($manager)));
}
else {
$this->setExpectedException(\BadMethodCallException::class, sprintf('%s does not support this method unless %s::$mapper is set.', get_class($manager), get_class($manager)));
}
$manager->getInstance($options);
}
}
<?php
namespace Drupal\Tests\Component\Plugin;
use Drupal\Component\Plugin\Mapper\MapperInterface;
use Drupal\Component\Plugin\PluginManagerBase;
/**
* Stubs \Drupal\Component\Plugin\PluginManagerBase to take a MapperInterface.
*/
final class StubPluginManagerBaseWithMapper extends PluginManagerBase {
/**
* Constructs a new instance.
*
* @param \Drupal\Component\Plugin\Mapper\MapperInterface $mapper
*/
public function __construct(MapperInterface $mapper) {
$this->mapper = $mapper;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment