class ReflectionFactoryTest
Same name in other branches
- 9 core/tests/Drupal/Tests/Component/Plugin/Factory/ReflectionFactoryTest.php \Drupal\Tests\Component\Plugin\Factory\ReflectionFactoryTest
- 10 core/tests/Drupal/Tests/Component/Plugin/Factory/ReflectionFactoryTest.php \Drupal\Tests\Component\Plugin\Factory\ReflectionFactoryTest
- 11.x core/tests/Drupal/Tests/Component/Plugin/Factory/ReflectionFactoryTest.php \Drupal\Tests\Component\Plugin\Factory\ReflectionFactoryTest
@group Plugin @coversDefaultClass \Drupal\Component\Plugin\Factory\ReflectionFactory
Hierarchy
- class \Drupal\Tests\Component\Plugin\Factory\ReflectionFactoryTest extends \PHPUnit\Framework\TestCase
Expanded class hierarchy of ReflectionFactoryTest
File
-
core/
tests/ Drupal/ Tests/ Component/ Plugin/ Factory/ ReflectionFactoryTest.php, line 19
Namespace
Drupal\Tests\Component\Plugin\FactoryView source
class ReflectionFactoryTest extends TestCase {
/**
* Data provider for testGetInstanceArguments.
*
* The classes used here are defined at the bottom of this file.
*
* @return array
* - Expected output.
* - Class to reflect for input to getInstanceArguments().
* - $plugin_id parameter to getInstanceArguments().
* - $plugin_definition parameter to getInstanceArguments().
* - $configuration parameter to getInstanceArguments().
*/
public function providerGetInstanceArguments() {
return [
[
[
'arguments_plugin_id',
],
'Drupal\\Tests\\Component\\Plugin\\Factory\\ArgumentsPluginId',
'arguments_plugin_id',
[
'arguments_plugin_id' => [
'class' => 'Drupal\\Tests\\Component\\Plugin\\Factory\\ArgumentsPluginId',
],
],
[],
],
[
[
[],
[
'arguments_many' => [
'class' => 'Drupal\\Tests\\Component\\Plugin\\Factory\\ArgumentsMany',
],
],
'arguments_many',
'default_value',
'what_default',
],
'Drupal\\Tests\\Component\\Plugin\\Factory\\ArgumentsMany',
'arguments_many',
[
'arguments_many' => [
'class' => 'Drupal\\Tests\\Component\\Plugin\\Factory\\ArgumentsMany',
],
],
[],
],
[
// Config array key exists and is set.
[
'thing',
],
'Drupal\\Tests\\Component\\Plugin\\Factory\\ArgumentsConfigArrayKey',
'arguments_config_array_key',
[
'arguments_config_array_key' => [
'class' => 'Drupal\\Tests\\Component\\Plugin\\Factory\\ArgumentsConfigArrayKey',
],
],
[
'config_name' => 'thing',
],
],
[
// Config array key exists and is not set.
[
NULL,
],
'Drupal\\Tests\\Component\\Plugin\\Factory\\ArgumentsConfigArrayKey',
'arguments_config_array_key',
[
'arguments_config_array_key' => [
'class' => 'Drupal\\Tests\\Component\\Plugin\\Factory\\ArgumentsConfigArrayKey',
],
],
[
'config_name' => NULL,
],
],
[
// Touch the else clause at the end of the method.
[
NULL,
NULL,
NULL,
NULL,
],
'Drupal\\Tests\\Component\\Plugin\\Factory\\ArgumentsAllNull',
'arguments_all_null',
[
'arguments_all_null' => [
'class' => 'Drupal\\Tests\\Component\\Plugin\\Factory\\ArgumentsAllNull',
],
],
[],
],
[
// A plugin with no constructor.
[
NULL,
NULL,
NULL,
NULL,
],
'Drupal\\Tests\\Component\\Plugin\\Factory\\ArgumentsNoConstructor',
'arguments_no_constructor',
[
'arguments_no_constructor' => [
'class' => 'Drupal\\Tests\\Component\\Plugin\\Factory\\ArgumentsNoConstructor',
],
],
[],
],
];
}
/**
* @covers ::createInstance
* @dataProvider providerGetInstanceArguments
*/
public function testCreateInstance($expected, $reflector_name, $plugin_id, $plugin_definition, $configuration) {
// Create a mock DiscoveryInterface which can return our plugin definition.
$mock_discovery = $this->getMockBuilder('Drupal\\Component\\Plugin\\Discovery\\DiscoveryInterface')
->setMethods([
'getDefinition',
'getDefinitions',
'hasDefinition',
])
->getMock();
$mock_discovery->expects($this->never())
->method('getDefinitions');
$mock_discovery->expects($this->never())
->method('hasDefinition');
$mock_discovery->expects($this->once())
->method('getDefinition')
->willReturn($plugin_definition);
// Create a stub ReflectionFactory object. We use StubReflectionFactory
// because createInstance() has a dependency on a static method.
// StubReflectionFactory overrides this static method.
$reflection_factory = new StubReflectionFactory($mock_discovery);
// Finally test that createInstance() returns an object of the class we
// want.
$this->assertInstanceOf($reflector_name, $reflection_factory->createInstance($plugin_id));
}
/**
* @covers ::getInstanceArguments
* @dataProvider providerGetInstanceArguments
*/
public function testGetInstanceArguments($expected, $reflector_name, $plugin_id, $plugin_definition, $configuration) {
$reflection_factory = $this->getMockBuilder('Drupal\\Component\\Plugin\\Factory\\ReflectionFactory')
->disableOriginalConstructor()
->getMock();
$get_instance_arguments_ref = new \ReflectionMethod($reflection_factory, 'getInstanceArguments');
$get_instance_arguments_ref->setAccessible(TRUE);
// Special case for plugin class without a constructor.
// getInstanceArguments() throws an exception if there's no constructor.
// This is not a documented behavior of getInstanceArguments(), but allows
// us to use one data set for this test method as well as
// testCreateInstance().
if ($plugin_id == 'arguments_no_constructor') {
$this->expectException('\\ReflectionException');
}
// Finally invoke getInstanceArguments() on our mocked factory.
$ref = new \ReflectionClass($reflector_name);
$result = $get_instance_arguments_ref->invoke($reflection_factory, $ref, $plugin_id, $plugin_definition, $configuration);
$this->assertEquals($expected, $result);
}
}
Members
Title Sort descending | Modifiers | Object type | Summary |
---|---|---|---|
ReflectionFactoryTest::providerGetInstanceArguments | public | function | Data provider for testGetInstanceArguments. |
ReflectionFactoryTest::testCreateInstance | public | function | @covers ::createInstance @dataProvider providerGetInstanceArguments |
ReflectionFactoryTest::testGetInstanceArguments | public | function | @covers ::getInstanceArguments @dataProvider providerGetInstanceArguments |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.