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(__DIR__ . '/../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(__DIR__ . '/../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(__DIR__ . '/../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(__DIR__ . '/../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(__DIR__ . '/../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 testCanBeExportedToJsonData(): void |
161
|
|
|
{ |
162
|
|
|
$config = new Config('./no-config.json'); |
163
|
|
|
$json = $config->getJsonData(); |
164
|
|
|
|
165
|
|
|
$this->assertIsArray($json); |
166
|
|
|
$this->assertIsArray($json['pre-commit']); |
167
|
|
|
$this->assertIsArray($json['commit-msg']); |
168
|
|
|
$this->assertIsArray($json['pre-push']); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function testCanBeExportedToJsonDataWithSettings(): void |
172
|
|
|
{ |
173
|
|
|
$config = new Config( |
174
|
|
|
'./no-config.json', |
175
|
|
|
false, |
176
|
|
|
['run-path' => '/usr/local/bin/captainhook', 'verbosity' => 'debug'] |
177
|
|
|
); |
178
|
|
|
$json = $config->getJsonData(); |
179
|
|
|
|
180
|
|
|
$this->assertIsArray($json); |
181
|
|
|
$this->assertIsArray($json['config']); |
182
|
|
|
$this->assertIsArray($json['config']['run']); |
183
|
|
|
$this->assertIsArray($json['pre-commit']); |
184
|
|
|
$this->assertIsArray($json['commit-msg']); |
185
|
|
|
$this->assertIsArray($json['pre-push']); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function testGetJsonDataWithoutEmptyConfig(): void |
189
|
|
|
{ |
190
|
|
|
$config = new Config('foo.json', true, []); |
191
|
|
|
$json = $config->getJsonData(); |
192
|
|
|
|
193
|
|
|
$this->assertArrayNotHasKey('config', $json); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function testGetJsonDataWithConfigSection(): void |
197
|
|
|
{ |
198
|
|
|
$config = new Config('foo.json', true, ['run-mode' => 'docker', 'run-exec' => 'foo']); |
199
|
|
|
$json = $config->getJsonData(); |
200
|
|
|
|
201
|
|
|
$this->assertIsArray($json); |
202
|
|
|
$this->assertIsArray($json['config']); |
203
|
|
|
$this->assertEquals('foo', $json['config']['run']['exec']); |
204
|
|
|
$this->assertArrayNotHasKey('plugins', $json); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function testGetPluginsReturnsEmptyArray(): void |
208
|
|
|
{ |
209
|
|
|
$config = new Config('foo.json'); |
210
|
|
|
|
211
|
|
|
$this->assertSame([], $config->getPlugins()); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
public function testGetPluginsReturnsArrayOfPlugins(): void |
215
|
|
|
{ |
216
|
|
|
$plugin1 = new class implements CaptainHookPlugin { |
217
|
|
|
}; |
218
|
|
|
$plugin1Name = get_class($plugin1); |
219
|
|
|
|
220
|
|
|
$plugin2 = new class implements CaptainHookPlugin { |
221
|
|
|
}; |
222
|
|
|
$plugin2Name = get_class($plugin2); |
223
|
|
|
|
224
|
|
|
$config = new Config('foo.json', true, [ |
225
|
|
|
'plugins' => [ |
226
|
|
|
[ |
227
|
|
|
'plugin' => $plugin1Name, |
228
|
|
|
'options' => [ |
229
|
|
|
'foo' => 'bar', |
230
|
|
|
], |
231
|
|
|
], |
232
|
|
|
[ |
233
|
|
|
'plugin' => $plugin2Name, |
234
|
|
|
], |
235
|
|
|
], |
236
|
|
|
]); |
237
|
|
|
|
238
|
|
|
$json = $config->getJsonData(); |
239
|
|
|
|
240
|
|
|
$this->assertIsArray($json); |
241
|
|
|
$this->assertIsArray($json['config']); |
242
|
|
|
$this->assertIsArray($json['config']['plugins']); |
243
|
|
|
$this->assertCount(2, $config->getPlugins()); |
244
|
|
|
$this->assertContainsOnlyInstancesOf(Plugin::class, $config->getPlugins()); |
245
|
|
|
$this->assertSame( |
246
|
|
|
[ |
247
|
|
|
[ |
248
|
|
|
'plugin' => $plugin1Name, |
249
|
|
|
'options' => ['foo' => 'bar'], |
250
|
|
|
], |
251
|
|
|
[ |
252
|
|
|
'plugin' => $plugin2Name, |
253
|
|
|
'options' => [], |
254
|
|
|
], |
255
|
|
|
], |
256
|
|
|
$json['config']['plugins'] |
257
|
|
|
); |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|