More updates to stop using dev or alpha or beta versions.
[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       if (method_exists($this, 'expectException')) {
127         $this->expectException('\ReflectionException');
128       }
129       else {
130         $this->setExpectedException('\ReflectionException');
131       }
132     }
133
134     // Finally invoke getInstanceArguments() on our mocked factory.
135     $ref = new \ReflectionClass($reflector_name);
136     $result = $get_instance_arguments_ref->invoke(
137       $reflection_factory, $ref, $plugin_id, $plugin_definition, $configuration);
138     $this->assertEquals($expected, $result);
139   }
140
141 }
142
143 /**
144  * Override ReflectionFactory because ::createInstance() calls a static method.
145  *
146  * We have to override getPluginClass so that we can stub out its return value.
147  */
148 class StubReflectionFactory extends ReflectionFactory {
149
150   /**
151    * {@inheritdoc}
152    */
153   public static function getPluginClass($plugin_id, $plugin_definition = NULL, $required_interface = NULL) {
154     // Return the class name from the plugin definition.
155     return $plugin_definition[$plugin_id]['class'];
156   }
157
158 }
159
160 /**
161  * A stub class used by testGetInstanceArguments().
162  *
163  * @see providerGetInstanceArguments()
164  */
165 class ArgumentsPluginId {
166
167   public function __construct($plugin_id) {
168     // No-op.
169   }
170
171 }
172
173 /**
174  * A stub class used by testGetInstanceArguments().
175  *
176  * @see providerGetInstanceArguments()
177  */
178 class ArgumentsMany {
179
180   public function __construct($configuration, $plugin_definition, $plugin_id, $foo = 'default_value', $what_am_i_doing_here = 'what_default') {
181     // No-op.
182   }
183
184 }
185
186 /**
187  * A stub class used by testGetInstanceArguments().
188  *
189  * @see providerGetInstanceArguments()
190  */
191 class ArgumentsConfigArrayKey {
192
193   public function __construct($config_name) {
194     // No-op.
195   }
196
197 }
198
199 /**
200  * A stub class used by testGetInstanceArguments().
201  *
202  * @see providerGetInstanceArguments()
203  */
204 class ArgumentsAllNull {
205
206   public function __construct($charismatic, $demure, $delightful, $electrostatic) {
207     // No-op.
208   }
209
210 }
211
212 /**
213  * A stub class used by testGetInstanceArguments().
214  *
215  * @see providerGetInstanceArguments()
216  */
217 class ArgumentsNoConstructor {
218
219 }