Passed
Pull Request — master (#26)
by Dominik
01:09
created

testResolveConditionalLogicOnParentLevel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 43
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 29
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 43
rs 9.456
1
<?php
2
3
namespace ACFComposer\Tests;
4
5
require_once dirname(__DIR__) . '/lib/ACFComposer/ResolveConfig.php';
6
7
use Exception;
8
use Brain\Monkey\Filters;
0 ignored issues
show
Bug introduced by
The type Brain\Monkey\Filters was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use ACFComposer\ResolveConfig;
10
11
class ResolveConfigForFieldTest extends TestCase
12
{
13
    public function testForFieldWithValidConfig()
14
    {
15
        $config = [
16
            'name' => 'someField',
17
            'label' => 'Some Field',
18
            'type' => 'someType'
19
        ];
20
        $output = ResolveConfig::forField($config);
21
        $config['key'] = 'field_someField';
22
        $this->assertEquals($config, $output);
23
    }
24
25
    public function testForFieldFailsWithoutName()
26
    {
27
        $config = [
28
            'label' => 'Some Field',
29
            'type' => 'someType'
30
        ];
31
        $this->expectException(Exception::class);
32
        ResolveConfig::forField($config);
33
    }
34
35
    public function testForFieldFailsWithoutLabel()
36
    {
37
        $config = [
38
            'name' => 'someField',
39
            'type' => 'someType'
40
        ];
41
        $this->expectException(Exception::class);
42
        ResolveConfig::forField($config);
43
    }
44
45
    public function testForFieldFailsWithoutType()
46
    {
47
        $config = [
48
            'name' => 'someField',
49
            'label' => 'Some Field'
50
        ];
51
        $this->expectException(Exception::class);
52
        ResolveConfig::forField($config);
53
    }
54
55
    public function testForFieldFailsWithKey()
56
    {
57
        $config = [
58
            'name' => 'someField',
59
            'label' => 'Some Field',
60
            'type' => 'someType',
61
            'key' => 'someKey'
62
        ];
63
        $this->expectException(Exception::class);
64
        ResolveConfig::forField($config);
65
    }
66
67
    public function testForFieldGetConfigFromFilter()
68
    {
69
        $config = 'ACFComposer/Fields/someField';
70
        $someField = [
71
            'name' => 'someField',
72
            'label' => 'Some Field',
73
            'type' => 'someType'
74
        ];
75
        Filters\expectApplied($config)
0 ignored issues
show
Bug introduced by
The function expectApplied was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

75
        /** @scrutinizer ignore-call */ 
76
        Filters\expectApplied($config)
Loading history...
76
            ->with(null)
77
            ->once()
78
            ->andReturn($someField);
79
        $output = ResolveConfig::forField($config);
0 ignored issues
show
Bug introduced by
$config of type string is incompatible with the type array expected by parameter $config of ACFComposer\ResolveConfig::forField(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

79
        $output = ResolveConfig::forField(/** @scrutinizer ignore-type */ $config);
Loading history...
80
        $someField['key'] = "field_someField";
81
        $this->assertEquals($someField, $output);
82
    }
83
84
    public function testForFieldGetConfigFromFilterWithArgument()
85
    {
86
        $config = 'ACFComposer/Fields/someField#prefix';
87
        $filter = 'ACFComposer/Fields/someField';
88
        $someField = [
89
            'name' => 'someField',
90
            'label' => 'Some Field',
91
            'type' => 'someType'
92
        ];
93
        Filters\expectApplied($filter)
0 ignored issues
show
Bug introduced by
The function expectApplied was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

93
        /** @scrutinizer ignore-call */ 
94
        Filters\expectApplied($filter)
Loading history...
94
            ->with(null, 'prefix')
95
            ->once()
96
            ->andReturn($someField);
97
        $output = ResolveConfig::forField($config);
0 ignored issues
show
Bug introduced by
$config of type string is incompatible with the type array expected by parameter $config of ACFComposer\ResolveConfig::forField(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

97
        $output = ResolveConfig::forField(/** @scrutinizer ignore-type */ $config);
Loading history...
98
        $someField['name'] = "prefix_someField";
99
        $someField['key'] = "field_prefix_someField";
100
        $this->assertEquals($someField, $output);
101
    }
102
103
    public function testMultipleForFieldGetConfigFromFilterWithArgument()
104
    {
105
        $config = 'ACFComposer/Fields/someField#prefix';
106
        $filter = 'ACFComposer/Fields/someField';
107
        $someField = [
108
            [
109
                'name' => 'someField',
110
                'label' => 'Some Field',
111
                'type' => 'someType'
112
            ],
113
            [
114
                'name' => 'someOtherField',
115
                'label' => 'Some Field',
116
                'type' => 'someType'
117
            ]
118
        ];
119
        Filters\expectApplied($filter)
0 ignored issues
show
Bug introduced by
The function expectApplied was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

119
        /** @scrutinizer ignore-call */ 
120
        Filters\expectApplied($filter)
Loading history...
120
            ->with(null, 'prefix')
121
            ->once()
122
            ->andReturn($someField);
123
        $output = ResolveConfig::forField($config);
0 ignored issues
show
Bug introduced by
$config of type string is incompatible with the type array expected by parameter $config of ACFComposer\ResolveConfig::forField(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

123
        $output = ResolveConfig::forField(/** @scrutinizer ignore-type */ $config);
Loading history...
124
        $someField[0]['name'] = "prefix_someField";
125
        $someField[0]['key'] = "field_prefix_someField";
126
        $someField[1]['name'] = "prefix_someOtherField";
127
        $someField[1]['key'] = "field_prefix_someOtherField";
128
        $this->assertEquals($someField, $output);
129
    }
130
131
    public function testForFieldGetConfigFromFilterWithArgumentAndConditionalLogic()
132
    {
133
        $config = [
134
            'ACFComposer/Fields/someField#prefix',
135
            'ACFComposer/Fields/someFieldWithConditional#otherprefix'
136
        ];
137
138
        $filter = 'ACFComposer/Fields/someField';
139
        $someField = [
140
            'name' => 'someField',
141
            'label' => 'Some Field',
142
            'type' => 'someType'
143
        ];
144
        Filters\expectApplied($filter)
0 ignored issues
show
Bug introduced by
The function expectApplied was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

144
        /** @scrutinizer ignore-call */ 
145
        Filters\expectApplied($filter)
Loading history...
145
            ->with(null, 'prefix')
146
            ->once()
147
            ->andReturn($someField);
148
149
        $filterConditional = 'ACFComposer/Fields/someFieldWithConditional';
150
        $someFieldWithConditional = [
151
            'name' => 'someOtherField',
152
            'label' => 'Some Other Field',
153
            'type' => 'someType',
154
            'conditional_logic' => [
155
                [
156
                    [
157
                        'fieldPath' => 'someField',
158
                        'operator' => '==',
159
                        'value' => 'someValue'
160
                    ]
161
                ]
162
            ]
163
        ];
164
        Filters\expectApplied($filterConditional)
165
            ->with(null, 'otherprefix')
166
            ->once()
167
            ->andReturn($someFieldWithConditional);
168
169
        $output = array_map(function ($singleConfig) {
170
            return ResolveConfig::forField($singleConfig);
171
        }, $config);
172
173
        $someField['key'] = 'field_prefix_someField';
174
        $someField['name'] = 'prefix_someField';
175
176
        $someFieldWithConditional['key'] = 'field_otherprefix_someOtherField';
177
        $someFieldWithConditional['name'] = 'otherprefix_someOtherField';
178
        $someFieldWithConditional['conditional_logic'][0][0]['field'] = 'field_otherprefix_someField';
179
        unset($someFieldWithConditional['conditional_logic'][0][0]['fieldPath']);
180
        $config = [
181
            $someField,
182
            $someFieldWithConditional
183
        ];
184
        $this->assertEquals($config, $output);
185
    }
186
187
    public function testForFieldTriggerErrorWithoutFilter()
188
    {
189
        set_error_handler(
190
            static function ($errno, $errstr) {
191
                restore_error_handler();
192
                throw new Exception($errstr, $errno);
193
            },
194
            E_ALL
195
        );
196
197
        $config = 'ACFComposer/Fields/someField';
198
        Filters\expectApplied($config)
0 ignored issues
show
Bug introduced by
The function expectApplied was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

198
        /** @scrutinizer ignore-call */ 
199
        Filters\expectApplied($config)
Loading history...
199
            ->once()
200
            ->andReturn(null);
201
        $this->expectException(Exception::class);
202
        ResolveConfig::forField($config);
0 ignored issues
show
Bug introduced by
$config of type string is incompatible with the type array expected by parameter $config of ACFComposer\ResolveConfig::forField(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

202
        ResolveConfig::forField(/** @scrutinizer ignore-type */ $config);
Loading history...
203
    }
204
205
    public function testForFieldReturnEmptyArrayWithoutFilter()
206
    {
207
        $config = 'ACFComposer/Fields/someField';
208
        Filters\expectApplied($config)
0 ignored issues
show
Bug introduced by
The function expectApplied was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

208
        /** @scrutinizer ignore-call */ 
209
        Filters\expectApplied($config)
Loading history...
209
            ->once()
210
            ->andReturn(null);
211
        $output = @ResolveConfig::forField($config);
0 ignored issues
show
Bug introduced by
$config of type string is incompatible with the type array expected by parameter $config of ACFComposer\ResolveConfig::forField(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

211
        $output = @ResolveConfig::forField(/** @scrutinizer ignore-type */ $config);
Loading history...
212
        $this->assertEquals($output, []);
213
    }
214
215
    public function testForFieldWithValidSubField()
216
    {
217
        $subFieldConfig = [
218
            'name' => 'subField',
219
            'label' => 'Sub Field',
220
            'type' => 'someType'
221
        ];
222
        $config = [
223
            'name' => 'someField',
224
            'label' => 'Some Field',
225
            'type' => 'someType',
226
            'sub_fields' => [$subFieldConfig]
227
        ];
228
        $output = ResolveConfig::forField($config);
229
        $subFieldConfig['key'] = 'field_someField_subField';
230
        $this->assertEquals($subFieldConfig, $output['sub_fields'][0]);
231
    }
232
233
    public function testForFieldFailWithInvalidSubField()
234
    {
235
        $subFieldConfig = [
236
            'name' => 'subField',
237
            'label' => 'Sub Field'
238
        ];
239
        $config = [
240
            'name' => 'someField',
241
            'label' => 'Some Field',
242
            'type' => 'someType',
243
            'sub_fields' => [$subFieldConfig]
244
        ];
245
        $this->expectException(Exception::class);
246
        ResolveConfig::forField($config);
247
    }
248
249
    public function testForFieldWithValidLayout()
250
    {
251
        $layoutConfig = [
252
            'name' => 'someLayout',
253
            'label' => 'Some Layout'
254
        ];
255
        $config = [
256
            'name' => 'someField',
257
            'label' => 'Some Field',
258
            'type' => 'someType',
259
            'layouts' => [$layoutConfig]
260
        ];
261
        $output = ResolveConfig::forField($config);
262
        $layoutConfig['key'] = 'field_someField_someLayout';
263
        $this->assertEquals($layoutConfig, $output['layouts'][0]);
264
    }
265
266
    public function testAppliesResolveFilterGeneric()
267
    {
268
        $config = [
269
            'name' => 'someField',
270
            'label' => 'Some Field',
271
            'type' => 'someType'
272
        ];
273
        $resolvedConfig = $config;
274
        $resolvedConfig['key'] = 'field_someField';
275
        Filters\expectApplied('ACFComposer/resolveEntity')
0 ignored issues
show
Bug introduced by
The function expectApplied was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

275
        /** @scrutinizer ignore-call */ 
276
        Filters\expectApplied('ACFComposer/resolveEntity')
Loading history...
276
            ->once()
277
            ->with($resolvedConfig)
278
            ->andReturn(array_merge($resolvedConfig, ['foo' => 'bar']));
279
280
        $output = ResolveConfig::forField($config);
281
        $resolvedConfig['foo'] = 'bar';
282
        $this->assertEquals($resolvedConfig, $output);
283
    }
284
285
    public function testAppliesResolveFilterByName()
286
    {
287
        $config = [
288
            'name' => 'someField',
289
            'label' => 'Some Field',
290
            'type' => 'someType'
291
        ];
292
        $resolvedConfig = $config;
293
        $resolvedConfig['key'] = 'field_someField';
294
        Filters\expectApplied('ACFComposer/resolveEntity?name=someField')
0 ignored issues
show
Bug introduced by
The function expectApplied was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

294
        /** @scrutinizer ignore-call */ 
295
        Filters\expectApplied('ACFComposer/resolveEntity?name=someField')
Loading history...
295
            ->once()
296
            ->with($resolvedConfig)
297
            ->andReturn(array_merge($resolvedConfig, ['foo' => 'bar']));
298
299
        $output = ResolveConfig::forField($config);
300
        $resolvedConfig['foo'] = 'bar';
301
        $this->assertEquals($resolvedConfig, $output);
302
    }
303
304
    public function testAppliesResolveFilterByKey()
305
    {
306
        $config = [
307
            'name' => 'someField',
308
            'label' => 'Some Field',
309
            'type' => 'someType'
310
        ];
311
        $resolvedConfig = $config;
312
        $resolvedConfig['key'] = 'field_someField';
313
        Filters\expectApplied('ACFComposer/resolveEntity?key=field_someField')
0 ignored issues
show
Bug introduced by
The function expectApplied was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

313
        /** @scrutinizer ignore-call */ 
314
        Filters\expectApplied('ACFComposer/resolveEntity?key=field_someField')
Loading history...
314
            ->once()
315
            ->with($resolvedConfig)
316
            ->andReturn(array_merge($resolvedConfig, ['foo' => 'bar']));
317
318
        $output = ResolveConfig::forField($config);
319
        $resolvedConfig['foo'] = 'bar';
320
        $this->assertEquals($resolvedConfig, $output);
321
    }
322
323
    public function testResolveConditionalLogicOnSameLevel()
324
    {
325
        $subFieldOne = [
326
            'name' => 'subField1',
327
            'label' => 'Sub Field 1',
328
            'type' => 'someType',
329
        ];
330
        $subFieldWithConditional = [
331
            'name' => 'subField2',
332
            'label' => 'Sub Field 2',
333
            'type' => 'someType',
334
            'conditional_logic' => [
335
                [
336
                    [
337
                        'fieldPath' => 'subField1',
338
                        'operator' => 'someOp',
339
                        'value' => 'someValue'
340
                    ]
341
                ]
342
            ]
343
        ];
344
        $config = [
345
            'name' => 'someField',
346
            'label' => 'Some Field',
347
            'type' => 'someType',
348
            'sub_fields' => [
349
                $subFieldOne,
350
                $subFieldWithConditional
351
            ]
352
        ];
353
354
        $output = ResolveConfig::forField($config);
355
356
        $config['key'] = 'field_someField';
357
        $subFieldOne['key'] = 'field_someField_subField1';
358
        $subFieldWithConditional['key'] = 'field_someField_subField2';
359
        $subFieldWithConditional['conditional_logic'][0][0]['field'] = 'field_someField_subField1';
360
        unset($subFieldWithConditional['conditional_logic'][0][0]['fieldPath']);
361
        $config['sub_fields'] = [
362
            $subFieldOne,
363
            $subFieldWithConditional
364
        ];
365
        $this->assertEquals($config, $output);
366
    }
367
368
    public function testResolveConditionalLogicOnParentLevel()
369
    {
370
        $subFieldOne = [
371
            'name' => 'subField1',
372
            'label' => 'Sub Field 1',
373
            'type' => 'someType',
374
        ];
375
        $subFieldWithConditional = [
376
            'name' => 'subField2',
377
            'label' => 'Sub Field 2',
378
            'type' => 'someType',
379
            'conditional_logic' => [
380
                [
381
                    [
382
                        'fieldPath' => '../subField1',
383
                        'operator' => 'someOp',
384
                        'value' => 'someValue'
385
                    ]
386
                ]
387
            ]
388
        ];
389
        $config = [
390
            'name' => 'someField',
391
            'label' => 'Some Field',
392
            'type' => 'someType',
393
            'sub_fields' => [
394
                $subFieldOne,
395
                $subFieldWithConditional
396
            ]
397
        ];
398
399
        $output = ResolveConfig::forField($config);
400
401
        $config['key'] = 'field_someField';
402
        $subFieldOne['key'] = 'field_someField_subField1';
403
        $subFieldWithConditional['key'] = 'field_someField_subField2';
404
        $subFieldWithConditional['conditional_logic'][0][0]['field'] = 'field_subField1';
405
        unset($subFieldWithConditional['conditional_logic'][0][0]['fieldPath']);
406
        $config['sub_fields'] = [
407
            $subFieldOne,
408
            $subFieldWithConditional
409
        ];
410
        $this->assertEquals($config, $output);
411
    }
412
413
    public function testResolveConditionalLogicOnParentsParentLevel()
414
    {
415
        $subFieldWithConditional = [
416
            'name' => 'subField2',
417
            'label' => 'Sub Field 2',
418
            'type' => 'someType',
419
            'conditional_logic' => [
420
                [
421
                    [
422
                        'fieldPath' => '../../subField1',
423
                        'operator' => 'someOp',
424
                        'value' => 'someValue'
425
                    ]
426
                ]
427
            ]
428
        ];
429
        $subFieldOne = [
430
            'name' => 'subField1',
431
            'label' => 'Sub Field 1',
432
            'type' => 'someType',
433
            'sub_fields' => [$subFieldWithConditional]
434
        ];
435
        $config = [
436
            'name' => 'someField',
437
            'label' => 'Some Field',
438
            'type' => 'someType',
439
            'sub_fields' => [$subFieldOne]
440
        ];
441
442
        $output = ResolveConfig::forField($config);
443
444
        $config['key'] = 'field_someField';
445
        $subFieldOne['key'] = 'field_someField_subField1';
446
        $subFieldWithConditional['key'] = 'field_someField_subField1_subField2';
447
        $subFieldWithConditional['conditional_logic'][0][0]['field'] = 'field_subField1';
448
        unset($subFieldWithConditional['conditional_logic'][0][0]['fieldPath']);
449
        $subFieldOne['sub_fields'] = [$subFieldWithConditional];
450
        $config['sub_fields'] = [$subFieldOne];
451
        $this->assertEquals($config, $output);
452
    }
453
454
    public function testResolveMultipleFieldsFromFilter()
455
    {
456
        $filter = 'ACFComposer/Fields/subField';
457
        $subFieldTwo = [
458
            'name' => 'subFieldTwo',
459
            'label' => 'Sub Field Two',
460
            'type' => 'someType',
461
        ];
462
        $config = [
463
            'name' => 'someField',
464
            'label' => 'Some Field',
465
            'type' => 'someType',
466
            'sub_fields' => [
467
                $filter,
468
                $subFieldTwo
469
            ]
470
        ];
471
        $filterFieldOne = [
472
            'name' => 'filterFieldOne',
473
            'label' => 'Filter Field One',
474
            'type' => 'someType',
475
        ];
476
        $filterFieldTwo = [
477
            'name' => 'filterFieldTwo',
478
            'label' => 'Filter Field Two',
479
            'type' => 'someType',
480
        ];
481
        Filters\expectApplied($filter)
0 ignored issues
show
Bug introduced by
The function expectApplied was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

481
        /** @scrutinizer ignore-call */ 
482
        Filters\expectApplied($filter)
Loading history...
482
            ->once()
483
            ->andReturn([
484
                $filterFieldOne,
485
                $filterFieldTwo
486
            ]);
487
        $output = ResolveConfig::forField($config);
488
        $subFieldTwo['key'] = 'field_someField_subFieldTwo';
489
        $filterFieldOne['key'] = 'field_someField_filterFieldOne';
490
        $filterFieldTwo['key'] = 'field_someField_filterFieldTwo';
491
        $config['key'] = 'field_someField';
492
        $config['sub_fields'] = [
493
            $filterFieldOne,
494
            $filterFieldTwo,
495
            $subFieldTwo
496
        ];
497
        $this->assertEquals($config, $output);
498
    }
499
}
500