|
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
|
|
|
public function testItCanBeCreatedWithoutFile(): void |
|
23
|
|
|
{ |
|
24
|
|
|
$config = new Config('./no-config.json'); |
|
25
|
|
|
$this->assertFalse($config->isLoadedFromFile()); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function testItCanBeLoadedFromFile(): void |
|
29
|
|
|
{ |
|
30
|
|
|
$config = new Config('valid.json', true); |
|
31
|
|
|
$this->assertTrue($config->isLoadedFromFile()); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function testItCanReturnAllHookConfigs(): void |
|
35
|
|
|
{ |
|
36
|
|
|
$config = new Config('valid.json', true); |
|
37
|
|
|
$this->assertNotEmpty($config->getHookConfigs()); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function testItDoesNotAllowInvalidHookNames(): void |
|
41
|
|
|
{ |
|
42
|
|
|
$this->expectException(Exception::class); |
|
43
|
|
|
$config = new Config('./no-config.json'); |
|
44
|
|
|
$config->getHookConfig('foo'); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function testItCombinesHooksAndVirtualHookConfigurations(): void |
|
48
|
|
|
{ |
|
49
|
|
|
$config = new Config('./no-config.json'); |
|
50
|
|
|
$config->getHookConfig('post-rewrite')->setEnabled(true); |
|
51
|
|
|
$config->getHookConfig('post-rewrite')->addAction(new Action('echo foo')); |
|
52
|
|
|
$config->getHookConfig('post-change')->setEnabled(true); |
|
53
|
|
|
$config->getHookConfig('post-change')->addAction(new Action('echo bar')); |
|
54
|
|
|
|
|
55
|
|
|
$hookConfig = $config->getHookConfigToExecute('post-rewrite'); |
|
56
|
|
|
|
|
57
|
|
|
$this->assertCount(2, $hookConfig->getActions()); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function testItAssumesCwdAsGitDir(): void |
|
61
|
|
|
{ |
|
62
|
|
|
$config = new Config('./no-config.json'); |
|
63
|
|
|
$this->assertEquals(getcwd() . '/.git', $config->getGitDirectory()); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function testItCanReturnTheConfigurationPath(): void |
|
67
|
|
|
{ |
|
68
|
|
|
$path = realpath(CH_PATH_FILES . '/config/valid.json'); |
|
69
|
|
|
$config = new Config($path); |
|
70
|
|
|
|
|
71
|
|
|
$this->assertEquals($path, $config->getPath()); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function testIsHasABootstrapDefault(): void |
|
75
|
|
|
{ |
|
76
|
|
|
$path = realpath(CH_PATH_FILES . '/config/valid.json'); |
|
77
|
|
|
$config = new Config($path); |
|
78
|
|
|
|
|
79
|
|
|
$this->assertEquals('vendor/autoload.php', $config->getBootstrap()); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function testItCanSetTheBootstrap(): void |
|
83
|
|
|
{ |
|
84
|
|
|
$path = realpath(CH_PATH_FILES . '/config/valid.json'); |
|
85
|
|
|
$config = new Config($path, true, ['bootstrap' => 'libs/autoload.php']); |
|
86
|
|
|
|
|
87
|
|
|
$this->assertEquals('libs/autoload.php', $config->getBootstrap()); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function testNoFailuresAreAllowedByDefault(): void |
|
91
|
|
|
{ |
|
92
|
|
|
$path = realpath(CH_PATH_FILES . '/config/valid.json'); |
|
93
|
|
|
$config = new Config($path, true); |
|
94
|
|
|
|
|
95
|
|
|
$this->assertFalse($config->isFailureAllowed()); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function testAllowFailureCanBeChanged(): void |
|
99
|
|
|
{ |
|
100
|
|
|
$path = realpath(CH_PATH_FILES . '/config/valid.json'); |
|
101
|
|
|
$config = new Config($path, true, ['allow-failure' => true]); |
|
102
|
|
|
|
|
103
|
|
|
$this->assertTrue($config->isFailureAllowed()); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function testColorsAreEnabledByDefault(): void |
|
107
|
|
|
{ |
|
108
|
|
|
$config = new Config('foo.json', true); |
|
109
|
|
|
$this->assertTrue($config->useAnsiColors()); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function testAnsiColorsCanBeDisabled(): void |
|
113
|
|
|
{ |
|
114
|
|
|
$config = new Config('foo.json', true, ['ansi-colors' => false]); |
|
115
|
|
|
$this->assertFalse($config->useAnsiColors()); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function testProvidesAccessToRunMode(): void |
|
119
|
|
|
{ |
|
120
|
|
|
$config = new Config('foo.json', true, ['run-mode' => 'docker', 'run-exec' => 'foo']); |
|
121
|
|
|
$this->assertEquals('docker', $config->getRunConfig()->getMode()); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function testProvidesAccessToRunExec(): void |
|
125
|
|
|
{ |
|
126
|
|
|
$config = new Config('foo.json', true, ['run-mode' => 'docker', 'run-exec' => 'foo']); |
|
127
|
|
|
$this->assertEquals('foo', $config->getRunConfig()->getDockerCommand()); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function testRunPathIsEmptyByDefault(): void |
|
131
|
|
|
{ |
|
132
|
|
|
$config = new Config('foo.json', true, ['run-mode' => 'docker', 'run-exec' => 'foo']); |
|
133
|
|
|
$this->assertEquals('', $config->getRunConfig()->getCaptainsPath()); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function testAllowsCustomSettings(): void |
|
137
|
|
|
{ |
|
138
|
|
|
$config = new Config('foo.json', true, ['custom' => ['foo' => 'foo']]); |
|
139
|
|
|
$this->assertEquals(['foo' => 'foo'], $config->getCustomSettings()); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
public function testProvidesAccessToRunPath(): void |
|
143
|
|
|
{ |
|
144
|
|
|
$config = new Config('foo.json', true, ['run-mode' => 'docker', 'run-exec' => 'foo', 'run-path' => '/foo']); |
|
145
|
|
|
$this->assertEquals('/foo', $config->getRunConfig()->getCaptainsPath()); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
public function testFailOnFirstErrorIsTrueByDefault(): void |
|
149
|
|
|
{ |
|
150
|
|
|
$config = new Config('foo.json', true, []); |
|
151
|
|
|
$this->assertTrue($config->failOnFirstError()); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
public function testFailOnFirstErrorCanBeChanged(): void |
|
155
|
|
|
{ |
|
156
|
|
|
$config = new Config('foo.json', true, ['fail-on-first-error' => false]); |
|
157
|
|
|
$this->assertFalse($config->failOnFirstError()); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
public function testExportsOnlyEnabledHooksOrHooksWithActionsToJsonData(): void |
|
161
|
|
|
{ |
|
162
|
|
|
$config = new Config('./no-config.json'); |
|
163
|
|
|
$config->getHookConfig('pre-commit')->setEnabled(true); |
|
164
|
|
|
$config->getHookConfig('pre-push')->addAction(new Action('foo')); |
|
165
|
|
|
$json = $config->getJsonData(); |
|
166
|
|
|
|
|
167
|
|
|
$this->assertIsArray($json); |
|
168
|
|
|
$this->assertIsArray($json['pre-commit']); |
|
169
|
|
|
$this->assertIsArray($json['pre-push']); |
|
170
|
|
|
$this->assertFalse(array_key_exists('commit-msg', $json)); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
public function testCanBeExportedToJsonDataWithSettings(): void |
|
174
|
|
|
{ |
|
175
|
|
|
$config = new Config( |
|
176
|
|
|
'./no-config.json', |
|
177
|
|
|
false, |
|
178
|
|
|
['run-path' => '/usr/local/bin/captainhook', 'verbosity' => 'debug'] |
|
179
|
|
|
); |
|
180
|
|
|
$json = $config->getJsonData(); |
|
181
|
|
|
|
|
182
|
|
|
$this->assertIsArray($json); |
|
183
|
|
|
$this->assertIsArray($json['config']); |
|
184
|
|
|
$this->assertIsArray($json['config']['run']); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
public function testGetJsonDataWithoutEmptyConfig(): void |
|
188
|
|
|
{ |
|
189
|
|
|
$config = new Config('foo.json', true, []); |
|
190
|
|
|
$json = $config->getJsonData(); |
|
191
|
|
|
|
|
192
|
|
|
$this->assertArrayNotHasKey('config', $json); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
public function testGetJsonDataWithConfigSection(): void |
|
196
|
|
|
{ |
|
197
|
|
|
$config = new Config('foo.json', true, ['run-mode' => 'docker', 'run-exec' => 'foo']); |
|
198
|
|
|
$json = $config->getJsonData(); |
|
199
|
|
|
|
|
200
|
|
|
$this->assertIsArray($json); |
|
201
|
|
|
$this->assertIsArray($json['config']); |
|
202
|
|
|
$this->assertEquals('foo', $json['config']['run']['exec']); |
|
203
|
|
|
$this->assertArrayNotHasKey('plugins', $json); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
public function testDoesNotExportIncludedActionsToJson(): void |
|
207
|
|
|
{ |
|
208
|
|
|
$localAction = new Action('foo'); |
|
209
|
|
|
$includedAction = new Action('bar'); |
|
210
|
|
|
$includedAction->markIncluded(); |
|
211
|
|
|
|
|
212
|
|
|
$config = new Config('foo.json', true, []); |
|
213
|
|
|
$config->getHookConfig('pre-commit')->setEnabled(true); |
|
214
|
|
|
$config->getHookConfig('pre-commit')->addAction($localAction); |
|
215
|
|
|
$config->getHookConfig('pre-commit')->addAction($includedAction); |
|
216
|
|
|
|
|
217
|
|
|
$json = $config->getJsonData(); |
|
218
|
|
|
$this->assertCount(1, $json['pre-commit']['actions']); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
public function testGetPluginsReturnsEmptyArray(): void |
|
222
|
|
|
{ |
|
223
|
|
|
$config = new Config('foo.json'); |
|
224
|
|
|
|
|
225
|
|
|
$this->assertSame([], $config->getPlugins()); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
public function testGetPluginsReturnsArrayOfPlugins(): void |
|
229
|
|
|
{ |
|
230
|
|
|
$plugin1 = new class implements CaptainHookPlugin { |
|
231
|
|
|
}; |
|
232
|
|
|
$plugin1Name = get_class($plugin1); |
|
233
|
|
|
|
|
234
|
|
|
$plugin2 = new class implements CaptainHookPlugin { |
|
235
|
|
|
}; |
|
236
|
|
|
$plugin2Name = get_class($plugin2); |
|
237
|
|
|
|
|
238
|
|
|
$config = new Config('foo.json', true, [ |
|
239
|
|
|
'plugins' => [ |
|
240
|
|
|
[ |
|
241
|
|
|
'plugin' => $plugin1Name, |
|
242
|
|
|
'options' => [ |
|
243
|
|
|
'foo' => 'bar', |
|
244
|
|
|
], |
|
245
|
|
|
], |
|
246
|
|
|
[ |
|
247
|
|
|
'plugin' => $plugin2Name, |
|
248
|
|
|
], |
|
249
|
|
|
], |
|
250
|
|
|
]); |
|
251
|
|
|
|
|
252
|
|
|
$json = $config->getJsonData(); |
|
253
|
|
|
|
|
254
|
|
|
$this->assertIsArray($json); |
|
255
|
|
|
$this->assertIsArray($json['config']); |
|
256
|
|
|
$this->assertIsArray($json['config']['plugins']); |
|
257
|
|
|
$this->assertCount(2, $config->getPlugins()); |
|
258
|
|
|
$this->assertContainsOnlyInstancesOf(Plugin::class, $config->getPlugins()); |
|
259
|
|
|
$this->assertSame( |
|
260
|
|
|
[ |
|
261
|
|
|
[ |
|
262
|
|
|
'plugin' => $plugin1Name, |
|
263
|
|
|
'options' => ['foo' => 'bar'], |
|
264
|
|
|
], |
|
265
|
|
|
[ |
|
266
|
|
|
'plugin' => $plugin2Name, |
|
267
|
|
|
'options' => [], |
|
268
|
|
|
], |
|
269
|
|
|
], |
|
270
|
|
|
$json['config']['plugins'] |
|
271
|
|
|
); |
|
272
|
|
|
} |
|
273
|
|
|
} |
|
274
|
|
|
|