class PluginDependencyTraitTest in Drupal 8
Same name and namespace in other branches
- 9 core/tests/Drupal/Tests/Core/Plugin/PluginDependencyTraitTest.php \Drupal\Tests\Core\Plugin\PluginDependencyTraitTest
@coversDefaultClass \Drupal\Core\Plugin\PluginDependencyTrait @group Plugin
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\Core\Plugin\PluginDependencyTraitTest
Expanded class hierarchy of PluginDependencyTraitTest
File
- core/
tests/ Drupal/ Tests/ Core/ Plugin/ PluginDependencyTraitTest.php, line 19
Namespace
Drupal\Tests\Core\PluginView source
class PluginDependencyTraitTest extends UnitTestCase {
/**
* @covers ::getPluginDependencies
*
* @dataProvider providerTestPluginDependencies
*/
public function testGetPluginDependencies(ProphecyInterface $plugin, $definition, array $expected) {
$test_class = new TestPluginDependency();
$module_handler = $this
->prophesize(ModuleHandlerInterface::class);
$module_handler
->moduleExists('test_module1')
->willReturn(TRUE);
$module_handler
->moduleExists('test_theme1')
->willReturn(FALSE);
$test_class
->setModuleHandler($module_handler
->reveal());
$theme_handler = $this
->prophesize(ThemeHandlerInterface::class);
$theme_handler
->themeExists('test_module1')
->willReturn(FALSE);
$theme_handler
->themeExists('test_theme1')
->willReturn(TRUE);
$test_class
->setThemeHandler($theme_handler
->reveal());
$plugin
->getPluginDefinition()
->willReturn($definition);
$actual = $test_class
->getPluginDependencies($plugin
->reveal());
$this
->assertEquals($expected, $actual);
$this
->assertEmpty($test_class
->getDependencies());
}
/**
* @covers ::calculatePluginDependencies
*
* @dataProvider providerTestPluginDependencies
*
* @param \Prophecy\Prophecy\ProphecyInterface $plugin
* A prophecy of a plugin instance.
* @param mixed $definition
* A plugin definition.
* @param array $expected
* The expected dependencies.
*/
public function testCalculatePluginDependencies(ProphecyInterface $plugin, $definition, array $expected) {
$test_class = new TestPluginDependency();
$module_handler = $this
->prophesize(ModuleHandlerInterface::class);
$module_handler
->moduleExists('test_module1')
->willReturn(TRUE);
$module_handler
->moduleExists('test_theme1')
->willReturn(FALSE);
$test_class
->setModuleHandler($module_handler
->reveal());
$theme_handler = $this
->prophesize(ThemeHandlerInterface::class);
$theme_handler
->themeExists('test_module1')
->willReturn(FALSE);
$theme_handler
->themeExists('test_theme1')
->willReturn(TRUE);
$test_class
->setThemeHandler($theme_handler
->reveal());
$plugin
->getPluginDefinition()
->willReturn($definition);
$test_class
->calculatePluginDependencies($plugin
->reveal());
$this
->assertEquals($expected, $test_class
->getDependencies());
}
/**
* Provides test data for plugin dependencies.
*/
public function providerTestPluginDependencies() {
$data = [];
$plugin = $this
->prophesize(PluginInspectionInterface::class);
$dependent_plugin = $this
->prophesize(PluginInspectionInterface::class)
->willImplement(DependentPluginInterface::class);
$dependent_plugin
->calculateDependencies()
->willReturn([
'module' => [
'test_module2',
],
]);
$data['dependent_plugin_from_module'] = [
$dependent_plugin,
[
'provider' => 'test_module1',
],
[
'module' => [
'test_module1',
'test_module2',
],
],
];
$data['dependent_plugin_from_core'] = [
$dependent_plugin,
[
'provider' => 'core',
],
[
'module' => [
'core',
'test_module2',
],
],
];
$data['dependent_plugin_from_theme'] = [
$dependent_plugin,
[
'provider' => 'test_theme1',
],
[
'module' => [
'test_module2',
],
'theme' => [
'test_theme1',
],
],
];
$data['array_with_config_dependencies'] = [
$plugin,
[
'provider' => 'test_module1',
'config_dependencies' => [
'module' => [
'test_module2',
],
],
],
[
'module' => [
'test_module1',
'test_module2',
],
],
];
$definition = $this
->prophesize(PluginDefinitionInterface::class);
$definition
->getProvider()
->willReturn('test_module1');
$data['object_definition'] = [
$plugin,
$definition
->reveal(),
[
'module' => [
'test_module1',
],
],
];
$dependent_definition = $this
->prophesize(PluginDefinitionInterface::class)
->willImplement(DependentPluginDefinitionInterface::class);
$dependent_definition
->getProvider()
->willReturn('test_module1');
$dependent_definition
->getConfigDependencies()
->willReturn([
'module' => [
'test_module2',
],
]);
$data['dependent_object_definition'] = [
$plugin,
$dependent_definition
->reveal(),
[
'module' => [
'test_module1',
'test_module2',
],
],
];
return $data;
}
/**
* @covers ::getPluginDependencies
*
* @group legacy
* @expectedDeprecated Declaring a dependency on an uninstalled module is deprecated in Drupal 8.7.0 and will not be supported in Drupal 9.0.0.
*/
public function testNeitherThemeNorModule() {
$test_class = new TestPluginDependency();
$plugin = $this
->prophesize(PluginInspectionInterface::class);
$definition = $this
->prophesize(PluginDefinitionInterface::class);
$definition
->getProvider()
->willReturn('neither_theme_nor_module');
$module_handler = $this
->prophesize(ModuleHandlerInterface::class);
$module_handler
->moduleExists('neither_theme_nor_module')
->willReturn(FALSE);
$test_class
->setModuleHandler($module_handler
->reveal());
$theme_handler = $this
->prophesize(ThemeHandlerInterface::class);
$theme_handler
->themeExists('neither_theme_nor_module')
->willReturn(FALSE);
$test_class
->setThemeHandler($theme_handler
->reveal());
$plugin
->getPluginDefinition()
->willReturn($definition);
$actual = $test_class
->getPluginDependencies($plugin
->reveal());
$expected = [
'module' => [
'neither_theme_nor_module',
],
];
$this
->assertEquals($expected, $actual);
}
}
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. | |
PluginDependencyTraitTest:: |
public | function | Provides test data for plugin dependencies. | |
PluginDependencyTraitTest:: |
public | function | @covers ::calculatePluginDependencies | |
PluginDependencyTraitTest:: |
public | function | @covers ::getPluginDependencies | |
PluginDependencyTraitTest:: |
public | function | @covers ::getPluginDependencies | |
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. | |
UnitTestCase:: |
protected | function | 340 |