class HookDiscoveryTest
Same name in other branches
- 9 core/tests/Drupal/Tests/Core/Plugin/Discovery/HookDiscoveryTest.php \Drupal\Tests\Core\Plugin\Discovery\HookDiscoveryTest
- 8.9.x core/tests/Drupal/Tests/Core/Plugin/Discovery/HookDiscoveryTest.php \Drupal\Tests\Core\Plugin\Discovery\HookDiscoveryTest
- 11.x core/tests/Drupal/Tests/Core/Plugin/Discovery/HookDiscoveryTest.php \Drupal\Tests\Core\Plugin\Discovery\HookDiscoveryTest
@coversDefaultClass \Drupal\Core\Plugin\Discovery\HookDiscovery @group Plugin
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait
- class \Drupal\Tests\Core\Plugin\Discovery\HookDiscoveryTest extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of HookDiscoveryTest
File
-
core/
tests/ Drupal/ Tests/ Core/ Plugin/ Discovery/ HookDiscoveryTest.php, line 15
Namespace
Drupal\Tests\Core\Plugin\DiscoveryView source
class HookDiscoveryTest extends UnitTestCase {
/**
* The mocked module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected $moduleHandler;
/**
* The tested hook discovery.
*
* @var \Drupal\Core\Plugin\Discovery\HookDiscovery
*/
protected $hookDiscovery;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$this->moduleHandler = $this->createMock('Drupal\\Core\\Extension\\ModuleHandlerInterface');
$this->hookDiscovery = new HookDiscovery($this->moduleHandler, 'test_plugin');
}
/**
* Tests the getDefinitions() method without any plugins.
*
* @see \Drupal\Core\Plugin\Discovery::getDefinitions()
*/
public function testGetDefinitionsWithoutPlugins() : void {
$this->assertCount(0, $this->hookDiscovery
->getDefinitions());
}
/**
* Tests the getDefinitions() method with some plugins.
*
* @see \Drupal\Core\Plugin\Discovery::getDefinitions()
*/
public function testGetDefinitions() : void {
$this->moduleHandler
->expects($this->atLeastOnce())
->method('invokeAllWith')
->with('test_plugin')
->willReturnCallback(function (string $hook, callable $callback) {
$callback(\Closure::fromCallable([
$this,
'hookDiscoveryTestTestPlugin',
]), 'hook_discovery_test');
$callback(\Closure::fromCallable([
$this,
'hookDiscoveryTest2TestPlugin',
]), 'hook_discovery_test2');
});
$this->moduleHandler
->expects($this->never())
->method('invoke');
$definitions = $this->hookDiscovery
->getDefinitions();
$this->assertCount(3, $definitions);
$this->assertEquals('Drupal\\plugin_test\\Plugin\\plugin_test\\fruit\\Apple', $definitions['test_id_1']['class']);
$this->assertEquals('Drupal\\plugin_test\\Plugin\\plugin_test\\fruit\\Orange', $definitions['test_id_2']['class']);
$this->assertEquals('Drupal\\plugin_test\\Plugin\\plugin_test\\fruit\\Cherry', $definitions['test_id_3']['class']);
// Ensure that the module was set.
$this->assertEquals('hook_discovery_test', $definitions['test_id_1']['provider']);
$this->assertEquals('hook_discovery_test', $definitions['test_id_2']['provider']);
$this->assertEquals('hook_discovery_test2', $definitions['test_id_3']['provider']);
}
/**
* Tests the getDefinition method with some plugins.
*
* @see \Drupal\Core\Plugin\Discovery::getDefinition()
*/
public function testGetDefinition() : void {
$this->moduleHandler
->expects($this->exactly(4))
->method('invokeAllWith')
->with('test_plugin')
->willReturnCallback(function (string $hook, callable $callback) {
$callback(\Closure::fromCallable([
$this,
'hookDiscoveryTestTestPlugin',
]), 'hook_discovery_test');
$callback(\Closure::fromCallable([
$this,
'hookDiscoveryTest2TestPlugin',
]), 'hook_discovery_test2');
});
$this->assertNull($this->hookDiscovery
->getDefinition('test_non_existent', FALSE));
$plugin_definition = $this->hookDiscovery
->getDefinition('test_id_1');
$this->assertEquals('Drupal\\plugin_test\\Plugin\\plugin_test\\fruit\\Apple', $plugin_definition['class']);
$this->assertEquals('hook_discovery_test', $plugin_definition['provider']);
$plugin_definition = $this->hookDiscovery
->getDefinition('test_id_2');
$this->assertEquals('Drupal\\plugin_test\\Plugin\\plugin_test\\fruit\\Orange', $plugin_definition['class']);
$this->assertEquals('hook_discovery_test', $plugin_definition['provider']);
$plugin_definition = $this->hookDiscovery
->getDefinition('test_id_3');
$this->assertEquals('Drupal\\plugin_test\\Plugin\\plugin_test\\fruit\\Cherry', $plugin_definition['class']);
$this->assertEquals('hook_discovery_test2', $plugin_definition['provider']);
}
/**
* Tests the getDefinition method with an unknown plugin ID.
*
* @see \Drupal\Core\Plugin\Discovery::getDefinition()
*/
public function testGetDefinitionWithUnknownID() : void {
$this->expectException(PluginNotFoundException::class);
$this->hookDiscovery
->getDefinition('test_non_existent', TRUE);
}
protected function hookDiscoveryTestTestPlugin() {
return [
'test_id_1' => [
'class' => 'Drupal\\plugin_test\\Plugin\\plugin_test\\fruit\\Apple',
],
'test_id_2' => [
'class' => 'Drupal\\plugin_test\\Plugin\\plugin_test\\fruit\\Orange',
],
];
}
protected function hookDiscoveryTest2TestPlugin() {
return [
'test_id_3' => [
'class' => 'Drupal\\plugin_test\\Plugin\\plugin_test\\fruit\\Cherry',
],
];
}
}
Members
Title Sort descending | Deprecated | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|---|
HookDiscoveryTest::$hookDiscovery | protected | property | The tested hook discovery. | |||
HookDiscoveryTest::$moduleHandler | protected | property | The mocked module handler. | |||
HookDiscoveryTest::hookDiscoveryTest2TestPlugin | protected | function | ||||
HookDiscoveryTest::hookDiscoveryTestTestPlugin | protected | function | ||||
HookDiscoveryTest::setUp | protected | function | Overrides UnitTestCase::setUp | |||
HookDiscoveryTest::testGetDefinition | public | function | Tests the getDefinition method with some plugins. | |||
HookDiscoveryTest::testGetDefinitions | public | function | Tests the getDefinitions() method with some plugins. | |||
HookDiscoveryTest::testGetDefinitionsWithoutPlugins | public | function | Tests the getDefinitions() method without any plugins. | |||
HookDiscoveryTest::testGetDefinitionWithUnknownID | public | function | Tests the getDefinition method with an unknown plugin ID. | |||
PhpUnitWarnings::$deprecationWarnings | private static | property | Deprecation warnings from PHPUnit to raise with @trigger_error(). | |||
PhpUnitWarnings::addWarning | public | function | Converts PHPUnit deprecation warnings to E_USER_DEPRECATED. | |||
RandomGeneratorTrait::getRandomGenerator | protected | function | Gets the random generator for the utility methods. | |||
RandomGeneratorTrait::randomMachineName | protected | function | Generates a unique random string containing letters and numbers. | |||
RandomGeneratorTrait::randomObject | public | function | Generates a random PHP object. | |||
RandomGeneratorTrait::randomString | public | function | Generates a pseudo-random string of ASCII characters of codes 32 to 126. | |||
RandomGeneratorTrait::randomStringValidate | Deprecated | public | function | Callback for random string validation. | ||
UnitTestCase::$root | protected | property | The app root. | 1 | ||
UnitTestCase::getClassResolverStub | protected | function | Returns a stub class resolver. | |||
UnitTestCase::getConfigFactoryStub | public | function | Returns a stub config factory that behaves according to the passed array. | |||
UnitTestCase::getConfigStorageStub | public | function | Returns a stub config storage that returns the supplied configuration. | |||
UnitTestCase::getContainerWithCacheTagsInvalidator | protected | function | Sets up a container with a cache tags invalidator. | |||
UnitTestCase::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | |||
UnitTestCase::setUpBeforeClass | public static | function | ||||
UnitTestCase::__get | public | function |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.