View source
<?php
namespace Drupal\Tests\Component\Plugin;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Component\Plugin\Mapper\MapperInterface;
use Drupal\Component\Plugin\PluginManagerBase;
use Drupal\Tests\PhpUnitCompatibilityTrait;
use PHPUnit\Framework\TestCase;
class PluginManagerBaseTest extends TestCase {
use PhpUnitCompatibilityTrait;
public function createInstanceCallback() {
$args = func_get_args();
$plugin_id = $args[0];
$configuration = $args[1];
if ('invalid' == $plugin_id) {
throw new PluginNotFoundException($plugin_id);
}
return [
'plugin_id' => $plugin_id,
'configuration' => $configuration,
];
}
public function getMockFactoryInterface($expects_count) {
$mock_factory = $this
->getMockBuilder('Drupal\\Component\\Plugin\\Factory\\FactoryInterface')
->setMethods([
'createInstance',
])
->getMockForAbstractClass();
$mock_factory
->expects($this
->exactly($expects_count))
->method('createInstance')
->willReturnCallback([
$this,
'createInstanceCallback',
]);
return $mock_factory;
}
public function testCreateInstance() {
$manager = $this
->getMockBuilder('Drupal\\Component\\Plugin\\PluginManagerBase')
->getMockForAbstractClass();
$factory_ref = new \ReflectionProperty($manager, 'factory');
$factory_ref
->setAccessible(TRUE);
$factory_ref
->setValue($manager, $this
->getMockFactoryInterface(1));
$configuration_array = [
'config' => 'something',
];
$result = $manager
->createInstance('valid', $configuration_array);
$this
->assertEquals('valid', $result['plugin_id']);
$this
->assertEquals($configuration_array, $result['configuration']);
}
public function testCreateInstanceFallback() {
$manager = new StubFallbackPluginManager();
$factory_ref = new \ReflectionProperty($manager, 'factory');
$factory_ref
->setAccessible(TRUE);
$configuration_array = [
'config' => 'something',
];
$factory_ref
->setValue($manager, $this
->getMockFactoryInterface(1));
$no_fallback_result = $manager
->createInstance('valid', $configuration_array);
$this
->assertEquals('valid', $no_fallback_result['plugin_id']);
$this
->assertEquals($configuration_array, $no_fallback_result['configuration']);
$factory_ref
->setValue($manager, $this
->getMockFactoryInterface(2));
$fallback_result = $manager
->createInstance('invalid', $configuration_array);
$this
->assertEquals('invalid_fallback', $fallback_result['plugin_id']);
$this
->assertEquals($configuration_array, $fallback_result['configuration']);
}
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));
}
public function testGetInstanceWithoutMapperShouldThrowException() {
$options = [
'foo' => 'F00',
'bar' => 'bAr',
];
$manager = $this
->getMockBuilder(PluginManagerBase::class)
->getMockForAbstractClass();
$this
->expectException(\BadMethodCallException::class);
$this
->expectExceptionMessage(sprintf('%s does not support this method unless %s::$mapper is set.', get_class($manager), get_class($manager)));
$manager
->getInstance($options);
}
}