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 Exception; |
15
|
|
|
use PHPUnit\Framework\TestCase; |
16
|
|
|
|
17
|
|
|
class ConfigTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Tests Config::__construct |
21
|
|
|
*/ |
22
|
|
|
public function testConstructor(): void |
23
|
|
|
{ |
24
|
|
|
$config = new Config('./no-config.json'); |
25
|
|
|
$this->assertFalse($config->isLoadedFromFile()); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Tests Config::isLoadedFromFile |
30
|
|
|
*/ |
31
|
|
|
public function testIsLoadedFromFile(): void |
32
|
|
|
{ |
33
|
|
|
$config = new Config('valid.json', true); |
34
|
|
|
$this->assertTrue($config->isLoadedFromFile()); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Tests Config::getHookConfig |
39
|
|
|
*/ |
40
|
|
|
public function testGetInvalidHook(): void |
41
|
|
|
{ |
42
|
|
|
$this->expectException(Exception::class); |
43
|
|
|
$config = new Config('./no-config.json'); |
44
|
|
|
$config->getHookConfig('foo'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Tests Config::getGitDirectory |
49
|
|
|
*/ |
50
|
|
|
public function testAssumeCwdAsGitDir(): void |
51
|
|
|
{ |
52
|
|
|
$config = new Config('./no-config.json'); |
53
|
|
|
$this->assertEquals(getcwd() . '/.git', $config->getGitDirectory()); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Tests Config::getPath |
58
|
|
|
*/ |
59
|
|
|
public function testGetPath(): void |
60
|
|
|
{ |
61
|
|
|
$path = realpath(__DIR__ . '/../files/config/valid.json'); |
62
|
|
|
$config = new Config($path); |
63
|
|
|
|
64
|
|
|
$this->assertEquals($path, $config->getPath()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Tests Config::getBootstrap |
69
|
|
|
*/ |
70
|
|
|
public function testGetBootstrapDefault(): void |
71
|
|
|
{ |
72
|
|
|
$path = realpath(__DIR__ . '/../files/config/valid.json'); |
73
|
|
|
$config = new Config($path); |
74
|
|
|
|
75
|
|
|
$this->assertEquals('vendor/autoload.php', $config->getBootstrap()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Tests Config::getBootstrap |
80
|
|
|
*/ |
81
|
|
|
public function testGetBootstrapSetting(): void |
82
|
|
|
{ |
83
|
|
|
$path = realpath(__DIR__ . '/../files/config/valid.json'); |
84
|
|
|
$config = new Config($path, true, ['bootstrap' => 'libs/autoload.php']); |
85
|
|
|
|
86
|
|
|
$this->assertEquals('libs/autoload.php', $config->getBootstrap()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Tests Config::useAnsiColors |
91
|
|
|
*/ |
92
|
|
|
public function testAnsiColorsEnabledByDefault(): void |
93
|
|
|
{ |
94
|
|
|
$config = new Config('foo.json', true); |
95
|
|
|
$this->assertEquals(true, $config->useAnsiColors()); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Tests Config::useAnsiColors |
100
|
|
|
*/ |
101
|
|
|
public function testDisableAnsiColors(): void |
102
|
|
|
{ |
103
|
|
|
$config = new Config('foo.json', true, ['ansi-colors' => false]); |
104
|
|
|
$this->assertEquals(false, $config->useAnsiColors()); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Tests Config::getRunMode |
109
|
|
|
*/ |
110
|
|
|
public function testGetRunMode(): void |
111
|
|
|
{ |
112
|
|
|
$config = new Config('foo.json', true, ['run-mode' => 'docker', 'run-exec' => 'foo']); |
113
|
|
|
$this->assertEquals('docker', $config->getRunMode()); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Tests Config::getRunExec |
118
|
|
|
*/ |
119
|
|
|
public function testGetRunExec(): void |
120
|
|
|
{ |
121
|
|
|
$config = new Config('foo.json', true, ['run-mode' => 'docker', 'run-exec' => 'foo']); |
122
|
|
|
$this->assertEquals('foo', $config->getRunExec()); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Tests Config::getRunPath |
127
|
|
|
*/ |
128
|
|
|
public function testGetRunPathEmptyByDefault(): void |
129
|
|
|
{ |
130
|
|
|
$config = new Config('foo.json', true, ['run-mode' => 'docker', 'run-exec' => 'foo']); |
131
|
|
|
$this->assertEquals('', $config->getRunPath()); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Tests Config::getRunPath |
136
|
|
|
*/ |
137
|
|
|
public function testGetRunPath(): void |
138
|
|
|
{ |
139
|
|
|
$config = new Config('foo.json', true, ['run-mode' => 'docker', 'run-exec' => 'foo', 'run-path' => '/foo']); |
140
|
|
|
$this->assertEquals('/foo', $config->getRunPath()); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Tests Config::failOnFirstError default |
145
|
|
|
*/ |
146
|
|
|
public function testFailOnFirstErrorDefault(): void |
147
|
|
|
{ |
148
|
|
|
$config = new Config('foo.json', true, []); |
149
|
|
|
$this->assertEquals(true, $config->failOnFirstError()); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Tests Config::failOnFirstError |
154
|
|
|
*/ |
155
|
|
|
public function testFailOnFirstError(): void |
156
|
|
|
{ |
157
|
|
|
$config = new Config('foo.json', true, ['fail-on-first-error' => false]); |
158
|
|
|
$this->assertEquals(false, $config->failOnFirstError()); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Tests Config::getJsonData |
163
|
|
|
*/ |
164
|
|
|
public function testGetJsonData(): void |
165
|
|
|
{ |
166
|
|
|
$config = new Config('./no-config.json'); |
167
|
|
|
$json = $config->getJsonData(); |
168
|
|
|
|
169
|
|
|
$this->assertIsArray($json); |
170
|
|
|
$this->assertIsArray($json['pre-commit']); |
171
|
|
|
$this->assertIsArray($json['commit-msg']); |
172
|
|
|
$this->assertIsArray($json['pre-push']); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Tests Config::getJsonData |
177
|
|
|
*/ |
178
|
|
|
public function testGetJsonDataWithoutEmptyConfig(): void |
179
|
|
|
{ |
180
|
|
|
$config = new Config('foo.json', true, []); |
181
|
|
|
$json = $config->getJsonData(); |
182
|
|
|
|
183
|
|
|
$this->assertArrayNotHasKey('config', $json); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Tests Config::getJsonData |
188
|
|
|
*/ |
189
|
|
|
public function testGetJsonDataWithConfigSection(): void |
190
|
|
|
{ |
191
|
|
|
$config = new Config('foo.json', true, ['run-mode' => 'docker', 'run-exec' => 'foo']); |
192
|
|
|
$json = $config->getJsonData(); |
193
|
|
|
|
194
|
|
|
$this->assertIsArray($json); |
195
|
|
|
$this->assertIsArray($json['config']); |
196
|
|
|
$this->assertEquals('foo', $json['config']['run-exec']); |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|