Passed
Push — master ( f5c231...9f4d35 )
by Sebastian
02:26
created

InstallerTest::testSetInvalidHook()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 11
rs 10
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\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 PHPUnit\Framework\TestCase;
21
22
class InstallerTest extends TestCase
23
{
24
    use ConfigMockery;
25
    use IOMockery;
26
    use CHMockery;
27
    use HookMockery;
28
29
    /**
30
     * Tests Installer::setHook
31
     *
32
     * @throws \CaptainHook\App\Exception\InvalidHookName
33
     */
34
    public function testSetInvalidHook(): void
35
    {
36
        $this->expectException(InvalidHookName::class);
37
38
        $io       = $this->createIOMock();
39
        $config   = $this->createConfigMock();
40
        $repo     = $this->createRepositoryMock();
41
        $template = $this->createTemplateMock();
42
43
        $runner = new Installer($io, $config, $repo, $template);
44
        $runner->setHook('itDoNotExist');
45
    }
46
47
    /**
48
     * Tests Installer::run
49
     */
50
    public function testHookInstallationDeclined(): void
51
    {
52
        $fakeRepo = new DummyRepo();
53
54
        $io       = $this->createIOMock();
55
        $config   = $this->createConfigMock();
56
        $repo     = $this->createRepositoryMock($fakeRepo->getRoot());
57
        $template = $this->createTemplateMock();
58
59
        $io->expects($this->atLeast(5))->method('ask')->willReturn('n');
60
61
        $runner = new Installer($io, $config, $repo, $template);
62
        $runner->run();
63
64
        $this->assertFileNotExists($fakeRepo->getHookDir() . '/pre-commit');
65
        $this->assertFileNotExists($fakeRepo->getHookDir() . '/pre-push');
66
    }
67
68
    /**
69
     * Tests Installer::run
70
     */
71
    public function testWriteHook(): void
72
    {
73
        $fakeRepo = new DummyRepo();
74
75
        $io       = $this->createIOMock();
76
        $config   = $this->createConfigMock();
77
        $repo     = $this->createRepositoryMock($fakeRepo->getRoot());
78
        $template = $this->createTemplateMock();
79
80
        $template->expects($this->once())
81
                 ->method('getCode')
82
                 ->with('pre-commit')
83
                 ->willReturn('');
84
85
        $runner = new Installer($io, $config, $repo, $template);
86
        $runner->setHook('pre-commit');
87
        $runner->run('pre-commit');
0 ignored issues
show
Unused Code introduced by
The call to CaptainHook\App\Runner\Installer::run() has too many arguments starting with 'pre-commit'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

87
        $runner->/** @scrutinizer ignore-call */ 
88
                 run('pre-commit');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
88
89
        $this->assertFileExists($fakeRepo->getHookDir() . '/pre-commit');
90
    }
91
92
    /**
93
     * Tests Installer::writeHookFile
94
     */
95
    public function testSkipExisting(): void
96
    {
97
        $io       = $this->createIOMock();
98
        $config   = $this->createConfigMock();
99
        $repo     = $this->createRepositoryMock();
100
        $template = $this->createTemplateMock();
101
102
        $io->expects($this->atLeast(1))->method('write');
103
        $repo->expects($this->once())->method('hookExists')->willReturn(true);
104
105
        $runner = new Installer($io, $config, $repo, $template);
106
        $runner->setSkipExisting(true);
107
        $runner->setHook('pre-commit');
108
        $runner->run();
109
    }
110
111
    /**
112
     * Tests Installer::writeHookFile
113
     */
114
    public function testDeclineOverwrite(): void
115
    {
116
        $io       = $this->createIOMock();
117
        $config   = $this->createConfigMock();
118
        $repo     = $this->createRepositoryMock();
119
        $template = $this->createTemplateMock();
120
121
        $io->expects($this->once())->method('ask')->willReturn('n');
122
        $repo->expects($this->once())->method('hookExists')->willReturn(true);
123
124
        $runner = new Installer($io, $config, $repo, $template);
125
        $runner->setHook('pre-commit');
126
        $runner->run();
127
    }
128
}
129