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; |
13
|
|
|
|
14
|
|
|
use CaptainHook\App\Config\Action; |
15
|
|
|
use CaptainHook\App\Config\Plugin; |
16
|
|
|
use CaptainHook\App\Plugin\CaptainHook as CaptainHookPlugin; |
17
|
|
|
use Exception; |
18
|
|
|
use PHPUnit\Framework\TestCase; |
19
|
|
|
|
20
|
|
|
class ConfigTest extends TestCase |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Tests Config::__construct |
24
|
|
|
*/ |
25
|
|
|
public function testConstructor(): void |
26
|
|
|
{ |
27
|
|
|
$config = new Config('./no-config.json'); |
28
|
|
|
$this->assertFalse($config->isLoadedFromFile()); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Tests Config::isLoadedFromFile |
33
|
|
|
*/ |
34
|
|
|
public function testIsLoadedFromFile(): void |
35
|
|
|
{ |
36
|
|
|
$config = new Config('valid.json', true); |
37
|
|
|
$this->assertTrue($config->isLoadedFromFile()); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Tests Config::getHookConfig |
42
|
|
|
*/ |
43
|
|
|
public function testGetInvalidHook(): void |
44
|
|
|
{ |
45
|
|
|
$this->expectException(Exception::class); |
46
|
|
|
$config = new Config('./no-config.json'); |
47
|
|
|
$config->getHookConfig('foo'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Tests Config::getHookConfigToExecute |
52
|
|
|
*/ |
53
|
|
|
public function testGetHookConfigWithVirtualHooks(): void |
54
|
|
|
{ |
55
|
|
|
$config = new Config('./no-config.json'); |
56
|
|
|
$config->getHookConfig('post-rewrite')->setEnabled(true); |
57
|
|
|
$config->getHookConfig('post-rewrite')->addAction(new Action('echo foo')); |
58
|
|
|
$config->getHookConfig('post-change')->setEnabled(true); |
59
|
|
|
$config->getHookConfig('post-change')->addAction(new Action('echo bar')); |
60
|
|
|
|
61
|
|
|
$hookConfig = $config->getHookConfigToExecute('post-rewrite'); |
62
|
|
|
|
63
|
|
|
$this->assertCount(2, $hookConfig->getActions()); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Tests Config::getGitDirectory |
68
|
|
|
*/ |
69
|
|
|
public function testAssumeCwdAsGitDir(): void |
70
|
|
|
{ |
71
|
|
|
$config = new Config('./no-config.json'); |
72
|
|
|
$this->assertEquals(getcwd() . '/.git', $config->getGitDirectory()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Tests Config::getPath |
77
|
|
|
*/ |
78
|
|
|
public function testGetPath(): void |
79
|
|
|
{ |
80
|
|
|
$path = realpath(__DIR__ . '/../files/config/valid.json'); |
81
|
|
|
$config = new Config($path); |
82
|
|
|
|
83
|
|
|
$this->assertEquals($path, $config->getPath()); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Tests Config::getBootstrap |
88
|
|
|
*/ |
89
|
|
|
public function testGetBootstrapDefault(): void |
90
|
|
|
{ |
91
|
|
|
$path = realpath(__DIR__ . '/../files/config/valid.json'); |
92
|
|
|
$config = new Config($path); |
93
|
|
|
|
94
|
|
|
$this->assertEquals('vendor/autoload.php', $config->getBootstrap()); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Tests Config::getBootstrap |
99
|
|
|
*/ |
100
|
|
|
public function testGetBootstrapSetting(): void |
101
|
|
|
{ |
102
|
|
|
$path = realpath(__DIR__ . '/../files/config/valid.json'); |
103
|
|
|
$config = new Config($path, true, ['bootstrap' => 'libs/autoload.php']); |
104
|
|
|
|
105
|
|
|
$this->assertEquals('libs/autoload.php', $config->getBootstrap()); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Tests Config::isFailureAllowed |
110
|
|
|
*/ |
111
|
|
|
public function testIsFailureAllowedDefault(): void |
112
|
|
|
{ |
113
|
|
|
$path = realpath(__DIR__ . '/../files/config/valid.json'); |
114
|
|
|
$config = new Config($path, true); |
115
|
|
|
|
116
|
|
|
$this->assertFalse($config->isFailureAllowed()); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Tests Config::isFailureAllowed |
121
|
|
|
*/ |
122
|
|
|
public function testIsFailureAllowedSet(): void |
123
|
|
|
{ |
124
|
|
|
$path = realpath(__DIR__ . '/../files/config/valid.json'); |
125
|
|
|
$config = new Config($path, true, ['allow-failure' => true]); |
126
|
|
|
|
127
|
|
|
$this->assertTrue($config->isFailureAllowed()); |
128
|
|
|
} |
129
|
|
|
/** |
130
|
|
|
* Tests Config::useAnsiColors |
131
|
|
|
*/ |
132
|
|
|
public function testAnsiColorsEnabledByDefault(): void |
133
|
|
|
{ |
134
|
|
|
$config = new Config('foo.json', true); |
135
|
|
|
$this->assertTrue($config->useAnsiColors()); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Tests Config::useAnsiColors |
140
|
|
|
*/ |
141
|
|
|
public function testDisableAnsiColors(): void |
142
|
|
|
{ |
143
|
|
|
$config = new Config('foo.json', true, ['ansi-colors' => false]); |
144
|
|
|
$this->assertFalse($config->useAnsiColors()); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Tests Config::getRunMode |
149
|
|
|
*/ |
150
|
|
|
public function testGetRunMode(): void |
151
|
|
|
{ |
152
|
|
|
$config = new Config('foo.json', true, ['run-mode' => 'docker', 'run-exec' => 'foo']); |
153
|
|
|
$this->assertEquals('docker', $config->getRunConfig()->getMode()); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Tests Config::getRunExec |
158
|
|
|
*/ |
159
|
|
|
public function testGetRunExec(): void |
160
|
|
|
{ |
161
|
|
|
$config = new Config('foo.json', true, ['run-mode' => 'docker', 'run-exec' => 'foo']); |
162
|
|
|
$this->assertEquals('foo', $config->getRunConfig()->getDockerCommand()); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Tests Config::getRunPath |
167
|
|
|
*/ |
168
|
|
|
public function testGetRunPathEmptyByDefault(): void |
169
|
|
|
{ |
170
|
|
|
$config = new Config('foo.json', true, ['run-mode' => 'docker', 'run-exec' => 'foo']); |
171
|
|
|
$this->assertEquals('', $config->getRunConfig()->getCaptainsPath()); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Tests Config::getCustomSettings |
176
|
|
|
*/ |
177
|
|
|
public function testGetCustomSettings(): void |
178
|
|
|
{ |
179
|
|
|
$config = new Config('foo.json', true, ['custom' => ['foo' => 'foo']]); |
180
|
|
|
$this->assertEquals(['foo' => 'foo'], $config->getCustomSettings()); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Tests Config::getRunPath |
185
|
|
|
*/ |
186
|
|
|
public function testGetRunPath(): void |
187
|
|
|
{ |
188
|
|
|
$config = new Config('foo.json', true, ['run-mode' => 'docker', 'run-exec' => 'foo', 'run-path' => '/foo']); |
189
|
|
|
$this->assertEquals('/foo', $config->getRunConfig()->getCaptainsPath()); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Tests Config::failOnFirstError default |
194
|
|
|
*/ |
195
|
|
|
public function testFailOnFirstErrorDefault(): void |
196
|
|
|
{ |
197
|
|
|
$config = new Config('foo.json', true, []); |
198
|
|
|
$this->assertTrue($config->failOnFirstError()); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Tests Config::failOnFirstError |
203
|
|
|
*/ |
204
|
|
|
public function testFailOnFirstError(): void |
205
|
|
|
{ |
206
|
|
|
$config = new Config('foo.json', true, ['fail-on-first-error' => false]); |
207
|
|
|
$this->assertFalse($config->failOnFirstError()); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Tests Config::getJsonData |
212
|
|
|
*/ |
213
|
|
|
public function testGetJsonData(): void |
214
|
|
|
{ |
215
|
|
|
$config = new Config('./no-config.json'); |
216
|
|
|
$json = $config->getJsonData(); |
217
|
|
|
|
218
|
|
|
$this->assertIsArray($json); |
219
|
|
|
$this->assertIsArray($json['pre-commit']); |
220
|
|
|
$this->assertIsArray($json['commit-msg']); |
221
|
|
|
$this->assertIsArray($json['pre-push']); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Tests Config::getJsonData |
226
|
|
|
*/ |
227
|
|
|
public function testGetJsonDataWithSettings(): void |
228
|
|
|
{ |
229
|
|
|
$config = new Config( |
230
|
|
|
'./no-config.json', |
231
|
|
|
false, |
232
|
|
|
['run-path' => '/usr/local/bin/captainhook', 'verbosity' => 'debug'] |
233
|
|
|
); |
234
|
|
|
$json = $config->getJsonData(); |
235
|
|
|
|
236
|
|
|
$this->assertIsArray($json); |
237
|
|
|
$this->assertIsArray($json['config']); |
238
|
|
|
$this->assertIsArray($json['config']['run']); |
239
|
|
|
$this->assertIsArray($json['pre-commit']); |
240
|
|
|
$this->assertIsArray($json['commit-msg']); |
241
|
|
|
$this->assertIsArray($json['pre-push']); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Tests Config::getJsonData |
246
|
|
|
*/ |
247
|
|
|
public function testGetJsonDataWithoutEmptyConfig(): void |
248
|
|
|
{ |
249
|
|
|
$config = new Config('foo.json', true, []); |
250
|
|
|
$json = $config->getJsonData(); |
251
|
|
|
|
252
|
|
|
$this->assertArrayNotHasKey('config', $json); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Tests Config::getJsonData |
257
|
|
|
*/ |
258
|
|
|
public function testGetJsonDataWithConfigSection(): void |
259
|
|
|
{ |
260
|
|
|
$config = new Config('foo.json', true, ['run-mode' => 'docker', 'run-exec' => 'foo']); |
261
|
|
|
$json = $config->getJsonData(); |
262
|
|
|
|
263
|
|
|
$this->assertIsArray($json); |
264
|
|
|
$this->assertIsArray($json['config']); |
265
|
|
|
$this->assertEquals('foo', $json['config']['run']['exec']); |
266
|
|
|
$this->assertArrayNotHasKey('plugins', $json); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
public function testGetPluginsReturnsEmptyArray(): void |
270
|
|
|
{ |
271
|
|
|
$config = new Config('foo.json'); |
272
|
|
|
|
273
|
|
|
$this->assertSame([], $config->getPlugins()); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
public function testGetPluginsReturnsArrayOfPlugins(): void |
277
|
|
|
{ |
278
|
|
|
$plugin1 = new class implements CaptainHookPlugin { |
279
|
|
|
}; |
280
|
|
|
$plugin1Name = get_class($plugin1); |
281
|
|
|
|
282
|
|
|
$plugin2 = new class implements CaptainHookPlugin { |
283
|
|
|
}; |
284
|
|
|
$plugin2Name = get_class($plugin2); |
285
|
|
|
|
286
|
|
|
$config = new Config('foo.json', true, [ |
287
|
|
|
'plugins' => [ |
288
|
|
|
[ |
289
|
|
|
'plugin' => $plugin1Name, |
290
|
|
|
'options' => [ |
291
|
|
|
'foo' => 'bar', |
292
|
|
|
], |
293
|
|
|
], |
294
|
|
|
[ |
295
|
|
|
'plugin' => $plugin2Name, |
296
|
|
|
], |
297
|
|
|
], |
298
|
|
|
]); |
299
|
|
|
|
300
|
|
|
$json = $config->getJsonData(); |
301
|
|
|
|
302
|
|
|
$this->assertIsArray($json); |
303
|
|
|
$this->assertIsArray($json['config']); |
304
|
|
|
$this->assertIsArray($json['config']['plugins']); |
305
|
|
|
$this->assertCount(2, $config->getPlugins()); |
306
|
|
|
$this->assertContainsOnlyInstancesOf(Plugin::class, $config->getPlugins()); |
307
|
|
|
$this->assertSame( |
308
|
|
|
[ |
309
|
|
|
[ |
310
|
|
|
'plugin' => $plugin1Name, |
311
|
|
|
'options' => ['foo' => 'bar'], |
312
|
|
|
], |
313
|
|
|
[ |
314
|
|
|
'plugin' => $plugin2Name, |
315
|
|
|
'options' => [], |
316
|
|
|
], |
317
|
|
|
], |
318
|
|
|
$json['config']['plugins'] |
319
|
|
|
); |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
|