testExecuteBlockSquash()   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
    /**
30
     * Tests BlockFixupAndSquashCommits::getRestriction
31
     */
32
    public function testConstraint(): void
33
    {
34
        $this->assertTrue(BlockFixupAndSquashCommits::getRestriction()->isApplicableFor('pre-push'));
35
        $this->assertFalse(BlockFixupAndSquashCommits::getRestriction()->isApplicableFor('pre-commit'));
36
    }
37
38
    /**
39
     * Tests BlockFixupAndSquashCommits::execute
40
     */
41
    public function testExecuteSuccess(): void
42
    {
43
        $input    = ['refs/heads/main 12345 refs/heads/main 98765'];
44
        $io       = $this->createIOMock();
45
        $repo     = $this->createRepositoryMock();
46
        $config   = $this->createConfigMock();
47
        $action   = $this->createActionConfigMock();
48
        $operator = $this->createGitLogOperator();
49
        $action->expects($this->once())->method('getOptions')->willReturn(new Options([]));
50
        $io->expects($this->once())->method('getStandardInput')->willReturn($input);
51
        $operator->method('getCommitsBetween')->willReturn($this->getFakeCommits());
52
        $repo->method('getLogOperator')->willReturn($operator);
53
54
        $blocker = new BlockFixupAndSquashCommits();
55
        $blocker->execute($config, $io, $repo, $action);
56
57
        $this->assertTrue(true);
58
    }
59
60
    /**
61
     * Tests BlockFixupAndSquashCommits::execute
62
     */
63
    public function testExecuteSuccessBecauseOfUnprotectedBranch(): void
64
    {
65
        $input    = ['refs/heads/foo 12345 refs/heads/foo 98765'];
66
        $io       = $this->createIOMock();
67
        $repo     = $this->createRepositoryMock();
68
        $config   = $this->createConfigMock();
69
        $action   = $this->createActionConfigMock();
70
        $operator = $this->createGitLogOperator();
71
        $options  = new Options(['protectedBranches' => ['main']]);
72
        $action->expects($this->once())->method('getOptions')->willReturn($options);
73
        $io->expects($this->once())->method('getStandardInput')->willReturn($input);
74
        $operator->method('getCommitsBetween')->willReturn($this->getFakeCommits());
75
        $repo->method('getLogOperator')->willReturn($operator);
76
77
        $blocker = new BlockFixupAndSquashCommits();
78
        $blocker->execute($config, $io, $repo, $action);
79
80
        $this->assertTrue(true);
81
    }
82
83
    /**
84
     * Tests BlockFixupAndSquashCommits::execute
85
     */
86
    public function testExecuteSuccessWithNoChangesFromLocalAndRemote(): void
87
    {
88
        $input    = ['refs/tags/5.14.2 4d89c05 refs/tags/5.14.2 0000000000000000000000000000000000000000'];
89
        $io       = $this->createIOMock();
90
        $repo     = $this->createRepositoryMock();
91
        $config   = $this->createConfigMock();
92
        $action   = $this->createActionConfigMock();
93
        $operator = $this->createGitLogOperator();
94
        $action->method('getOptions')->willReturn(new Options([]));
95
        $io->expects($this->once())->method('getStandardInput')->willReturn($input);
96
        $operator->method('getCommitsBetween')->willReturn($this->getFakeCommits());
97
        $repo->method('getLogOperator')->willReturn($operator);
98
99
        $blocker = new BlockFixupAndSquashCommits();
100
        $blocker->execute($config, $io, $repo, $action);
101
102
        $this->assertTrue(true);
103
    }
104
105
    /**
106
     * Tests BlockFixupAndSquashCommits::execute
107
     */
108
    public function testExecuteBlockFixup(): void
109
    {
110
        $this->expectException(Exception::class);
111
112
        $input    = ['refs/heads/main 12345 refs/heads/main 98765'];
113
        $io       = $this->createIOMock();
114
        $repo     = $this->createRepositoryMock();
115
        $config   = $this->createConfigMock();
116
        $action   = $this->createActionConfigMock();
117
        $operator = $this->createGitLogOperator();
118
        $action->expects($this->once())->method('getOptions')->willReturn(new Options([]));
119
        $io->expects($this->once())->method('getStandardInput')->willReturn($input);
120
        $operator->method('getCommitsBetween')->willReturn($this->getFakeCommits('fixup! Foo'));
121
        $repo->method('getLogOperator')->willReturn($operator);
122
123
        $blocker = new BlockFixupAndSquashCommits();
124
        $blocker->execute($config, $io, $repo, $action);
125
    }
126
127
    /**
128
     * Tests BlockFixupAndSquashCommits::execute
129
     */
130
    public function testExecuteBlockFixupForProtectedBranch(): void
131
    {
132
        $this->expectException(Exception::class);
133
134
        $input    = ['refs/heads/main 12345 refs/heads/main 98765'];
135
        $io       = $this->createIOMock();
136
        $repo     = $this->createRepositoryMock();
137
        $config   = $this->createConfigMock();
138
        $action   = $this->createActionConfigMock();
139
        $operator = $this->createGitLogOperator();
140
        $options  = new Options(['protectedBranches' => ['main']]);
141
        $action->expects($this->once())->method('getOptions')->willReturn($options);
142
        $io->expects($this->once())->method('getStandardInput')->willReturn($input);
143
        $operator->method('getCommitsBetween')->willReturn($this->getFakeCommits('fixup! Foo'));
144
        $repo->method('getLogOperator')->willReturn($operator);
145
146
        $blocker = new BlockFixupAndSquashCommits();
147
        $blocker->execute($config, $io, $repo, $action);
148
    }
149
150
    /**
151
     * Tests BlockFixupAndSquashCommits::execute
152
     */
153
    public function testExecuteBlockSquash(): void
154
    {
155
        $this->expectException(Exception::class);
156
157
        $input    = ['refs/heads/main 12345 refs/heads/main 98765'];
158
        $io       = $this->createIOMock();
159
        $repo     = $this->createRepositoryMock();
160
        $config   = $this->createConfigMock();
161
        $action   = $this->createActionConfigMock();
162
        $operator = $this->createGitLogOperator();
163
        $action->expects($this->once())->method('getOptions')->willReturn(new Options([]));
164
        $io->expects($this->once())->method('getStandardInput')->willReturn($input);
165
        $operator->method('getCommitsBetween')->willReturn($this->getFakeCommits('squash! Foo'));
166
        $repo->method('getLogOperator')->willReturn($operator);
167
168
        $blocker = new BlockFixupAndSquashCommits();
169
        $blocker->execute($config, $io, $repo, $action);
170
    }
171
172
    /**
173
     * Tests BlockFixupAndSquashCommits::execute
174
     */
175
    public function testExecuteNoPushInfo(): void
176
    {
177
        $input    = [''];
178
        $io       = $this->createIOMock();
179
        $repo     = $this->createRepositoryMock();
180
        $config   = $this->createConfigMock();
181
        $action   = $this->createActionConfigMock();
182
        $io->expects($this->once())->method('getStandardInput')->willReturn($input);
183
184
        $blocker = new BlockFixupAndSquashCommits();
185
        $blocker->execute($config, $io, $repo, $action);
186
187
        $this->assertTrue(true);
188
    }
189
190
191
    /**
192
     * @return array<\SebastianFeldmann\Git\Log\Commit>
193
     */
194
    private function getFakeCommits(string $subject = ''): array
195
    {
196
        return [
197
            new Commit('12345', [], 'Test commit #1', '', new DateTimeImmutable(), ''),
198
            new Commit('98765', [], $subject, '', new DateTimeImmutable(), ''),
199
        ];
200
    }
201
}
202