ItemGeneratorUnitTest::mapProvider()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 189
Code Lines 133

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 133
nc 1
nop 0
dl 0
loc 189
ccs 0
cts 2
cp 0
crap 2
rs 8.2857
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Drupal\Tests\paragraphs_editor\Unit\WidgetBinder\Generators;
4
5
use Drupal\Core\Field\FieldConfigInterface;
6
use Drupal\Core\Render\RenderContext;
7
use Drupal\Tests\UnitTestCase;
8
use Drupal\Tests\paragraphs_editor\Traits\MockContextTrait;
9
use Drupal\entity_reference_revisions\EntityReferenceRevisionsFieldItemList;
10
use Drupal\paragraphs_editor\WidgetBinder\Generators\ItemGenerator;
11
use Drupal\paragraphs_editor\WidgetBinder\WidgetBinderData;
12
use Drupal\paragraphs_editor\WidgetBinder\WidgetBinderDataCompilerState;
13
14
/**
15
 * @coversDefaultClass \Drupal\paragraphs_editor\WidgetBinder\Generators\ItemGenerator
16
 * @group paragraphs_editor
17
 */
18
class ItemGeneratorUnitTest extends UnitTestCase {
19
  use MockContextTrait;
20
21
  /**
22
   * @dataProvider mapProvider
23
   */
24 4
  public function testProcessMethods(array $items, array $expected_map) {
25
26 4
    $context = $this->createContext([ 'context_id' => 'root_context' ]);
27 4
    $item = $context->getEditBuffer()->createItem($this->createMockParagraph([
28 4
      'id' => 1,
29
      'uuid' => 'root_uuid',
30
    ]));
31 4
    $data = new WidgetBinderData();
32 4
    $state = new WidgetBinderDataCompilerState([], $data, $context, $item);
33 4
    $render_context = new RenderContext();
34 4
    $generator = new ItemGenerator();
35
36 4
    $generator->initialize($data, $state, $item->getEntity());
37
38 4
    $paragraph = NULL;
0 ignored issues
show
Unused Code introduced by
The assignment to $paragraph is dead and can be removed.
Loading history...
39 4
    foreach ($items as $item) {
40 4
      if ($item['type'] == 'paragraph_begin' || $item['type'] == 'paragraph_end') {
41 4
        $paragraph = $this->createMockParagraph([ 'uuid' => $item['uuid'] ]);
42 4
        if ($item['type'] == 'paragraph_begin') {
43 4
          $generator->processParagraph($data, $state, $paragraph);
44
        }
45
        else {
46 4
          $generator->postprocessParagraph($data, $state, $paragraph);
47
        }
48
      }
49 4
      else if ($item['type'] == 'field_begin' || $item['type'] == 'field_end') {
50 4
        $prophecy = $this->prophesize(EntityReferenceRevisionsFieldItemList::CLASS);
0 ignored issues
show
Bug introduced by
The constant Drupal\entity_reference_...onsFieldItemList::CLASS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
51 4
        $prophecy->getEntity()->willReturn($this->createMockParagraph([ 'uuid' => $item['uuid']]));
52 4
        $prophecy_factory = $this;
53 4
        $prophecy->getFieldDefinition()->will(function() use ($prophecy_factory, $item) {
54 4
          $prophecy = $prophecy_factory->prophesize(FieldConfigInterface::CLASS);
0 ignored issues
show
Bug introduced by
The constant Drupal\Core\Field\FieldConfigInterface::CLASS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
55 4
          $prophecy->id()->willReturn($item['id']);
56 4
          $prophecy->getName()->willReturn($item['id']);
57 4
          return $prophecy->reveal();
58 4
        });
59 4
        if ($item['type'] == 'field_begin') {
60 4
          $generator->processField($data, $state, $prophecy->reveal(), $item['editor']);
61
        }
62
        else {
63 4
          $generator->postprocessField($data, $state, $prophecy->reveal(), $item['editor']);
64
        }
65
      }
66
    }
67
68 4
    $generator->complete($data, $state, $render_context, 'test_markup');
69 4
    $this->assertEquals([
70
      'root_uuid' => [
71 4
        'contextId' => 'root_context',
72 4
        'markup' => 'test_markup',
73 4
        'type' => 'default',
74 4
        'fields' => $expected_map,
75
      ]
76 4
    ], $data->getModels('editBufferItem'));
77 4
  }
78
79
  public function mapProvider() {
80
    return [
81
      // Test case 0: No editables in input.
82
      [
83
        [
84
          [ 'type' => 'paragraph_begin', 'uuid' => 'root_uuid' ],
85
            [ 'type' => 'field_begin', 'id' => 'field_parent', 'editor' => FALSE, 'uuid' => 'root_uuid' ],
86
              [ 'type' => 'paragraph_begin', 'uuid' => 'uuid1' ],
87
              [ 'type' => 'paragraph_end', 'uuid' => 'uuid1' ],
88
            [ 'type' => 'field_begin', 'id' => 'field_parent', 'editor' => FALSE, 'uuid' => 'root_uuid' ],
89
          [ 'type' => 'paragraph_end', 'uuid' => 'root_uuid' ],
90
        ],
91
        [
92
          'field_parent' => [
93
            'type' => 'field',
94
            'name' => 'field_parent',
95
            'children' => [
96
              'uuid1' => [
97
                'type' => 'widget',
98
                'uuid' => 'uuid1',
99
                'children' => [],
100
              ],
101
            ],
102
          ],
103
        ]
104
      ],
105
106
      // Test case 1: create path to editable
107
      [
108
        [
109
          [ 'type' => 'paragraph_begin', 'uuid' => 'root_uuid' ],
110
            [ 'type' => 'field_begin', 'id' => 'field_parent', 'editor' => FALSE, 'uuid' => 'root_uuid' ],
111
              [ 'type' => 'paragraph_begin', 'uuid' => 'uuid1' ],
112
                [ 'type' => 'field_begin', 'id' => 'field_child', 'editor' => TRUE, 'uuid' => 'uuid1' ],
113
                [ 'type' => 'field_end', 'id' => 'field_child', 'editor' => TRUE, 'uuid' => 'uuid1' ],
114
              [ 'type' => 'paragraph_end', 'uuid' => 'uuid1' ],
115
            [ 'type' => 'field_end', 'id' => 'field_parent', 'editor' => FALSE, 'uuid' => 'root_uuid' ],
116
          [ 'type' => 'paragraph_end', 'uuid' => 'root_uuid' ],
117
        ],
118
        [
119
          'field_parent' => [
120
            'type' => 'field',
121
            'name' => 'field_parent',
122
            'children' => [
123
              'uuid1' => [
124
                'type' => 'widget',
125
                'uuid' => 'uuid1',
126
                'children' => [
127
                  'field_child' => [
128
                    'type' => 'field',
129
                    'name' => 'field_child',
130
                    'children' => [],
131
                  ],
132
                ],
133
              ],
134
            ],
135
          ],
136
        ],
137
      ],
138
139
      // Test case 2: different instances of the same field.
140
      [
141
        [
142
          [ 'type' => 'paragraph_begin', 'uuid' => 'root_uuid' ],
143
            [ 'type' => 'field_begin', 'id' => 'field_parent', 'editor' => FALSE, 'uuid' => 'root_uuid' ],
144
              [ 'type' => 'paragraph_begin', 'uuid' => 'uuid1' ],
145
                [ 'type' => 'field_begin', 'id' => 'field_child', 'editor' => TRUE, 'uuid' => 'uuid1' ],
146
                [ 'type' => 'field_end', 'id' => 'field_child', 'editor' => TRUE, 'uuid' => 'uuid1' ],
147
              [ 'type' => 'paragraph_end', 'uuid' => 'uuid1' ],
148
              [ 'type' => 'paragraph_begin', 'uuid' => 'uuid2' ],
149
                [ 'type' => 'field_begin', 'id' => 'field_child', 'editor' => TRUE, 'uuid' => 'uuid2' ],
150
                [ 'type' => 'field_end', 'id' => 'field_child', 'editor' => TRUE, 'uuid' => 'uuid2' ],
151
              [ 'type' => 'paragraph_end', 'uuid' => 'uuid2' ],
152
            [ 'type' => 'field_end', 'id' => 'field_parent', 'editor' => FALSE, 'uuid' => 'root_uuid' ],
153
          [ 'type' => 'paragraph_end', 'uuid' => 'root_uuid' ],
154
        ],
155
        [
156
          'field_parent' => [
157
            'type' => 'field',
158
            'name' => 'field_parent',
159
            'children' => [
160
              'uuid1' => [
161
                'type' => 'widget',
162
                'uuid' => 'uuid1',
163
                'children' => [
164
                  'field_child' => [
165
                    'type' => 'field',
166
                    'name' => 'field_child',
167
                    'children' => [],
168
                  ],
169
                ],
170
              ],
171
              'uuid2' => [
172
                'type' => 'widget',
173
                'uuid' => 'uuid2',
174
                'children' => [
175
                  'field_child' => [
176
                    'type' => 'field',
177
                    'name' => 'field_child',
178
                    'children' => [],
179
                  ],
180
                ],
181
              ],
182
            ],
183
          ],
184
        ],
185
      ],
186
187
      // Test case 3: complex nesting
188
      [
189
        [
190
          [ 'type' => 'paragraph_begin', 'uuid' => 'root_uuid' ],
191
            [ 'type' => 'field_begin', 'id' => 'field_parent1', 'editor' => FALSE, 'uuid' => 'root_uuid' ],
192
            [ 'type' => 'field_end', 'id' => 'field_parent1', 'editor' => FALSE, 'uuid' => 'root_uuid' ],
193
            [ 'type' => 'field_begin', 'id' => 'field_parent2', 'editor' => FALSE, 'uuid' => 'root_uuid' ],
194
              [ 'type' => 'paragraph_begin', 'uuid' => 'uuid1' ],
195
                [ 'type' => 'field_begin', 'id' => 'field_nothing', 'editor' => FALSE, 'uuid' => 'uuid1' ],
196
                [ 'type' => 'field_end', 'id' => 'field_nothing', 'editor' => FALSE, 'uuid' => 'uuid1' ],
197
              [ 'type' => 'paragraph_end', 'uuid' => 'uuid1' ],
198
              [ 'type' => 'paragraph_begin', 'uuid' => 'uuid2' ],
199
                [ 'type' => 'field_begin', 'id' => 'field_child1', 'editor' => TRUE, 'uuid' => 'uuid2' ],
200
                [ 'type' => 'field_end', 'id' => 'field_child1', 'editor' => TRUE, 'uuid' => 'uuid2' ],
201
                [ 'type' => 'field_begin', 'id' => 'field_child2', 'editor' => TRUE, 'uuid' => 'uuid2' ],
202
                [ 'type' => 'field_end', 'id' => 'field_child2', 'editor' => TRUE, 'uuid' => 'uuid2' ],
203
              [ 'type' => 'paragraph_end', 'uuid' => 'uuid2' ],
204
              [ 'type' => 'paragraph_begin', 'uuid' => 'uuid3' ],
205
                [ 'type' => 'field_begin', 'id' => 'field_child3', 'editor' => TRUE, 'uuid' => 'uuid3' ],
206
                [ 'type' => 'field_end', 'id' => 'field_child3', 'editor' => TRUE, 'uuid' => 'uuid3' ],
207
              [ 'type' => 'paragraph_end', 'uuid' => 'uuid3' ],
208
            [ 'type' => 'field_end', 'id' => 'field_parent2', 'editor' => FALSE, 'uuid' => 'root_uuid' ],
209
              [ 'type' => 'field_begin', 'id' => 'field_parent4', 'editor' => FALSE, 'uuid' => 'root_id' ],
210
                [ 'type' => 'paragraph_begin', 'uuid' => 'uuid4' ],
211
                  [ 'type' => 'field_begin', 'id' => 'field_child4', 'editor' => TRUE, 'uuid' => 'uuid4' ],
212
                  [ 'type' => 'field_end', 'id' => 'field_child4', 'editor' => TRUE, 'uuid' => 'uuid4' ],
213
                [ 'type' => 'paragraph_end', 'uuid' => 'uuid4' ],
214
              [ 'type' => 'field_end', 'id' => 'field_parent4', 'editor' => FALSE, 'uuid' => 'root_id' ],
215
          [ 'type' => 'paragraph_end', 'uuid' => 'root_uuid' ],
216
        ],
217
        [
218
          'field_parent2' => [
219
            'type' => 'field',
220
            'name' => 'field_parent2',
221
            'children' => [
222
              'uuid1' => [
223
                'type' => 'widget',
224
                'uuid' => 'uuid1',
225
                'children' => [],
226
              ],
227
              'uuid2' => [
228
                'type' => 'widget',
229
                'uuid' => 'uuid2',
230
                'children' => [
231
                  'field_child1' => [
232
                    'type' => 'field',
233
                    'name' => 'field_child1',
234
                    'children' => [],
235
                  ],
236
                  'field_child2' => [
237
                    'type' => 'field',
238
                    'name' => 'field_child2',
239
                    'children' => [],
240
                  ],
241
                ],
242
              ],
243
              'uuid3' => [
244
                'type' => 'widget',
245
                'uuid' => 'uuid3',
246
                'children' => [
247
                  'field_child3' => [
248
                    'type' => 'field',
249
                    'name' => 'field_child3',
250
                    'children' => [],
251
                  ],
252
                ],
253
              ],
254
            ],
255
          ],
256
          'field_parent4' => [
257
            'type' => 'field',
258
            'name' => 'field_parent4',
259
            'children' => [
260
              'uuid4' => [
261
                'type' => 'widget',
262
                'uuid' => 'uuid4',
263
                'children' => [
264
                  'field_child4' => [
265
                    'type' => 'field',
266
                    'name' => 'field_child4',
267
                    'children' => [],
268
                  ],
269
                ],
270
              ],
271
            ],
272
          ]
273
        ],
274
      ],
275
276
    ];
277
  }
278
}
279