1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of CaptainHook. |
4
|
|
|
* |
5
|
|
|
* (c) Sebastian Feldmann <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace CaptainHook\App; |
11
|
|
|
|
12
|
|
|
use CaptainHook\App\Config\Hook; |
13
|
|
|
use Exception; |
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
|
16
|
|
|
class ConfigTest extends TestCase |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Tests Config::__construct |
20
|
|
|
*/ |
21
|
|
|
public function testConstructor(): void |
22
|
|
|
{ |
23
|
|
|
$config = new Config('./no-config.json'); |
24
|
|
|
$this->assertFalse($config->isLoadedFromFile()); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Tests Config::isLoadedFromFile |
29
|
|
|
*/ |
30
|
|
|
public function testIsLoadedFromFile(): void |
31
|
|
|
{ |
32
|
|
|
$config = new Config('valid.json', true); |
33
|
|
|
$this->assertTrue($config->isLoadedFromFile()); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Tests Config::getHookConfig |
38
|
|
|
*/ |
39
|
|
|
public function testGetInvalidHook(): void |
40
|
|
|
{ |
41
|
|
|
$this->expectException(Exception::class); |
42
|
|
|
$config = new Config('./no-config.json'); |
43
|
|
|
$config->getHookConfig('foo'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Tests Config::getPath |
48
|
|
|
*/ |
49
|
|
|
public function testGetPath(): void |
50
|
|
|
{ |
51
|
|
|
$path = realpath(__DIR__ . '/../files/config/valid.json'); |
52
|
|
|
$config = new Config($path); |
53
|
|
|
|
54
|
|
|
$this->assertEquals($path, $config->getPath()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Tests Config::getVendorDirectory |
59
|
|
|
*/ |
60
|
|
|
public function testGetVendorDirectoryDefault(): void |
61
|
|
|
{ |
62
|
|
|
$path = realpath(__DIR__ . '/../files/config/valid.json'); |
63
|
|
|
$config = new Config($path); |
64
|
|
|
|
65
|
|
|
$this->assertEquals(getcwd() . DIRECTORY_SEPARATOR . 'vendor', $config->getVendorDirectory()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Tests Config::getVendorDirectory |
70
|
|
|
*/ |
71
|
|
|
public function testGetVendorDirectorySetting(): void |
72
|
|
|
{ |
73
|
|
|
$path = realpath(__DIR__ . '/../files/config/valid.json'); |
74
|
|
|
$config = new Config($path, true, ['vendor-directory' => 'libs/composer']); |
75
|
|
|
|
76
|
|
|
$this->assertEquals(dirname($path) . DIRECTORY_SEPARATOR . 'libs/composer', $config->getVendorDirectory()); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Tests Config::useAnsiColors |
81
|
|
|
*/ |
82
|
|
|
public function testAnsiColorsEnabledByDefault(): void |
83
|
|
|
{ |
84
|
|
|
$config = new Config('foo.json', true); |
85
|
|
|
$this->assertEquals(true, $config->useAnsiColors()); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Tests Config::useAnsiColors |
90
|
|
|
*/ |
91
|
|
|
public function testDisableAnsiColors(): void |
92
|
|
|
{ |
93
|
|
|
$config = new Config('foo.json', true, ['ansi-colors' => false]); |
94
|
|
|
$this->assertEquals(false, $config->useAnsiColors()); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Tests Config::getRunMode |
99
|
|
|
*/ |
100
|
|
|
public function testGetRunMode(): void |
101
|
|
|
{ |
102
|
|
|
$config = new Config('foo.json', true, ['run-mode' => 'docker', 'run-exec' => 'foo']); |
103
|
|
|
$this->assertEquals('docker', $config->getRunMode()); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Tests Config::getRunExec |
108
|
|
|
*/ |
109
|
|
|
public function testGetRunExec(): void |
110
|
|
|
{ |
111
|
|
|
$config = new Config('foo.json', true, ['run-mode' => 'docker', 'run-exec' => 'foo']); |
112
|
|
|
$this->assertEquals('foo', $config->getRunExec()); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Tests Config::getRunPath |
117
|
|
|
*/ |
118
|
|
|
public function testGetRunPathEmptyByDefault(): void |
119
|
|
|
{ |
120
|
|
|
$config = new Config('foo.json', true, ['run-mode' => 'docker', 'run-exec' => 'foo']); |
121
|
|
|
$this->assertEquals('', $config->getRunPath()); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Tests Config::getRunPath |
126
|
|
|
*/ |
127
|
|
|
public function testGetRunPath(): void |
128
|
|
|
{ |
129
|
|
|
$config = new Config('foo.json', true, ['run-mode' => 'docker', 'run-exec' => 'foo', 'run-path' => '/foo']); |
130
|
|
|
$this->assertEquals('/foo', $config->getRunPath()); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Tests Config::getJsonData |
135
|
|
|
*/ |
136
|
|
|
public function testGetJsonData(): void |
137
|
|
|
{ |
138
|
|
|
$config = new Config('./no-config.json'); |
139
|
|
|
$json = $config->getJsonData(); |
140
|
|
|
|
141
|
|
|
$this->assertIsArray($json); |
142
|
|
|
$this->assertIsArray($json['pre-commit']); |
143
|
|
|
$this->assertIsArray($json['commit-msg']); |
144
|
|
|
$this->assertIsArray($json['pre-push']); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Tests Config::getJsonData |
149
|
|
|
*/ |
150
|
|
|
public function testGetJsonDataWithoutEmptyConfig(): void |
151
|
|
|
{ |
152
|
|
|
$config = new Config('foo.json', true, []); |
153
|
|
|
$json = $config->getJsonData(); |
154
|
|
|
|
155
|
|
|
$this->assertArrayNotHasKey('config', $json); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Tests Config::getJsonData |
160
|
|
|
*/ |
161
|
|
|
public function testGetJsonDataWithConfigSection(): void |
162
|
|
|
{ |
163
|
|
|
$config = new Config('foo.json', true, ['run-mode' => 'docker', 'run-exec' => 'foo']); |
164
|
|
|
$json = $config->getJsonData(); |
165
|
|
|
|
166
|
|
|
$this->assertIsArray($json); |
167
|
|
|
$this->assertIsArray($json['config']); |
168
|
|
|
$this->assertEquals('foo', $json['config']['run-exec']); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|