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 $this->setExpectedException(PluginNotFoundException::class);
73 $method_ref->invoke($trait, $definitions, $plugin_id, TRUE);
77 * @covers ::getDefinition
78 * @dataProvider providerDoGetDefinition
80 public function testGetDefinition($expected, $definitions, $plugin_id) {
81 // Since getDefinition is a wrapper around doGetDefinition(), we can re-use
82 // its data provider. We just have to tell abstract method getDefinitions()
83 // to use the $definitions array.
84 $trait = $this->getMockForTrait('Drupal\Component\Plugin\Discovery\DiscoveryTrait');
85 $trait->expects($this->once())
86 ->method('getDefinitions')
87 ->willReturn($definitions);
88 // Call getDefinition(), with $exception_on_invalid always FALSE.
91 $trait->getDefinition($plugin_id, FALSE)
96 * @covers ::getDefinition
97 * @dataProvider providerDoGetDefinitionException
98 * @uses \Drupal\Component\Plugin\Exception\PluginNotFoundException
100 public function testGetDefinitionException($expected, $definitions, $plugin_id) {
101 // Since getDefinition is a wrapper around doGetDefinition(), we can re-use
102 // its data provider. We just have to tell abstract method getDefinitions()
103 // to use the $definitions array.
104 $trait = $this->getMockForTrait('Drupal\Component\Plugin\Discovery\DiscoveryTrait');
105 $trait->expects($this->once())
106 ->method('getDefinitions')
107 ->willReturn($definitions);
108 // Call getDefinition(), with $exception_on_invalid always TRUE.
109 $this->setExpectedException(PluginNotFoundException::class);
110 $trait->getDefinition($plugin_id, TRUE);
114 * Data provider for testHasDefinition().
117 * - Expected TRUE or FALSE.
118 * - Plugin ID to look for.
120 public function providerHasDefinition() {
123 [FALSE, 'not_valid'],
128 * @covers ::hasDefinition
129 * @dataProvider providerHasDefinition
131 public function testHasDefinition($expected, $plugin_id) {
132 $trait = $this->getMockBuilder('Drupal\Component\Plugin\Discovery\DiscoveryTrait')
133 ->setMethods(['getDefinition'])
135 // Set up our mocked getDefinition() to return TRUE for 'valid' and FALSE
137 $trait->expects($this->once())
138 ->method('getDefinition')
139 ->will($this->returnValueMap([
140 ['valid', FALSE, TRUE],
141 ['not_valid', FALSE, FALSE],
143 // Call hasDefinition().
146 $trait->hasDefinition($plugin_id)