Passed
Push — main ( cebfe5...3df9c7 )
by Sebastian
04:33
created

IntegrateBeforePushTest::testRestrictions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
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\Notify\Action;
13
14
use CaptainHook\App\Config\Action;
15
use CaptainHook\App\Config\Mockery as ConfigMockery;
16
use CaptainHook\App\Console\IO\Mockery as IOMockery;
17
use CaptainHook\App\Hooks;
18
use CaptainHook\App\Mockery as AppMockery;
19
use DateTimeImmutable;
20
use Exception;
21
use PHPUnit\Framework\TestCase;
22
use SebastianFeldmann\Git\Log\Commit;
23
24
/**
25
 * Class IntegrateBeforePushTest
26
 *
27
 * @package CaptainHook
28
 * @author  Sebastian Feldmann <[email protected]>
29
 * @link    https://github.com/captainhookphp/captainhook
30
 * @since   Class available since Release 5.19.1
31
 */
32
class IntegrateBeforePushTest extends TestCase
33
{
34
    use AppMockery;
35
    use ConfigMockery;
36
    use IOMockery;
37
38
    /**
39
     * Tests IntegrateBeforePush::getRestriction
40
     */
41
    public function testRestrictions(): void
42
    {
43
        $restriction = IntegrateBeforePush::getRestriction();
44
45
        $this->assertTrue($restriction->isApplicableFor(Hooks::PRE_PUSH));
46
        $this->assertFalse($restriction->isApplicableFor(Hooks::PRE_COMMIT));
47
    }
48
49
    /**
50
     * Tests: IntegrateBeforePush::execute
51
     */
52
    public function testBlockPush(): void
53
    {
54
        $io           = $this->createIOMock();
55
        $config       = $this->createConfigMock();
56
        $actionConfig = new Action(
57
            'IntegrateBeforePush',
58
            [
59
                'trigger' => '[integrate]',
60
                'branch'  => 'origin/main',
61
            ]
62
        );
63
64
        $repository = $this->createRepositoryMock();
65
        $remote     = $this->createGitRemoteOperator();
66
        $log        = $this->createGitLogOperator();
67
        $logs       = [
68
            new Commit('12345', [], 'foo msg', '', new DateTimeImmutable(), 'sf'),
69
            new Commit('67890', [], 'bar msg', '', new DateTimeImmutable(), 'sf'),
70
            new Commit('45678', [], '[integrate] config update', '', new DateTimeImmutable(), 'sf'),
71
        ];
72
73
        $remote->expects($this->once())->method('fetchBranch');
74
        $log->expects($this->once())->method('getCommitsBetween')->willReturn($logs);
75
        $repository->expects($this->once())->method('getLogOperator')->willReturn($log);
76
        $repository->expects($this->once())->method('getRemoteOperator')->willReturn($remote);
77
78
        $this->expectException(Exception::class);
79
80
        $action = new IntegrateBeforePush();
81
        $action->execute($config, $io, $repository, $actionConfig);
82
    }
83
84
    /**
85
     * Tests: IntegrateBeforePush::execute
86
     */
87
    public function testNoTrigger(): void
88
    {
89
        $io           = $this->createIOMock();
90
        $config       = $this->createConfigMock();
91
        $actionConfig = new Action(
92
            'IntegrateBeforePush',
93
            [
94
                'trigger' => '[integrate]',
95
                'branch'  => 'origin/main',
96
            ]
97
        );
98
99
        $repository = $this->createRepositoryMock();
100
        $remote     = $this->createGitRemoteOperator();
101
        $log        = $this->createGitLogOperator();
102
        $logs       = [
103
            new Commit('12345', [], 'foo msg', '', new DateTimeImmutable(), 'sf'),
104
            new Commit('67890', [], 'bar msg', '', new DateTimeImmutable(), 'sf'),
105
            new Commit('45678', [], 'baz msg', '', new DateTimeImmutable(), 'sf'),
106
        ];
107
108
        $remote->expects($this->once())->method('fetchBranch');
109
        $log->expects($this->once())->method('getCommitsBetween')->willReturn($logs);
110
        $repository->expects($this->once())->method('getLogOperator')->willReturn($log);
111
        $repository->expects($this->once())->method('getRemoteOperator')->willReturn($remote);
112
113
        $action = new IntegrateBeforePush();
114
        $action->execute($config, $io, $repository, $actionConfig);
115
    }
116
}
117