View source
<?php
namespace Drupal\Tests\plugin\Unit\Plugin\Plugin\PluginSelector;
use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
use Drupal\Component\Plugin\Factory\FactoryInterface;
use Drupal\Component\Plugin\PluginInspectionInterface;
use Drupal\Core\Form\FormState;
use Drupal\plugin\Plugin\Plugin\PluginSelector\PluginSelectorBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PluginSelectorBaseTest extends PluginSelectorBaseTestBase {
protected $sut;
protected function setUp() : void {
parent::setUp();
$configuration = [];
$this->sut = $this
->getMockBuilder(PluginSelectorBase::class)
->setConstructorArgs([
$configuration,
$this->pluginId,
$this->pluginDefinition,
$this->defaultPluginResolver,
])
->getMockForAbstractClass();
}
function testCreate() {
$container = $this
->createMock(ContainerInterface::class);
$map = [
[
'plugin.default_plugin_resolver',
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$this->defaultPluginResolver,
],
];
$container
->expects($this
->any())
->method('get')
->willReturnMap($map);
$class = get_class($this->sut);
$plugin = $class::create($container, [], $this->pluginId, $this->pluginDefinition);
$this
->assertInstanceOf(get_class($this->sut), $plugin);
}
public function testDefaultConfiguration() {
$configuration = $this->sut
->defaultConfiguration();
$this
->assertIsArray($configuration);
}
public function testCalculateDependencies() {
$this
->assertSame([], $this->sut
->calculateDependencies());
}
public function testGetConfiguration() {
$configuration = array(
$this
->randomMachineName(),
);
$this
->assertSame($this->sut, $this->sut
->setConfiguration($configuration));
$this
->assertSame($configuration, $this->sut
->getConfiguration());
}
public function testGetLabel() {
$label = $this
->randomMachineName();
$this
->assertSame($this->sut, $this->sut
->setLabel($label));
$this
->assertSame($label, $this->sut
->getLabel());
}
public function testGetDescription() {
$description = $this
->randomMachineName();
$this
->assertSame($this->sut, $this->sut
->setDescription($description));
$this
->assertSame($description, $this->sut
->getDescription());
}
public function testGetCollectPluginConfiguration() {
$collect = (bool) mt_rand(0, 1);
$this
->assertSame($this->sut, $this->sut
->setCollectPluginConfiguration($collect));
$this
->assertSame($collect, $this->sut
->getCollectPluginConfiguration());
}
public function testGetPreviouslySelectedPlugins() {
$plugin = $this
->createMock(PluginInspectionInterface::class);
$this->sut
->setPreviouslySelectedPlugins([
$plugin,
]);
$this
->assertSame([
$plugin,
], $this->sut
->getPreviouslySelectedPlugins());
}
public function testGetKeepPreviouslySelectedPlugins() {
$keep = (bool) mt_rand(0, 1);
$plugin = $this
->createMock(PluginInspectionInterface::class);
$this->sut
->setPreviouslySelectedPlugins([
$plugin,
]);
$this
->assertSame($this->sut, $this->sut
->setKeepPreviouslySelectedPlugins($keep));
$this
->assertSame($keep, $this->sut
->getKeepPreviouslySelectedPlugins());
$this->sut
->setPreviouslySelectedPlugins([
$plugin,
]);
$this->sut
->setKeepPreviouslySelectedPlugins(FALSE);
$this
->assertEmpty($this->sut
->getPreviouslySelectedPlugins());
}
public function testGetSelectedPlugin() {
$this->sut
->setSelectablePluginType($this->selectablePluginType);
$plugin = $this
->createMock(PluginInspectionInterface::class);
$this
->assertSame($this->sut, $this->sut
->setSelectedPlugin($plugin));
$this
->assertSame($plugin, $this->sut
->getSelectedPlugin());
}
public function testGetRequired() {
$this
->assertFalse($this->sut
->isRequired());
$this
->assertSame($this->sut, $this->sut
->setRequired());
$this
->assertTrue($this->sut
->isRequired());
$this->sut
->setRequired(FALSE);
$this
->assertFalse($this->sut
->isRequired());
}
public function testBuildSelectorForm() {
$this->sut
->setSelectablePluginType($this->selectablePluginType);
$form = [];
$form_state = new FormState();
$form = $this->sut
->buildSelectorForm($form, $form_state);
$this
->assertIsArray($form);
}
}