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 $this->setExpectedException('\ReflectionException');
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);
139 * Override ReflectionFactory because ::createInstance() calls a static method.
141 * We have to override getPluginClass so that we can stub out its return value.
143 class StubReflectionFactory extends ReflectionFactory {
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'];
156 * A stub class used by testGetInstanceArguments().
158 * @see providerGetInstanceArguments()
160 class ArgumentsPluginId {
162 public function __construct($plugin_id) {
169 * A stub class used by testGetInstanceArguments().
171 * @see providerGetInstanceArguments()
173 class ArgumentsMany {
175 public function __construct($configuration, $plugin_definition, $plugin_id, $foo = 'default_value', $what_am_i_doing_here = 'what_default') {
182 * A stub class used by testGetInstanceArguments().
184 * @see providerGetInstanceArguments()
186 class ArgumentsConfigArrayKey {
188 public function __construct($config_name) {
195 * A stub class used by testGetInstanceArguments().
197 * @see providerGetInstanceArguments()
199 class ArgumentsAllNull {
201 public function __construct($charismatic, $demure, $delightful, $electrostatic) {
208 * A stub class used by testGetInstanceArguments().
210 * @see providerGetInstanceArguments()
212 class ArgumentsNoConstructor {