9f4eb95dfcaffab51dd5de0e65b25d33fc8c2741
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Plugin / Discovery / HookDiscoveryTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Plugin\Discovery;
4
5 use Drupal\Component\Plugin\Exception\PluginNotFoundException;
6 use Drupal\Core\Plugin\Discovery\HookDiscovery;
7 use Drupal\Tests\UnitTestCase;
8
9 /**
10  * @coversDefaultClass \Drupal\Core\Plugin\Discovery\HookDiscovery
11  * @group Plugin
12  */
13 class HookDiscoveryTest extends UnitTestCase {
14
15   /**
16    * The mocked module handler.
17    *
18    * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
19    */
20   protected $moduleHandler;
21
22   /**
23    * The tested hook discovery.
24    *
25    * @var \Drupal\Core\Plugin\Discovery\HookDiscovery
26    */
27   protected $hookDiscovery;
28
29   /**
30    * {@inheritdoc}
31    */
32   protected function setUp() {
33     $this->moduleHandler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
34     $this->hookDiscovery = new HookDiscovery($this->moduleHandler, 'test_plugin');
35   }
36
37   /**
38    * Tests the getDefinitions() method without any plugins.
39    *
40    * @see \Drupal\Core\Plugin\Discovery::getDefinitions()
41    */
42   public function testGetDefinitionsWithoutPlugins() {
43     $this->moduleHandler->expects($this->once())
44       ->method('getImplementations')
45       ->with('test_plugin')
46       ->will($this->returnValue([]));
47
48     $this->assertCount(0, $this->hookDiscovery->getDefinitions());
49   }
50
51   /**
52    * Tests the getDefinitions() method with some plugins.
53    *
54    * @see \Drupal\Core\Plugin\Discovery::getDefinitions()
55    */
56   public function testGetDefinitions() {
57     $this->moduleHandler->expects($this->once())
58       ->method('getImplementations')
59       ->with('test_plugin')
60       ->will($this->returnValue(['hook_discovery_test', 'hook_discovery_test2']));
61
62     $this->moduleHandler->expects($this->at(1))
63       ->method('invoke')
64       ->with('hook_discovery_test', 'test_plugin')
65       ->will($this->returnValue($this->hookDiscoveryTestTestPlugin()));
66     $this->moduleHandler->expects($this->at(2))
67       ->method('invoke')
68       ->with('hook_discovery_test2', 'test_plugin')
69       ->will($this->returnValue($this->hookDiscoveryTest2TestPlugin()));
70
71     $definitions = $this->hookDiscovery->getDefinitions();
72
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');
77
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');
82   }
83
84   /**
85    * Tests the getDefinition method with some plugins.
86    *
87    * @see \Drupal\Core\Plugin\Discovery::getDefinition()
88    */
89   public function testGetDefinition() {
90     $this->moduleHandler->expects($this->exactly(4))
91       ->method('getImplementations')
92       ->with('test_plugin')
93       ->will($this->returnValue(['hook_discovery_test', 'hook_discovery_test2']));
94
95     $this->moduleHandler->expects($this->any())
96       ->method('invoke')
97       ->will($this->returnValueMap([
98           ['hook_discovery_test', 'test_plugin', [], $this->hookDiscoveryTestTestPlugin()],
99           ['hook_discovery_test2', 'test_plugin', [], $this->hookDiscoveryTest2TestPlugin()],
100         ]
101       ));
102
103     $this->assertNull($this->hookDiscovery->getDefinition('test_non_existant', FALSE));
104
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');
108
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');
112
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');
116   }
117
118   /**
119    * Tests the getDefinition method with an unknown plugin ID.
120    *
121    * @see \Drupal\Core\Plugin\Discovery::getDefinition()
122    */
123   public function testGetDefinitionWithUnknownID() {
124     $this->moduleHandler->expects($this->once())
125       ->method('getImplementations')
126       ->will($this->returnValue([]));
127
128     $this->setExpectedException(PluginNotFoundException::class);
129     $this->hookDiscovery->getDefinition('test_non_existant', TRUE);
130   }
131
132   protected function hookDiscoveryTestTestPlugin() {
133     return [
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'],
136     ];
137   }
138   protected function hookDiscoveryTest2TestPlugin() {
139     return [
140       'test_id_3' => ['class' => 'Drupal\plugin_test\Plugin\plugin_test\fruit\Cherry'],
141     ];
142   }
143
144 }