3 namespace Drupal\Tests\Core\Plugin\Discovery;
5 use Drupal\Component\Plugin\Exception\PluginNotFoundException;
6 use Drupal\Core\Plugin\Discovery\HookDiscovery;
7 use Drupal\Tests\UnitTestCase;
10 * @coversDefaultClass \Drupal\Core\Plugin\Discovery\HookDiscovery
13 class HookDiscoveryTest extends UnitTestCase {
16 * The mocked module handler.
18 * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
20 protected $moduleHandler;
23 * The tested hook discovery.
25 * @var \Drupal\Core\Plugin\Discovery\HookDiscovery
27 protected $hookDiscovery;
32 protected function setUp() {
33 $this->moduleHandler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
34 $this->hookDiscovery = new HookDiscovery($this->moduleHandler, 'test_plugin');
38 * Tests the getDefinitions() method without any plugins.
40 * @see \Drupal\Core\Plugin\Discovery::getDefinitions()
42 public function testGetDefinitionsWithoutPlugins() {
43 $this->moduleHandler->expects($this->once())
44 ->method('getImplementations')
46 ->will($this->returnValue([]));
48 $this->assertCount(0, $this->hookDiscovery->getDefinitions());
52 * Tests the getDefinitions() method with some plugins.
54 * @see \Drupal\Core\Plugin\Discovery::getDefinitions()
56 public function testGetDefinitions() {
57 $this->moduleHandler->expects($this->once())
58 ->method('getImplementations')
60 ->will($this->returnValue(['hook_discovery_test', 'hook_discovery_test2']));
62 $this->moduleHandler->expects($this->at(1))
64 ->with('hook_discovery_test', 'test_plugin')
65 ->will($this->returnValue($this->hookDiscoveryTestTestPlugin()));
66 $this->moduleHandler->expects($this->at(2))
68 ->with('hook_discovery_test2', 'test_plugin')
69 ->will($this->returnValue($this->hookDiscoveryTest2TestPlugin()));
71 $definitions = $this->hookDiscovery->getDefinitions();
73 $this->assertCount(3, $definitions);
74 $this->assertEquals($definitions['test_id_1']['class'], 'Drupal\plugin_test\Plugin\plugin_test\fruit\Apple');
75 $this->assertEquals($definitions['test_id_2']['class'], 'Drupal\plugin_test\Plugin\plugin_test\fruit\Orange');
76 $this->assertEquals($definitions['test_id_3']['class'], 'Drupal\plugin_test\Plugin\plugin_test\fruit\Cherry');
78 // Ensure that the module was set.
79 $this->assertEquals($definitions['test_id_1']['provider'], 'hook_discovery_test');
80 $this->assertEquals($definitions['test_id_2']['provider'], 'hook_discovery_test');
81 $this->assertEquals($definitions['test_id_3']['provider'], 'hook_discovery_test2');
85 * Tests the getDefinition method with some plugins.
87 * @see \Drupal\Core\Plugin\Discovery::getDefinition()
89 public function testGetDefinition() {
90 $this->moduleHandler->expects($this->exactly(4))
91 ->method('getImplementations')
93 ->will($this->returnValue(['hook_discovery_test', 'hook_discovery_test2']));
95 $this->moduleHandler->expects($this->any())
97 ->will($this->returnValueMap([
98 ['hook_discovery_test', 'test_plugin', [], $this->hookDiscoveryTestTestPlugin()],
99 ['hook_discovery_test2', 'test_plugin', [], $this->hookDiscoveryTest2TestPlugin()],
103 $this->assertNull($this->hookDiscovery->getDefinition('test_non_existant', FALSE));
105 $plugin_definition = $this->hookDiscovery->getDefinition('test_id_1');
106 $this->assertEquals($plugin_definition['class'], 'Drupal\plugin_test\Plugin\plugin_test\fruit\Apple');
107 $this->assertEquals($plugin_definition['provider'], 'hook_discovery_test');
109 $plugin_definition = $this->hookDiscovery->getDefinition('test_id_2');
110 $this->assertEquals($plugin_definition['class'], 'Drupal\plugin_test\Plugin\plugin_test\fruit\Orange');
111 $this->assertEquals($plugin_definition['provider'], 'hook_discovery_test');
113 $plugin_definition = $this->hookDiscovery->getDefinition('test_id_3');
114 $this->assertEquals($plugin_definition['class'], 'Drupal\plugin_test\Plugin\plugin_test\fruit\Cherry');
115 $this->assertEquals($plugin_definition['provider'], 'hook_discovery_test2');
119 * Tests the getDefinition method with an unknown plugin ID.
121 * @see \Drupal\Core\Plugin\Discovery::getDefinition()
123 public function testGetDefinitionWithUnknownID() {
124 $this->moduleHandler->expects($this->once())
125 ->method('getImplementations')
126 ->will($this->returnValue([]));
128 $this->setExpectedException(PluginNotFoundException::class);
129 $this->hookDiscovery->getDefinition('test_non_existant', TRUE);
132 protected function hookDiscoveryTestTestPlugin() {
134 'test_id_1' => ['class' => 'Drupal\plugin_test\Plugin\plugin_test\fruit\Apple'],
135 'test_id_2' => ['class' => 'Drupal\plugin_test\Plugin\plugin_test\fruit\Orange'],
138 protected function hookDiscoveryTest2TestPlugin() {
140 'test_id_3' => ['class' => 'Drupal\plugin_test\Plugin\plugin_test\fruit\Cherry'],