3 namespace Drupal\Tests\Component\Plugin\Discovery;
5 use Drupal\Component\Plugin\Exception\PluginNotFoundException;
6 use PHPUnit\Framework\TestCase;
10 * @coversDefaultClass \Drupal\Component\Plugin\Discovery\DiscoveryTrait
12 class DiscoveryTraitTest extends TestCase {
15 * Data provider for testDoGetDefinition().
18 * - Expected plugin definition.
19 * - Plugin definition array, to pass to doGetDefinition().
20 * - Plugin ID to get, passed to doGetDefinition().
22 public function providerDoGetDefinition() {
24 ['definition', ['plugin_name' => 'definition'], 'plugin_name'],
25 [NULL, ['plugin_name' => 'definition'], 'bad_plugin_name'],
30 * @covers ::doGetDefinition
31 * @dataProvider providerDoGetDefinition
33 public function testDoGetDefinition($expected, $definitions, $plugin_id) {
35 $trait = $this->getMockForTrait('Drupal\Component\Plugin\Discovery\DiscoveryTrait');
36 // Un-protect the method using reflection.
37 $method_ref = new \ReflectionMethod($trait, 'doGetDefinition');
38 $method_ref->setAccessible(TRUE);
39 // Call doGetDefinition, with $exception_on_invalid always FALSE.
42 $method_ref->invoke($trait, $definitions, $plugin_id, FALSE)
47 * Data provider for testDoGetDefinitionException()
50 * - Expected plugin definition.
51 * - Plugin definition array, to pass to doGetDefinition().
52 * - Plugin ID to get, passed to doGetDefinition().
54 public function providerDoGetDefinitionException() {
56 [FALSE, ['plugin_name' => 'definition'], 'bad_plugin_name'],
61 * @covers ::doGetDefinition
62 * @dataProvider providerDoGetDefinitionException
63 * @uses \Drupal\Component\Plugin\Exception\PluginNotFoundException
65 public function testDoGetDefinitionException($expected, $definitions, $plugin_id) {
67 $trait = $this->getMockForTrait('Drupal\Component\Plugin\Discovery\DiscoveryTrait');
68 // Un-protect the method using reflection.
69 $method_ref = new \ReflectionMethod($trait, 'doGetDefinition');
70 $method_ref->setAccessible(TRUE);
71 // Call doGetDefinition, with $exception_on_invalid always TRUE.
72 if (method_exists($this, 'expectException')) {
73 $this->expectException(PluginNotFoundException::class);
76 $this->setExpectedException(PluginNotFoundException::class);
78 $method_ref->invoke($trait, $definitions, $plugin_id, TRUE);
82 * @covers ::getDefinition
83 * @dataProvider providerDoGetDefinition
85 public function testGetDefinition($expected, $definitions, $plugin_id) {
86 // Since getDefinition is a wrapper around doGetDefinition(), we can re-use
87 // its data provider. We just have to tell abstract method getDefinitions()
88 // to use the $definitions array.
89 $trait = $this->getMockForTrait('Drupal\Component\Plugin\Discovery\DiscoveryTrait');
90 $trait->expects($this->once())
91 ->method('getDefinitions')
92 ->willReturn($definitions);
93 // Call getDefinition(), with $exception_on_invalid always FALSE.
96 $trait->getDefinition($plugin_id, FALSE)
101 * @covers ::getDefinition
102 * @dataProvider providerDoGetDefinitionException
103 * @uses \Drupal\Component\Plugin\Exception\PluginNotFoundException
105 public function testGetDefinitionException($expected, $definitions, $plugin_id) {
106 // Since getDefinition is a wrapper around doGetDefinition(), we can re-use
107 // its data provider. We just have to tell abstract method getDefinitions()
108 // to use the $definitions array.
109 $trait = $this->getMockForTrait('Drupal\Component\Plugin\Discovery\DiscoveryTrait');
110 $trait->expects($this->once())
111 ->method('getDefinitions')
112 ->willReturn($definitions);
113 // Call getDefinition(), with $exception_on_invalid always TRUE.
114 if (method_exists($this, 'expectException')) {
115 $this->expectException(PluginNotFoundException::class);
118 $this->setExpectedException(PluginNotFoundException::class);
120 $trait->getDefinition($plugin_id, TRUE);
124 * Data provider for testHasDefinition().
127 * - Expected TRUE or FALSE.
128 * - Plugin ID to look for.
130 public function providerHasDefinition() {
133 [FALSE, 'not_valid'],
138 * @covers ::hasDefinition
139 * @dataProvider providerHasDefinition
141 public function testHasDefinition($expected, $plugin_id) {
142 $trait = $this->getMockBuilder('Drupal\Component\Plugin\Discovery\DiscoveryTrait')
143 ->setMethods(['getDefinition'])
145 // Set up our mocked getDefinition() to return TRUE for 'valid' and FALSE
147 $trait->expects($this->once())
148 ->method('getDefinition')
149 ->will($this->returnValueMap([
150 ['valid', FALSE, TRUE],
151 ['not_valid', FALSE, FALSE],
153 // Call hasDefinition().
156 $trait->hasDefinition($plugin_id)