class PluginTypeManagerTest in Plugin 8.2
Same name in this branch
- 8.2 tests/src/Unit/PluginType/PluginTypeManagerTest.php \Drupal\Tests\plugin\Unit\PluginType\PluginTypeManagerTest
- 8.2 tests/src/Kernel/PluginType/PluginTypeManagerTest.php \Drupal\Tests\plugin\Kernel\PluginType\PluginTypeManagerTest
@coversDefaultClass \Drupal\plugin\PluginType\PluginTypeManager
@group Plugin
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\plugin\Unit\PluginType\PluginTypeManagerTest
Expanded class hierarchy of PluginTypeManagerTest
File
- tests/
src/ Unit/ PluginType/ PluginTypeManagerTest.php, line 25
Namespace
Drupal\Tests\plugin\Unit\PluginTypeView source
class PluginTypeManagerTest extends UnitTestCase {
/**
* The service container.
*
* @var \Symfony\Component\DependencyInjection\ContainerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $container;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $moduleHandler;
/**
* The plugin type's plugin managers.
*
* @var \Drupal\Component\Plugin\PluginManagerInterface[]
* Keys are plugin type IDs.
*/
protected $pluginManagers = [];
/**
* The plugin type definitions.
*
* @var array[]
*/
protected $pluginTypeDefinitions = [];
/**
* The class under test.
*
* @var \Drupal\plugin\PluginType\PluginTypeManager
*/
protected $sut;
/**
* The typed configuration manager.
*
* @var \Drupal\Core\Config\TypedConfigManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $typedConfigurationManager;
/**
* Builds a plugin type definition file.
*
* @param string $id
*
* @return string
*/
protected function buildPluginDefinitionYaml($id, $label, $description, $provider, $plugin_manager_service_id) {
return <<<EOT
{<span class="php-variable">$id</span>}:
label: "{<span class="php-variable">$label</span>}"
description: "{<span class="php-variable">$description</span>}"
provider: {<span class="php-variable">$provider</span>}
plugin_manager_service_id: {<span class="php-variable">$plugin_manager_service_id</span>}
EOT;
}
protected function setUp() : void {
FileCacheFactory::setPrefix($this
->randomMachineName());
$plugin_type_id_a = 'foo';
$this->pluginTypeDefinitions[$plugin_type_id_a] = [
'label' => 'Foo',
'description' => 'This is Foo.',
'provider' => 'foo',
'plugin_manager_service_id' => 'plugin.manager.foo',
];
$plugin_type_id_b = 'bar';
$this->pluginTypeDefinitions[$plugin_type_id_b] = [
'label' => 'Bar',
'description' => 'I am Bar(t).',
'provider' => 'bar',
'plugin_manager_service_id' => 'plugin.manager.bar',
];
$this->pluginManagers = [
$plugin_type_id_a => $this
->createMock(PluginManagerInterface::class),
$plugin_type_id_b => $this
->createMock(PluginManagerInterface::class),
];
vfsStreamWrapper::register();
$root = new vfsStreamDirectory('modules');
vfsStreamWrapper::setRoot($root);
$this->moduleHandler = $this
->createMock(ModuleHandlerInterface::class);
$this->moduleHandler
->expects($this
->any())
->method('getModuleDirectories')
->willReturn(array(
'module_a' => vfsStream::url('modules/module_a'),
'module_b' => vfsStream::url('modules/module_b'),
));
$class_resolver = $this
->createMock(ClassResolverInterface::class);
$this->typedConfigurationManager = $this
->createMock(TypedConfigManagerInterface::class);
$this->container = $this
->createMock(ContainerInterface::class);
$map = [
[
'class_resolver',
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$class_resolver,
],
[
'config.typed',
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$this->typedConfigurationManager,
],
[
'string_translation',
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$this
->getStringTranslationStub(),
],
[
$this->pluginTypeDefinitions[$plugin_type_id_a]['plugin_manager_service_id'],
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$this->pluginManagers[$plugin_type_id_a],
],
[
$this->pluginTypeDefinitions[$plugin_type_id_b]['plugin_manager_service_id'],
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$this->pluginManagers[$plugin_type_id_b],
],
];
$this->container
->expects($this
->any())
->method('get')
->willReturnMap($map);
$url = vfsStream::url('modules');
mkdir($url . '/module_a');
file_put_contents($url . '/module_a/module_a.plugin_type.yml', $this
->buildPluginDefinitionYaml($plugin_type_id_a, $this->pluginTypeDefinitions[$plugin_type_id_a]['label'], $this->pluginTypeDefinitions[$plugin_type_id_a]['description'], $this->pluginTypeDefinitions[$plugin_type_id_a]['provider'], $this->pluginTypeDefinitions[$plugin_type_id_a]['plugin_manager_service_id']));
mkdir($url . '/module_b');
file_put_contents($url . '/module_b/module_b.plugin_type.yml', $this
->buildPluginDefinitionYaml($plugin_type_id_b, $this->pluginTypeDefinitions[$plugin_type_id_b]['label'], $this->pluginTypeDefinitions[$plugin_type_id_b]['description'], $this->pluginTypeDefinitions[$plugin_type_id_b]['provider'], $this->pluginTypeDefinitions[$plugin_type_id_b]['plugin_manager_service_id']));
$this->sut = new PluginTypeManager($this->container, $this->moduleHandler);
}
/**
* @covers ::__construct
*/
public function testConstruct() {
$this->sut = new PluginTypeManager($this->container, $this->moduleHandler);
$this
->assertInstanceOf(PluginTypeManager::class, $this->sut);
}
/**
* @covers ::hasPluginType
*
* @dataProvider providerHasPluginType
*/
public function testHasPluginType($expected, $plugin_type_id, $module_name, $module_exists) {
$modules = [];
if ($module_exists) {
$extension = $this
->getMockBuilder(Extension::class)
->disableOriginalConstructor()
->getMock();
$extension
->expects($this
->atLeastOnce())
->method('getName')
->willReturn($module_name);
$modules[] = $extension;
}
$this->moduleHandler
->expects($this
->any())
->method('getModuleList')
->willReturn($modules);
$this->typedConfigurationManager
->expects($this
->any())
->method('hasConfigSchema')
->with(sprintf('plugin.plugin_configuration.%s.*', $plugin_type_id))
->willReturn(TRUE);
$this
->assertSame($expected, $this->sut
->hasPluginType($plugin_type_id));
}
/**
* Provides data to self::testHasPluginType().
*/
public function providerHasPluginType() {
$data = [];
// This hardcoded the IDs in $this->pluginTypeDefinitions.
foreach ([
'foo',
'bar',
] as $key) {
$data[] = [
TRUE,
$key,
$key,
TRUE,
];
$data[] = [
FALSE,
$key,
$key,
FALSE,
];
}
$data[] = [
FALSE,
$this
->randomMachineName(),
$this
->randomMachineName(),
TRUE,
];
$data[] = [
FALSE,
$this
->randomMachineName(),
$this
->randomMachineName(),
FALSE,
];
return $data;
}
/**
* @covers ::getPluginType
*
* @dataProvider providerGetPluginType
*/
public function testGetPluginType($expected_success, $plugin_type_id, $module_name, $module_exists) {
$modules = [];
if ($module_exists) {
$extension = $this
->getMockBuilder(Extension::class)
->disableOriginalConstructor()
->getMock();
$extension
->expects($this
->atLeastOnce())
->method('getName')
->willReturn($module_name);
$modules[] = $extension;
}
$this->moduleHandler
->expects($this
->any())
->method('getModuleList')
->willReturn($modules);
$this->typedConfigurationManager
->expects($this
->any())
->method('hasConfigSchema')
->with(sprintf('plugin.plugin_configuration.%s.*', $plugin_type_id))
->willReturn(TRUE);
if ($expected_success) {
$this
->assertInstanceOf(PluginTypeInterface::class, $this->sut
->getPluginType($plugin_type_id));
}
else {
$this
->expectException('\\InvalidArgumentException');
$this->sut
->getPluginType($plugin_type_id);
}
}
/**
* Provides data to self::testGetPluginType().
*/
public function providerGetPluginType() {
$data = [];
// This hardcoded the IDs in $this->pluginTypeDefinitions.
foreach ([
'foo',
'bar',
] as $key) {
$data[] = [
TRUE,
$key,
$key,
TRUE,
];
$data[] = [
FALSE,
$key,
$key,
FALSE,
];
}
$data[] = [
FALSE,
$this
->randomMachineName(),
$this
->randomMachineName(),
TRUE,
];
$data[] = [
FALSE,
$this
->randomMachineName(),
$this
->randomMachineName(),
FALSE,
];
return $data;
}
/**
* @covers ::getPluginType
*/
public function testGetPluginTypeWithInvalidPluginTypeId() {
$this
->expectException(InvalidArgumentException::class);
$this->moduleHandler
->expects($this
->any())
->method('getModuleList')
->willReturn([]);
$this->sut
->getPluginType($this
->randomMachineName());
}
/**
* @covers ::getPluginTypes
*/
public function testGetPluginTypes() {
$modules = array_map(function (array $plugin_type_definition) {
$extension = $this
->getMockBuilder(Extension::class)
->disableOriginalConstructor()
->getMock();
$extension
->expects($this
->atLeastOnce())
->method('getName')
->willReturn($plugin_type_definition['provider']);
return $extension;
}, $this->pluginTypeDefinitions);
$this->moduleHandler
->expects($this
->any())
->method('getModuleList')
->willReturn($modules);
$this->typedConfigurationManager
->expects($this
->any())
->method('hasConfigSchema')
->willReturn('TRUE');
foreach ($this->sut
->getPluginTypes() as $plugin_type) {
$this
->assertPluginTypeIntegrity($plugin_type
->getId(), $this->pluginTypeDefinitions[$plugin_type
->getId()], $this->pluginManagers[$plugin_type
->getId()], $plugin_type);
}
}
/**
* Asserts the integrity of a plugin type based on its definition.
*
* @param string $plugin_type_id
* @param mixed[] $plugin_type_definition
* @param \Drupal\Component\Plugin\PluginManagerInterface $plugin_manager
* @param mixed $plugin_type
*/
protected function assertPluginTypeIntegrity($plugin_type_id, $plugin_type_definition, PluginManagerInterface $plugin_manager, $plugin_type) {
$this
->assertInstanceOf(PluginTypeInterface::class, $plugin_type);
$this
->assertSame($plugin_type_id, $plugin_type
->getId());
$this
->assertSame($plugin_type_definition['label'], $plugin_type
->getLabel()
->getUntranslatedString());
$this
->assertSame($plugin_type_definition['description'], $plugin_type
->getDescription()
->getUntranslatedString());
$this
->assertSame($plugin_type_definition['provider'], $plugin_type
->getProvider());
$this
->assertSame($plugin_manager, $plugin_type
->getPluginManager());
}
}
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. | |
PluginTypeManagerTest:: |
protected | property | The service container. | |
PluginTypeManagerTest:: |
protected | property | The module handler. | |
PluginTypeManagerTest:: |
protected | property | The plugin type's plugin managers. | |
PluginTypeManagerTest:: |
protected | property | The plugin type definitions. | |
PluginTypeManagerTest:: |
protected | property | The class under test. | |
PluginTypeManagerTest:: |
protected | property | The typed configuration manager. | |
PluginTypeManagerTest:: |
protected | function | Asserts the integrity of a plugin type based on its definition. | |
PluginTypeManagerTest:: |
protected | function | Builds a plugin type definition file. | |
PluginTypeManagerTest:: |
public | function | Provides data to self::testGetPluginType(). | |
PluginTypeManagerTest:: |
public | function | Provides data to self::testHasPluginType(). | |
PluginTypeManagerTest:: |
protected | function |
Overrides UnitTestCase:: |
|
PluginTypeManagerTest:: |
public | function | @covers ::__construct | |
PluginTypeManagerTest:: |
public | function | @covers ::getPluginType | |
PluginTypeManagerTest:: |
public | function | @covers ::getPluginTypes | |
PluginTypeManagerTest:: |
public | function | @covers ::getPluginType | |
PluginTypeManagerTest:: |
public | function | @covers ::hasPluginType | |
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. |