CommitMsgTest::testRunHookSkippedBecauseOfFixup()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 22
c 2
b 0
f 0
nc 2
nop 0
dl 0
loc 29
rs 9.568
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\Hook;
13
14
use CaptainHook\App\Config\Mockery as ConfigMockery;
15
use CaptainHook\App\Console\IO\Mockery as IOMockery;
16
use CaptainHook\App\Git\DummyRepo;
17
use CaptainHook\App\Mockery as CHMockery;
18
use Exception;
19
use PHPUnit\Framework\TestCase;
20
use SebastianFeldmann\Git\CommitMessage as GitCommitMessage;
21
use SebastianFeldmann\Git\Operator\Config as ConfigOperator;
22
23
/**
24
 * Class CommitMsgTest
25
 *
26
 * @package CaptainHook
27
 * @author  Sebastian Feldmann <[email protected]>
28
 * @link    https://github.com/captainhook-git/captainhook
29
 * @since   Class available since Release 3.1.0
30
 */
31
class CommitMsgTest extends TestCase
32
{
33
    use ConfigMockery;
34
    use IOMockery;
35
    use CHMockery;
36
37
    public function testRunHookEnabled(): void
38
    {
39
        if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
40
            $this->markTestSkipped('not tested on windows');
41
        }
42
43
        $io       = $this->createIOMock();
44
        $config   = $this->createConfigMock();
45
        $configOp = $this->createMock(ConfigOperator::class);
46
        $configOp->expects($this->once())->method('getSettingSafely')->willReturn('#');
47
48
        $dummy = new DummyRepo(['hooks' => ['commit-msg' => '# hook script']]);
49
        $repo  = $this->createRepositoryMock($dummy->getRoot());
50
        $repo->method('getHooksDir')->willReturn($dummy->getHookDir());
51
        $repo->expects($this->once())->method('getConfigOperator')->willReturn($configOp);
52
53
        $hookConfig   = $this->createHookConfigMock();
54
        $actionConfig = $this->createActionConfigMock();
55
        $actionConfig->method('getAction')->willReturn(CH_PATH_FILES . '/bin/success');
56
        $hookConfig->expects($this->once())->method('getActions')->willReturn([$actionConfig]);
57
        $config->expects($this->once())->method('getHookConfigToExecute')->willReturn($hookConfig);
58
        $config->expects($this->atLeastOnce())->method('isHookEnabled')->willReturn(true);
59
        $io->expects($this->atLeast(1))->method('write');
60
        $io->expects($this->once())->method('getArgument')->willReturn(CH_PATH_FILES . '/git/message/valid.txt');
61
62
        $runner = new CommitMsg($io, $config, $repo);
63
        $runner->run();
64
    }
65
66
    public function testRunHookSkippedBecauseOfFixup(): void
67
    {
68
        if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
69
            $this->markTestSkipped('not tested on windows');
70
        }
71
72
        $io       = $this->createIOMock();
73
        $config   = $this->createConfigMock();
74
        $configOp = $this->createMock(ConfigOperator::class);
75
        $configOp->expects($this->once())->method('getSettingSafely')->willReturn('#');
76
77
        $dummy = new DummyRepo(['hooks' => ['commit-msg' => '# hook script']]);
78
        $repo  = $this->createRepositoryMock($dummy->getRoot());
79
        $repo->method('getHooksDir')->willReturn($dummy->getHookDir());
80
        $repo->expects($this->once())->method('getConfigOperator')->willReturn($configOp);
81
        $repo->expects($this->once())->method('getCommitMsg')->willReturn(new GitCommitMessage('fixup! foo', '#'));
82
83
        $hookConfig   = $this->createHookConfigMock();
84
        $actionConfig = $this->createActionConfigMock();
85
        $actionConfig->method('getAction')->willReturn(CH_PATH_FILES . '/bin/success');
86
        $hookConfig->method('isEnabled')->willReturn(true);
87
        $hookConfig->method('getActions')->willReturn([$actionConfig]);
88
        $config->method('getHookConfigToExecute')->willReturn($hookConfig);
89
        $config->expects($this->atLeastOnce())->method('isHookEnabled')->willReturn(true);
90
        $io->expects($this->atLeast(1))->method('write');
91
        $io->expects($this->once())->method('getArgument')->willReturn(CH_PATH_FILES . '/git/message/valid.txt');
92
93
        $runner = new CommitMsg($io, $config, $repo);
94
        $runner->run();
95
    }
96
97
    public function testRunWithoutCommitMsgFile(): void
98
    {
99
        $this->expectException(Exception::class);
100
101
        $io         = $this->createIOMock();
102
        $config     = $this->createConfigMock();
103
        $hookConfig = $this->createHookConfigMock();
104
105
        $dummy = new DummyRepo(['hooks' => ['commit-msg' => '# hook script']]);
106
        $repo  = $this->createRepositoryMock($dummy->getRoot());
107
        $repo->method('getHooksDir')->willReturn($dummy->getHookDir());
108
109
        $configOp     = $this->createGitConfigOperator();
110
        $actionConfig = $this->createActionConfigMock();
111
        $actionConfig->method('getAction')->willReturn(CH_PATH_FILES . '/bin/success');
112
        $hookConfig->method('isEnabled')->willReturn(true);
113
        $hookConfig->method('getActions')->willReturn([$actionConfig]);
114
        $config->method('getHookConfigToExecute')->willReturn($hookConfig);
115
        $config->expects($this->atLeastOnce())->method('isHookEnabled')->willReturn(true);
116
        $configOp->method('getSettingSafely')->willReturn('#');
117
        $repo->method('getConfigOperator')->willReturn($configOp);
118
        $io->expects($this->once())->method('getArgument')->willReturn('');
119
120
        $runner = new CommitMsg($io, $config, $repo);
121
        $runner->run();
122
    }
123
}
124