class ReflectionFactoryTest in Zircon Profile 8
Same name and namespace in other branches
- 8.0 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\UnitTestCase extends \Drupal\Tests\PHPUnit_Framework_TestCase
- class \Drupal\Tests\Component\Plugin\Factory\ReflectionFactoryTest
Expanded class hierarchy of ReflectionFactoryTest
File
- core/
tests/ Drupal/ Tests/ Component/ Plugin/ Factory/ ReflectionFactoryTest.php, line 19 - Contains \Drupal\Tests\Component\Plugin\Factory\ReflectionFactoryTest.
Namespace
Drupal\Tests\Component\Plugin\FactoryView source
class ReflectionFactoryTest extends UnitTestCase {
/**
* 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(array(
'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
->setExpectedException('\\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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ReflectionFactoryTest:: |
public | function | Data provider for testGetInstanceArguments. | |
ReflectionFactoryTest:: |
public | function | @covers ::createInstance @dataProvider providerGetInstanceArguments | |
ReflectionFactoryTest:: |
public | function | @covers ::getInstanceArguments @dataProvider providerGetInstanceArguments | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed in array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
UnitTestCase:: |
protected | function | 259 |