3 namespace Drupal\Tests\Component\Plugin;
5 use Drupal\Component\Plugin\Exception\PluginNotFoundException;
6 use Drupal\Tests\UnitTestCase;
9 * @coversDefaultClass \Drupal\Component\Plugin\PluginManagerBase
12 class PluginManagerBaseTest extends UnitTestCase {
15 * A callback method for mocking FactoryInterface objects.
17 public function createInstanceCallback() {
18 $args = func_get_args();
19 $plugin_id = $args[0];
20 $configuration = $args[1];
21 if ('invalid' == $plugin_id) {
22 throw new PluginNotFoundException($plugin_id);
25 'plugin_id' => $plugin_id,
26 'configuration' => $configuration,
31 * Generates a mocked FactoryInterface object with known properties.
33 public function getMockFactoryInterface($expects_count) {
34 $mock_factory = $this->getMockBuilder('Drupal\Component\Plugin\Factory\FactoryInterface')
35 ->setMethods(['createInstance'])
36 ->getMockForAbstractClass();
37 $mock_factory->expects($this->exactly($expects_count))
38 ->method('createInstance')
39 ->willReturnCallback([$this, 'createInstanceCallback']);
44 * Tests createInstance() with no fallback methods.
46 * @covers ::createInstance
48 public function testCreateInstance() {
49 $manager = $this->getMockBuilder('Drupal\Component\Plugin\PluginManagerBase')
50 ->getMockForAbstractClass();
51 // PluginManagerBase::createInstance() looks for a factory object and then
52 // calls createInstance() on it. So we have to mock a factory object.
53 $factory_ref = new \ReflectionProperty($manager, 'factory');
54 $factory_ref->setAccessible(TRUE);
55 $factory_ref->setValue($manager, $this->getMockFactoryInterface(1));
58 $configuration_array = ['config' => 'something'];
59 $result = $manager->createInstance('valid', $configuration_array);
60 $this->assertEquals('valid', $result['plugin_id']);
61 $this->assertArrayEquals($configuration_array, $result['configuration']);
65 * Tests createInstance() with a fallback method.
67 * @covers ::createInstance
69 public function testCreateInstanceFallback() {
70 // We use our special stub class which extends PluginManagerBase and also
71 // implements FallbackPluginManagerInterface.
72 $manager = new StubFallbackPluginManager();
73 // Put our stubbed factory on the base object.
74 $factory_ref = new \ReflectionProperty($manager, 'factory');
75 $factory_ref->setAccessible(TRUE);
77 // Set up the configuration array.
78 $configuration_array = ['config' => 'something'];
80 // Test with fallback interface and valid plugin_id.
81 $factory_ref->setValue($manager, $this->getMockFactoryInterface(1));
82 $no_fallback_result = $manager->createInstance('valid', $configuration_array);
83 $this->assertEquals('valid', $no_fallback_result['plugin_id']);
84 $this->assertArrayEquals($configuration_array, $no_fallback_result['configuration']);
86 // Test with fallback interface and invalid plugin_id.
87 $factory_ref->setValue($manager, $this->getMockFactoryInterface(2));
88 $fallback_result = $manager->createInstance('invalid', $configuration_array);
89 $this->assertEquals('invalid_fallback', $fallback_result['plugin_id']);
90 $this->assertArrayEquals($configuration_array, $fallback_result['configuration']);