LazyPluginCollectionTestBase.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace Drupal\Tests\Core\Plugin;
  3. use Drupal\Core\Plugin\DefaultLazyPluginCollection;
  4. use Drupal\Tests\UnitTestCase;
  5. use PHPUnit\Framework\MockObject\Matcher\InvokedRecorder;
  6. /**
  7. * Provides a base class for plugin collection tests.
  8. */
  9. abstract class LazyPluginCollectionTestBase extends UnitTestCase {
  10. /**
  11. * The mocked plugin manager.
  12. *
  13. * @var \Drupal\Component\Plugin\PluginManagerInterface|\PHPUnit\Framework\MockObject\MockObject
  14. */
  15. protected $pluginManager;
  16. /**
  17. * The tested plugin collection.
  18. *
  19. * @var \Drupal\Core\Plugin\DefaultLazyPluginCollection|\PHPUnit\Framework\MockObject\MockObject
  20. */
  21. protected $defaultPluginCollection;
  22. /**
  23. * Stores all setup plugin instances.
  24. *
  25. * @var \Drupal\Component\Plugin\PluginInspectionInterface[]
  26. */
  27. protected $pluginInstances;
  28. /**
  29. * Contains the plugin configuration.
  30. *
  31. * @var array
  32. */
  33. protected $config = [
  34. 'banana' => ['id' => 'banana', 'key' => 'value'],
  35. 'cherry' => ['id' => 'cherry', 'key' => 'value'],
  36. 'apple' => ['id' => 'apple', 'key' => 'value'],
  37. ];
  38. protected function setUp() {
  39. $this->pluginManager = $this->createMock('Drupal\Component\Plugin\PluginManagerInterface');
  40. $this->pluginManager->expects($this->any())
  41. ->method('getDefinitions')
  42. ->will($this->returnValue($this->getPluginDefinitions()));
  43. }
  44. /**
  45. * Sets up the default plugin collection.
  46. *
  47. * @param \PHPUnit\Framework\MockObject\Matcher\InvokedRecorder|null $create_count
  48. * (optional) The number of times that createInstance() is expected to be
  49. * called. For example, $this->any(), $this->once(), $this->exactly(6).
  50. * Defaults to $this->never().
  51. */
  52. protected function setupPluginCollection(InvokedRecorder $create_count = NULL) {
  53. $this->pluginInstances = [];
  54. $map = [];
  55. foreach ($this->getPluginDefinitions() as $plugin_id => $definition) {
  56. // Create a mock plugin instance.
  57. $this->pluginInstances[$plugin_id] = $this->getPluginMock($plugin_id, $definition);
  58. $map[] = [$plugin_id, $this->config[$plugin_id], $this->pluginInstances[$plugin_id]];
  59. }
  60. $create_count = $create_count ?: $this->never();
  61. $this->pluginManager->expects($create_count)
  62. ->method('createInstance')
  63. ->will($this->returnCallback([$this, 'returnPluginMap']));
  64. $this->defaultPluginCollection = new DefaultLazyPluginCollection($this->pluginManager, $this->config);
  65. }
  66. /**
  67. * Return callback for createInstance.
  68. *
  69. * @param string $plugin_id
  70. * The plugin ID to return the mock plugin for.
  71. *
  72. * @return \Drupal\Component\Plugin\PluginInspectionInterface|\PHPUnit\Framework\MockObject\MockObject
  73. * The mock plugin object.
  74. */
  75. public function returnPluginMap($plugin_id) {
  76. if (isset($this->pluginInstances[$plugin_id])) {
  77. return $this->pluginInstances[$plugin_id];
  78. }
  79. }
  80. /**
  81. * Returns a mocked plugin object.
  82. *
  83. * @param string $plugin_id
  84. * The plugin ID.
  85. * @param array $definition
  86. * The plugin definition.
  87. *
  88. * @return \Drupal\Component\Plugin\PluginInspectionInterface|\PHPUnit\Framework\MockObject\MockObject
  89. */
  90. protected function getPluginMock($plugin_id, array $definition) {
  91. // Create a mock plugin instance.
  92. $mock = $this->createMock('Drupal\Component\Plugin\PluginInspectionInterface');
  93. $mock->expects($this->any())
  94. ->method('getPluginId')
  95. ->will($this->returnValue($plugin_id));
  96. return $mock;
  97. }
  98. /**
  99. * Returns some example plugin definitions.
  100. *
  101. * @return array
  102. * The example plugin definitions.
  103. */
  104. protected function getPluginDefinitions() {
  105. $definitions = [
  106. 'apple' => [
  107. 'id' => 'apple',
  108. 'label' => 'Apple',
  109. 'color' => 'green',
  110. 'class' => 'Drupal\plugin_test\Plugin\plugin_test\fruit\Apple',
  111. 'provider' => 'plugin_test',
  112. ],
  113. 'banana' => [
  114. 'id' => 'banana',
  115. 'label' => 'Banana',
  116. 'color' => 'yellow',
  117. 'uses' => [
  118. 'bread' => 'Banana bread',
  119. ],
  120. 'class' => 'Drupal\plugin_test\Plugin\plugin_test\fruit\Banana',
  121. 'provider' => 'plugin_test',
  122. ],
  123. 'cherry' => [
  124. 'id' => 'cherry',
  125. 'label' => 'Cherry',
  126. 'color' => 'red',
  127. 'class' => 'Drupal\plugin_test\Plugin\plugin_test\fruit\Cherry',
  128. 'provider' => 'plugin_test',
  129. ],
  130. ];
  131. return $definitions;
  132. }
  133. }