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

ResolveConfigForLayoutTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 84
c 1
b 0
f 0
dl 0
loc 140
rs 10
wmc 8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testForLayoutFailsWithKey() 0 9 1
A testForLayoutGetConfigFromFilter() 0 13 1
A testForLayoutFailWithInvalidSubField() 0 13 1
A testForLayoutFailsWithoutLabel() 0 7 1
A testForLayoutWithValidSubField() 0 15 1
A testForLayoutWithValidConfig() 0 9 1
A testForLayoutFailsWithoutName() 0 7 1
A testforLayoutGetConfigFromFilterWithArgumentAndNestedConditionalLogic() 0 52 1
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 ResolveConfigForLayoutTest extends TestCase
12
{
13
    public function testForLayoutWithValidConfig()
14
    {
15
        $config = [
16
            'name' => 'someLayout',
17
            'label' => 'Some Layout'
18
        ];
19
        $output = ResolveConfig::forLayout($config);
20
        $config['key'] = 'field_someLayout';
21
        $this->assertEquals($config, $output);
22
    }
23
24
    public function testForLayoutFailsWithoutName()
25
    {
26
        $config = [
27
            'label' => 'Some Layout'
28
        ];
29
        $this->expectException(Exception::class);
30
        ResolveConfig::forLayout($config);
31
    }
32
33
    public function testForLayoutFailsWithoutLabel()
34
    {
35
        $config = [
36
            'name' => 'someLayout'
37
        ];
38
        $this->expectException(Exception::class);
39
        ResolveConfig::forLayout($config);
40
    }
41
42
    public function testForLayoutFailsWithKey()
43
    {
44
        $config = [
45
            'name' => 'someLayout',
46
            'label' => 'Some Layout',
47
            'key' => 'someKey'
48
        ];
49
        $this->expectException(Exception::class);
50
        ResolveConfig::forLayout($config);
51
    }
52
53
    public function testForLayoutGetConfigFromFilter()
54
    {
55
        $config = 'ACFComposer/Layout/someLayout';
56
        $someLayout = [
57
            'name' => 'someLayout',
58
            'label' => 'Some Layout'
59
        ];
60
        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

60
        /** @scrutinizer ignore-call */ 
61
        Filters\expectApplied($config)
Loading history...
61
            ->once()
62
            ->andReturn($someLayout);
63
        $output = ResolveConfig::forLayout($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::forLayout(). ( Ignorable by Annotation )

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

63
        $output = ResolveConfig::forLayout(/** @scrutinizer ignore-type */ $config);
Loading history...
64
        $someLayout['key'] = 'field_someLayout';
65
        $this->assertEquals($someLayout, $output);
66
    }
67
    public function testForLayoutWithValidSubField()
68
    {
69
        $subFieldConfig = [
70
            'name' => 'subField',
71
            'label' => 'Sub Field',
72
            'type' => 'someType'
73
        ];
74
        $config = [
75
            'name' => 'someLayout',
76
            'label' => 'Some Layout',
77
            'sub_fields' => [$subFieldConfig]
78
        ];
79
        $output = ResolveConfig::forLayout($config);
80
        $subFieldConfig['key'] = 'field_someLayout_subField';
81
        $this->assertEquals($subFieldConfig, $output['sub_fields'][0]);
82
    }
83
84
    public function testForLayoutFailWithInvalidSubField()
85
    {
86
        $subFieldConfig = [
87
            'name' => 'subField',
88
            'label' => 'Sub Field'
89
        ];
90
        $config = [
91
            'name' => 'someLayout',
92
            'label' => 'Some Layout',
93
            'sub_fields' => [$subFieldConfig]
94
        ];
95
        $this->expectException(Exception::class);
96
        ResolveConfig::forLayout($config);
97
    }
98
99
    public function testforLayoutGetConfigFromFilterWithArgumentAndNestedConditionalLogic()
100
    {
101
        $config = 'ACFComposer/Fields/someField#prefix';
102
        $filter = 'ACFComposer/Fields/someField';
103
        $layout = [
104
            'name' => 'layout',
105
            'label' => 'Layout',
106
            'sub_fields' => [
107
                [
108
                    'name' => 'someBoolean',
109
                    'label' => 'Some Boolean',
110
                    'type' => 'boolean'
111
                ],
112
                [
113
                    'name' => 'someRepeater',
114
                    'label' => 'Some Repeater',
115
                    'type' => 'repeater',
116
                    'sub_fields' => [
117
                        [
118
                            'name' => 'someNestedImage',
119
                            'label' => 'Some Nested Image',
120
                            'type' => 'image',
121
                            'conditional_logic' => [
122
                                [
123
                                    [
124
                                        'fieldPath' => '../someBoolean',
125
                                        'operator' => '==',
126
                                        'value' => '1'
127
                                    ]
128
                                ]
129
                            ]
130
                        ]
131
                    ]
132
                ]
133
            ]
134
        ];
135
136
        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

136
        /** @scrutinizer ignore-call */ 
137
        Filters\expectApplied($filter)
Loading history...
137
          ->with(null, 'prefix')
138
          ->once()
139
          ->andReturn($layout);
140
141
        $output = ResolveConfig::forLayout($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::forLayout(). ( Ignorable by Annotation )

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

141
        $output = ResolveConfig::forLayout(/** @scrutinizer ignore-type */ $config);
Loading history...
142
143
        $layout['key'] = 'field_prefix_layout';
144
        $layout['name'] = 'prefix_layout';
145
        $layout['sub_fields'][0]['key'] = 'field_prefix_layout_someBoolean';
146
        $layout['sub_fields'][1]['key'] = 'field_prefix_layout_someRepeater';
147
        $layout['sub_fields'][1]['sub_fields'][0]['key'] = 'field_prefix_layout_someRepeater_someNestedImage';
148
        $layout['sub_fields'][1]['sub_fields'][0]['conditional_logic'][0][0]['field'] = 'field_prefix_layout_someBoolean';
149
        unset($layout['sub_fields'][1]['sub_fields'][0]['conditional_logic'][0][0]['fieldPath']);
150
        $this->assertEquals($layout, $output);
151
    }
152
}
153