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
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace CaptainHook\App\Hook\Template; |
15
|
|
|
|
16
|
|
|
use CaptainHook\App\Config\Mockery as ConfigMockery; |
17
|
|
|
use CaptainHook\App\Config\Run; |
18
|
|
|
use CaptainHook\App\Git\DummyRepo; |
19
|
|
|
use CaptainHook\App\Mockery as AppMockery; |
20
|
|
|
use Exception; |
21
|
|
|
use PHPUnit\Framework\TestCase; |
22
|
|
|
|
23
|
|
|
class BuilderTest extends TestCase |
24
|
|
|
{ |
25
|
|
|
use AppMockery; |
26
|
|
|
use ConfigMockery; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Tests Builder::build |
30
|
|
|
*/ |
31
|
|
|
public function testBuildDockerTemplate(): void |
32
|
|
|
{ |
33
|
|
|
$repo = new DummyRepo( |
34
|
|
|
[], |
35
|
|
|
[ |
36
|
|
|
'captainhook.json' => '{}', |
37
|
|
|
'vendor' => [ |
38
|
|
|
'autoload.php' => '', |
39
|
|
|
'bin' => [ |
40
|
|
|
'captainhook' => '' |
41
|
|
|
] |
42
|
|
|
] |
43
|
|
|
] |
44
|
|
|
); |
45
|
|
|
|
46
|
|
|
$resolver = $this->createResolverMock($repo->getRoot() . '/vendor/bin/captainhook', false); |
47
|
|
|
$repository = $this->createRepositoryMock($repo->getRoot()); |
48
|
|
|
$config = $this->createConfigMock(true, $repo->getRoot() . '/captainhook.json'); |
49
|
|
|
$runConfig = new Run(['mode' => 'docker', 'exec' => 'docker exec captain-container', 'path' => '']); |
50
|
|
|
$config->method('getRunConfig')->willReturn($runConfig); |
51
|
|
|
$config->method('getBootstrap')->willReturn('vendor/autoload.php'); |
52
|
|
|
|
53
|
|
|
$template = Builder::build($config, $repository, $resolver); |
54
|
|
|
$this->assertInstanceOf(Docker::class, $template); |
55
|
|
|
|
56
|
|
|
$code = $template->getCode('pre-commit'); |
57
|
|
|
$this->assertStringContainsString('pre-commit', $code); |
58
|
|
|
$this->assertStringContainsString('docker exec -i captain-container', $code); |
59
|
|
|
$this->assertStringContainsString('vendor/bin/captainhook', $code); |
60
|
|
|
$this->assertStringContainsString('--bootstrap=vendor/autoload.php', $code); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Tests Builder::build |
65
|
|
|
*/ |
66
|
|
|
public function testBuildDockerTemplateWithoutBootstrapFromPHAR(): void |
67
|
|
|
{ |
68
|
|
|
$repo = new DummyRepo( |
69
|
|
|
[], |
70
|
|
|
[ |
71
|
|
|
'captainhook.json' => '{}', |
72
|
|
|
'vendor' => [ |
73
|
|
|
'autoload.php' => '', |
74
|
|
|
'bin' => [ |
75
|
|
|
'captainhook' => '' |
76
|
|
|
] |
77
|
|
|
] |
78
|
|
|
] |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
$resolver = $this->createResolverMock($repo->getRoot() . '/vendor/bin/captainhook', true); |
82
|
|
|
$repository = $this->createRepositoryMock($repo->getRoot()); |
83
|
|
|
$config = $this->createConfigMock(true, $repo->getRoot() . '/captainhook.json'); |
84
|
|
|
$runConfig = new Run(['mode' => 'docker', 'exec' => 'docker exec captain-container', 'path' => '']); |
85
|
|
|
$config->method('getRunConfig')->willReturn($runConfig); |
86
|
|
|
$config->method('getBootstrap')->willReturn(''); |
87
|
|
|
|
88
|
|
|
$template = Builder::build($config, $repository, $resolver); |
89
|
|
|
$this->assertInstanceOf(Docker::class, $template); |
90
|
|
|
|
91
|
|
|
$code = $template->getCode('pre-commit'); |
92
|
|
|
$this->assertStringContainsString('pre-commit', $code); |
93
|
|
|
$this->assertStringContainsString('docker exec -i captain-container', $code); |
94
|
|
|
$this->assertStringNotContainsString('--bootstrap=', $code); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Tests Builder::build |
99
|
|
|
*/ |
100
|
|
|
public function testBuildDockerTemplateWithBinaryOutsideRepo(): void |
101
|
|
|
{ |
102
|
|
|
$repo = new DummyRepo( |
103
|
|
|
[], |
104
|
|
|
[ |
105
|
|
|
'captainhook.json' => '{}', |
106
|
|
|
'vendor' => [ |
107
|
|
|
'autoload.php' => '', |
108
|
|
|
] |
109
|
|
|
] |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
$executable = realpath(__DIR__ . '/../../../../bin/captainhook'); |
113
|
|
|
$resolver = $this->createResolverMock($executable, false); |
114
|
|
|
$repository = $this->createRepositoryMock($repo->getRoot()); |
115
|
|
|
$config = $this->createConfigMock(true, $repo->getRoot() . '/captainhook.json'); |
116
|
|
|
$runConfig = new Run(['mode' => 'docker', 'exec' => 'docker exec captain-container', 'path' => '']); |
117
|
|
|
$config->method('getRunConfig')->willReturn($runConfig); |
118
|
|
|
$config->method('getBootstrap')->willReturn('vendor/autoload.php'); |
119
|
|
|
|
120
|
|
|
$template = Builder::build($config, $repository, $resolver); |
121
|
|
|
$code = $template->getCode('pre-commit'); |
122
|
|
|
|
123
|
|
|
$this->assertInstanceOf(Docker::class, $template); |
124
|
|
|
$this->assertStringContainsString('pre-commit', $code); |
125
|
|
|
$this->assertStringContainsString('docker exec -i captain-container', $code); |
126
|
|
|
$this->assertStringContainsString($executable, $code); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Tests Builder::build |
131
|
|
|
*/ |
132
|
|
|
public function testBuildLocalTemplateWithoutBootstrapFromPHAR(): void |
133
|
|
|
{ |
134
|
|
|
$resolver = $this->createResolverMock(CH_PATH_FILES . '/bin/captainhook', true); |
135
|
|
|
$repository = $this->createRepositoryMock(CH_PATH_FILES); |
136
|
|
|
$config = $this->createConfigMock(true, CH_PATH_FILES . '/template/captainhook.json'); |
137
|
|
|
$runConfig = new Run(['mode' => 'shell', 'exec' => '', 'path' => '']); |
138
|
|
|
$config->method('getRunConfig')->willReturn($runConfig); |
139
|
|
|
$config->method('getBootstrap')->willReturn(''); |
140
|
|
|
|
141
|
|
|
$template = Builder::build($config, $repository, $resolver); |
142
|
|
|
$this->assertInstanceOf(Local\Shell::class, $template); |
143
|
|
|
|
144
|
|
|
$code = $template->getCode('pre-commit'); |
145
|
|
|
$this->assertStringContainsString('pre-commit', $code); |
146
|
|
|
$this->assertStringNotContainsString('--bootstrap=', $code); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Tests Builder::build |
151
|
|
|
*/ |
152
|
|
|
public function testBuildLocalTemplate(): void |
153
|
|
|
{ |
154
|
|
|
$resolver = $this->createResolverMock(CH_PATH_FILES . '/bin/captainhook', false); |
155
|
|
|
$repository = $this->createRepositoryMock(CH_PATH_FILES); |
156
|
|
|
$config = $this->createConfigMock(true, CH_PATH_FILES . '/template/captainhook.json'); |
157
|
|
|
$runConfig = new Run(['mode' => 'php', 'exec' => '', 'path' => '']); |
158
|
|
|
$config->method('getRunConfig')->willReturn($runConfig); |
159
|
|
|
$config->method('getBootstrap')->willReturn('vendor/autoload.php'); |
160
|
|
|
|
161
|
|
|
$template = Builder::build($config, $repository, $resolver); |
162
|
|
|
$this->assertInstanceOf(Local\PHP::class, $template); |
163
|
|
|
|
164
|
|
|
$code = $template->getCode('pre-commit'); |
165
|
|
|
$this->assertStringContainsString('pre-commit', $code); |
166
|
|
|
$this->assertStringContainsString('$captainHook->run', $code); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Tests Builder::build |
171
|
|
|
*/ |
172
|
|
|
public function testBuildWSLTemplate(): void |
173
|
|
|
{ |
174
|
|
|
$resolver = $this->createResolverMock(CH_PATH_FILES . '/bin/captainhook', false); |
175
|
|
|
$repository = $this->createRepositoryMock(CH_PATH_FILES); |
176
|
|
|
$config = $this->createConfigMock(true, CH_PATH_FILES . '/template/captainhook.json'); |
177
|
|
|
$runConfig = new Run(['mode' => 'wsl', 'exec' => '', 'path' => '']); |
178
|
|
|
$config->method('getRunConfig')->willReturn($runConfig); |
179
|
|
|
$config->method('getBootstrap')->willReturn('vendor/autoload.php'); |
180
|
|
|
|
181
|
|
|
$template = Builder::build($config, $repository, $resolver); |
182
|
|
|
$this->assertInstanceOf(Local\WSL::class, $template); |
183
|
|
|
|
184
|
|
|
$code = $template->getCode('pre-commit'); |
185
|
|
|
$this->assertStringContainsString('pre-commit', $code); |
186
|
|
|
$this->assertStringContainsString('wsl.exe', $code); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Tests Builder::build |
191
|
|
|
*/ |
192
|
|
|
public function testBuildBootstrapNotFound(): void |
193
|
|
|
{ |
194
|
|
|
$this->expectException(Exception::class); |
195
|
|
|
|
196
|
|
|
$resolver = $this->createResolverMock(CH_PATH_FILES . '/bin/captainhook'); |
197
|
|
|
$repository = $this->createRepositoryMock(CH_PATH_FILES); |
198
|
|
|
$config = $this->createConfigMock(true, CH_PATH_FILES . '/template/captainhook.json'); |
199
|
|
|
$runConfig = new Run(['mode' => 'php', 'exec' => '', 'path' => '']); |
200
|
|
|
$config->method('getRunConfig')->willReturn($runConfig); |
201
|
|
|
$config->method('getBootstrap')->willReturn('vendorXX/autoload.php'); |
202
|
|
|
|
203
|
|
|
$template = Builder::build($config, $repository, $resolver); |
204
|
|
|
$this->assertInstanceOf(Local\PHP::class, $template); |
205
|
|
|
|
206
|
|
|
$code = $template->getCode('pre-commit'); |
207
|
|
|
$this->assertStringContainsString('pre-commit', $code); |
208
|
|
|
$this->assertStringContainsString('$captainHook->run', $code); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Tests Builder::build |
213
|
|
|
*/ |
214
|
|
|
public function testBuildInvalidVendor(): void |
215
|
|
|
{ |
216
|
|
|
$this->expectException(Exception::class); |
217
|
|
|
|
218
|
|
|
$resolver = $this->createResolverMock('./captainhook'); |
219
|
|
|
$repository = $this->createRepositoryMock(CH_PATH_FILES . '/config'); |
220
|
|
|
$config = $this->createConfigMock(true, CH_PATH_FILES . '/config/valid.json'); |
221
|
|
|
$runConfig = new Run(['mode' => 'php', 'exec' => '', 'path' => '']); |
222
|
|
|
$config->method('getRunConfig')->willReturn($runConfig); |
223
|
|
|
$config->method('getBootstrap')->willReturn('file-not-there.php'); |
224
|
|
|
|
225
|
|
|
Builder::build($config, $repository, $resolver); |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|