Passed
Push — main ( 604bfc...f70419 )
by Sebastian
04:51
created

PrePushTest::testGetChangedFilesRemoteExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 14
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 23
rs 9.7998
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\Git\ChangedFiles\Detector;
13
14
use CaptainHook\App\Console\IO\Mockery as IOMockery;
15
use CaptainHook\App\Mockery as AppMockery;
16
use PHPUnit\Framework\TestCase;
17
18
/**
19
 * Class PrePushTest
20
 *
21
 * @package CaptainHook
22
 * @author  Sebastian Feldmann <[email protected]>
23
 * @link    https://github.com/captainhookphp/captainhook
24
 * @since   Class available since Release 5.20.0
25
 */
26
class PrePushTest extends TestCase
27
{
28
    use IOMockery;
29
    use AppMockery;
30
31
    /**
32
     * Tests: PrePush::getChangedFiles
33
     */
34
    public function testGetChangedFilesNoRanges(): void
35
    {
36
        $io    = $this->createIOMock();
37
        $repo  = $this->createRepositoryMock();
38
39
        $io->expects($this->atLeastOnce())
40
            ->method('getStandardInput')
41
            ->willReturn([]);
42
43
        $d     = new PrePush($io, $repo);
44
        $files = $d->getChangedFiles();
45
46
        $this->assertEquals([], $files);
47
    }
48
49
    /**
50
     * Tests: PrePush::getChangedFiles
51
     */
52
    public function testGetChangedFilesRemoteExists(): void
53
    {
54
        $io    = $this->createIOMock();
55
        $repo  = $this->createRepositoryMock();
56
        $diff  = $this->createGitDiffOperator(['foo.txt', 'bar.txt']);
57
        $log   = $this->createGitLogOperator();
58
59
        $io->expects($this->atLeastOnce())
60
            ->method('getStandardInput')
61
            ->willReturn(
62
                [
63
                    'refs/heads/main 9dfa0fa6221d75f48b2dfac359127324bedf8409' .
64
                    ' refs/heads/main 8309f6e16097754469c485e604900c573bf2c5d8'
65
                ]
66
            );
67
68
        $repo->method('getLogOperator')->willReturn($log);
69
        $repo->method('getDiffOperator')->willReturn($diff);
70
71
        $d     = new PrePush($io, $repo);
72
        $files = $d->getChangedFiles();
73
74
        $this->assertEquals('foo.txt', $files[0]);
75
    }
76
77
    /**
78
     * Tests: PrePush::getChangedFiles
79
     */
80
    public function testGetChangedFilesNewBranch(): void
81
    {
82
        $io    = $this->createIOMock();
83
        $repo  = $this->createRepositoryMock();
84
        $diff  = $this->createGitDiffOperator(['foo.txt', 'bar.txt']);
85
        $log   = $this->createGitLogOperator();
86
87
        $io->expects($this->atLeastOnce())
88
            ->method('getStandardInput')
89
            ->willReturn(
90
                [
91
                    'refs/heads/main 9dfa0fa6221d75f48b2dfac359127324bedf8409' .
92
                    ' refs/heads/main 0000000000000000000000000000000000000000'
93
                ]
94
            );
95
96
        $log->expects($this->atLeastOnce())
97
            ->method('getBranchRevFromRefLog')
98
            ->willReturn('8309f6e16097754469c485e604900c573bf2c5d8');
99
100
        $repo->method('getLogOperator')->willReturn($log);
101
        $repo->method('getDiffOperator')->willReturn($diff);
102
103
        $d     = new PrePush($io, $repo);
104
        $files = $d->getChangedFiles();
105
106
        $this->assertEquals('foo.txt', $files[0]);
107
    }
108
109
110
    /**
111
     * Tests: PrePush::getChangedFiles
112
     */
113
    public function testGetChangedFilesNewBranchNoReflog(): void
114
    {
115
        $io    = $this->createIOMock();
116
        $repo  = $this->createRepositoryMock();
117
        $diff  = $this->createGitDiffOperator(['foo.txt', 'bar.txt']);
118
        $log   = $this->createGitLogOperator();
119
120
        $io->expects($this->atLeastOnce())
121
            ->method('getStandardInput')
122
            ->willReturn(
123
                [
124
                    'refs/heads/main 9dfa0fa6221d75f48b2dfac359127324bedf8409' .
125
                    ' refs/heads/main 0000000000000000000000000000000000000000'
126
                ]
127
            );
128
129
        $log->expects($this->atLeastOnce())
130
            ->method('getBranchRevFromRefLog')
131
            ->willReturn('');
132
        $log->expects($this->atLeastOnce())
133
            ->method('getBranchRevsFromRefLog')
134
            ->willReturn(['9dfa0fa6221d75f48b2dfac359127324bedf8409', '8309f6e16097754469c485e604900c573bf2c5d8']);
135
        $log->expects($this->atLeastOnce())
136
            ->method('getChangedFilesInRevisions')
137
            ->willReturn(['foo.txt', 'bar.txt']);
138
139
        $repo->method('getLogOperator')->willReturn($log);
140
        $repo->method('getDiffOperator')->willReturn($diff);
141
142
        $d     = new PrePush($io, $repo);
143
        $files = $d->getChangedFiles();
144
145
        $this->assertEquals('foo.txt', $files[0]);
146
    }
147
}
148