Upgraded drupal core with security updates
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Plugin / LazyPluginCollectionTestBase.php
1 <?php
2
3 namespace Drupal\Tests\Core\Plugin;
4
5 use Drupal\Core\Plugin\DefaultLazyPluginCollection;
6 use Drupal\Tests\UnitTestCase;
7
8 /**
9  * Provides a base class for plugin collection tests.
10  */
11 abstract class LazyPluginCollectionTestBase extends UnitTestCase {
12
13   /**
14    * The mocked plugin manager.
15    *
16    * @var \Drupal\Component\Plugin\PluginManagerInterface|\PHPUnit_Framework_MockObject_MockObject
17    */
18   protected $pluginManager;
19
20   /**
21    * The tested plugin collection.
22    *
23    * @var \Drupal\Core\Plugin\DefaultLazyPluginCollection|\PHPUnit_Framework_MockObject_MockObject
24    */
25   protected $defaultPluginCollection;
26
27   /**
28    * Stores all setup plugin instances.
29    *
30    * @var \Drupal\Component\Plugin\PluginInspectionInterface[]
31    */
32   protected $pluginInstances;
33
34   /**
35    * Contains the plugin configuration.
36    *
37    * @var array
38    */
39   protected $config = [
40     'banana' => ['id' => 'banana', 'key' => 'value'],
41     'cherry' => ['id' => 'cherry', 'key' => 'value'],
42     'apple' => ['id' => 'apple', 'key' => 'value'],
43   ];
44
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()));
50
51   }
52
53   /**
54    * Sets up the default plugin collection.
55    *
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().
60    */
61   protected function setupPluginCollection(\PHPUnit_Framework_MockObject_Matcher_InvokedRecorder $create_count = NULL) {
62     $this->pluginInstances = [];
63     $map = [];
64     foreach ($this->getPluginDefinitions() as $plugin_id => $definition) {
65       // Create a mock plugin instance.
66       $this->pluginInstances[$plugin_id] = $this->getPluginMock($plugin_id, $definition);
67
68       $map[] = [$plugin_id, $this->config[$plugin_id], $this->pluginInstances[$plugin_id]];
69     }
70     $create_count = $create_count ?: $this->never();
71     $this->pluginManager->expects($create_count)
72       ->method('createInstance')
73       ->will($this->returnCallback([$this, 'returnPluginMap']));
74
75     $this->defaultPluginCollection = new DefaultLazyPluginCollection($this->pluginManager, $this->config);
76   }
77
78   /**
79    * Return callback for createInstance.
80    *
81    * @param string $plugin_id
82    *   The plugin ID to return the mock plugin for.
83    *
84    * @return \Drupal\Component\Plugin\PluginInspectionInterface|\PHPUnit_Framework_MockObject_MockObject
85    *   The mock plugin object.
86    */
87   public function returnPluginMap($plugin_id) {
88     if (isset($this->pluginInstances[$plugin_id])) {
89       return $this->pluginInstances[$plugin_id];
90     }
91   }
92
93   /**
94    * Returns a mocked plugin object.
95    *
96    * @param string $plugin_id
97    *   The plugin ID.
98    * @param array $definition
99    *   The plugin definition.
100    *
101    * @return \Drupal\Component\Plugin\PluginInspectionInterface|\PHPUnit_Framework_MockObject_MockObject
102    */
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));
109     return $mock;
110   }
111
112   /**
113    * Returns some example plugin definitions.
114    *
115    * @return array
116    *   The example plugin definitions.
117    */
118   protected function getPluginDefinitions() {
119     $definitions = [
120       'apple' => [
121         'id' => 'apple',
122         'label' => 'Apple',
123         'color' => 'green',
124         'class' => 'Drupal\plugin_test\Plugin\plugin_test\fruit\Apple',
125         'provider' => 'plugin_test',
126       ],
127       'banana' => [
128         'id' => 'banana',
129         'label' => 'Banana',
130         'color' => 'yellow',
131         'uses' => [
132           'bread' => 'Banana bread',
133         ],
134         'class' => 'Drupal\plugin_test\Plugin\plugin_test\fruit\Banana',
135         'provider' => 'plugin_test',
136       ],
137       'cherry' => [
138         'id' => 'cherry',
139         'label' => 'Cherry',
140         'color' => 'red',
141         'class' => 'Drupal\plugin_test\Plugin\plugin_test\fruit\Cherry',
142         'provider' => 'plugin_test',
143       ],
144     ];
145     return $definitions;
146   }
147
148 }