5 * Contains \Drupal\Tests\Component\Plugin\Factory\ReflectionFactoryTest.
7 * Also contains Argument* classes used as data for testing.
10 namespace Drupal\Tests\Component\Plugin\Factory;
12 use Drupal\Component\Plugin\Factory\ReflectionFactory;
13 use PHPUnit\Framework\TestCase;
17 * @coversDefaultClass \Drupal\Component\Plugin\Factory\ReflectionFactory
19 class ReflectionFactoryTest extends TestCase {
22 * Data provider for testGetInstanceArguments.
24 * The classes used here are defined at the bottom of this file.
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().
33 public function providerGetInstanceArguments() {
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']],
43 [[], ['arguments_many' => ['class' => 'Drupal\Tests\Component\Plugin\Factory\ArgumentsMany']], 'arguments_many', 'default_value', 'what_default'],
44 'Drupal\Tests\Component\Plugin\Factory\ArgumentsMany',
46 ['arguments_many' => ['class' => 'Drupal\Tests\Component\Plugin\Factory\ArgumentsMany']],
50 // Config array key exists and is set.
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'],
58 // Config array key exists and is not set.
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],
66 // Touch the else clause at the end of the method.
67 [NULL, NULL, NULL, NULL],
68 'Drupal\Tests\Component\Plugin\Factory\ArgumentsAllNull',
70 ['arguments_all_null' => ['class' => 'Drupal\Tests\Component\Plugin\Factory\ArgumentsAllNull']],
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']],
85 * @covers ::createInstance
86 * @dataProvider providerGetInstanceArguments
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'])
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);
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);
104 // Finally test that createInstance() returns an object of the class we
106 $this->assertInstanceOf($reflector_name, $reflection_factory->createInstance($plugin_id));
110 * @covers ::getInstanceArguments
111 * @dataProvider providerGetInstanceArguments
113 public function testGetInstanceArguments($expected, $reflector_name, $plugin_id, $plugin_definition, $configuration) {
114 $reflection_factory = $this->getMockBuilder('Drupal\Component\Plugin\Factory\ReflectionFactory')
115 ->disableOriginalConstructor()
117 $get_instance_arguments_ref = new \ReflectionMethod($reflection_factory, 'getInstanceArguments');
118 $get_instance_arguments_ref->setAccessible(TRUE);
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');
130 $this->setExpectedException('\ReflectionException');
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);
144 * Override ReflectionFactory because ::createInstance() calls a static method.
146 * We have to override getPluginClass so that we can stub out its return value.
148 class StubReflectionFactory extends ReflectionFactory {
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'];
161 * A stub class used by testGetInstanceArguments().
163 * @see providerGetInstanceArguments()
165 class ArgumentsPluginId {
167 public function __construct($plugin_id) {
174 * A stub class used by testGetInstanceArguments().
176 * @see providerGetInstanceArguments()
178 class ArgumentsMany {
180 public function __construct($configuration, $plugin_definition, $plugin_id, $foo = 'default_value', $what_am_i_doing_here = 'what_default') {
187 * A stub class used by testGetInstanceArguments().
189 * @see providerGetInstanceArguments()
191 class ArgumentsConfigArrayKey {
193 public function __construct($config_name) {
200 * A stub class used by testGetInstanceArguments().
202 * @see providerGetInstanceArguments()
204 class ArgumentsAllNull {
206 public function __construct($charismatic, $demure, $delightful, $electrostatic) {
213 * A stub class used by testGetInstanceArguments().
215 * @see providerGetInstanceArguments()
217 class ArgumentsNoConstructor {