Passed
Push — master ( 85012e...9f5f8f )
by Sebastian
02:03
created

testOverwriteConfigSettingsBySettingsConfigFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
    /**
20
     * Tests Factory::create
21
     *
22
     * @throws \Exception
23
     */
24
    public function testCreate(): void
25
    {
26
        $config = Factory::create(realpath(__DIR__ . '/../../files/config/valid.json'));
27
28
        $this->assertTrue($config->getHookConfig('pre-commit')->isEnabled());
29
        $this->assertCount(1, $config->getHookConfig('pre-commit')->getActions());
30
    }
31
32
    /**
33
     * Tests Factory::create
34
     *
35
     * @throws \Exception
36
     */
37
    public function testOverwriteConfigSettingsBySettingsConfigFile(): void
38
    {
39
        $config = Factory::create(realpath(__DIR__ . '/../../files/config/config-file/captainhook.json'));
40
41
        $this->assertEquals('quiet', $config->getVerbosity());
42
    }
43
44
    /**
45
     * Tests Factory::create
46
     *
47
     * @throws \Exception
48
     */
49
    public function testCreateWithAbsoluteGitDir(): void
50
    {
51
        $config = Factory::create(
52
            realpath(__DIR__ . '/../../files/config/valid.json'),
53
            ['git-directory' => '/foo']
54
        );
55
56
        $this->assertTrue($config->getHookConfig('pre-commit')->isEnabled());
57
        $this->assertCount(1, $config->getHookConfig('pre-commit')->getActions());
58
        $this->assertEquals('/foo', $config->getGitDirectory());
59
    }
60
61
    /**
62
     * Tests Factory::create
63
     *
64
     * @throws \Exception
65
     */
66
    public function testCreateWithRelativeGitDir(): void
67
    {
68
        $path   = realpath(__DIR__ . '/../../files/config/valid.json');
69
        $config = Factory::create($path, ['git-directory' => '../.git']);
70
71
        $this->assertTrue($config->getHookConfig('pre-commit')->isEnabled());
72
        $this->assertCount(1, $config->getHookConfig('pre-commit')->getActions());
73
        $this->assertEquals(dirname($path) . DIRECTORY_SEPARATOR . '../.git', $config->getGitDirectory());
74
    }
75
76
    /**
77
     * Tests Factory::create
78
     *
79
     * @throws \Exception
80
     */
81
    public function testCreateWithConditions(): void
82
    {
83
        $config = Factory::create(realpath(__DIR__ . '/../../files/config/valid-with-conditions.json'));
84
85
        $this->assertTrue($config->getHookConfig('pre-commit')->isEnabled());
86
        $this->assertCount(1, $config->getHookConfig('pre-commit')->getActions());
87
    }
88
89
    /**
90
     * Tests Factory::create
91
     *
92
     * @throws \Exception
93
     */
94
    public function testCreateWithAllSetting(): void
95
    {
96
        $path   = realpath(__DIR__ . '/../../files/config/valid-with-all-settings.json');
97
        $gitDir = \dirname($path) . DIRECTORY_SEPARATOR . '../../../.git';
98
        $config = Factory::create($path);
99
100
        $this->assertTrue($config->getHookConfig('pre-commit')->isEnabled());
101
        $this->assertCount(1, $config->getHookConfig('pre-commit')->getActions());
102
        $this->assertEquals('verbose', $config->getVerbosity());
103
        $this->assertEquals($gitDir, $config->getGitDirectory());
104
        $this->assertEquals(false, $config->useAnsiColors());
105
        $this->assertEquals('docker', $config->getRunMode());
106
        $this->assertEquals('docker exec CONTAINER_NAME', $config->getRunExec());
107
    }
108
109
    /**
110
     * Tests Factory::create
111
     *
112
     * @throws \Exception
113
     */
114
    public function testCreateWithIncludes(): void
115
    {
116
        $config = Factory::create(realpath(__DIR__ . '/../../files/config/valid-with-includes.json'));
117
118
        $this->assertTrue($config->getHookConfig('pre-commit')->isEnabled());
119
        $this->assertCount(2, $config->getHookConfig('pre-commit')->getActions());
120
    }
121
122
    /**
123
     * Tests Factory::create
124
     *
125
     * @throws \Exception
126
     */
127
    public function testCreateWithValidNestedIncludes(): void
128
    {
129
        $config = Factory::create(realpath(__DIR__ . '/../../files/config/valid-with-nested-includes.json'));
130
131
        $this->assertTrue($config->getHookConfig('pre-commit')->isEnabled());
132
        $this->assertCount(3, $config->getHookConfig('pre-commit')->getActions());
133
    }
134
135
    /**
136
     * Tests Factory::create
137
     *
138
     * @throws \Exception
139
     */
140
    public function testCreateWithInvalidNestedIncludes(): void
141
    {
142
        $config = Factory::create(realpath(__DIR__ . '/../../files/config/invalid-with-nested-includes.json'));
143
144
        $this->assertTrue($config->getHookConfig('pre-commit')->isEnabled());
145
        $this->assertCount(2, $config->getHookConfig('pre-commit')->getActions());
146
    }
147
148
    /**
149
     * Tests Factory::create
150
     *
151
     * @throws \Exception
152
     */
153
    public function testCreateWithInvalidIncludes(): void
154
    {
155
        $this->expectException(Exception::class);
156
        Factory::create(realpath(__DIR__ . '/../../files/config/valid-with-invalid-includes.json'));
157
    }
158
159
    /**
160
     * Tests Factory::create
161
     *
162
     * @throws \Exception
163
     */
164
    public function testCreateEmptyWithIncludes(): void
165
    {
166
        $config = Factory::create(realpath(__DIR__ . '/../../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
    /**
173
     * Tests Factory::create
174
     *
175
     * @throws \Exception
176
     */
177
    public function testWithMainConfigurationOverridingInclude(): void
178
    {
179
        $config = Factory::create(realpath(__DIR__ . '/../../files/config/valid-with-disabled-action.json'));
180
181
        $this->assertFalse($config->getHookConfig('pre-commit')->isEnabled());
182
    }
183
}
184