Completed
Pull Request — master (#157)
by
unknown
01:57
created

testFieldManagerMatchedDefinitions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 9
nc 1
nop 3
1
<?php
2
3
namespace Drupal\Tests\Driver;
4
5
/**
6
 * Tests the Driver's plugin managers.
7
 */
8
class DriverPluginManagersTest extends \PHPUnit_Framework_TestCase {
9
10
  /**
11
   * {@inheritdoc}
12
   */
13
  public function tearDown() {
14
    \Mockery::close();
15
  }
16
17
  /**
18
   * Factory method to build and returned a mocked field handler.
19
   *
20
   * @param array $target
21
   *   The properties to find a matching plugin for.
22
   * @param array $mockFilters
23
   *   The possible filters for the mocked plugin manager.
24
   * @param array $mockCriteria
25
   *   The specificity criteria for the mocked plugin manager.
26
   * @param array $mockDefinitions
27
   *   The plugins to be discovered by the mocked plugin manager.
28
   *
29
   * @return array
30
   *   The ids of the matching mock definitions.
31
   */
32 View Code Duplication
  public function getMatchedPluginIds(array $target, array $mockFilters, array $mockCriteria, array $mockDefinitions) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
33
34
    $mock = \Mockery::mock('Drupal\Driver\Plugin\DriverPluginManagerBase');
35
    $mock->makePartial();
36
    $mock->shouldAllowMockingProtectedMethods();
37
    $mock->shouldReceive('getFilters')->andReturn($mockFilters);
38
    $mock->shouldReceive('getSpecificityCriteria')->andReturn($mockCriteria);
39
    $mock->shouldReceive('getDefinitions')->andReturn($mockDefinitions);
40
41
    $matchedDefinitions = $mock->getMatchedDefinitions($target);
0 ignored issues
show
Bug introduced by
The method getMatchedDefinitions() does not seem to exist on object<Mockery\MockInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
42
    $matchedIds = array_column($matchedDefinitions, 'id');
43
    return $matchedIds;
44
  }
45
46
  /**
47
   * Tests the plugin manager base's definition matching.
48
   *
49
   * @param array $target
50
   *   The properties to find a matching plugin for.
51
   * @param array $mockFilters
52
   *   The possible filters for the mocked plugin manager.
53
   * @param array $mockCriteria
54
   *   The specificity criteria for the mocked plugin manager.
55
   * @param array $mockDefinitions
56
   *   The plugins to be discovered by the mocked plugin manager.
57
   * @param array $expectedIds
58
   *   The ids of the mock definitions that match the target.
59
   *
60
   * @dataProvider managerBaseMatchedDefinitionsData
61
   */
62 View Code Duplication
  public function testManagerBaseMatchedDefinitions(array $target, array $mockFilters, array $mockCriteria, array $mockDefinitions, array $expectedIds) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
    $mock = \Mockery::mock('Drupal\Driver\Plugin\DriverPluginManagerBase');
64
    $mock->makePartial();
65
    $mock->shouldAllowMockingProtectedMethods();
66
    $mock->shouldReceive('getFilters')->andReturn($mockFilters);
67
    $mock->shouldReceive('getSpecificityCriteria')->andReturn($mockCriteria);
68
    $mock->shouldReceive('getDefinitions')->andReturn($mockDefinitions);
69
70
    $matchedDefinitions = $mock->getMatchedDefinitions($target);
0 ignored issues
show
Bug introduced by
The method getMatchedDefinitions() does not seem to exist on object<Mockery\MockInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
71
    $ids = array_column($matchedDefinitions, 'id');
72
    $this->assertEquals($expectedIds, $ids);
73
  }
74
75
  /**
76
   * Data provider for testManagerBaseMatchedDefinitions().
77
   *
78
   * @return array
79
   *   An array of test data.
80
   */
81
  public function managerBaseMatchedDefinitionsData() {
82
    $mockFilters = ['a', 'b'];
83
84
    $mockCriteria = [
85
        ['a', 'b'],
86
        ['a'],
87
        ['b'],
88
    ];
89
90
    $mockDefinitions = [
91
        ['id' => 'A', 'weight' => 0, 'a' => [1], 'b' => [1]],
92
        ['id' => 'B', 'weight' => 0, 'a' => [1]],
93
        ['id' => 'C', 'weight' => 0, 'b' => [1]],
94
        ['id' => 'D', 'weight' => 0, 'a' => [2], 'b' => [1]],
95
        ['id' => 'E', 'weight' => 0, 'a' => [1], 'b' => [2]],
96
    ];
97
98
    $multivalueDefinitions = $mockDefinitions;
99
    $multivalueDefinitions[0]['a'] = [1, 3];
100
    $multivalueDefinitions[2]['b'] = [1, 2];
101
102
    $alphaAdjustedDefinitions = $mockDefinitions;
103
    $alphaAdjustedDefinitions[] = $alphaAdjustedDefinitions[0];
104
    $alphaAdjustedDefinitions[0]['id'] = 'F';
105
106
    return array(
107
    // Test non-matching values are rejected over multiple filters.
108
    [
109
    ['a' => 2, 'b' => 2],
110
      $mockFilters,
111
      $mockCriteria,
112
      $mockDefinitions,
113
    [],
114
    ],
115
116
    // Test all matching values are accepted.
117
    [
118
    ['a' => 1, 'b' => 1],
119
      $mockFilters,
120
      $mockCriteria,
121
      $mockDefinitions,
122
    ['A', 'B', 'C'],
123
    ],
124
125
    // Test specific comes before general regardless of definition order.
126
    [
127
    ['a' => 1, 'b' => 2],
128
      $mockFilters,
129
      $mockCriteria,
130
      $mockDefinitions,
131
    ['E', 'B'],
132
    ],
133
134
    // Test specific comes before general regardless of definition order.
135
    [
136
    ['a' => 2, 'b' => 1],
137
      $mockFilters,
138
      $mockCriteria,
139
      $mockDefinitions,
140
    ['D', 'C'],
141
    ],
142
143
    // Test weight overrules specificity.
144
    [
145
    ['a' => 1, 'b' => 1],
146
      $mockFilters,
147
      $mockCriteria,
148
    [
149
      ['id' => 'A', 'weight' => 0, 'a' => [1], 'b' => [1]],
150
      ['id' => 'B', 'weight' => 10, 'a' => [1]],
151
      ['id' => 'C', 'weight' => 0, 'b' => [1]],
152
      ['id' => 'D', 'weight' => 0, 'a' => [2], 'b' => [1]],
153
      ['id' => 'E', 'weight' => 0, 'a' => [1], 'b' => [2]],
154
    ],
155
    ['B', 'A', 'C'],
156
    ],
157
158
    // Test value in multivalue definitions.
159
    [
160
    ['a' => 1, 'b' => 1],
161
      $mockFilters,
162
      $mockCriteria,
163
      $multivalueDefinitions,
164
    ['A', 'B', 'C'],
165
    ],
166
167
    // Test plugins are sorted by id if weight and specificity are equal.
168
    [
169
    ['a' => 1, 'b' => 1],
170
      $mockFilters,
171
      $mockCriteria,
172
      $alphaAdjustedDefinitions,
173
    ['A', 'F', 'B', 'C'],
174
    ],
175
176
    );
177
  }
178
179
  /**
180
   * Tests the plugin manager base's definition matching.
181
   *
182
   * @param array $target
183
   *   The properties to find a matching plugin for.
184
   * @param array $mockDefinitions
185
   *   The plugins to be discovered by the mocked plugin manager.
186
   * @param array $expectedIds
187
   *   The ids of the mock definitions that match the target.
188
   *
189
   * @dataProvider fieldManagerMatchedDefinitionsData
190
   */
191 View Code Duplication
  public function testFieldManagerMatchedDefinitions(array $target, array $mockDefinitions, array $expectedIds) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
192
    $mock = \Mockery::mock('Drupal\Driver\Plugin\DriverFieldPluginManager');
193
    $mock->makePartial();
194
    $mock->shouldReceive('getDefinitions')->andReturn($mockDefinitions);
195
    $mock->shouldAllowMockingProtectedMethods();
196
    $mock->shouldReceive('getFilterableTarget')->andReturn($target);
197
198
    $matchedDefinitions = $mock->getMatchedDefinitions($target);
0 ignored issues
show
Bug introduced by
The method getMatchedDefinitions() does not seem to exist on object<Mockery\MockInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
199
    $ids = array_column($matchedDefinitions, 'id');
200
    $this->assertEquals($expectedIds, $ids);
201
  }
202
203
  /**
204
   * Data provider for testManagerBaseMatchedDefinitions().
205
   *
206
   * @return array
207
   *   An array of test data.
208
   */
209
  public function fieldManagerMatchedDefinitionsData() {
210
    $mockDefinitions = [
211
        [
212
          'id' => 'A',
213
          'weight' => 0,
214
          'entityTypes' => ['node'],
215
          'fieldTypes' => ['datetime'],
216
          'fieldNames' => ['datefield'],
217
        ],
218
        [
219
          'id' => 'B',
220
          'weight' => 0,
221
          'fieldTypes' => ['datetime'],
222
        ],
223
        [
224
          'id' => 'C',
225
          'weight' => 0,
226
          'entityTypes' => ['node'],
227
          'fieldNames' => ['datefield'],
228
        ],
229
        [
230
          'id' => 'D',
231
          'weight' => 0,
232
          'entityTypes' => ['node'],
233
        ],
234
        [
235
          'id' => 'E',
236
          'weight' => 0,
237
          'entityTypes' => ['node'],
238
          'entityBundles' => ['article'],
239
          'fieldTypes' => ['datetime'],
240
          'fieldNames' => ['datefield'],
241
        ],
242
        [
243
          'id' => 'F',
244
          'weight' => 0,
245
        ],
246
    ];
247
248
    $reweightedDefinitions = $mockDefinitions;
249
    $reweightedDefinitions[0]['weight'] = 10;
250
251
    $capitalisedDefinitions = $mockDefinitions;
252
    $capitalisedDefinitions[0]['entityTypes'][0] = 'Node';
253
    $capitalisedDefinitions[0]['fieldTypes'][0] = 'Datetime';
254
    $capitalisedDefinitions[0]['fieldNames'][0] = 'DATEFIELD';
255
256
    return array(
257
    // Test specificity order.
258
    [
259
    [
260
      'entityTypes' => 'node',
261
      'entityBundles' => 'article',
262
      'fieldTypes' => 'datetime',
263
      'fieldNames' => 'datefield',
264
    ],
265
      $mockDefinitions,
266
    ['E', 'A', 'C', 'B', 'D', 'F'],
267
    ],
268
269
    // Test entity type must not conflict.
270
    [
271
    [
272
      'entityTypes' => 'user',
273
      'entityBundles' => 'article',
274
      'fieldTypes' => 'datetime',
275
      'fieldNames' => 'datefield',
276
    ],
277
      $mockDefinitions,
278
    ['B', 'F'],
279
    ],
280
281
    // Test entity bundle must not conflict.
282
    [
283
    [
284
      'entityTypes' => 'node',
285
      'entityBundles' => 'page',
286
      'fieldTypes' => 'datetime',
287
      'fieldNames' => 'datefield',
288
    ],
289
      $mockDefinitions,
290
    ['A', 'C', 'B', 'D', 'F'],
291
    ],
292
293
    // Test field type must not conflict.
294
    [
295
    [
296
      'entityTypes' => 'node',
297
      'entityBundles' => 'article',
298
      'fieldTypes' => 'string',
299
      'fieldNames' => 'datefield',
300
    ],
301
      $mockDefinitions,
302
    ['C', 'D', 'F'],
303
    ],
304
305
    // Test field name must not conflict.
306
    [
307
    [
308
      'entityTypes' => 'node',
309
      'entityBundles' => 'page',
310
      'fieldTypes' => 'datetime',
311
      'fieldNames' => 'otherdatefield',
312
    ],
313
      $mockDefinitions,
314
    ['B', 'D', 'F'],
315
    ],
316
317
    // Weight trumps specificity.
318
    [
319
    [
320
      'entityTypes' => 'node',
321
      'entityBundles' => 'article',
322
      'fieldTypes' => 'datetime',
323
      'fieldNames' => 'datefield',
324
    ],
325
      $reweightedDefinitions,
326
    ['A', 'E', 'C', 'B', 'D', 'F'],
327
    ],
328
329
    // Test case insensitivity.
330
    [
331
    [
332
      'entityTypes' => 'node',
333
      'entityBundles' => 'article',
334
      'fieldTypes' => 'datetime',
335
      'fieldNames' => 'datefield',
336
    ],
337
      $capitalisedDefinitions,
338
    ['E', 'A', 'C', 'B', 'D', 'F'],
339
    ],
340
341
    );
342
  }
343
344
}
345