class PluginTypeTest in Plugin 8.2
@coversDefaultClass \Drupal\plugin\PluginType\PluginType
@group Plugin
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\plugin\Unit\PluginType\PluginTypeTest
Expanded class hierarchy of PluginTypeTest
File
- tests/
src/ Unit/ PluginType/ PluginTypeTest.php, line 20
Namespace
Drupal\Tests\plugin\Unit\PluginTypeView source
class PluginTypeTest extends UnitTestCase {
/**
* The service container.
*
* @var \Symfony\Component\DependencyInjection\ContainerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $container;
/**
* The plugin manager.
*
* @var \Drupal\Component\Plugin\PluginManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $pluginManager;
/**
* The plugin type definition.
*
* @var mixed[]
*/
protected $pluginTypeDefinition;
/**
* The class under test.
*
* @var \Drupal\plugin\PluginType\PluginType
*/
protected $sut;
protected function setUp() : void {
$this->pluginTypeDefinition = [
'id' => $this
->randomMachineName(),
'label' => $this
->getRandomGenerator()
->string(),
'description' => $this
->getRandomGenerator()
->string(),
'provider' => $this
->randomMachineName(),
'plugin_manager_service_id' => $this
->randomMachineName(),
'field_type' => (bool) mt_rand(0, 1),
];
$class_resolver = $this
->createMock(ClassResolverInterface::class);
$typed_config_manager = $this
->createMock(TypedConfigManagerInterface::class);
$typed_config_manager
->expects($this
->atLeastOnce())
->method('hasConfigSchema')
->willReturn(TRUE);
$this->pluginManager = $this
->createMock(PluginManagerInterface::class);
$this->container = $this
->createMock(ContainerInterface::class);
$map = [
[
'class_resolver',
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$class_resolver,
],
[
'config.typed',
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$typed_config_manager,
],
[
'string_translation',
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$this
->getStringTranslationStub(),
],
[
$this->pluginTypeDefinition['plugin_manager_service_id'],
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$this->pluginManager,
],
];
$this->container
->expects($this
->atLeastOnce())
->method('get')
->willReturnMap($map);
$this->sut = PluginType::createFromDefinition($this->container, $this->pluginTypeDefinition);
}
/**
* @covers ::createFromDefinition
* @covers ::__construct
*/
public function testCreateFromDefinition() {
$this->sut = PluginType::createFromDefinition($this->container, $this->pluginTypeDefinition);
}
/**
* @covers ::getId
*/
public function testGetPluginId() {
$this
->assertSame($this->pluginTypeDefinition['id'], $this->sut
->getId());
}
/**
* @covers ::getLabel
*/
public function testGetLabel() {
$this
->assertSame($this->pluginTypeDefinition['label'], $this->sut
->getLabel()
->getUntranslatedString());
}
/**
* @covers ::getDescription
*/
public function testGetDescription() {
$this
->assertSame($this->pluginTypeDefinition['description'], $this->sut
->getDescription()
->getUntranslatedString());
}
/**
* @covers ::getProvider
*/
public function testGetProvider() {
$this
->assertSame($this->pluginTypeDefinition['provider'], $this->sut
->getProvider());
}
/**
* @covers ::getPluginManager
*/
public function testGetPluginManager() {
$this
->assertSame($this->pluginManager, $this->sut
->getPluginManager());
}
/**
* @covers ::isFieldType
*/
public function testGetFieldType() {
$this
->assertSame($this->pluginTypeDefinition['field_type'], $this->sut
->isFieldType());
}
/**
* @covers ::getPluginConfigurationSchemaId
*/
public function testGetPluginConfigurationSchemaIdWithDefaultId() {
$plugin_id = 'FooBarQux';
$expected_schema_id = sprintf('plugin.plugin_configuration.%s.%s', $this->pluginTypeDefinition['id'], $plugin_id);
$this
->assertSame($expected_schema_id, $this->sut
->getPluginConfigurationSchemaId($plugin_id));
}
/**
* @covers ::getPluginConfigurationSchemaId
*/
public function testGetPluginConfigurationSchemaIdWithDefinedId() {
$plugin_id = 'FooBarQux';
$schema_id = 'foo_bar.qux.[plugin_id]';
$this->pluginTypeDefinition['plugin_configuration_schema_id'] = $schema_id;
$this->sut = PluginType::createFromDefinition($this->container, $this->pluginTypeDefinition);
$expected_schema_id = 'foo_bar.qux.' . $plugin_id;
$this
->assertSame($expected_schema_id, $this->sut
->getPluginConfigurationSchemaId($plugin_id));
}
/**
* @covers ::ensureTypedPluginDefinition
* @covers ::createFromDefinition
* @covers ::__construct
*/
public function testEnsureTypedPluginDefinition() {
$decorated_plugin_definition = [
'foo' => $this
->randomMachineName(),
];
$this->pluginTypeDefinition['plugin_definition_decorator_class'] = ArrayPluginDefinitionDecorator::class;
$this->sut = PluginType::createFromDefinition($this->container, $this->pluginTypeDefinition);
$typed_plugin_definition = $this->sut
->ensureTypedPluginDefinition($decorated_plugin_definition);
$this
->assertInstanceOf(PluginDefinitionInterface::class, $typed_plugin_definition);
// We use ArrayPluginDefinitionDecorator for testing. The following
// assertion makes sure the method under test correctly passes on the
// decorated plugin definition to the decorator. The array handling is not
// part of this test.
/** @var \Drupal\plugin\PluginDefinition\ArrayPluginDefinitionDecorator $typed_plugin_definition */
$this
->assertSame($decorated_plugin_definition, $typed_plugin_definition
->getArrayDefinition());
}
/**
* @covers ::ensureTypedPluginDefinition
* @covers ::createFromDefinition
* @covers ::__construct
*/
public function testEnsureTypedPluginDefinitionWithAlreadyTypedDefinition() {
$decorated_plugin_definition = $this
->createMock(PluginDefinitionInterface::class);
$typed_plugin_definition = $this->sut
->ensureTypedPluginDefinition($decorated_plugin_definition);
$this
->assertInstanceOf(PluginDefinitionInterface::class, $typed_plugin_definition);
}
/**
* @covers ::ensureTypedPluginDefinition
*/
public function testEnsureTypedPluginDefinitionWithoutDecorator() {
$this
->expectException(Exception::class);
$decorated_plugin_definition = [
'foo' => $this
->randomMachineName(),
];
$this->sut
->ensureTypedPluginDefinition($decorated_plugin_definition);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
PluginTypeTest:: |
protected | property | The service container. | |
PluginTypeTest:: |
protected | property | The plugin manager. | |
PluginTypeTest:: |
protected | property | The plugin type definition. | |
PluginTypeTest:: |
protected | property | The class under test. | |
PluginTypeTest:: |
protected | function |
Overrides UnitTestCase:: |
|
PluginTypeTest:: |
public | function | @covers ::createFromDefinition @covers ::__construct | |
PluginTypeTest:: |
public | function | @covers ::ensureTypedPluginDefinition @covers ::createFromDefinition @covers ::__construct | |
PluginTypeTest:: |
public | function | @covers ::ensureTypedPluginDefinition @covers ::createFromDefinition @covers ::__construct | |
PluginTypeTest:: |
public | function | @covers ::ensureTypedPluginDefinition | |
PluginTypeTest:: |
public | function | @covers ::getDescription | |
PluginTypeTest:: |
public | function | @covers ::isFieldType | |
PluginTypeTest:: |
public | function | @covers ::getLabel | |
PluginTypeTest:: |
public | function | @covers ::getPluginConfigurationSchemaId | |
PluginTypeTest:: |
public | function | @covers ::getPluginConfigurationSchemaId | |
PluginTypeTest:: |
public | function | @covers ::getId | |
PluginTypeTest:: |
public | function | @covers ::getPluginManager | |
PluginTypeTest:: |
public | function | @covers ::getProvider | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |