class FactoryTest
Same name and namespace in other branches
- 11.x core/tests/Drupal/KernelTests/Core/Plugin/FactoryTest.php \Drupal\KernelTests\Core\Plugin\FactoryTest
- 10 core/tests/Drupal/KernelTests/Core/Plugin/FactoryTest.php \Drupal\KernelTests\Core\Plugin\FactoryTest
- 8.9.x core/tests/Drupal/KernelTests/Core/Plugin/FactoryTest.php \Drupal\KernelTests\Core\Plugin\FactoryTest
Tests that plugins are correctly instantiated.
@group Plugin
Hierarchy
- class \Drupal\KernelTests\KernelTestBase extends \Drupal\Core\DependencyInjection\ServiceProviderInterface uses \Drupal\KernelTests\AssertLegacyTrait, \Drupal\KernelTests\AssertContentTrait, \Drupal\Tests\RandomGeneratorTrait, \Drupal\Tests\ConfigTestTrait, \Drupal\Tests\ExtensionListTestTrait, \Drupal\Tests\TestRequirementsTrait, \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait implements \PHPUnit\Framework\TestCase
- class \Drupal\KernelTests\Core\Plugin\PluginTestBase implements \Drupal\KernelTests\KernelTestBase
- class \Drupal\KernelTests\Core\Plugin\FactoryTest implements \Drupal\KernelTests\Core\Plugin\PluginTestBase
- class \Drupal\KernelTests\Core\Plugin\PluginTestBase implements \Drupal\KernelTests\KernelTestBase
Expanded class hierarchy of FactoryTest
File
-
core/
tests/ Drupal/ KernelTests/ Core/ Plugin/ FactoryTest.php, line 12
Namespace
Drupal\KernelTests\Core\PluginView source
class FactoryTest extends PluginTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = [
'node',
'user',
];
/**
* Tests that DefaultFactory can create a plugin instance.
*/
public function testDefaultFactory() {
// Ensure a non-derivative plugin can be instantiated.
$plugin = $this->testPluginManager
->createInstance('user_login', [
'title' => 'Please enter your login name and password',
]);
$this->assertSame('Drupal\\plugin_test\\Plugin\\plugin_test\\mock_block\\MockUserLoginBlock', get_class($plugin), 'Correct plugin class instantiated with default factory.');
$this->assertSame('Please enter your login name and password', $plugin->getTitle(), 'Plugin instance correctly configured.');
// Ensure that attempting to instantiate non-existing plugins throws a
// PluginException.
try {
$this->testPluginManager
->createInstance('non_existing');
$this->fail('Drupal\\Component\\Plugin\\Exception\\ExceptionInterface expected');
} catch (\Exception $e) {
$this->assertInstanceOf(ExceptionInterface::class, $e);
}
}
/**
* Tests that the Reflection factory can create a plugin instance.
*
* The mock plugin classes use different values for their constructors
* allowing us to test the reflection capabilities as well.
*
* We use derivative classes here because the block test type has the
* reflection factory and it provides some additional variety in plugin
* object creation.
*/
public function testReflectionFactory() {
// Ensure a non-derivative plugin can be instantiated.
$plugin = $this->mockBlockManager
->createInstance('user_login', [
'title' => 'Please enter your login name and password',
]);
$this->assertSame('Drupal\\plugin_test\\Plugin\\plugin_test\\mock_block\\MockUserLoginBlock', get_class($plugin), 'Correct plugin class instantiated.');
$this->assertSame('Please enter your login name and password', $plugin->getTitle(), 'Plugin instance correctly configured.');
// Ensure a derivative plugin can be instantiated.
$plugin = $this->mockBlockManager
->createInstance('menu:main_menu', [
'depth' => 2,
]);
$this->assertSame('<ul><li>1<ul><li>1.1</li></ul></li></ul>', $plugin->getContent(), 'Derived plugin instance correctly instantiated and configured.');
// Ensure that attempting to instantiate non-existing plugins throws a
// PluginException. Test this for a non-existing base plugin, a non-existing
// derivative plugin, and a base plugin that may not be used without
// deriving.
foreach ([
'non_existing',
'menu:non_existing',
'menu',
] as $invalid_id) {
try {
$this->mockBlockManager
->createInstance($invalid_id);
$this->fail('Drupal\\Component\\Plugin\\Exception\\ExceptionInterface expected');
} catch (\Exception $e) {
$this->assertInstanceOf(ExceptionInterface::class, $e);
}
}
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.