PostMergeTest::testRunHookWithConditionsApply()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 16
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 20
rs 9.7333
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;
15
use CaptainHook\App\Config\Mockery as ConfigMockery;
16
use CaptainHook\App\Console\IO\Mockery as IOMockery;
17
use CaptainHook\App\Git\DummyRepo;
18
use CaptainHook\App\Mockery as CHMockery;
19
use PHPUnit\Framework\TestCase;
20
21
class PostMergeTest extends TestCase
22
{
23
    use ConfigMockery;
24
    use IOMockery;
25
    use CHMockery;
26
27
    /**
28
     * Tests PostMerge::run
29
     *
30
     * @throws \Exception
31
     */
32
    public function testRunHookEnabled(): void
33
    {
34
        if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
35
            $this->markTestSkipped('not tested on windows');
36
        }
37
        $dummy = new DummyRepo(['hooks' => ['post-merge' => '# hook script']]);
38
        $repo  = $this->createRepositoryMock($dummy->getRoot());
39
        $repo->method('getHooksDir')->willReturn($dummy->getHookDir());
40
41
        $io           = $this->createIOMock();
42
        $config       = $this->createConfigMock();
43
        $hookConfig   = $this->createHookConfigMock();
44
        $actionConfig = $this->createActionConfigMock();
45
        $hookConfig->method('isEnabled')->willReturn(true);
46
        $hookConfig->expects($this->once())->method('getActions')->willReturn([$actionConfig]);
47
        $config->expects($this->once())->method('getHookConfigToExecute')->willReturn($hookConfig);
48
        $config->expects($this->atLeastOnce())->method('isHookEnabled')->willReturn(true);
49
        $io->expects($this->atLeast(1))->method('write');
50
51
        $runner = new PostMerge($io, $config, $repo);
52
        $runner->run();
53
    }
54
55
    /**
56
     * Tests PostMerge::run
57
     *
58
     * @throws \Exception
59
     */
60
    public function testRunHookWithConditionsApply(): void
61
    {
62
        $dummy = new DummyRepo(['hooks' => ['post-merge' => '# hook script']]);
63
        $repo  = $this->createRepositoryMock($dummy->getRoot());
64
        $repo->method('getHooksDir')->willReturn($dummy->getHookDir());
65
66
        $io              = $this->createIOMock();
67
        $config          = $this->createConfigMock();
68
        $hookConfig      = $this->createHookConfigMock();
69
        $actionConfig    = $this->createActionConfigMock();
70
        $conditionConfig = new Config\Condition(CH_PATH_FILES . '/bin/success');
71
        $actionConfig->expects($this->once())->method('getConditions')->willReturn([$conditionConfig]);
72
        $hookConfig->method('isEnabled')->willReturn(true);
73
        $hookConfig->expects($this->once())->method('getActions')->willReturn([$actionConfig]);
74
        $config->expects($this->once())->method('getHookConfigToExecute')->willReturn($hookConfig);
75
        $config->expects($this->atLeastOnce())->method('isHookEnabled')->willReturn(true);
76
        $io->expects($this->atLeast(1))->method('write');
77
78
        $runner = new PostMerge($io, $config, $repo);
79
        $runner->run();
80
    }
81
82
83
    /**
84
     * Tests PostMerge::run
85
     *
86
     * @throws \Exception
87
     */
88
    public function testRunHookWithConditionsFail()
89
    {
90
        $dummy = new DummyRepo(['hooks' => ['post-merge' => '# hook script']]);
91
        $repo  = $this->createRepositoryMock($dummy->getRoot());
92
        $repo->method('getHooksDir')->willReturn($dummy->getHookDir());
93
94
        $io              = $this->createIOMock();
95
        $config          = $this->createConfigMock();
96
        $hookConfig      = $this->createHookConfigMock();
97
        $actionConfig    = $this->createActionConfigMock();
98
        $conditionConfig = new Config\Condition(CH_PATH_FILES . '/bin/failure');
99
        $actionConfig->expects($this->once())->method('getConditions')->willReturn([$conditionConfig]);
100
        $hookConfig->method('isEnabled')->willReturn(true);
101
        $hookConfig->expects($this->once())->method('getActions')->willReturn([$actionConfig]);
102
        $config->expects($this->once())->method('getHookConfigToExecute')->willReturn($hookConfig);
103
        $config->expects($this->atLeastOnce())->method('isHookEnabled')->willReturn(true);
104
        $io->expects($this->atLeast(1))->method('write');
105
106
        $runner = new PostMerge($io, $config, $repo);
107
        $runner->run();
108
    }
109
110
    /**
111
     * Tests PostMerge::run
112
     *
113
     * @throws \Exception
114
     */
115
    public function testRunHookDisabled()
116
    {
117
        $io           = $this->createIOMock();
118
        $config       = $this->createConfigMock();
119
        $repo         = $this->createRepositoryMock();
120
        $config->expects($this->atLeastOnce())->method('isHookEnabled')->willReturn(false);
121
        $io->expects($this->atLeast(1))->method('write');
122
123
        $runner = new PostMerge($io, $config, $repo);
124
        $runner->run();
125
    }
126
}
127