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
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Tests Builder::build |
64
|
|
|
*/ |
65
|
|
|
public function testBuildDockerTemplateWithBinaryOutsideRepo(): void |
66
|
|
|
{ |
67
|
|
|
$repo = new DummyRepo( |
68
|
|
|
[], |
69
|
|
|
[ |
70
|
|
|
'captainhook.json' => '{}', |
71
|
|
|
'vendor' => [ |
72
|
|
|
'autoload.php' => '', |
73
|
|
|
] |
74
|
|
|
] |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
$executable = realpath(__DIR__ . '/../../../../bin/captainhook'); |
78
|
|
|
$resolver = $this->createResolverMock($executable, false); |
79
|
|
|
$repository = $this->createRepositoryMock($repo->getRoot()); |
80
|
|
|
$config = $this->createConfigMock(true, $repo->getRoot() . '/captainhook.json'); |
81
|
|
|
$runConfig = new Run(['mode' => 'docker', 'exec' => 'docker exec captain-container', 'path' => '']); |
82
|
|
|
$config->method('getRunConfig')->willReturn($runConfig); |
83
|
|
|
$config->method('getBootstrap')->willReturn('vendor/autoload.php'); |
84
|
|
|
|
85
|
|
|
$template = Builder::build($config, $repository, $resolver); |
86
|
|
|
$code = $template->getCode('pre-commit'); |
87
|
|
|
|
88
|
|
|
$this->assertInstanceOf(Docker::class, $template); |
89
|
|
|
$this->assertStringContainsString('pre-commit', $code); |
90
|
|
|
$this->assertStringContainsString('docker exec -i captain-container', $code); |
91
|
|
|
$this->assertStringContainsString($executable, $code); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Tests Builder::build |
96
|
|
|
*/ |
97
|
|
|
public function testBuildLocalTemplate(): void |
98
|
|
|
{ |
99
|
|
|
$resolver = $this->createResolverMock(CH_PATH_FILES . '/bin/captainhook', false); |
100
|
|
|
$repository = $this->createRepositoryMock(CH_PATH_FILES); |
101
|
|
|
$config = $this->createConfigMock(true, CH_PATH_FILES . '/template/captainhook.json'); |
102
|
|
|
$runConfig = new Run(['mode' => 'php', 'exec' => '', 'path' => '']); |
103
|
|
|
$config->method('getRunConfig')->willReturn($runConfig); |
104
|
|
|
$config->method('getBootstrap')->willReturn('vendor/autoload.php'); |
105
|
|
|
|
106
|
|
|
$template = Builder::build($config, $repository, $resolver); |
107
|
|
|
$this->assertInstanceOf(Local\PHP::class, $template); |
108
|
|
|
|
109
|
|
|
$code = $template->getCode('pre-commit'); |
110
|
|
|
$this->assertStringContainsString('pre-commit', $code); |
111
|
|
|
$this->assertStringContainsString('$captainHook->run', $code); |
112
|
|
|
} |
113
|
|
|
/** |
114
|
|
|
* Tests Builder::build |
115
|
|
|
*/ |
116
|
|
|
public function testBuildWSLTemplate(): void |
117
|
|
|
{ |
118
|
|
|
$resolver = $this->createResolverMock(CH_PATH_FILES . '/bin/captainhook', false); |
119
|
|
|
$repository = $this->createRepositoryMock(CH_PATH_FILES); |
120
|
|
|
$config = $this->createConfigMock(true, CH_PATH_FILES . '/template/captainhook.json'); |
121
|
|
|
$runConfig = new Run(['mode' => 'wsl', 'exec' => '', 'path' => '']); |
122
|
|
|
$config->method('getRunConfig')->willReturn($runConfig); |
123
|
|
|
$config->method('getBootstrap')->willReturn('vendor/autoload.php'); |
124
|
|
|
|
125
|
|
|
$template = Builder::build($config, $repository, $resolver); |
126
|
|
|
$this->assertInstanceOf(Local\WSL::class, $template); |
127
|
|
|
|
128
|
|
|
$code = $template->getCode('pre-commit'); |
129
|
|
|
$this->assertStringContainsString('pre-commit', $code); |
130
|
|
|
$this->assertStringContainsString('wsl.exe', $code); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Tests Builder::build |
135
|
|
|
*/ |
136
|
|
|
public function testBuildBootstrapNotFound(): void |
137
|
|
|
{ |
138
|
|
|
$this->expectException(Exception::class); |
139
|
|
|
|
140
|
|
|
$resolver = $this->createResolverMock(CH_PATH_FILES . '/bin/captainhook', false); |
141
|
|
|
$repository = $this->createRepositoryMock(CH_PATH_FILES); |
142
|
|
|
$config = $this->createConfigMock(true, CH_PATH_FILES . '/template/captainhook.json'); |
143
|
|
|
$runConfig = new Run(['mode' => 'php', 'exec' => '', 'path' => '']); |
144
|
|
|
$config->method('getRunConfig')->willReturn($runConfig); |
145
|
|
|
$config->method('getBootstrap')->willReturn('vendorXX/autoload.php'); |
146
|
|
|
|
147
|
|
|
$template = Builder::build($config, $repository, $resolver); |
148
|
|
|
$this->assertInstanceOf(Local\PHP::class, $template); |
149
|
|
|
|
150
|
|
|
$code = $template->getCode('pre-commit'); |
151
|
|
|
$this->assertStringContainsString('pre-commit', $code); |
152
|
|
|
$this->assertStringContainsString('$captainHook->run', $code); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Tests Builder::build |
157
|
|
|
*/ |
158
|
|
|
public function testBuildInvalidVendor(): void |
159
|
|
|
{ |
160
|
|
|
$this->expectException(Exception::class); |
161
|
|
|
|
162
|
|
|
$resolver = $this->createResolverMock('./captainhook', false); |
163
|
|
|
$repository = $this->createRepositoryMock(CH_PATH_FILES . '/config'); |
164
|
|
|
$config = $this->createConfigMock(true, CH_PATH_FILES . '/config/valid.json'); |
165
|
|
|
$runConfig = new Run(['mode' => 'php', 'exec' => '', 'path' => '']); |
166
|
|
|
$config->method('getRunConfig')->willReturn($runConfig); |
167
|
|
|
$config->method('getBootstrap')->willReturn('file-not-there.php'); |
168
|
|
|
|
169
|
|
|
Builder::build($config, $repository, $resolver); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|