Passed
Push — main ( 25f4a8...799be8 )
by Sebastian
03:52
created

FactoryTest::testCreateWithValidNestedIncludes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
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\Config;
13
14
use Exception;
15
use PHPUnit\Framework\TestCase;
16
17
class FactoryTest extends TestCase
18
{
19
    public function testCreate(): void
20
    {
21
        $config = Factory::create(realpath(CH_PATH_FILES . '/config/valid.json'));
22
23
        $this->assertTrue($config->getHookConfig('pre-commit')->isEnabled());
24
        $this->assertCount(1, $config->getHookConfig('pre-commit')->getActions());
25
    }
26
27
    public function testCustomValuesCanBeDefined(): void
28
    {
29
        $config = Factory::create(CH_PATH_FILES . '/config/valid-with-all-settings.json');
30
        $custom = $config->getCustomSettings();
31
32
        $this->assertEquals('bar', $custom['foo']);
33
    }
34
35
    public function testOverwriteConfigSettingsBySettingsConfigFile(): void
36
    {
37
        $config = Factory::create(realpath(CH_PATH_FILES . '/config/config-file/captainhook.json'));
38
39
        $this->assertEquals('quiet', $config->getVerbosity());
40
    }
41
42
    public function testCreateWithAbsoluteGitDir(): void
43
    {
44
        $config = Factory::create(
45
            realpath(CH_PATH_FILES . '/config/valid.json'),
46
            ['git-directory' => '/foo']
47
        );
48
49
        $this->assertTrue($config->getHookConfig('pre-commit')->isEnabled());
50
        $this->assertCount(1, $config->getHookConfig('pre-commit')->getActions());
51
        $this->assertEquals('/foo', $config->getGitDirectory());
52
    }
53
54
    public function testCreateWithInvalidPhpPath(): void
55
    {
56
        $this->expectException(Exception::class);
57
58
        Factory::create(
59
            realpath(CH_PATH_FILES . '/config/valid.json'),
60
            ['php-path' => '/foo/bar/baz']
61
        );
62
    }
63
64
    public function testCreateWithRelativeGitDir(): void
65
    {
66
        $path   = realpath(CH_PATH_FILES . '/config/valid.json');
67
        $config = Factory::create($path, ['git-directory' => '../.git']);
68
69
        $this->assertTrue($config->getHookConfig('pre-commit')->isEnabled());
70
        $this->assertCount(1, $config->getHookConfig('pre-commit')->getActions());
71
        $this->assertEquals(dirname($path) . DIRECTORY_SEPARATOR . '../.git', $config->getGitDirectory());
72
    }
73
74
    public function testCreateWithRunConfig(): void
75
    {
76
        $path   = realpath(CH_PATH_FILES . '/config/valid-run-config-nested.json');
77
        $config = Factory::create($path, []);
78
79
        $this->assertEquals('./vendor/bin/captainhook', $config->getRunConfig()->getCaptainsPath());
80
        $this->assertEquals('docker', $config->getRunConfig()->getMode());
81
        $this->assertEquals('/docker/.git', $config->getRunConfig()->getGitPath());
82
    }
83
84
    public function testCreateWithRunConfigLegacy(): void
85
    {
86
        $path   = realpath(CH_PATH_FILES . '/config/valid-run-config-legacy.json');
87
        $config = Factory::create($path, []);
88
89
        $this->assertEquals('./vendor/bin/captainhook', $config->getRunConfig()->getCaptainsPath());
90
        $this->assertEquals('docker', $config->getRunConfig()->getMode());
91
        $this->assertEquals('/docker/.git', $config->getRunConfig()->getGitPath());
92
    }
93
94
    public function testCreateWithConditions(): void
95
    {
96
        $config = Factory::create(realpath(CH_PATH_FILES . '/config/valid-with-conditions.json'));
97
98
        $this->assertTrue($config->getHookConfig('pre-commit')->isEnabled());
99
        $this->assertCount(1, $config->getHookConfig('pre-commit')->getActions());
100
    }
101
102
    public function testCreateWithSettings(): void
103
    {
104
        $config = Factory::create(realpath(CH_PATH_FILES . '/config/valid-with-conditions.json'));
105
106
        $this->assertTrue($config->getHookConfig('pre-commit')->getActions()[0]->isFailureAllowed());
107
    }
108
109
    public function testCreateWithCrazyPHPPath(): void
110
    {
111
        $config = Factory::create(realpath(CH_PATH_FILES . '/config/valid-with-strange-settings.json'));
112
113
        $this->assertEquals("tests/_files/bin/success foo", $config->getPhpPath());
114
    }
115
116
    public function testCreateWithAllSetting(): void
117
    {
118
        $path   = realpath(CH_PATH_FILES . '/config/valid-with-all-settings.json');
119
        $gitDir = dirname($path) . DIRECTORY_SEPARATOR . '../../../.git';
120
        $config = Factory::create($path);
121
122
        $this->assertTrue($config->getHookConfig('pre-commit')->isEnabled());
123
        $this->assertCount(1, $config->getHookConfig('pre-commit')->getActions());
124
        $this->assertEquals('verbose', $config->getVerbosity());
125
        $this->assertEquals($gitDir, $config->getGitDirectory());
126
        $this->assertEquals(false, $config->useAnsiColors());
127
        $this->assertEquals('docker', $config->getRunConfig()->getMode());
128
        $this->assertEquals('docker exec CONTAINER_NAME', $config->getRunConfig()->getDockerCommand());
129
        $this->assertEquals(false, $config->failOnFirstError());
130
    }
131
132
    public function testCreateWithIncludes(): void
133
    {
134
        $config = Factory::create(realpath(CH_PATH_FILES . '/config/valid-with-includes.json'));
135
136
        $this->assertTrue($config->getHookConfig('pre-commit')->isEnabled());
137
        $this->assertCount(2, $config->getHookConfig('pre-commit')->getActions());
138
    }
139
140
    public function testCreateWithValidNestedIncludes(): void
141
    {
142
        $config = Factory::create(realpath(CH_PATH_FILES . '/config/valid-with-nested-includes.json'));
143
144
        $this->assertTrue($config->getHookConfig('pre-commit')->isEnabled());
145
        $this->assertCount(3, $config->getHookConfig('pre-commit')->getActions());
146
        $this->assertFalse($config->getHookConfig('pre-push')->isEnabled());
147
        $this->assertCount(2, $config->getHookConfig('pre-push')->getActions());
148
    }
149
150
    public function testCreateWithInvalidNestedIncludes(): void
151
    {
152
        $config = Factory::create(realpath(CH_PATH_FILES . '/config/invalid-with-nested-includes.json'));
153
154
        $this->assertTrue($config->getHookConfig('pre-commit')->isEnabled());
155
        $this->assertCount(2, $config->getHookConfig('pre-commit')->getActions());
156
    }
157
158
    public function testCreateWithInvalidIncludes(): void
159
    {
160
        $this->expectException(Exception::class);
161
        Factory::create(realpath(CH_PATH_FILES . '/config/valid-with-invalid-includes.json'));
162
    }
163
164
    public function testCreateEmptyWithIncludes(): void
165
    {
166
        $config = Factory::create(realpath(CH_PATH_FILES . '/config/empty-with-includes.json'));
167
168
        $this->assertTrue($config->getHookConfig('pre-commit')->isEnabled());
169
        $this->assertCount(1, $config->getHookConfig('pre-commit')->getActions());
170
    }
171
172
    public function testCreateWithNestedAndConditions(): void
173
    {
174
        $config = Factory::create(realpath(CH_PATH_FILES . '/config/valid-with-nested-and-conditions.json'));
175
176
        $this->assertTrue($config->getHookConfig('pre-commit')->isEnabled());
177
        $this->assertCount(1, $config->getHookConfig('pre-commit')->getActions());
178
    }
179
180
    public function testWithMainConfigurationOverridingInclude(): void
181
    {
182
        $config = Factory::create(realpath(CH_PATH_FILES . '/config/valid-with-disabled-action.json'));
183
184
        $this->assertFalse($config->getHookConfig('pre-commit')->isEnabled());
185
    }
186
187
    public function testMaxIncludeLevel(): void
188
    {
189
        // one of the included files will not be loaded because of the includes-level value of 2
190
        $config = Factory::create(realpath(CH_PATH_FILES . '/config/valid-with-exceeded-max-include-level.json'));
191
        // all files have combined 6 pre-commit actions but one should not be loaded
192
        $this->assertCount(5, $config->getHookConfig('pre-commit')->getActions());
193
    }
194
}
195