7e8fbefd1d1cb48b334caf6cf49bbcfcc3bb84e8
[yaffs-website] / web / core / tests / Drupal / Tests / Component / Plugin / Factory / ReflectionFactoryTest.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Tests\Component\Plugin\Factory\ReflectionFactoryTest.
6  *
7  * Also contains Argument* classes used as data for testing.
8  */
9
10 namespace Drupal\Tests\Component\Plugin\Factory;
11
12 use Drupal\Component\Plugin\Factory\ReflectionFactory;
13 use PHPUnit\Framework\TestCase;
14
15 /**
16  * @group Plugin
17  * @coversDefaultClass \Drupal\Component\Plugin\Factory\ReflectionFactory
18  */
19 class ReflectionFactoryTest extends TestCase {
20
21   /**
22    * Data provider for testGetInstanceArguments.
23    *
24    * The classes used here are defined at the bottom of this file.
25    *
26    * @return array
27    *   - Expected output.
28    *   - Class to reflect for input to getInstanceArguments().
29    *   - $plugin_id parameter to getInstanceArguments().
30    *   - $plugin_definition parameter to getInstanceArguments().
31    *   - $configuration parameter to getInstanceArguments().
32    */
33   public function providerGetInstanceArguments() {
34     return [
35       [
36         ['arguments_plugin_id'],
37         'Drupal\Tests\Component\Plugin\Factory\ArgumentsPluginId',
38         'arguments_plugin_id',
39         ['arguments_plugin_id' => ['class' => 'Drupal\Tests\Component\Plugin\Factory\ArgumentsPluginId']],
40         [],
41       ],
42       [
43         [[], ['arguments_many' => ['class' => 'Drupal\Tests\Component\Plugin\Factory\ArgumentsMany']], 'arguments_many', 'default_value', 'what_default'],
44         'Drupal\Tests\Component\Plugin\Factory\ArgumentsMany',
45         'arguments_many',
46         ['arguments_many' => ['class' => 'Drupal\Tests\Component\Plugin\Factory\ArgumentsMany']],
47         [],
48       ],
49       [
50         // Config array key exists and is set.
51         ['thing'],
52         'Drupal\Tests\Component\Plugin\Factory\ArgumentsConfigArrayKey',
53         'arguments_config_array_key',
54         ['arguments_config_array_key' => ['class' => 'Drupal\Tests\Component\Plugin\Factory\ArgumentsConfigArrayKey']],
55         ['config_name' => 'thing'],
56       ],
57       [
58         // Config array key exists and is not set.
59         [NULL],
60         'Drupal\Tests\Component\Plugin\Factory\ArgumentsConfigArrayKey',
61         'arguments_config_array_key',
62         ['arguments_config_array_key' => ['class' => 'Drupal\Tests\Component\Plugin\Factory\ArgumentsConfigArrayKey']],
63         ['config_name' => NULL],
64       ],
65       [
66         // Touch the else clause at the end of the method.
67         [NULL, NULL, NULL, NULL],
68         'Drupal\Tests\Component\Plugin\Factory\ArgumentsAllNull',
69         'arguments_all_null',
70         ['arguments_all_null' => ['class' => 'Drupal\Tests\Component\Plugin\Factory\ArgumentsAllNull']],
71         [],
72       ],
73       [
74         // A plugin with no constructor.
75         [NULL, NULL, NULL, NULL],
76         'Drupal\Tests\Component\Plugin\Factory\ArgumentsNoConstructor',
77         'arguments_no_constructor',
78         ['arguments_no_constructor' => ['class' => 'Drupal\Tests\Component\Plugin\Factory\ArgumentsNoConstructor']],
79         [],
80       ],
81     ];
82   }
83
84   /**
85    * @covers ::createInstance
86    * @dataProvider providerGetInstanceArguments
87    */
88   public function testCreateInstance($expected, $reflector_name, $plugin_id, $plugin_definition, $configuration) {
89     // Create a mock DiscoveryInterface which can return our plugin definition.
90     $mock_discovery = $this->getMockBuilder('Drupal\Component\Plugin\Discovery\DiscoveryInterface')
91       ->setMethods(['getDefinition', 'getDefinitions', 'hasDefinition'])
92       ->getMock();
93     $mock_discovery->expects($this->never())->method('getDefinitions');
94     $mock_discovery->expects($this->never())->method('hasDefinition');
95     $mock_discovery->expects($this->once())
96       ->method('getDefinition')
97       ->willReturn($plugin_definition);
98
99     // Create a stub ReflectionFactory object. We use StubReflectionFactory
100     // because createInstance() has a dependency on a static method.
101     // StubReflectionFactory overrides this static method.
102     $reflection_factory = new StubReflectionFactory($mock_discovery);
103
104     // Finally test that createInstance() returns an object of the class we
105     // want.
106     $this->assertInstanceOf($reflector_name, $reflection_factory->createInstance($plugin_id));
107   }
108
109   /**
110    * @covers ::getInstanceArguments
111    * @dataProvider providerGetInstanceArguments
112    */
113   public function testGetInstanceArguments($expected, $reflector_name, $plugin_id, $plugin_definition, $configuration) {
114     $reflection_factory = $this->getMockBuilder('Drupal\Component\Plugin\Factory\ReflectionFactory')
115       ->disableOriginalConstructor()
116       ->getMock();
117     $get_instance_arguments_ref = new \ReflectionMethod($reflection_factory, 'getInstanceArguments');
118     $get_instance_arguments_ref->setAccessible(TRUE);
119
120     // Special case for plugin class without a constructor.
121     // getInstanceArguments() throws an exception if there's no constructor.
122     // This is not a documented behavior of getInstanceArguments(), but allows
123     // us to use one data set for this test method as well as
124     // testCreateInstance().
125     if ($plugin_id == 'arguments_no_constructor') {
126       $this->setExpectedException('\ReflectionException');
127     }
128
129     // Finally invoke getInstanceArguments() on our mocked factory.
130     $ref = new \ReflectionClass($reflector_name);
131     $result = $get_instance_arguments_ref->invoke(
132       $reflection_factory, $ref, $plugin_id, $plugin_definition, $configuration);
133     $this->assertEquals($expected, $result);
134   }
135
136 }
137
138 /**
139  * Override ReflectionFactory because ::createInstance() calls a static method.
140  *
141  * We have to override getPluginClass so that we can stub out its return value.
142  */
143 class StubReflectionFactory extends ReflectionFactory {
144
145   /**
146    * {@inheritdoc}
147    */
148   public static function getPluginClass($plugin_id, $plugin_definition = NULL, $required_interface = NULL) {
149     // Return the class name from the plugin definition.
150     return $plugin_definition[$plugin_id]['class'];
151   }
152
153 }
154
155 /**
156  * A stub class used by testGetInstanceArguments().
157  *
158  * @see providerGetInstanceArguments()
159  */
160 class ArgumentsPluginId {
161
162   public function __construct($plugin_id) {
163     // No-op.
164   }
165
166 }
167
168 /**
169  * A stub class used by testGetInstanceArguments().
170  *
171  * @see providerGetInstanceArguments()
172  */
173 class ArgumentsMany {
174
175   public function __construct($configuration, $plugin_definition, $plugin_id, $foo = 'default_value', $what_am_i_doing_here = 'what_default') {
176     // No-op.
177   }
178
179 }
180
181 /**
182  * A stub class used by testGetInstanceArguments().
183  *
184  * @see providerGetInstanceArguments()
185  */
186 class ArgumentsConfigArrayKey {
187
188   public function __construct($config_name) {
189     // No-op.
190   }
191
192 }
193
194 /**
195  * A stub class used by testGetInstanceArguments().
196  *
197  * @see providerGetInstanceArguments()
198  */
199 class ArgumentsAllNull {
200
201   public function __construct($charismatic, $demure, $delightful, $electrostatic) {
202     // No-op.
203   }
204
205 }
206
207 /**
208  * A stub class used by testGetInstanceArguments().
209  *
210  * @see providerGetInstanceArguments()
211  */
212 class ArgumentsNoConstructor {
213
214 }