Commit f17dcbf8 authored by webchick's avatar webchick
Browse files

Issue #2137947 by tim.plunkett, damiankloip: Finish unit tests for PluginBag and subclasses.

parent 5ef08242
......@@ -77,8 +77,7 @@ protected function initializePlugin($instance_id) {
if (!isset($configuration[$this->pluginKey])) {
throw new UnknownPluginException($instance_id);
}
$this->pluginInstances[$instance_id] = $this->manager->createInstance($configuration[$this->pluginKey], $configuration);
$this->addInstanceId($instance_id);
$this->set($instance_id, $this->manager->createInstance($configuration[$this->pluginKey], $configuration));
}
/**
......@@ -130,6 +129,15 @@ public function getConfiguration() {
return $instances;
}
/**
* {@inheritdoc}
*/
public function setInstanceIds(array $instance_ids) {
parent::setInstanceIds($instance_ids);
// Ensure the new order matches the original order.
$this->instanceIDs = $this->originalOrder = array_intersect_assoc($this->originalOrder, $this->instanceIDs);
}
/**
* Updates the configuration for a plugin instance.
*
......
......@@ -54,7 +54,7 @@ public function __construct(PluginManagerInterface $manager, array $instance_ids
* {@inheritdoc}
*/
protected function initializePlugin($instance_id) {
$this->pluginInstances[$instance_id] = $this->manager->createInstance($instance_id, $this->configuration);
$this->set($instance_id, $this->manager->createInstance($instance_id, $this->configuration));
}
}
......@@ -77,6 +77,7 @@ public function &get($instance_id) {
*/
public function set($instance_id, $value) {
$this->pluginInstances[$instance_id] = $value;
$this->addInstanceId($instance_id);
}
/**
......
<?php
/**
* @file
* Contains \Drupal\system\Tests\Plugin\PluginBagTest.
*/
namespace Drupal\system\Tests\Plugin;
use Drupal\plugin_test\Plugin\TestPluginBag;
use Drupal\plugin_test\Plugin\plugin_test\mock_block\MockTestPluginInterface;
/**
* Tests the generic plugin bag.
*
* @see \Drupal\Component\Plugin\PluginBag
* @see \Drupal\plugin_test\Plugin\TestPluginBag
*/
class PluginBagTest extends PluginTestBase {
public static function getInfo() {
return array(
'name' => 'Plugin Bag',
'description' => 'Tests the generic plugin bag.',
'group' => 'Plugin API',
);
}
/**
* Tests the generic plugin bag.
*/
protected function testPluginBag() {
// Setup the plugin bag as well as the available plugin definitions.
$plugin_bag = new TestPluginBag($this->mockBlockManager);
$definitions = $this->mockBlockManager->getDefinitions();
$first_instance_id = key($definitions);
foreach ($definitions as $instance_id => $definition) {
$this->assertTrue($plugin_bag->has($instance_id), format_string('Plugin instance @instance_id exits on the bag', array('@instance_id' => $instance_id)));
$this->assertTrue($plugin_bag->get($instance_id) instanceof $definition['class'], 'Getting the plugin from the bag worked.');
}
// A non existing instance_id shouldn't exist on the bag.
$random_name = $this->randomName();
$this->assertFalse($plugin_bag->has($random_name), 'A random instance_id should not exist on the plugin bag.');
// Set a new plugin instance to the bag, to test offsetSet.
$plugin_bag->set($random_name, $this->mockBlockManager->createInstance($first_instance_id, array()));
$this->assertTrue($plugin_bag->has($random_name), 'A random instance_id should exist after manual setting on the plugin bag.');
// Remove the previous added element and check whether it still exists.
$plugin_bag->remove($random_name);
$this->assertFalse($plugin_bag->has($random_name), 'A random instance_id should not exist on the plugin bag after removing.');
// Test that iterating over the plugins work.
$expected_instance_ids = array_keys($definitions);
$counter = 0;
foreach ($plugin_bag as $instance_id => $plugin) {
$this->assertEqual($expected_instance_ids[$counter], $instance_id, format_string('The iteration works as expected for plugin instance @instance_id', array('@instance_id' => $instance_id)));
$counter++;
}
$this->assertEqual(count($plugin_bag), count($expected_instance_ids), 'The amount of items in plugin bag is as expected.');
}
}
<?php
/**
* @file
* Contains \Drupal\Tests\Component\Plugin\ConfigurablePluginBagTest.
*/
namespace Drupal\Tests\Component\Plugin;
use Drupal\Component\Plugin\ConfigurablePluginInterface;
use Drupal\Component\Plugin\PluginBase;
/**
* Tests the default plugin bag with configurable plugins.
*
* @see \Drupal\Component\Plugin\ConfigurablePluginInterface
* @see \Drupal\Component\Plugin\DefaultPluginBag
*
* @group Drupal
* @group Drupal_Plugin
*/
class ConfigurablePluginBagTest extends PluginBagTestBase {
/**
* Stores all setup plugin instances.
*
* @var \Drupal\Component\Plugin\ConfigurablePluginInterface[]
*/
protected $pluginInstances;
/**
* {@inheritdoc}
*/
public static function getInfo() {
return array(
'name' => 'Configurable plugin bag',
'description' => 'Tests the plugin bag with configurable plugins.',
'group' => 'Plugin API',
);
}
/**
* {@inheritdoc}
*/
protected function getPluginMock($plugin_id, array $definition) {
return new TestConfigurablePlugin($this->config[$plugin_id], $plugin_id, $definition);
}
/**
* Tests the getConfiguration() method with configurable plugins.
*/
public function testConfigurableGetConfiguration() {
$this->setupPluginBag($this->exactly(3));
$config = $this->defaultPluginBag->getConfiguration();
$this->assertSame($this->config, $config);
}
/**
* Tests the setConfiguration() method with configurable plugins.
*/
public function testConfigurableSetConfiguration() {
$this->setupPluginBag($this->exactly(3));
$this->defaultPluginBag->getConfiguration();
$this->defaultPluginBag->setConfiguration('apple', array('value' => 'pineapple'));
$expected = $this->config;
$expected['apple'] = array('value' => 'pineapple');
$config = $this->defaultPluginBag->getConfiguration();
$this->assertSame($expected, $config);
$plugin = $this->pluginInstances['apple'];
$this->assertSame($expected['apple'], $plugin->getConfiguration());
}
}
class TestConfigurablePlugin extends PluginBase implements ConfigurablePluginInterface {
/**
* {@inheritdoc}
*/
public function getConfiguration() {
return $this->configuration;
}
/**
* {@inheritdoc}
*/
public function setConfiguration(array $configuration) {
$this->configuration = $configuration;
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return array();
}
}
......@@ -7,9 +7,6 @@
namespace Drupal\Tests\Component\Plugin;
use Drupal\Component\Plugin\DefaultPluginBag;
use Drupal\Tests\UnitTestCase;
/**
* Tests the default plugin bag.
*
......@@ -18,80 +15,26 @@
* @group Drupal
* @group Drupal_Plugin
*/
class DefaultPluginBagTest extends UnitTestCase {
/**
* The mocked plugin manager.
*
* @var \PHPUnit_Framework_MockObject_MockObject|\Drupal\Component\Plugin\PluginManagerInterface
*/
protected $pluginManager;
class DefaultPluginBagTest extends PluginBagTestBase {
/**
* The tested plugin bag.
*
* @var \PHPUnit_Framework_MockObject_MockObject|\Drupal\Component\Plugin\DefaultPluginBag
* {@inheritdoc}
*/
protected $defaultPluginBag;
/**
* Stores all setup plugin instances.
*
* @var array
*/
protected $pluginInstances;
/**
* Contains the plugin configuration.
*
* @var array
*/
protected $config = array(
'banana' => array('id' => 'banana', 'key' => 'value'),
'cherry' => array('id' => 'cherry', 'key' => 'value'),
'apple' => array('id' => 'apple', 'key' => 'value'),
);
public static function getInfo() {
return array(
'name' => 'Default plugin bag',
'description' => 'Tests the default plugin bag.',
'group' => 'PHP Storage',
'group' => 'Plugin API',
);
}
protected function setUp() {
$this->pluginManager = $this->getMock('Drupal\Component\Plugin\PluginManagerInterface');
$definitions = $this->getPluginDefinitions();
$this->pluginManager->expects($this->any())
->method('getDefinitions')
->will($this->returnValue($definitions));
$this->pluginInstances = array();
$map = array();
foreach ($definitions as $plugin_id => $definition) {
// Create a mock plugin instance.
$mock = $this->getMock('Drupal\Component\Plugin\PluginInspectionInterface');
$mock->expects($this->any())
->method('getPluginId')
->will($this->returnValue($plugin_id));
$this->pluginInstances[$plugin_id] = $mock;
$map[] = array($plugin_id, $this->config[$plugin_id], $this->pluginInstances[$plugin_id]);
}
$this->pluginManager->expects($this->any())
->method('createInstance')
->will($this->returnValueMap($map));
$this->defaultPluginBag = new DefaultPluginBag($this->pluginManager, $this->config);
}
/**
* Tests the has method.
*
* @see \Drupal\Component\Plugin\DefaultPluginBag::has()
*/
public function testHas() {
$this->setupPluginBag();
$definitions = $this->getPluginDefinitions();
$this->assertFalse($this->defaultPluginBag->has($this->randomName()), 'Nonexistent plugin found.');
......@@ -107,9 +50,10 @@ public function testHas() {
* @see \Drupal\Component\Plugin\DefaultPluginBag::get()
*/
public function testGet() {
$this->setupPluginBag($this->once());
$apple = $this->pluginInstances['apple'];
$this->assertEquals($apple, $this->defaultPluginBag->get('apple'));
$this->assertSame($apple, $this->defaultPluginBag->get('apple'));
}
/**
......@@ -118,45 +62,10 @@ public function testGet() {
* @expectedException \Drupal\Component\Plugin\Exception\PluginException
*/
public function testGetNotExistingPlugin() {
$this->setupPluginBag();
$this->defaultPluginBag->get('pear');
}
/**
* Returns some example plugin definitions.
*
* @return array
* The example plugin definitions.
*/
protected function getPluginDefinitions() {
$definitions = array(
'apple' => array(
'id' => 'apple',
'label' => 'Apple',
'color' => 'green',
'class' => 'Drupal\plugin_test\Plugin\plugin_test\fruit\Apple',
'provider' => 'plugin_test',
),
'banana' => array(
'id' => 'banana',
'label' => 'Banana',
'color' => 'yellow',
'uses' => array(
'bread' => 'Banana bread',
),
'class' => 'Drupal\plugin_test\Plugin\plugin_test\fruit\Banana',
'provider' => 'plugin_test',
),
'cherry' => array(
'id' => 'cherry',
'label' => 'Cherry',
'color' => 'red',
'class' => 'Drupal\plugin_test\Plugin\plugin_test\fruit\Cherry',
'provider' => 'plugin_test',
),
);
return $definitions;
}
/**
* Provides test data for testSortHelper.
*
......@@ -185,6 +94,7 @@ public function providerTestSortHelper() {
* @dataProvider providerTestSortHelper
*/
public function testSortHelper($plugin_id_1, $plugin_id_2, $expected) {
$this->setupPluginBag($this->any());
if ($expected != 0) {
$expected = $expected > 0 ? 1 : -1;
}
......@@ -197,6 +107,7 @@ public function testSortHelper($plugin_id_1, $plugin_id_2, $expected) {
* @see \Drupal\Component\Plugin\DefaultPluginBag::getConfiguration()
*/
public function testGetConfiguration() {
$this->setupPluginBag($this->exactly(3));
// The expected order matches $this->config.
$expected = array('banana', 'cherry', 'apple');
......@@ -215,4 +126,92 @@ public function testGetConfiguration() {
$this->assertSame($expected, array_keys($ids), 'After sorting, the order of the instances is also sorted.');
}
/**
* Tests the removeInstanceId() method.
*
* @see \Drupal\Component\Plugin\DefaultPluginBag::removeInstanceId()
*/
public function testRemoveInstanceId() {
$this->setupPluginBag($this->exactly(2));
$this->defaultPluginBag->removeInstanceId('cherry');
$config = $this->defaultPluginBag->getConfiguration();
$this->assertArrayNotHasKey('cherry', $config, 'After removing an instance, the configuration is updated.');
}
/**
* Tests the setConfiguration() method.
*
* @see \Drupal\Component\Plugin\DefaultPluginBag::setConfiguration()
*/
public function testSetConfiguration() {
$this->setupPluginBag($this->exactly(4));
$expected = array(
'id' => 'cherry',
'key' => 'value',
'custom' => 'bananas',
);
$this->defaultPluginBag->setConfiguration('cherry', $expected);
$config = $this->defaultPluginBag->getConfiguration();
$this->assertSame($expected, $config['cherry']);
}
/**
* Tests the count() method.
*/
public function testCount() {
$this->setupPluginBag();
$this->assertSame(3, $this->defaultPluginBag->count());
}
/**
* Tests the clear() method.
*/
public function testClear() {
$this->setupPluginBag($this->exactly(6));
$this->defaultPluginBag->getConfiguration();
$this->defaultPluginBag->getConfiguration();
$this->defaultPluginBag->clear();
$this->defaultPluginBag->getConfiguration();
}
/**
* Tests the setInstanceIds() method.
*/
public function testSetInstanceIds() {
$this->setupPluginBag($this->any());
// Set the instance IDs in a different order than the original.
$this->defaultPluginBag->setInstanceIds(array(
'apple' => 'apple',
'cherry' => 'cherry',
));
$expected = array(
'cherry' => 'cherry',
'apple' => 'apple',
);
$config = $this->defaultPluginBag->getConfiguration();
$instance_ids = $this->defaultPluginBag->getInstanceIds();
$this->assertSame($expected, $instance_ids);
$this->assertSame(array_keys($expected), array_keys($config));
}
/**
* Tests the set() method.
*/
public function testSet() {
$this->setupPluginBag($this->exactly(4));
$instance = $this->pluginManager->createInstance('cherry', $this->config['cherry']);
$this->defaultPluginBag->set('cherry2', $instance);
$this->defaultPluginBag->setConfiguration('cherry2', $this->config['cherry']);
$expected = array(
'banana',
'cherry',
'apple',
'cherry2',
);
$config = $this->defaultPluginBag->getConfiguration();
$this->assertSame($expected, array_keys($config));
}
}
<?php
/**
* @file
* Contains \Drupal\Tests\Component\Plugin\DefaultSinglePluginBagTest.
*/
namespace Drupal\Tests\Component\Plugin;
use Drupal\Component\Plugin\DefaultSinglePluginBag;
/**
* Tests the default single plugin bag.
*
* @see \Drupal\Component\Plugin\DefaultSinglePluginBag
*
* @group Drupal
* @group Drupal_Plugin
*/
class DefaultSinglePluginBagTest extends PluginBagTestBase {
/**
* {@inheritdoc}
*/
public static function getInfo() {
return array(
'name' => 'Default single plugin bag',
'description' => 'Tests the default single plugin bag.',
'group' => 'Plugin API',
);
}
/**
* {@inheritdoc}
*/
protected function setupPluginBag(\PHPUnit_Framework_MockObject_Matcher_InvokedRecorder $create_count = NULL) {
$definitions = $this->getPluginDefinitions();
$this->pluginInstances['apple'] = $this->getPluginMock('apple', $definitions['apple']);
$create_count = $create_count ?: $this->never();
$this->pluginManager->expects($create_count)
->method('createInstance')
->will($this->returnValue($this->pluginInstances['apple']));
$this->defaultPluginBag = new DefaultSinglePluginBag($this->pluginManager, array_keys($this->pluginInstances), array('id' => 'apple', 'key' => 'value'));
}
/**
* Tests the get() method.
*/
public function testGet() {
$this->setupPluginBag($this->once());
$apple = $this->pluginInstances['apple'];
$this->assertSame($apple, $this->defaultPluginBag->get('apple'));
}
}
<?php
/**
* @file
* Contains \Drupal\Tests\Component\Plugin\PluginBagTestBase.
*/
namespace Drupal\Tests\Component\Plugin;
use Drupal\Component\Plugin\DefaultPluginBag;
use Drupal\Tests\UnitTestCase;
/**
* Provides a base class for plugin bag tests.
*/
abstract class PluginBagTestBase extends UnitTestCase {
/**
* The mocked plugin manager.
*
* @var \Drupal\Component\Plugin\PluginManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $pluginManager;
/**
* The tested plugin bag.
*
* @var \Drupal\Component\Plugin\DefaultPluginBag|\PHPUnit_Framework_MockObject_MockObject
*/
protected $defaultPluginBag;
/**
* Stores all setup plugin instances.
*
* @var \Drupal\Component\Plugin\PluginInspectionInterface[]
*/
protected $pluginInstances;
/**
* Contains the plugin configuration.
*
* @var array
*/
protected $config = array(
'banana' => array('id' => 'banana', 'key' => 'value'),
'cherry' => array('id' => 'cherry', 'key' => 'value'),
'apple' => array('id' => 'apple', 'key' => 'value'),
);
protected function setUp() {
$this->pluginManager = $this->getMock('Drupal\Component\Plugin\PluginManagerInterface');
$this->pluginManager->expects($this->any())
->method('getDefinitions')
->will($this->returnValue($this->getPluginDefinitions()));
}
/**
* Sets up the default plugin bag.
*
* @param \PHPUnit_Framework_MockObject_Matcher_InvokedRecorder|null $create_count
* (optional) The number of times that createInstance() is expected to be
* called. For example, $this->any(), $this->once(), $this->exactly(6).
* Defaults to $this->never().
*/
protected function setupPluginBag(\PHPUnit_Framework_MockObject_Matcher_InvokedRecorder $create_count = NULL) {