testExecuteBlockFixup()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 13
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 17
rs 9.8333
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\Hook\Branch\Action;
13
14
use CaptainHook\App\Config\Mockery as ConfigMockery;
15
use CaptainHook\App\Config\Options;
16
use CaptainHook\App\Console\IO\Mockery as IOMockery;
17
use CaptainHook\App\Mockery as AppMockery;
18
use DateTimeImmutable;
19
use Exception;
20
use PHPUnit\Framework\TestCase;
21
use SebastianFeldmann\Git\Log\Commit;
22
23
class BlockFixupAndSquashCommitsTest extends TestCase
24
{
25
    use AppMockery;
26
    use IOMockery;
27
    use ConfigMockery;
28
29
    public function testConstraint(): void
30
    {
31
        $this->assertTrue(BlockFixupAndSquashCommits::getRestriction()->isApplicableFor('pre-push'));
32
        $this->assertFalse(BlockFixupAndSquashCommits::getRestriction()->isApplicableFor('pre-commit'));
33
    }
34
35
    public function testExecuteSuccess(): void
36
    {
37
        $input    = ['refs/heads/main 12345 refs/heads/main 98765'];
38
        $io       = $this->createIOMock();
39
        $repo     = $this->createRepositoryMock();
40
        $config   = $this->createConfigMock();
41
        $action   = $this->createActionConfigMock();
42
        $operator = $this->createGitLogOperator();
43
        $action->expects($this->once())->method('getOptions')->willReturn(new Options([]));
44
        $io->expects($this->once())->method('getStandardInput')->willReturn($input);
45
        $operator->method('getCommitsBetween')->willReturn($this->getFakeCommits());
46
        $repo->method('getLogOperator')->willReturn($operator);
47
48
        $blocker = new BlockFixupAndSquashCommits();
49
        $blocker->execute($config, $io, $repo, $action);
50
51
        $this->assertTrue(true);
52
    }
53
54
    public function testExecuteSuccessBecauseOfUnprotectedBranch(): void
55
    {
56
        $input    = ['refs/heads/foo 12345 refs/heads/foo 98765'];
57
        $io       = $this->createIOMock();
58
        $repo     = $this->createRepositoryMock();
59
        $config   = $this->createConfigMock();
60
        $action   = $this->createActionConfigMock();
61
        $operator = $this->createGitLogOperator();
62
        $options  = new Options(['protectedBranches' => ['main']]);
63
        $action->expects($this->once())->method('getOptions')->willReturn($options);
64
        $io->expects($this->once())->method('getStandardInput')->willReturn($input);
65
        $operator->method('getCommitsBetween')->willReturn($this->getFakeCommits());
66
        $repo->method('getLogOperator')->willReturn($operator);
67
68
        $blocker = new BlockFixupAndSquashCommits();
69
        $blocker->execute($config, $io, $repo, $action);
70
71
        $this->assertTrue(true);
72
    }
73
74
    public function testExecuteSuccessWithNoChangesFromLocalAndRemote(): void
75
    {
76
        $input    = ['refs/tags/5.14.2 4d89c05 refs/tags/5.14.2 0000000000000000000000000000000000000000'];
77
        $io       = $this->createIOMock();
78
        $repo     = $this->createRepositoryMock();
79
        $config   = $this->createConfigMock();
80
        $action   = $this->createActionConfigMock();
81
        $operator = $this->createGitLogOperator();
82
        $action->method('getOptions')->willReturn(new Options([]));
83
        $io->expects($this->once())->method('getStandardInput')->willReturn($input);
84
        $operator->method('getCommitsBetween')->willReturn($this->getFakeCommits());
85
        $repo->method('getLogOperator')->willReturn($operator);
86
87
        $blocker = new BlockFixupAndSquashCommits();
88
        $blocker->execute($config, $io, $repo, $action);
89
90
        $this->assertTrue(true);
91
    }
92
93
    public function testExecuteBlockFixup(): void
94
    {
95
        $this->expectException(Exception::class);
96
97
        $input    = ['refs/heads/main 12345 refs/heads/main 98765'];
98
        $io       = $this->createIOMock();
99
        $repo     = $this->createRepositoryMock();
100
        $config   = $this->createConfigMock();
101
        $action   = $this->createActionConfigMock();
102
        $operator = $this->createGitLogOperator();
103
        $action->expects($this->once())->method('getOptions')->willReturn(new Options([]));
104
        $io->expects($this->once())->method('getStandardInput')->willReturn($input);
105
        $operator->method('getCommitsBetween')->willReturn($this->getFakeCommits('fixup! Foo'));
106
        $repo->method('getLogOperator')->willReturn($operator);
107
108
        $blocker = new BlockFixupAndSquashCommits();
109
        $blocker->execute($config, $io, $repo, $action);
110
    }
111
112
    public function testExecuteBlockFixupForProtectedBranch(): void
113
    {
114
        $this->expectException(Exception::class);
115
116
        $input    = ['refs/heads/main 12345 refs/heads/main 98765'];
117
        $io       = $this->createIOMock();
118
        $repo     = $this->createRepositoryMock();
119
        $config   = $this->createConfigMock();
120
        $action   = $this->createActionConfigMock();
121
        $operator = $this->createGitLogOperator();
122
        $options  = new Options(['protectedBranches' => ['main']]);
123
        $action->expects($this->once())->method('getOptions')->willReturn($options);
124
        $io->expects($this->once())->method('getStandardInput')->willReturn($input);
125
        $operator->method('getCommitsBetween')->willReturn($this->getFakeCommits('fixup! Foo'));
126
        $repo->method('getLogOperator')->willReturn($operator);
127
128
        $blocker = new BlockFixupAndSquashCommits();
129
        $blocker->execute($config, $io, $repo, $action);
130
    }
131
132
    public function testExecuteBlockSquash(): void
133
    {
134
        $this->expectException(Exception::class);
135
136
        $input    = ['refs/heads/main 12345 refs/heads/main 98765'];
137
        $io       = $this->createIOMock();
138
        $repo     = $this->createRepositoryMock();
139
        $config   = $this->createConfigMock();
140
        $action   = $this->createActionConfigMock();
141
        $operator = $this->createGitLogOperator();
142
        $action->expects($this->once())->method('getOptions')->willReturn(new Options([]));
143
        $io->expects($this->once())->method('getStandardInput')->willReturn($input);
144
        $operator->method('getCommitsBetween')->willReturn($this->getFakeCommits('squash! Foo'));
145
        $repo->method('getLogOperator')->willReturn($operator);
146
147
        $blocker = new BlockFixupAndSquashCommits();
148
        $blocker->execute($config, $io, $repo, $action);
149
    }
150
151
    public function testExecuteNoPushInfo(): void
152
    {
153
        $input    = [''];
154
        $io       = $this->createIOMock();
155
        $repo     = $this->createRepositoryMock();
156
        $config   = $this->createConfigMock();
157
        $action   = $this->createActionConfigMock();
158
        $io->expects($this->once())->method('getStandardInput')->willReturn($input);
159
160
        $blocker = new BlockFixupAndSquashCommits();
161
        $blocker->execute($config, $io, $repo, $action);
162
163
        $this->assertTrue(true);
164
    }
165
166
167
    /**
168
     * @return array<\SebastianFeldmann\Git\Log\Commit>
169
     */
170
    private function getFakeCommits(string $subject = ''): array
171
    {
172
        return [
173
            new Commit('12345', [], 'Test commit #1', '', new DateTimeImmutable(), ''),
174
            new Commit('98765', [], $subject, '', new DateTimeImmutable(), ''),
175
        ];
176
    }
177
}
178