View source
<?php
namespace Drupal\Tests\Core\Plugin;
use Drupal\Core\Plugin\DefaultLazyPluginCollection;
use Drupal\Tests\UnitTestCase;
use PHPUnit\Framework\MockObject\Matcher\InvokedRecorder;
abstract class LazyPluginCollectionTestBase extends UnitTestCase {
protected $pluginManager;
protected $defaultPluginCollection;
protected $pluginInstances;
protected $config = [
'banana' => [
'id' => 'banana',
'key' => 'value',
],
'cherry' => [
'id' => 'cherry',
'key' => 'value',
],
'apple' => [
'id' => 'apple',
'key' => 'value',
],
];
protected function setUp() {
$this->pluginManager = $this
->createMock('Drupal\\Component\\Plugin\\PluginManagerInterface');
$this->pluginManager
->expects($this
->any())
->method('getDefinitions')
->will($this
->returnValue($this
->getPluginDefinitions()));
}
protected function setupPluginCollection(InvokedRecorder $create_count = NULL) {
$this->pluginInstances = [];
$map = [];
foreach ($this
->getPluginDefinitions() as $plugin_id => $definition) {
$this->pluginInstances[$plugin_id] = $this
->getPluginMock($plugin_id, $definition);
$map[] = [
$plugin_id,
$this->config[$plugin_id],
$this->pluginInstances[$plugin_id],
];
}
$create_count = $create_count ?: $this
->never();
$this->pluginManager
->expects($create_count)
->method('createInstance')
->will($this
->returnCallback([
$this,
'returnPluginMap',
]));
$this->defaultPluginCollection = new DefaultLazyPluginCollection($this->pluginManager, $this->config);
}
public function returnPluginMap($plugin_id) {
if (isset($this->pluginInstances[$plugin_id])) {
return $this->pluginInstances[$plugin_id];
}
}
protected function getPluginMock($plugin_id, array $definition) {
$mock = $this
->createMock('Drupal\\Component\\Plugin\\PluginInspectionInterface');
$mock
->expects($this
->any())
->method('getPluginId')
->will($this
->returnValue($plugin_id));
return $mock;
}
protected function getPluginDefinitions() {
$definitions = [
'apple' => [
'id' => 'apple',
'label' => 'Apple',
'color' => 'green',
'class' => 'Drupal\\plugin_test\\Plugin\\plugin_test\\fruit\\Apple',
'provider' => 'plugin_test',
],
'banana' => [
'id' => 'banana',
'label' => 'Banana',
'color' => 'yellow',
'uses' => [
'bread' => 'Banana bread',
],
'class' => 'Drupal\\plugin_test\\Plugin\\plugin_test\\fruit\\Banana',
'provider' => 'plugin_test',
],
'cherry' => [
'id' => 'cherry',
'label' => 'Cherry',
'color' => 'red',
'class' => 'Drupal\\plugin_test\\Plugin\\plugin_test\\fruit\\Cherry',
'provider' => 'plugin_test',
],
];
return $definitions;
}
}