Passed
Push — main ( 9185d0...afd76b )
by Sebastian
03:36
created

UninstallerTest::testRemoveOnlyDisabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 17
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 29
rs 9.7
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\Runner;
13
14
use CaptainHook\App\Config\Mockery as ConfigMockery;
15
use CaptainHook\App\Console\IO\Mockery as IOMockery;
16
use CaptainHook\App\Exception\InvalidHookName;
17
use CaptainHook\App\Git\DummyRepo;
18
use CaptainHook\App\Hook\Mockery as HookMockery;
19
use CaptainHook\App\Mockery as CHMockery;
20
use Exception;
21
use org\bovigo\vfs\vfsStream;
22
use PHPUnit\Framework\TestCase;
23
use RuntimeException;
24
use SebastianFeldmann\Git\Repository;
25
26
class UninstallerTest extends TestCase
27
{
28
    use ConfigMockery;
29
    use IOMockery;
30
    use CHMockery;
31
    use HookMockery;
32
33
    /**
34
     * Tests Uninstaller::setHook
35
     *
36
     * @throws \CaptainHook\App\Exception\InvalidHookName
37
     */
38
    public function testSetInvalidHook(): void
39
    {
40
        $this->expectException(InvalidHookName::class);
41
42
        $io       = $this->createIOMock();
43
        $config   = $this->createConfigMock(true);
44
        $repo     = $this->createRepositoryMock();
45
46
        $runner = new Uninstaller($io, $config, $repo);
47
        $runner->setHook('itDoesNotExist');
48
    }
49
50
    /**
51
     * Tests Uninstaller::setHook
52
     *
53
     * @throws \CaptainHook\App\Exception\InvalidHookName
54
     */
55
    public function testSetHookAndEnabledOnly(): void
56
    {
57
        $this->expectException(Exception::class);
58
59
        $io       = $this->createIOMock();
60
        $config   = $this->createConfigMock();
61
        $repo     = $this->createRepositoryMock();
62
63
        $runner = new Uninstaller($io, $config, $repo);
64
        $runner->setHook('pre-push');
65
        $runner->setOnlyDisabled(true);
66
    }
67
68
    /**
69
     * Tests Uninstaller::run
70
     */
71
    public function testHookUninstallationDeclined(): void
72
    {
73
        $fakeRepo = new DummyRepo([
74
            'config' => '# fake git config',
75
            'hooks'  => [
76
                'pre-commit' => '# fake pre-commit hook file',
77
                'pre-push'   => '# fake pre-push hook file',
78
            ]
79
        ]);
80
81
        $io     = $this->createIOMock();
82
        $config = $this->createConfigMock(true);
83
        $repo   = new Repository($fakeRepo->getRoot());
84
85
        $io->expects($this->atLeast(2))->method('ask')->willReturn('n');
86
87
        $runner = new Uninstaller($io, $config, $repo);
88
        $runner->setHook('');
89
        $runner->run();
90
91
        $this->assertFileExists($fakeRepo->getHookDir() . '/pre-commit');
92
        $this->assertFileExists($fakeRepo->getHookDir() . '/pre-push');
93
    }
94
95
    /**
96
     * Tests Uninstaller::run
97
     */
98
    public function testForcedHookUninstallation(): void
99
    {
100
        $fakeRepo = new DummyRepo([
101
            'config' => '# fake git config',
102
            'hooks'  => [
103
                'pre-commit' => '# fake pre-commit hook file',
104
                'pre-push'   => '# fake pre-push hook file',
105
            ]
106
        ]);
107
108
        $io     = $this->createIOMock();
109
        $config = $this->createConfigMock(true);
110
        $repo   = new Repository($fakeRepo->getRoot());
111
112
        $io->expects($this->exactly(0))->method('ask');
113
114
        $runner = new Uninstaller($io, $config, $repo);
115
        $runner->setForce(true);
116
        $runner->run();
117
118
        $this->assertFileDoesNotExist($fakeRepo->getHookDir() . '/pre-commit');
119
        $this->assertFileDoesNotExist($fakeRepo->getHookDir() . '/pre-push');
120
    }
121
122
    /**
123
     * Tests Uninstaller::run
124
     */
125
    public function testRemoveHook(): void
126
    {
127
        $fakeRepo = new DummyRepo(
128
            [
129
                'config' => '# fake git config',
130
                'hooks'  => [
131
                    'pre-commit' => '# fake pre-commit file',
132
                    'pre-push'   => '# fake pre-push file',
133
                ]
134
            ]
135
        );
136
137
        $io       = $this->createIOMock();
138
        $config   = $this->createConfigMock(true);
139
        $repo     = new Repository($fakeRepo->getRoot());
140
141
        $runner = new Uninstaller($io, $config, $repo);
142
        $runner->setHook('pre-commit');
143
        $runner->run();
144
145
        $this->assertFileDoesNotExist($fakeRepo->getHookDir() . '/pre-commit');
146
        $this->assertFileExists($fakeRepo->getHookDir() . '/pre-push');
147
    }
148
149
    /**
150
     * Tests Uninstaller::run
151
     */
152
    public function testRemoveOnlyDisabled(): void
153
    {
154
        $fakeRepo = new DummyRepo(
155
            [
156
                'config' => '# fake git config',
157
                'hooks'  => [
158
                    'pre-commit' => '# fake pre-commit file',
159
                    'pre-push'   => '# fake pre-push file',
160
                ]
161
            ]
162
        );
163
164
        $io       = $this->createIOMock();
165
        $config   = $this->createConfigMock(true);
166
        $repo     = new Repository($fakeRepo->getRoot());
167
168
        $config->method('isHookEnabled')->willReturnCallback(
169
            function (string $hook): bool {
170
                return $hook === 'pre-push';
171
            }
172
        );
173
174
        $runner = new Uninstaller($io, $config, $repo);
175
        $runner->setOnlyDisabled(true);
176
        $runner->setForce(true);
177
        $runner->run();
178
179
        $this->assertFileDoesNotExist($fakeRepo->getHookDir() . '/pre-commit');
180
        $this->assertFileExists($fakeRepo->getHookDir() . '/pre-push');
181
    }
182
183
    /**
184
     * Tests Uninstaller::run
185
     */
186
    public function testMoveExistingHook(): void
187
    {
188
        $fakeRepo = new DummyRepo(
189
            [
190
                'config' => '# fake git config',
191
                'hooks'  => [
192
                    'post-merge' => '# fake pre-commit file',
193
                    'pre-push'   => '# fake pre-push file',
194
                ]
195
            ]
196
        );
197
198
        $io     = $this->createIOMock();
199
        $config = $this->createConfigMock(true, $fakeRepo->getRoot() . '/captainhook.json');
200
        $repo   = new Repository($fakeRepo->getRoot());
201
202
        $runner = new Uninstaller($io, $config, $repo);
203
        $runner->setHook('post-merge')
204
               ->setMoveExistingTo('foo/bar/')
205
               ->run();
206
207
        $this->assertFileDoesNotExist($fakeRepo->getHookDir() . '/post-merge');
208
        $this->assertFileExists($fakeRepo->getRoot() . '/foo/bar/post-merge');
209
    }
210
211
    /**
212
     * Tests Uninstaller::run
213
     */
214
    public function testMoveExistingHookTargetIsFile(): void
215
    {
216
        $this->expectException(RuntimeException::class);
217
218
        $fakeRepo = new DummyRepo(
219
            // git repo
220
            [
221
                'config' => '# fake git config',
222
                'hooks'  => [
223
                    'pre-commit' => '# fake pre-commit file',
224
                    'pre-push'   => '# fake pre-push file',
225
                ]
226
            ],
227
            // files
228
            [
229
                'foo' => '# some random file'
230
            ]
231
        );
232
233
        $io     = $this->createIOMock();
234
        $config = $this->createConfigMock(true, $fakeRepo->getRoot() . '/captainhook.json');
235
        $repo   = new Repository($fakeRepo->getRoot());
236
237
        $runner = new Uninstaller($io, $config, $repo);
238
        $runner->setHook('pre-commit')
239
               ->setMoveExistingTo('foo')
240
               ->run();
241
    }
242
243
    /**
244
     * Tests Uninstaller::run
245
     */
246
    public function testMoveExistingHookWhenMoveExistingIsAnAbsolutePath(): void
247
    {
248
        $virtualFs = vfsStream::setup('root');
249
250
        $fakeRepo = new DummyRepo(
251
            // git repo
252
            [
253
                'config' => '# fake git config',
254
                'hooks'  => [
255
                    'pre-commit' => '# fake pre-commit file',
256
                    'pre-push'   => '# fake pre-push file',
257
                ]
258
            ],
259
            // files
260
            [
261
                'foo' => []
262
            ]
263
        );
264
265
        $io     = $this->createIOMock();
266
        $config = $this->createConfigMock(true, $fakeRepo->getRoot() . '/captainhook.json');
267
        $repo   = new Repository($fakeRepo->getRoot());
268
269
        $runner = new Uninstaller($io, $config, $repo);
270
        $runner->setHook('pre-commit')
271
               ->setMoveExistingTo($virtualFs->url() . '/foo/bar')
272
               ->run();
273
274
        $this->assertFileDoesNotExist($fakeRepo->getHookDir() . '/pre-commit');
275
        $this->assertFileExists($fakeRepo->getRoot() . '/foo/bar/pre-commit');
276
    }
277
}
278