UtilTest::testMergeSettings()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 12
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 20
rs 9.8666
1
<?php
2
3
/**
4
 * This file is part of CaptainHook
5
 *
6
 * (c) Sebastian Feldmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CaptainHook\App\Config;
13
14
use CaptainHook\App\Config;
15
use Exception;
16
use PHPUnit\Framework\TestCase;
17
18
class UtilTest extends TestCase
19
{
20
    public function testEnabledMissing(): void
21
    {
22
        $this->expectException(Exception::class);
23
24
        Util::validateJsonConfiguration(['pre-commit' => ['actions' => []]]);
25
    }
26
27
    public function testActionsMissing(): void
28
    {
29
        $this->expectException(Exception::class);
30
31
        Util::validateJsonConfiguration(['pre-commit' => ['enabled' => true]]);
32
    }
33
34
    public function testActionsNoArray(): void
35
    {
36
        $this->expectException(Exception::class);
37
38
        Util::validateJsonConfiguration(['pre-commit' => ['enabled' => true, 'actions' => false]]);
39
    }
40
41
    public function testActionMissing(): void
42
    {
43
        $this->expectException(Exception::class);
44
45
        Util::validateJsonConfiguration(
46
            [
47
                'pre-commit' => [
48
                    'enabled' => true,
49
                    'actions' => [
50
                        ['options' => []]
51
                    ]
52
                ]
53
            ]
54
        );
55
56
        $this->assertTrue(true);
57
    }
58
59
    public function testActionEmpty(): void
60
    {
61
        $this->expectException(Exception::class);
62
63
        Util::validateJsonConfiguration(
64
            [
65
                'pre-commit' => [
66
                    'enabled' => true,
67
                    'actions' => [
68
                        ['action'  => '']
69
                    ]
70
                ]
71
            ]
72
        );
73
    }
74
75
    public function testConditionExecMissing(): void
76
    {
77
        $this->expectException(Exception::class);
78
79
        Util::validateJsonConfiguration(
80
            [
81
                'pre-commit' => [
82
                    'enabled' => true,
83
                    'actions' => [
84
                        [
85
                            'action'    => 'foo',
86
                            'conditions' => [
87
                                [
88
                                    'args' => [
89
                                        'foo' => 'fiz',
90
                                        'bar' => 'baz'
91
                                    ]
92
                                ]
93
                            ]
94
                        ]
95
                    ]
96
                ]
97
            ]
98
        );
99
    }
100
101
    public function testConditionArgsNoArray(): void
102
    {
103
        $this->expectException(Exception::class);
104
105
        Util::validateJsonConfiguration(
106
            [
107
                'pre-commit' => [
108
                    'enabled' => true,
109
                    'actions' => [
110
                        [
111
                            'action'    => 'foo',
112
                            'conditions' => [
113
                                [
114
                                    'exec' => '\\Foo',
115
                                    'args' => 'fooBarBaz'
116
                                ]
117
                            ]
118
                        ]
119
                    ]
120
                ]
121
            ]
122
        );
123
    }
124
125
    public function testValid(): void
126
    {
127
        Util::validateJsonConfiguration(
128
            [
129
                'pre-commit' => [
130
                    'enabled' => true,
131
                    'actions' => [
132
                        ['action'  => 'foo']
133
                    ]
134
                ]
135
            ]
136
        );
137
138
        $this->assertTrue(true);
139
    }
140
141
    public function testValidWithCondition(): void
142
    {
143
        Util::validateJsonConfiguration(
144
            [
145
                'config' => [
146
                    'plugins' => [
147
                        [
148
                            'plugin' => '\\Foo\\Bar',
149
                        ]
150
                    ],
151
                ],
152
                'pre-commit' => [
153
                    'enabled' => true,
154
                    'actions' => [
155
                        [
156
                            'action'    => 'foo',
157
                            'conditions' => [
158
                                [
159
                                    'exec' => '\\Fiz\\Baz',
160
                                    'args' => [
161
                                        'foo' => 'fiz',
162
                                        'bar' => 'baz'
163
                                    ]
164
                                ]
165
                            ]
166
                        ]
167
                    ]
168
                ]
169
            ]
170
        );
171
172
        $this->assertTrue(true);
173
    }
174
175
    public function testPluginsMustBeAnArray(): void
176
    {
177
        $this->expectException(Exception::class);
178
        $this->expectExceptionMessage('Config error: \'plugins\' must be an array');
179
180
        Util::validateJsonConfiguration([
181
            'config' => [
182
                'plugins' => 'foobar',
183
            ],
184
        ]);
185
    }
186
187
    public function testPluginMissing(): void
188
    {
189
        $this->expectException(Exception::class);
190
        $this->expectExceptionMessage('Config error: \'plugin\' missing');
191
192
        Util::validateJsonConfiguration([
193
            'config' => [
194
                'plugins' => [
195
                    [
196
                        'foo' => 'bar',
197
                    ],
198
                ],
199
            ],
200
        ]);
201
    }
202
203
    public function testPluginEmpty(): void
204
    {
205
        $this->expectException(Exception::class);
206
        $this->expectExceptionMessage('Config error: \'plugin\' can\'t be empty');
207
208
        Util::validateJsonConfiguration([
209
            'config' => [
210
                'plugins' => [
211
                    [
212
                        'plugin' => '',
213
                    ],
214
                ],
215
            ],
216
        ]);
217
    }
218
219
    public function testMergeSettings()
220
    {
221
        $s1 = [
222
            Config::SETTING_INCLUDES => [
223
                'foo',
224
                'bar'
225
            ],
226
            CONFIG::SETTING_COLORS => true,
227
        ];
228
        $s2 = [
229
            Config::SETTING_INCLUDES => [
230
                'baz'
231
            ],
232
            CONFIG::SETTING_GIT_DIR => '/var/.git'
233
        ];
234
235
        $merged = Util::mergeSettings($s2, $s1);
236
237
        $this->assertEquals(3, count($merged[Config::SETTING_INCLUDES]));
238
        $this->assertContains('baz', $merged[Config::SETTING_INCLUDES]);
239
    }
240
}
241