Drupal 8.0.0 beta 12. More info: https://www.drupal.org/node/2514176

This commit is contained in:
Pantheon Automation 2015-08-17 17:00:26 -07:00 committed by Greg Anderson
commit 9921556621
13277 changed files with 1459781 additions and 0 deletions

View file

@ -0,0 +1,106 @@
<?php
/**
* @file
* Contains \Drupal\Tests\Component\Plugin\Context\ContextTest.
*/
namespace Drupal\Tests\Component\Plugin\Context;
use Drupal\Component\Plugin\Context\Context;
use Drupal\Tests\UnitTestCase;
/**
* @coversDefaultClass \Drupal\Component\Plugin\Context\Context
* @group Plugin
*/
class ContextTest extends UnitTestCase {
/**
* Data provider for testGetContextValue.
*/
public function providerGetContextValue() {
return [
['context_value', 'context_value', FALSE, 'data_type'],
[NULL, NULL, FALSE, 'data_type'],
['will throw exception', NULL, TRUE, 'data_type'],
];
}
/**
* @covers ::getContextValue
* @dataProvider providerGetContextValue
*/
public function testGetContextValue($expected, $context_value, $is_required, $data_type) {
// Mock a Context object.
$mock_context = $this->getMockBuilder('Drupal\Component\Plugin\Context\Context')
->disableOriginalConstructor()
->setMethods(array('getContextDefinition'))
->getMock();
// If the context value exists, getContextValue() behaves like a normal
// getter.
if ($context_value) {
// Set visibility of contextValue.
$ref_context_value = new \ReflectionProperty($mock_context, 'contextValue');
$ref_context_value->setAccessible(TRUE);
// Set contextValue to a testable state.
$ref_context_value->setValue($mock_context, $context_value);
// Exercise getContextValue().
$this->assertEquals($context_value, $mock_context->getContextValue());
}
// If no context value exists, we have to cover either returning NULL or
// throwing an exception if the definition requires it.
else {
// Create a mock definition.
$mock_definition = $this->getMockBuilder('Drupal\Component\Plugin\Context\ContextDefinitionInterface')
->setMethods(array('isRequired', 'getDataType'))
->getMockForAbstractClass();
// Set expectation for isRequired().
$mock_definition->expects($this->once())
->method('isRequired')
->willReturn($is_required);
// Set expectation for getDataType().
$mock_definition->expects($this->exactly(
$is_required ? 1 : 0
))
->method('getDataType')
->willReturn($data_type);
// Set expectation for getContextDefinition().
$mock_context->expects($this->once())
->method('getContextDefinition')
->willReturn($mock_definition);
// Set expectation for exception.
if ($is_required) {
$this->setExpectedException(
'Drupal\Component\Plugin\Exception\ContextException',
sprintf("The %s context is required and not present.", $data_type)
);
}
// Exercise getContextValue().
$this->assertEquals($context_value, $mock_context->getContextValue());
}
}
/**
* @covers ::getContextValue
*/
public function testDefaultValue() {
$mock_definition = $this->getMockBuilder('Drupal\Component\Plugin\Context\ContextDefinitionInterface')
->setMethods(array('getDefaultValue'))
->getMockForAbstractClass();
$mock_definition->expects($this->once())
->method('getDefaultValue')
->willReturn('test');
$context = new Context($mock_definition);
$this->assertEquals('test', $context->getContextValue());
}
}

View file

@ -0,0 +1,71 @@
<?php
/**
* @file
* Contains \Drupal\Tests\Component\Plugin\DefaultFactoryTest.
*/
namespace Drupal\Tests\Component\Plugin;
use Drupal\Component\Plugin\Factory\DefaultFactory;
use Drupal\Tests\UnitTestCase;
/**
* @coversDefaultClass \Drupal\Component\Plugin\Factory\DefaultFactory
* @group Plugin
*/
class DefaultFactoryTest extends UnitTestCase {
/**
* Tests getPluginClass() with a valid plugin.
*/
public function testGetPluginClassWithValidPlugin() {
$plugin_class = 'Drupal\plugin_test\Plugin\plugin_test\fruit\Cherry';
$class = DefaultFactory::getPluginClass('cherry', ['class' => $plugin_class]);
$this->assertEquals($plugin_class, $class);
}
/**
* Tests getPluginClass() with a missing class definition.
*
* @expectedException \Drupal\Component\Plugin\Exception\PluginException
* @expectedExceptionMessage The plugin (cherry) did not specify an instance class.
*/
public function testGetPluginClassWithMissingClass() {
DefaultFactory::getPluginClass('cherry', []);
}
/**
* Tests getPluginClass() with a not existing class definition.
*
* @expectedException \Drupal\Component\Plugin\Exception\PluginException
* @expectedExceptionMessage Plugin (kiwifruit) instance class "\Drupal\plugin_test\Plugin\plugin_test\fruit\Kiwifruit" does not exist.
*/
public function testGetPluginClassWithNotExistingClass() {
DefaultFactory::getPluginClass('kiwifruit', ['class' => '\Drupal\plugin_test\Plugin\plugin_test\fruit\Kiwifruit']);
}
/**
* Tests getPluginClass() with a required interface.
*/
public function testGetPluginClassWithInterface() {
$plugin_class = 'Drupal\plugin_test\Plugin\plugin_test\fruit\Cherry';
$class = DefaultFactory::getPluginClass('cherry', ['class' => $plugin_class], '\Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface');
$this->assertEquals($plugin_class, $class);
}
/**
* Tests getPluginClass() with a required interface but no implementation.
*
* @expectedException \Drupal\Component\Plugin\Exception\PluginException
* @expectedExceptionMessage Plugin "cherry" (Drupal\plugin_test\Plugin\plugin_test\fruit\Kale) must implement interface \Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface.
*/
public function testGetPluginClassWithInterfaceAndInvalidClass() {
$plugin_class = 'Drupal\plugin_test\Plugin\plugin_test\fruit\Kale';
DefaultFactory::getPluginClass('cherry', ['class' => $plugin_class, 'provider' => 'core'], '\Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface');
}
}

View file

@ -0,0 +1,70 @@
<?php
/**
* @file
* Contains \Drupal\Tests\Component\Plugin\Discovery\DiscoveryCachedTraitTest.
*/
namespace Drupal\Tests\Component\Plugin\Discovery;
use Drupal\Tests\UnitTestCase;
/**
* @coversDefaultClass Drupal\Component\Plugin\Discovery\DiscoveryCachedTrait
* @uses Drupal\Component\Plugin\Discovery\DiscoveryTrait
* @group Plugin
*/
class DiscoveryCachedTraitTest extends UnitTestCase {
/**
* Data provider for testGetDefinition().
*
* @return array
* - Expected result from getDefinition().
* - Cached definitions to be placed into self::$definitions
* - Definitions to be returned by getDefinitions().
* - Plugin name to query for.
*/
public function providerGetDefinition() {
return array(
['definition', [], ['plugin_name' => 'definition'], 'plugin_name'],
['definition', ['plugin_name' => 'definition'], [], 'plugin_name'],
[NULL, ['plugin_name' => 'definition'], [], 'bad_plugin_name'],
);
}
/**
* @covers ::getDefinition
* @dataProvider providerGetDefinition
*/
public function testGetDefinition($expected, $cached_definitions, $get_definitions, $plugin_id) {
// Mock a DiscoveryCachedTrait.
$trait = $this->getMockForTrait('Drupal\Component\Plugin\Discovery\DiscoveryCachedTrait');
$reflection_definitions = new \ReflectionProperty($trait, 'definitions');
$reflection_definitions->setAccessible(TRUE);
// getDefinition() needs the ::$definitions property to be set in one of two
// ways: 1) As existing cached data, or 2) as a side-effect of calling
// getDefinitions().
// If there are no cached definitions, then we have to fake the side-effect
// of getDefinitions().
if (count($cached_definitions) < 1) {
$trait->expects($this->once())
->method('getDefinitions')
// Use a callback method, so we can perform the side-effects.
->willReturnCallback(function() use ($reflection_definitions, $trait, $get_definitions) {
$reflection_definitions->setValue($trait, $get_definitions);
return $get_definitions;
});
}
else {
// Put $cached_definitions into our mocked ::$definitions.
$reflection_definitions->setValue($trait, $cached_definitions);
}
// Call getDefinition(), with $exception_on_invalid always FALSE.
$this->assertSame(
$expected,
$trait->getDefinition($plugin_id, FALSE)
);
}
}

View file

@ -0,0 +1,160 @@
<?php
/**
* @file
* Contains \Drupal\Tests\Component\Plugin\Discovery\DiscoveryTraitTest.
*/
namespace Drupal\Tests\Component\Plugin\Discovery;
use Drupal\Tests\UnitTestCase;
/**
* @group Plugin
* @coversDefaultClass Drupal\Component\Plugin\Discovery\DiscoveryTrait
*/
class DiscoveryTraitTest extends UnitTestCase {
/**
* Data provider for testDoGetDefinition().
*
* @return array
* - Expected plugin definition.
* - Plugin definition array, to pass to doGetDefinition().
* - Plugin ID to get, passed to doGetDefinition().
*/
public function providerDoGetDefinition() {
return array(
['definition', ['plugin_name' => 'definition'], 'plugin_name'],
[NULL, ['plugin_name' => 'definition'], 'bad_plugin_name'],
);
}
/**
* @covers ::doGetDefinition
* @dataProvider providerDoGetDefinition
*/
public function testDoGetDefinition($expected, $definitions, $plugin_id) {
// Mock the trait.
$trait = $this->getMockForTrait('Drupal\Component\Plugin\Discovery\DiscoveryTrait');
// Un-protect the method using reflection.
$method_ref = new \ReflectionMethod($trait, 'doGetDefinition');
$method_ref->setAccessible(TRUE);
// Call doGetDefinition, with $exception_on_invalid always FALSE.
$this->assertSame(
$expected,
$method_ref->invoke($trait, $definitions, $plugin_id, FALSE)
);
}
/**
* Data provider for testDoGetDefinitionException()
*
* @return array
* - Expected plugin definition.
* - Plugin definition array, to pass to doGetDefinition().
* - Plugin ID to get, passed to doGetDefinition().
*/
public function providerDoGetDefinitionException() {
return array(
[FALSE, ['plugin_name' => 'definition'], 'bad_plugin_name'],
);
}
/**
* @covers ::doGetDefinition
* @expectedException Drupal\Component\Plugin\Exception\PluginNotFoundException
* @dataProvider providerDoGetDefinitionException
* @uses Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public function testDoGetDefinitionException($expected, $definitions, $plugin_id) {
// Mock the trait.
$trait = $this->getMockForTrait('Drupal\Component\Plugin\Discovery\DiscoveryTrait');
// Un-protect the method using reflection.
$method_ref = new \ReflectionMethod($trait, 'doGetDefinition');
$method_ref->setAccessible(TRUE);
// Call doGetDefinition, with $exception_on_invalid always TRUE.
$this->assertSame(
$expected,
$method_ref->invoke($trait, $definitions, $plugin_id, TRUE)
);
}
/**
* @covers ::getDefinition
* @dataProvider providerDoGetDefinition
*/
public function testGetDefinition($expected, $definitions, $plugin_id) {
// Since getDefinition is a wrapper around doGetDefinition(), we can re-use
// its data provider. We just have to tell abstract method getDefinitions()
// to use the $definitions array.
$trait = $this->getMockForTrait('Drupal\Component\Plugin\Discovery\DiscoveryTrait');
$trait->expects($this->once())
->method('getDefinitions')
->willReturn($definitions);
// Call getDefinition(), with $exception_on_invalid always FALSE.
$this->assertSame(
$expected,
$trait->getDefinition($plugin_id, FALSE)
);
}
/**
* @covers ::getDefinition
* @expectedException Drupal\Component\Plugin\Exception\PluginNotFoundException
* @dataProvider providerDoGetDefinitionException
* @uses Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public function testGetDefinitionException($expected, $definitions, $plugin_id) {
// Since getDefinition is a wrapper around doGetDefinition(), we can re-use
// its data provider. We just have to tell abstract method getDefinitions()
// to use the $definitions array.
$trait = $this->getMockForTrait('Drupal\Component\Plugin\Discovery\DiscoveryTrait');
$trait->expects($this->once())
->method('getDefinitions')
->willReturn($definitions);
// Call getDefinition(), with $exception_on_invalid always TRUE.
$this->assertSame(
$expected,
$trait->getDefinition($plugin_id, TRUE)
);
}
/**
* Data provider for testHasDefinition().
*
* @return array
* - Expected TRUE or FALSE.
* - Plugin ID to look for.
*/
public function providerHasDefinition() {
return array(
[TRUE, 'valid'],
[FALSE, 'not_valid'],
);
}
/**
* @covers ::hasDefinition
* @dataProvider providerHasDefinition
*/
public function testHasDefinition($expected, $plugin_id) {
$trait = $this->getMockBuilder('Drupal\Component\Plugin\Discovery\DiscoveryTrait')
->setMethods(array('getDefinition'))
->getMockForTrait();
// Set up our mocked getDefinition() to return TRUE for 'valid' and FALSE
// for 'not_valid'.
$trait->expects($this->once())
->method('getDefinition')
->will($this->returnValueMap(array(
['valid', FALSE, TRUE],
['not_valid', FALSE, FALSE],
)));
// Call hasDefinition().
$this->assertSame(
$expected,
$trait->hasDefinition($plugin_id)
);
}
}

View file

@ -0,0 +1,234 @@
<?php
/**
* @file
* Contains \Drupal\Tests\Component\Plugin\Discovery\StaticDiscoveryDecoratorTest.
*/
namespace Drupal\Tests\Component\Plugin\Discovery;
use Drupal\Tests\UnitTestCase;
/**
* @group Plugin
* @coversDefaultClass Drupal\Component\Plugin\Discovery\StaticDiscoveryDecorator
*/
class StaticDiscoveryDecoratorTest extends UnitTestCase {
/**
* Helper method to provide a mocked callback object with expectations.
*
* If there should be a registered definition, then we have to place a
* \Callable in the mock object. The return value of this callback is
* never used.
*
* @return mock
* Mocked object with expectation of registerDefinitionsCallback() being
* called once.
*/
public function getRegisterDefinitionsCallback() {
$mock_callable = $this->getMockBuilder('\stdClass')
->setMethods(array('registerDefinitionsCallback'))
->getMock();
// Set expectations for the callback method.
$mock_callable->expects($this->once())
->method('registerDefinitionsCallback');
return $mock_callable;
}
/**
* Data provider for testGetDefinitions().
*
* @return array
* - Expected plugin definition.
* - Whether we require the method to register definitions through a
* callback.
* - Whether to throw an exception if the definition is invalid.
* - A plugin definition.
* - Base plugin ID.
*/
public function providerGetDefinition() {
return [
['is_defined', TRUE, FALSE, ['plugin-definition' => 'is_defined'], 'plugin-definition'],
// Make sure we don't call the decorated method if we shouldn't.
['is_defined', FALSE, FALSE, ['plugin-definition' => 'is_defined'], 'plugin-definition'],
// Return NULL for bad plugin id.
[NULL, FALSE, FALSE, ['plugin-definition' => 'is_defined'], 'BAD-plugin-definition'],
// Generate an exception.
[NULL, FALSE, TRUE, ['plugin-definition' => 'is_defined'], 'BAD-plugin-definition'],
];
}
/**
* @covers ::getDefinition
* @dataProvider providerGetDefinition
*/
public function testGetDefinition($expected, $has_register_definitions, $exception_on_invalid, $definitions, $base_plugin_id) {
// Mock our StaticDiscoveryDecorator.
$mock_decorator = $this->getMockBuilder('Drupal\Component\Plugin\Discovery\StaticDiscoveryDecorator')
->disableOriginalConstructor()
->setMethods(array('registeredDefintionCallback'))
->getMock();
// Set up the ::$registerDefinitions property.
$ref_register_definitions = new \ReflectionProperty($mock_decorator, 'registerDefinitions');
$ref_register_definitions->setAccessible(TRUE);
if ($has_register_definitions) {
// Set the callback object on the mocked decorator.
$ref_register_definitions->setValue(
$mock_decorator,
array($this->getRegisterDefinitionsCallback(), 'registerDefinitionsCallback')
);
}
else {
// There should be no registerDefinitions callback.
$ref_register_definitions->setValue($mock_decorator, NULL);
}
// Set up ::$definitions to an empty array.
$ref_definitions = new \ReflectionProperty($mock_decorator, 'definitions');
$ref_definitions->setAccessible(TRUE);
$ref_definitions->setValue($mock_decorator, array());
// Mock a decorated object.
$mock_decorated = $this->getMockBuilder('Drupal\Component\Plugin\Discovery\DiscoveryInterface')
->setMethods(array('getDefinitions'))
->getMockForAbstractClass();
// Return our definitions from getDefinitions().
$mock_decorated->expects($this->once())
->method('getDefinitions')
->willReturn($definitions);
// Set up ::$decorated to our mocked decorated object.
$ref_decorated = new \ReflectionProperty($mock_decorator, 'decorated');
$ref_decorated->setAccessible(TRUE);
$ref_decorated->setValue($mock_decorator, $mock_decorated);
if ($exception_on_invalid) {
$this->setExpectedException('Drupal\Component\Plugin\Exception\PluginNotFoundException');
}
// Exercise getDefinition(). It calls parent::getDefinition().
$this->assertEquals(
$expected,
$mock_decorator->getDefinition($base_plugin_id, $exception_on_invalid)
);
}
/**
* Data provider for testGetDefinitions().
*
* @return array
* - bool Whether the test mock has a callback.
* - array Plugin definitions.
*/
public function providerGetDefinitions() {
return [
[TRUE, ['definition' => 'is_fake']],
[FALSE, ['definition' => 'array_of_stuff']],
];
}
/**
* @covers ::getDefinitions
* @dataProvider providerGetDefinitions
*/
public function testGetDefinitions($has_register_definitions, $definitions) {
// Mock our StaticDiscoveryDecorator.
$mock_decorator = $this->getMockBuilder('Drupal\Component\Plugin\Discovery\StaticDiscoveryDecorator')
->disableOriginalConstructor()
->setMethods(array('registeredDefintionCallback'))
->getMock();
// Set up the ::$registerDefinitions property.
$ref_register_definitions = new \ReflectionProperty($mock_decorator, 'registerDefinitions');
$ref_register_definitions->setAccessible(TRUE);
if ($has_register_definitions) {
// Set the callback object on the mocked decorator.
$ref_register_definitions->setValue(
$mock_decorator,
array($this->getRegisterDefinitionsCallback(), 'registerDefinitionsCallback')
);
}
else {
// There should be no registerDefinitions callback.
$ref_register_definitions->setValue($mock_decorator, NULL);
}
// Set up ::$definitions to an empty array.
$ref_definitions = new \ReflectionProperty($mock_decorator, 'definitions');
$ref_definitions->setAccessible(TRUE);
$ref_definitions->setValue($mock_decorator, array());
// Mock a decorated object.
$mock_decorated = $this->getMockBuilder('Drupal\Component\Plugin\Discovery\DiscoveryInterface')
->setMethods(array('getDefinitions'))
->getMockForAbstractClass();
// Our mocked method will return any arguments sent to it.
$mock_decorated->expects($this->once())
->method('getDefinitions')
->willReturn($definitions);
// Set up ::$decorated to our mocked decorated object.
$ref_decorated = new \ReflectionProperty($mock_decorator, 'decorated');
$ref_decorated->setAccessible(TRUE);
$ref_decorated->setValue($mock_decorator, $mock_decorated);
// Exercise getDefinitions(). It calls parent::getDefinitions() but in this
// case there will be no side-effects.
$this->assertArrayEquals(
$definitions,
$mock_decorator->getDefinitions()
);
}
/**
* Data provider for testCall().
*
* @return array
* - Method name.
* - Array of arguments to pass to the method, with the expectation that our
* mocked __call() will return them.
*/
public function providerCall() {
return [
['complexArguments', ['1', 2.0, 3, ['4' => 'five']]],
['noArguments', []],
];
}
/**
* @covers ::__call
* @dataProvider providerCall
*/
public function testCall($method, $args) {
// Mock a decorated object.
$mock_decorated = $this->getMockBuilder('Drupal\Component\Plugin\Discovery\DiscoveryInterface')
->setMethods(array($method))
->getMockForAbstractClass();
// Our mocked method will return any arguments sent to it.
$mock_decorated->expects($this->once())
->method($method)
->willReturnCallback(
function () {
return \func_get_args();
}
);
// Create a mock decorator.
$mock_decorator = $this->getMockBuilder('Drupal\Component\Plugin\Discovery\StaticDiscoveryDecorator')
->disableOriginalConstructor()
->getMock();
// Poke the decorated object into our decorator.
$ref_decorated = new \ReflectionProperty($mock_decorator, 'decorated');
$ref_decorated->setAccessible(TRUE);
$ref_decorated->setValue($mock_decorator, $mock_decorated);
// Exercise __call.
$this->assertArrayEquals(
$args,
\call_user_func_array(array($mock_decorated, $method), $args)
);
}
}

View file

@ -0,0 +1,215 @@
<?php
/**
* @file
* Contains \Drupal\Tests\Component\Plugin\Factory\ReflectionFactoryTest.
*
* Also contains Argument* classes used as data for testing.
*/
namespace Drupal\Tests\Component\Plugin\Factory;
use Drupal\Component\Plugin\Factory\ReflectionFactory;
use Drupal\Tests\UnitTestCase;
/**
* @group Plugin
* @coversDefaultClass Drupal\Component\Plugin\Factory\ReflectionFactory
*/
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);
}
}
/**
* Override ReflectionFactory because ::createInstance() calls a static method.
*
* We have to override getPluginClass so that we can stub out its return value.
*/
class StubReflectionFactory extends ReflectionFactory {
/**
* {@inheritdoc}
*/
public static function getPluginClass($plugin_id, $plugin_definition = NULL, $required_interface = NULL) {
// Return the class name from the plugin definition.
return $plugin_definition[$plugin_id]['class'];
}
}
/**
* A stub class used by testGetInstanceArguments().
*
* @see providerGetInstanceArguments()
*/
class ArgumentsPluginId {
public function __construct($plugin_id) {
// No-op.
}
}
/**
* A stub class used by testGetInstanceArguments().
*
* @see providerGetInstanceArguments()
*/
class ArgumentsMany {
public function __construct(
$configuration, $plugin_definition, $plugin_id, $foo = 'default_value', $what_am_i_doing_here = 'what_default'
) {
// No-op.
}
}
/**
* A stub class used by testGetInstanceArguments().
*
* @see providerGetInstanceArguments()
*/
class ArgumentsConfigArrayKey {
public function __construct($config_name) {
// No-op.
}
}
/**
* A stub class used by testGetInstanceArguments().
*
* @see providerGetInstanceArguments()
*/
class ArgumentsAllNull {
public function __construct($charismatic, $demure, $delightful, $electrostatic) {
// No-op.
}
}
/**
* A stub class used by testGetInstanceArguments().
*
* @see providerGetInstanceArguments()
*/
class ArgumentsNoConstructor {
}

View file

@ -0,0 +1,112 @@
<?php
/**
* @file
* Contains \Drupal\Tests\Component\Plugin\PluginBaseTest.
*/
namespace Drupal\Tests\Component\Plugin;
use Drupal\Tests\UnitTestCase;
/**
* @coversDefaultClass \Drupal\Component\Plugin\PluginBase
* @group Plugin
*/
class PluginBaseTest extends UnitTestCase {
/**
* @dataProvider providerTestGetPluginId
* @covers ::getPluginId
*/
public function testGetPluginId($plugin_id, $expected) {
$plugin_base = $this->getMockForAbstractClass('Drupal\Component\Plugin\PluginBase', array(
array(),
$plugin_id,
array(),
));
$this->assertEquals($expected, $plugin_base->getPluginId());
}
/**
* Returns test data for testGetPluginId().
*
* @return array
*/
public function providerTestGetPluginId() {
return array(
array('base_id', 'base_id'),
array('base_id:derivative', 'base_id:derivative'),
);
}
/**
* @dataProvider providerTestGetBaseId
* @coves ::getBaseId
*/
public function testGetBaseId($plugin_id, $expected) {
/** @var \Drupal\Component\Plugin\PluginBase|\PHPUnit_Framework_MockObject_MockObject $plugin_base */
$plugin_base = $this->getMockForAbstractClass('Drupal\Component\Plugin\PluginBase', array(
array(),
$plugin_id,
array(),
));
$this->assertEquals($expected, $plugin_base->getBaseId());
}
/**
* Returns test data for testGetBaseId().
*
* @return array
*/
public function providerTestGetBaseId() {
return array(
array('base_id', 'base_id'),
array('base_id:derivative', 'base_id'),
);
}
/**
* @dataProvider providerTestGetDerivativeId
* @covers ::getDerivativeId
*/
public function testGetDerivativeId($plugin_id = NULL, $expected = NULL) {
/** @var \Drupal\Component\Plugin\PluginBase|\PHPUnit_Framework_MockObject_MockObject $plugin_base */
$plugin_base = $this->getMockForAbstractClass('Drupal\Component\Plugin\PluginBase', array(
array(),
$plugin_id,
array(),
));
$this->assertEquals($expected, $plugin_base->getDerivativeId());
}
/**
* Returns test data for testGetDerivativeId().
*
* @return array
*/
public function providerTestGetDerivativeId() {
return array(
array('base_id', NULL),
array('base_id:derivative', 'derivative'),
);
}
/**
* @covers ::getPluginDefinition
*/
public function testGetPluginDefinition() {
$plugin_base = $this->getMockForAbstractClass('Drupal\Component\Plugin\PluginBase', array(
array(),
'plugin_id',
array('value', array('key' => 'value')),
));
$this->assertEquals(array('value', array('key' => 'value')), $plugin_base->getPluginDefinition());
}
}

View file

@ -0,0 +1,98 @@
<?php
/**
* @file
* Contains \Drupal\Tests\Component\Plugin\PluginManagerBaseTest.
*/
namespace Drupal\Tests\Component\Plugin;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Tests\UnitTestCase;
/**
* @coversDefaultClass \Drupal\Component\Plugin\PluginManagerBase
* @group Plugin
*/
class PluginManagerBaseTest extends UnitTestCase {
/**
* A callback method for mocking FactoryInterface objects.
*/
public function createInstanceCallback() {
$args = func_get_args();
$plugin_id = $args[0];
$configuration = $args[1];
if ('invalid' == $plugin_id) {
throw new PluginNotFoundException($plugin_id);
}
return array(
'plugin_id' => $plugin_id,
'configuration' => $configuration,
);
}
/**
* Generates a mocked FactoryInterface object with known properties.
*/
public function getMockFactoryInterface($expects_count) {
$mock_factory = $this->getMockBuilder('Drupal\Component\Plugin\Factory\FactoryInterface')
->setMethods(array('createInstance'))
->getMockForAbstractClass();
$mock_factory->expects($this->exactly($expects_count))
->method('createInstance')
->willReturnCallback(array($this, 'createInstanceCallback'));
return $mock_factory;
}
/**
* Tests createInstance() with no fallback methods.
*
* @covers ::createInstance
*/
public function testCreateInstance() {
$manager = $this->getMockBuilder('Drupal\Component\Plugin\PluginManagerBase')
->getMockForAbstractClass();
// PluginManagerBase::createInstance() looks for a factory object and then
// calls createInstance() on it. So we have to mock a factory object.
$factory_ref = new \ReflectionProperty($manager, 'factory');
$factory_ref->setAccessible(TRUE);
$factory_ref->setValue($manager, $this->getMockFactoryInterface(1));
// Finally the test.
$configuration_array = array('config' => 'something');
$result = $manager->createInstance('valid', $configuration_array);
$this->assertEquals('valid', $result['plugin_id']);
$this->assertArrayEquals($configuration_array, $result['configuration']);
}
/**
* Tests createInstance() with a fallback method.
*
* @covers ::createInstance
*/
public function testCreateInstanceFallback() {
// We use our special stub class which extends PluginManagerBase and also
// implements FallbackPluginManagerInterface.
$manager = new StubFallbackPluginManager();
// Put our stubbed factory on the base object.
$factory_ref = new \ReflectionProperty($manager, 'factory');
$factory_ref->setAccessible(TRUE);
// Set up the configuration array.
$configuration_array = array('config' => 'something');
// Test with fallback interface and valid plugin_id.
$factory_ref->setValue($manager, $this->getMockFactoryInterface(1));
$no_fallback_result = $manager->createInstance('valid', $configuration_array);
$this->assertEquals('valid', $no_fallback_result['plugin_id']);
$this->assertArrayEquals($configuration_array, $no_fallback_result['configuration']);
// Test with fallback interface and invalid plugin_id.
$factory_ref->setValue($manager, $this->getMockFactoryInterface(2));
$fallback_result = $manager->createInstance('invalid', $configuration_array);
$this->assertEquals('invalid_fallback', $fallback_result['plugin_id']);
$this->assertArrayEquals($configuration_array, $fallback_result['configuration']);
}
}

View file

@ -0,0 +1,33 @@
<?php
/**
* @file
* Contains \Drupal\Tests\Component\Plugin\StubFallbackPluginManager.
*/
namespace Drupal\Tests\Component\Plugin;
use Drupal\Component\Plugin\FallbackPluginManagerInterface;
use Drupal\Component\Plugin\PluginManagerBase;
/**
* Stubs \Drupal\Component\Plugin\FallbackPluginManagerInterface.
*
* We have to stub \Drupal\Component\Plugin\FallbackPluginManagerInterface for
* \Drupal\Tests\Component\Plugin\PluginManagerBaseTest so that we can
* implement ::getFallbackPluginId().
*
* We do this so we can have it just return the plugin ID passed to it, with
* '_fallback' appended.
*/
class StubFallbackPluginManager extends PluginManagerBase implements FallbackPluginManagerInterface {
/**
* {@inheritdoc}
*/
public function getFallbackPluginId($plugin_id, array $configuration = array()) {
// Minimally implement getFallbackPluginId so that we can test it.
return $plugin_id . '_fallback';
}
}