3 namespace Drupal\Tests\Core\Plugin;
5 use Drupal\Core\Plugin\DefaultLazyPluginCollection;
6 use Drupal\Tests\UnitTestCase;
9 * Provides a base class for plugin collection tests.
11 abstract class LazyPluginCollectionTestBase extends UnitTestCase {
14 * The mocked plugin manager.
16 * @var \Drupal\Component\Plugin\PluginManagerInterface|\PHPUnit_Framework_MockObject_MockObject
18 protected $pluginManager;
21 * The tested plugin collection.
23 * @var \Drupal\Core\Plugin\DefaultLazyPluginCollection|\PHPUnit_Framework_MockObject_MockObject
25 protected $defaultPluginCollection;
28 * Stores all setup plugin instances.
30 * @var \Drupal\Component\Plugin\PluginInspectionInterface[]
32 protected $pluginInstances;
35 * Contains the plugin configuration.
40 'banana' => ['id' => 'banana', 'key' => 'value'],
41 'cherry' => ['id' => 'cherry', 'key' => 'value'],
42 'apple' => ['id' => 'apple', 'key' => 'value'],
45 protected function setUp() {
46 $this->pluginManager = $this->getMock('Drupal\Component\Plugin\PluginManagerInterface');
47 $this->pluginManager->expects($this->any())
48 ->method('getDefinitions')
49 ->will($this->returnValue($this->getPluginDefinitions()));
54 * Sets up the default plugin collection.
56 * @param \PHPUnit_Framework_MockObject_Matcher_InvokedRecorder|null $create_count
57 * (optional) The number of times that createInstance() is expected to be
58 * called. For example, $this->any(), $this->once(), $this->exactly(6).
59 * Defaults to $this->never().
61 protected function setupPluginCollection(\PHPUnit_Framework_MockObject_Matcher_InvokedRecorder $create_count = NULL) {
62 $this->pluginInstances = [];
64 foreach ($this->getPluginDefinitions() as $plugin_id => $definition) {
65 // Create a mock plugin instance.
66 $this->pluginInstances[$plugin_id] = $this->getPluginMock($plugin_id, $definition);
68 $map[] = [$plugin_id, $this->config[$plugin_id], $this->pluginInstances[$plugin_id]];
70 $create_count = $create_count ?: $this->never();
71 $this->pluginManager->expects($create_count)
72 ->method('createInstance')
73 ->will($this->returnCallback([$this, 'returnPluginMap']));
75 $this->defaultPluginCollection = new DefaultLazyPluginCollection($this->pluginManager, $this->config);
79 * Return callback for createInstance.
81 * @param string $plugin_id
82 * The plugin ID to return the mock plugin for.
84 * @return \Drupal\Component\Plugin\PluginInspectionInterface|\PHPUnit_Framework_MockObject_MockObject
85 * The mock plugin object.
87 public function returnPluginMap($plugin_id) {
88 if (isset($this->pluginInstances[$plugin_id])) {
89 return $this->pluginInstances[$plugin_id];
94 * Returns a mocked plugin object.
96 * @param string $plugin_id
98 * @param array $definition
99 * The plugin definition.
101 * @return \Drupal\Component\Plugin\PluginInspectionInterface|\PHPUnit_Framework_MockObject_MockObject
103 protected function getPluginMock($plugin_id, array $definition) {
104 // Create a mock plugin instance.
105 $mock = $this->getMock('Drupal\Component\Plugin\PluginInspectionInterface');
106 $mock->expects($this->any())
107 ->method('getPluginId')
108 ->will($this->returnValue($plugin_id));
113 * Returns some example plugin definitions.
116 * The example plugin definitions.
118 protected function getPluginDefinitions() {
124 'class' => 'Drupal\plugin_test\Plugin\plugin_test\fruit\Apple',
125 'provider' => 'plugin_test',
132 'bread' => 'Banana bread',
134 'class' => 'Drupal\plugin_test\Plugin\plugin_test\fruit\Banana',
135 'provider' => 'plugin_test',
141 'class' => 'Drupal\plugin_test\Plugin\plugin_test\fruit\Cherry',
142 'provider' => 'plugin_test',