Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\Component\Plugin;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Plugin\Exception\PluginNotFoundException;
|
Chris@17
|
6 use Drupal\Component\Plugin\Mapper\MapperInterface;
|
Chris@17
|
7 use Drupal\Component\Plugin\PluginManagerBase;
|
Chris@0
|
8 use PHPUnit\Framework\TestCase;
|
Chris@0
|
9
|
Chris@0
|
10 /**
|
Chris@0
|
11 * @coversDefaultClass \Drupal\Component\Plugin\PluginManagerBase
|
Chris@0
|
12 * @group Plugin
|
Chris@0
|
13 */
|
Chris@0
|
14 class PluginManagerBaseTest extends TestCase {
|
Chris@0
|
15
|
Chris@0
|
16 /**
|
Chris@0
|
17 * A callback method for mocking FactoryInterface objects.
|
Chris@0
|
18 */
|
Chris@0
|
19 public function createInstanceCallback() {
|
Chris@0
|
20 $args = func_get_args();
|
Chris@0
|
21 $plugin_id = $args[0];
|
Chris@0
|
22 $configuration = $args[1];
|
Chris@0
|
23 if ('invalid' == $plugin_id) {
|
Chris@0
|
24 throw new PluginNotFoundException($plugin_id);
|
Chris@0
|
25 }
|
Chris@0
|
26 return [
|
Chris@0
|
27 'plugin_id' => $plugin_id,
|
Chris@0
|
28 'configuration' => $configuration,
|
Chris@0
|
29 ];
|
Chris@0
|
30 }
|
Chris@0
|
31
|
Chris@0
|
32 /**
|
Chris@0
|
33 * Generates a mocked FactoryInterface object with known properties.
|
Chris@0
|
34 */
|
Chris@0
|
35 public function getMockFactoryInterface($expects_count) {
|
Chris@0
|
36 $mock_factory = $this->getMockBuilder('Drupal\Component\Plugin\Factory\FactoryInterface')
|
Chris@0
|
37 ->setMethods(['createInstance'])
|
Chris@0
|
38 ->getMockForAbstractClass();
|
Chris@0
|
39 $mock_factory->expects($this->exactly($expects_count))
|
Chris@0
|
40 ->method('createInstance')
|
Chris@0
|
41 ->willReturnCallback([$this, 'createInstanceCallback']);
|
Chris@0
|
42 return $mock_factory;
|
Chris@0
|
43 }
|
Chris@0
|
44
|
Chris@0
|
45 /**
|
Chris@0
|
46 * Tests createInstance() with no fallback methods.
|
Chris@0
|
47 *
|
Chris@0
|
48 * @covers ::createInstance
|
Chris@0
|
49 */
|
Chris@0
|
50 public function testCreateInstance() {
|
Chris@0
|
51 $manager = $this->getMockBuilder('Drupal\Component\Plugin\PluginManagerBase')
|
Chris@0
|
52 ->getMockForAbstractClass();
|
Chris@0
|
53 // PluginManagerBase::createInstance() looks for a factory object and then
|
Chris@0
|
54 // calls createInstance() on it. So we have to mock a factory object.
|
Chris@0
|
55 $factory_ref = new \ReflectionProperty($manager, 'factory');
|
Chris@0
|
56 $factory_ref->setAccessible(TRUE);
|
Chris@0
|
57 $factory_ref->setValue($manager, $this->getMockFactoryInterface(1));
|
Chris@0
|
58
|
Chris@0
|
59 // Finally the test.
|
Chris@0
|
60 $configuration_array = ['config' => 'something'];
|
Chris@0
|
61 $result = $manager->createInstance('valid', $configuration_array);
|
Chris@0
|
62 $this->assertEquals('valid', $result['plugin_id']);
|
Chris@0
|
63 $this->assertEquals($configuration_array, $result['configuration']);
|
Chris@0
|
64 }
|
Chris@0
|
65
|
Chris@0
|
66 /**
|
Chris@0
|
67 * Tests createInstance() with a fallback method.
|
Chris@0
|
68 *
|
Chris@0
|
69 * @covers ::createInstance
|
Chris@0
|
70 */
|
Chris@0
|
71 public function testCreateInstanceFallback() {
|
Chris@0
|
72 // We use our special stub class which extends PluginManagerBase and also
|
Chris@0
|
73 // implements FallbackPluginManagerInterface.
|
Chris@0
|
74 $manager = new StubFallbackPluginManager();
|
Chris@0
|
75 // Put our stubbed factory on the base object.
|
Chris@0
|
76 $factory_ref = new \ReflectionProperty($manager, 'factory');
|
Chris@0
|
77 $factory_ref->setAccessible(TRUE);
|
Chris@0
|
78
|
Chris@0
|
79 // Set up the configuration array.
|
Chris@0
|
80 $configuration_array = ['config' => 'something'];
|
Chris@0
|
81
|
Chris@0
|
82 // Test with fallback interface and valid plugin_id.
|
Chris@0
|
83 $factory_ref->setValue($manager, $this->getMockFactoryInterface(1));
|
Chris@0
|
84 $no_fallback_result = $manager->createInstance('valid', $configuration_array);
|
Chris@0
|
85 $this->assertEquals('valid', $no_fallback_result['plugin_id']);
|
Chris@0
|
86 $this->assertEquals($configuration_array, $no_fallback_result['configuration']);
|
Chris@0
|
87
|
Chris@0
|
88 // Test with fallback interface and invalid plugin_id.
|
Chris@0
|
89 $factory_ref->setValue($manager, $this->getMockFactoryInterface(2));
|
Chris@0
|
90 $fallback_result = $manager->createInstance('invalid', $configuration_array);
|
Chris@0
|
91 $this->assertEquals('invalid_fallback', $fallback_result['plugin_id']);
|
Chris@0
|
92 $this->assertEquals($configuration_array, $fallback_result['configuration']);
|
Chris@0
|
93 }
|
Chris@0
|
94
|
Chris@17
|
95 /**
|
Chris@17
|
96 * @covers ::getInstance
|
Chris@17
|
97 */
|
Chris@17
|
98 public function testGetInstance() {
|
Chris@17
|
99 $options = [
|
Chris@17
|
100 'foo' => 'F00',
|
Chris@17
|
101 'bar' => 'bAr',
|
Chris@17
|
102 ];
|
Chris@17
|
103 $instance = new \stdClass();
|
Chris@17
|
104 $mapper = $this->prophesize(MapperInterface::class);
|
Chris@17
|
105 $mapper->getInstance($options)
|
Chris@17
|
106 ->shouldBeCalledTimes(1)
|
Chris@17
|
107 ->willReturn($instance);
|
Chris@17
|
108 $manager = new StubPluginManagerBaseWithMapper($mapper->reveal());
|
Chris@17
|
109 $this->assertEquals($instance, $manager->getInstance($options));
|
Chris@17
|
110 }
|
Chris@17
|
111
|
Chris@17
|
112 /**
|
Chris@17
|
113 * @covers ::getInstance
|
Chris@17
|
114 */
|
Chris@17
|
115 public function testGetInstanceWithoutMapperShouldThrowException() {
|
Chris@17
|
116 $options = [
|
Chris@17
|
117 'foo' => 'F00',
|
Chris@17
|
118 'bar' => 'bAr',
|
Chris@17
|
119 ];
|
Chris@17
|
120 /** @var \Drupal\Component\Plugin\PluginManagerBase $manager */
|
Chris@17
|
121 $manager = $this->getMockBuilder(PluginManagerBase::class)
|
Chris@17
|
122 ->getMockForAbstractClass();
|
Chris@17
|
123 // Set the expected exception thrown by ::getInstance.
|
Chris@17
|
124 if (method_exists($this, 'expectException')) {
|
Chris@17
|
125 $this->expectException(\BadMethodCallException::class);
|
Chris@17
|
126 $this->expectExceptionMessage(sprintf('%s does not support this method unless %s::$mapper is set.', get_class($manager), get_class($manager)));
|
Chris@17
|
127 }
|
Chris@17
|
128 else {
|
Chris@17
|
129 $this->setExpectedException(\BadMethodCallException::class, sprintf('%s does not support this method unless %s::$mapper is set.', get_class($manager), get_class($manager)));
|
Chris@17
|
130 }
|
Chris@17
|
131 $manager->getInstance($options);
|
Chris@17
|
132 }
|
Chris@17
|
133
|
Chris@0
|
134 }
|