-
-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathFactoryTest.php
More file actions
186 lines (147 loc) · 7.32 KB
/
Copy pathFactoryTest.php
File metadata and controls
186 lines (147 loc) · 7.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
/**
* This file is part of CaptainHook
*
* (c) Sebastian Feldmann <sf@sebastian-feldmann.info>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CaptainHook\App\Config;
use Exception;
use PHPUnit\Framework\TestCase;
class FactoryTest extends TestCase
{
public function testCreate(): void
{
$config = Factory::create(realpath(CH_PATH_FILES . '/config/valid.json'));
$this->assertTrue($config->getHookConfig('pre-commit')->isEnabled());
$this->assertCount(1, $config->getHookConfig('pre-commit')->getActions());
}
public function testOverwriteConfigSettingsBySettingsConfigFile(): void
{
$config = Factory::create(realpath(CH_PATH_FILES . '/config/config-file/captainhook.json'));
$this->assertEquals('quiet', $config->getVerbosity());
}
public function testCreateWithAbsoluteGitDir(): void
{
$config = Factory::create(
realpath(CH_PATH_FILES . '/config/valid.json'),
['git-directory' => '/foo']
);
$this->assertTrue($config->getHookConfig('pre-commit')->isEnabled());
$this->assertCount(1, $config->getHookConfig('pre-commit')->getActions());
$this->assertEquals('/foo', $config->getGitDirectory());
}
public function testCreateWithInvalidPhpPath(): void
{
$this->expectException(Exception::class);
Factory::create(
realpath(CH_PATH_FILES . '/config/valid.json'),
['php-path' => '/foo/bar/baz']
);
}
public function testCreateWithRelativeGitDir(): void
{
$path = realpath(CH_PATH_FILES . '/config/valid.json');
$config = Factory::create($path, ['git-directory' => '../.git']);
$this->assertTrue($config->getHookConfig('pre-commit')->isEnabled());
$this->assertCount(1, $config->getHookConfig('pre-commit')->getActions());
$this->assertEquals(dirname($path) . DIRECTORY_SEPARATOR . '../.git', $config->getGitDirectory());
}
public function testCreateWithRunConfig(): void
{
$path = realpath(CH_PATH_FILES . '/config/valid-run-config-nested.json');
$config = Factory::create($path, []);
$this->assertEquals('./vendor/bin/captainhook', $config->getRunConfig()->getCaptainsPath());
$this->assertEquals('docker', $config->getRunConfig()->getMode());
$this->assertEquals('/docker/.git', $config->getRunConfig()->getGitPath());
}
public function testCreateWithRunConfigLegacy(): void
{
$path = realpath(CH_PATH_FILES . '/config/valid-run-config-legacy.json');
$config = Factory::create($path, []);
$this->assertEquals('./vendor/bin/captainhook', $config->getRunConfig()->getCaptainsPath());
$this->assertEquals('docker', $config->getRunConfig()->getMode());
$this->assertEquals('/docker/.git', $config->getRunConfig()->getGitPath());
}
public function testCreateWithConditions(): void
{
$config = Factory::create(realpath(CH_PATH_FILES . '/config/valid-with-conditions.json'));
$this->assertTrue($config->getHookConfig('pre-commit')->isEnabled());
$this->assertCount(1, $config->getHookConfig('pre-commit')->getActions());
}
public function testCreateWithSettings(): void
{
$config = Factory::create(realpath(CH_PATH_FILES . '/config/valid-with-conditions.json'));
$this->assertTrue($config->getHookConfig('pre-commit')->getActions()[0]->isFailureAllowed());
}
public function testCreateWithCrazyPHPPath(): void
{
$config = Factory::create(realpath(CH_PATH_FILES . '/config/valid-with-strange-settings.json'));
$this->assertEquals("tests/_files/bin/success foo", $config->getPhpPath());
}
public function testCreateWithAllSetting(): void
{
$path = realpath(CH_PATH_FILES . '/config/valid-with-all-settings.json');
$gitDir = dirname($path) . DIRECTORY_SEPARATOR . '../../../.git';
$config = Factory::create($path);
$this->assertTrue($config->getHookConfig('pre-commit')->isEnabled());
$this->assertCount(1, $config->getHookConfig('pre-commit')->getActions());
$this->assertEquals('verbose', $config->getVerbosity());
$this->assertEquals($gitDir, $config->getGitDirectory());
$this->assertEquals(false, $config->useAnsiColors());
$this->assertEquals('docker', $config->getRunConfig()->getMode());
$this->assertEquals('docker exec CONTAINER_NAME', $config->getRunConfig()->getDockerCommand());
$this->assertEquals(false, $config->failOnFirstError());
}
public function testCreateWithIncludes(): void
{
$config = Factory::create(realpath(CH_PATH_FILES . '/config/valid-with-includes.json'));
$this->assertTrue($config->getHookConfig('pre-commit')->isEnabled());
$this->assertCount(2, $config->getHookConfig('pre-commit')->getActions());
}
public function testCreateWithValidNestedIncludes(): void
{
$config = Factory::create(realpath(CH_PATH_FILES . '/config/valid-with-nested-includes.json'));
$this->assertTrue($config->getHookConfig('pre-commit')->isEnabled());
$this->assertCount(3, $config->getHookConfig('pre-commit')->getActions());
$this->assertFalse($config->getHookConfig('pre-push')->isEnabled());
$this->assertCount(2, $config->getHookConfig('pre-push')->getActions());
}
public function testCreateWithInvalidNestedIncludes(): void
{
$config = Factory::create(realpath(CH_PATH_FILES . '/config/invalid-with-nested-includes.json'));
$this->assertTrue($config->getHookConfig('pre-commit')->isEnabled());
$this->assertCount(2, $config->getHookConfig('pre-commit')->getActions());
}
public function testCreateWithInvalidIncludes(): void
{
$this->expectException(Exception::class);
Factory::create(realpath(CH_PATH_FILES . '/config/valid-with-invalid-includes.json'));
}
public function testCreateEmptyWithIncludes(): void
{
$config = Factory::create(realpath(CH_PATH_FILES . '/config/empty-with-includes.json'));
$this->assertTrue($config->getHookConfig('pre-commit')->isEnabled());
$this->assertCount(1, $config->getHookConfig('pre-commit')->getActions());
}
public function testCreateWithNestedAndConditions(): void
{
$config = Factory::create(realpath(CH_PATH_FILES . '/config/valid-with-nested-and-conditions.json'));
$this->assertTrue($config->getHookConfig('pre-commit')->isEnabled());
$this->assertCount(1, $config->getHookConfig('pre-commit')->getActions());
}
public function testWithMainConfigurationOverridingInclude(): void
{
$config = Factory::create(realpath(CH_PATH_FILES . '/config/valid-with-disabled-action.json'));
$this->assertFalse($config->getHookConfig('pre-commit')->isEnabled());
}
public function testMaxIncludeLevel(): void
{
// one of the included files will not be loaded because of the includes-level value of 2
$config = Factory::create(realpath(CH_PATH_FILES . '/config/valid-with-exceeded-max-include-level.json'));
// all files have combined 6 pre-commit actions but one should not be loaded
$this->assertCount(5, $config->getHookConfig('pre-commit')->getActions());
}
}