class PluginManagerBaseTest in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/tests/Drupal/Tests/Component/Plugin/PluginManagerBaseTest.php \Drupal\Tests\Component\Plugin\PluginManagerBaseTest
@coversDefaultClass \Drupal\Component\Plugin\PluginManagerBase @group Plugin
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \Drupal\Tests\PHPUnit_Framework_TestCase
- class \Drupal\Tests\Component\Plugin\PluginManagerBaseTest
Expanded class hierarchy of PluginManagerBaseTest
File
- core/
tests/ Drupal/ Tests/ Component/ Plugin/ PluginManagerBaseTest.php, line 17 - Contains \Drupal\Tests\Component\Plugin\PluginManagerBaseTest.
Namespace
Drupal\Tests\Component\PluginView source
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']);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PluginManagerBaseTest:: |
public | function | A callback method for mocking FactoryInterface objects. | |
PluginManagerBaseTest:: |
public | function | Generates a mocked FactoryInterface object with known properties. | |
PluginManagerBaseTest:: |
public | function | Tests createInstance() with no fallback methods. | |
PluginManagerBaseTest:: |
public | function | Tests createInstance() with a fallback method. | |
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 |