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\Runner; |
13
|
|
|
|
14
|
|
|
use CaptainHook\App\Config\Mockery as ConfigMockery; |
15
|
|
|
use CaptainHook\App\Console\IO\Mockery as IOMockery; |
16
|
|
|
use CaptainHook\App\Exception\InvalidHookName; |
17
|
|
|
use CaptainHook\App\Git\DummyRepo; |
18
|
|
|
use CaptainHook\App\Hook\Mockery as HookMockery; |
19
|
|
|
use CaptainHook\App\Mockery as CHMockery; |
20
|
|
|
use PHPUnit\Framework\TestCase; |
21
|
|
|
use SebastianFeldmann\Git\Repository; |
22
|
|
|
|
23
|
|
|
class InstallerTest extends TestCase |
24
|
|
|
{ |
25
|
|
|
use ConfigMockery; |
26
|
|
|
use IOMockery; |
27
|
|
|
use CHMockery; |
28
|
|
|
use HookMockery; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Tests Installer::setHook |
32
|
|
|
* |
33
|
|
|
* @throws \CaptainHook\App\Exception\InvalidHookName |
34
|
|
|
*/ |
35
|
|
|
public function testSetInvalidHook(): void |
36
|
|
|
{ |
37
|
|
|
$this->expectException(InvalidHookName::class); |
38
|
|
|
|
39
|
|
|
$io = $this->createIOMock(); |
40
|
|
|
$config = $this->createConfigMock(); |
41
|
|
|
$repo = $this->createRepositoryMock(); |
42
|
|
|
$template = $this->createTemplateMock(); |
43
|
|
|
|
44
|
|
|
$runner = new Installer($io, $config, $repo, $template); |
45
|
|
|
$runner->setHook('itDoNotExist'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Tests Installer::setHook |
50
|
|
|
* |
51
|
|
|
* @throws \CaptainHook\App\Exception\InvalidHookName |
52
|
|
|
*/ |
53
|
|
|
public function testMoveAfterSkippingFail(): void |
54
|
|
|
{ |
55
|
|
|
$this->expectException(\RuntimeException::class); |
56
|
|
|
|
57
|
|
|
$io = $this->createIOMock(); |
58
|
|
|
$config = $this->createConfigMock(); |
59
|
|
|
$repo = $this->createRepositoryMock(); |
60
|
|
|
$template = $this->createTemplateMock(); |
61
|
|
|
|
62
|
|
|
$runner = new Installer($io, $config, $repo, $template); |
63
|
|
|
$runner->setHook('pre-commit'); |
64
|
|
|
$runner->setSkipExisting(true); |
65
|
|
|
$runner->setMoveExistingTo('/tmp/'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Tests Installer::setHook |
70
|
|
|
* |
71
|
|
|
* @throws \CaptainHook\App\Exception\InvalidHookName |
72
|
|
|
*/ |
73
|
|
|
public function testSkipAfterMovingFail(): void |
74
|
|
|
{ |
75
|
|
|
$this->expectException(\RuntimeException::class); |
76
|
|
|
|
77
|
|
|
$io = $this->createIOMock(); |
78
|
|
|
$config = $this->createConfigMock(); |
79
|
|
|
$repo = $this->createRepositoryMock(); |
80
|
|
|
$template = $this->createTemplateMock(); |
81
|
|
|
|
82
|
|
|
$runner = new Installer($io, $config, $repo, $template); |
83
|
|
|
$runner->setHook('pre-commit'); |
84
|
|
|
$runner->setMoveExistingTo('/tmp/'); |
85
|
|
|
$runner->setSkipExisting(true); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Tests Installer::run |
90
|
|
|
*/ |
91
|
|
|
public function testHookInstallationDeclined(): void |
92
|
|
|
{ |
93
|
|
|
$fakeRepo = new DummyRepo(); |
94
|
|
|
|
95
|
|
|
$io = $this->createIOMock(); |
96
|
|
|
$config = $this->createConfigMock(); |
97
|
|
|
$repo = $this->createRepositoryMock($fakeRepo->getRoot()); |
98
|
|
|
$template = $this->createTemplateMock(); |
99
|
|
|
|
100
|
|
|
$io->expects($this->atLeast(5))->method('ask')->willReturn('n'); |
101
|
|
|
|
102
|
|
|
$runner = new Installer($io, $config, $repo, $template); |
103
|
|
|
$runner->run(); |
104
|
|
|
|
105
|
|
|
$this->assertFileNotExists($fakeRepo->getHookDir() . '/pre-commit'); |
106
|
|
|
$this->assertFileNotExists($fakeRepo->getHookDir() . '/pre-push'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Tests Installer::run |
111
|
|
|
*/ |
112
|
|
|
public function testWriteHook(): void |
113
|
|
|
{ |
114
|
|
|
$fakeRepo = new DummyRepo(); |
115
|
|
|
|
116
|
|
|
$io = $this->createIOMock(); |
117
|
|
|
$config = $this->createConfigMock(); |
118
|
|
|
$repo = $this->createRepositoryMock($fakeRepo->getRoot()); |
119
|
|
|
$template = $this->createTemplateMock(); |
120
|
|
|
|
121
|
|
|
$template->expects($this->once()) |
122
|
|
|
->method('getCode') |
123
|
|
|
->with('pre-commit') |
124
|
|
|
->willReturn(''); |
125
|
|
|
|
126
|
|
|
$runner = new Installer($io, $config, $repo, $template); |
127
|
|
|
$runner->setHook('pre-commit'); |
128
|
|
|
$runner->run(); |
129
|
|
|
|
130
|
|
|
$this->assertFileExists($fakeRepo->getHookDir() . '/pre-commit'); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Tests Installer::run |
135
|
|
|
*/ |
136
|
|
|
public function testMoveExistingHook(): void |
137
|
|
|
{ |
138
|
|
|
$fakeRepo = new DummyRepo( |
139
|
|
|
// git repo |
140
|
|
|
[ |
141
|
|
|
'config' => '# fake git config', |
142
|
|
|
'hooks' => [ |
143
|
|
|
'pre-commit' => '# fake pre-commit file', |
144
|
|
|
'pre-push' => '# fake pre-push file', |
145
|
|
|
] |
146
|
|
|
], |
147
|
|
|
// files |
148
|
|
|
[ |
149
|
|
|
'foo' => [] |
150
|
|
|
] |
151
|
|
|
); |
152
|
|
|
|
153
|
|
|
$io = $this->createIOMock(); |
154
|
|
|
$config = $this->createConfigMock(true, $fakeRepo->getRoot() . '/captainhook.json'); |
155
|
|
|
$template = $this->createTemplateMock(); |
156
|
|
|
$repo = new Repository($fakeRepo->getRoot()); |
157
|
|
|
|
158
|
|
|
$template->expects($this->once()) |
159
|
|
|
->method('getCode') |
160
|
|
|
->with('pre-commit') |
161
|
|
|
->willReturn(''); |
162
|
|
|
|
163
|
|
|
$runner = new Installer($io, $config, $repo, $template); |
164
|
|
|
$runner->setHook('pre-commit') |
165
|
|
|
->setMoveExistingTo('foo/bar/') |
166
|
|
|
->run(); |
167
|
|
|
|
168
|
|
|
$this->assertFileExists($fakeRepo->getHookDir() . '/pre-commit'); |
169
|
|
|
$this->assertFileExists($fakeRepo->getRoot() . '/foo/bar/pre-commit'); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Tests Installer::run |
174
|
|
|
*/ |
175
|
|
|
public function testMoveNotExistingHook(): void |
176
|
|
|
{ |
177
|
|
|
$fakeRepo = new DummyRepo( |
178
|
|
|
// git repo |
179
|
|
|
[ |
180
|
|
|
'config' => '# fake git config', |
181
|
|
|
'hooks' => [ |
182
|
|
|
'pre-push' => '# fake pre-push file', |
183
|
|
|
] |
184
|
|
|
], |
185
|
|
|
// files |
186
|
|
|
[ |
187
|
|
|
'foo' => [] |
188
|
|
|
] |
189
|
|
|
); |
190
|
|
|
|
191
|
|
|
$io = $this->createIOMock(); |
192
|
|
|
$config = $this->createConfigMock(true, $fakeRepo->getRoot() . '/captainhook.json'); |
193
|
|
|
$template = $this->createTemplateMock(); |
194
|
|
|
$repo = new Repository($fakeRepo->getRoot()); |
195
|
|
|
|
196
|
|
|
$template->expects($this->once()) |
197
|
|
|
->method('getCode') |
198
|
|
|
->with('pre-commit') |
199
|
|
|
->willReturn(''); |
200
|
|
|
|
201
|
|
|
$runner = new Installer($io, $config, $repo, $template); |
202
|
|
|
$runner->setHook('pre-commit') |
203
|
|
|
->setMoveExistingTo('foo/bar/') |
204
|
|
|
->run(); |
205
|
|
|
|
206
|
|
|
$this->assertFileExists($fakeRepo->getHookDir() . '/pre-commit'); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Tests Installer::run |
211
|
|
|
*/ |
212
|
|
|
public function testMoveExistingHookTargetIsFile(): void |
213
|
|
|
{ |
214
|
|
|
$this->expectException(\RuntimeException::class); |
215
|
|
|
|
216
|
|
|
$fakeRepo = new DummyRepo( |
217
|
|
|
// git repo |
218
|
|
|
[ |
219
|
|
|
'config' => '# fake git config', |
220
|
|
|
'hooks' => [ |
221
|
|
|
'pre-commit' => '# fake pre-commit file', |
222
|
|
|
'pre-push' => '# fake pre-push file', |
223
|
|
|
] |
224
|
|
|
], |
225
|
|
|
// files |
226
|
|
|
[ |
227
|
|
|
'foo' => '# some random file' |
228
|
|
|
] |
229
|
|
|
); |
230
|
|
|
|
231
|
|
|
$io = $this->createIOMock(); |
232
|
|
|
$config = $this->createConfigMock(true, $fakeRepo->getRoot() . '/captainhook.json'); |
233
|
|
|
$template = $this->createTemplateMock(); |
234
|
|
|
$repo = new Repository($fakeRepo->getRoot()); |
235
|
|
|
|
236
|
|
|
$runner = new Installer($io, $config, $repo, $template); |
237
|
|
|
$runner->setHook('pre-commit') |
238
|
|
|
->setMoveExistingTo('foo') |
239
|
|
|
->run(); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Tests Installer::writeHookFile |
244
|
|
|
*/ |
245
|
|
|
public function testSkipExisting(): void |
246
|
|
|
{ |
247
|
|
|
$io = $this->createIOMock(); |
248
|
|
|
$config = $this->createConfigMock(); |
249
|
|
|
$repo = $this->createRepositoryMock(); |
250
|
|
|
$template = $this->createTemplateMock(); |
251
|
|
|
|
252
|
|
|
$io->expects($this->atLeast(1))->method('write'); |
253
|
|
|
$repo->expects($this->once())->method('hookExists')->willReturn(true); |
254
|
|
|
|
255
|
|
|
$runner = new Installer($io, $config, $repo, $template); |
256
|
|
|
$runner->setSkipExisting(true); |
257
|
|
|
$runner->setHook('pre-commit'); |
258
|
|
|
$runner->run(); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Tests Installer::writeHookFile |
263
|
|
|
*/ |
264
|
|
|
public function testDeclineOverwrite(): void |
265
|
|
|
{ |
266
|
|
|
$io = $this->createIOMock(); |
267
|
|
|
$config = $this->createConfigMock(); |
268
|
|
|
$repo = $this->createRepositoryMock(); |
269
|
|
|
$template = $this->createTemplateMock(); |
270
|
|
|
|
271
|
|
|
$io->expects($this->once())->method('ask')->willReturn('n'); |
272
|
|
|
$repo->expects($this->once())->method('hookExists')->willReturn(true); |
273
|
|
|
|
274
|
|
|
$runner = new Installer($io, $config, $repo, $template); |
275
|
|
|
$runner->setHook('pre-commit'); |
276
|
|
|
$runner->run(); |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|