Passed
Push — master ( 7bee4d...457c92 )
by Sebastian
01:54
created

PrepareCommitMsgTest::testRunHookEnabled()   A

Complexity

Conditions 2
Paths 8

Size

Total Lines 38
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 38
rs 9.456
c 0
b 0
f 0
cc 2
nc 8
nop 0
1
<?php
2
/**
3
 * This file is part of CaptainHook.
4
 *
5
 * (c) Sebastian Feldmann <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace CaptainHook\App\Runner\Hook;
11
12
use CaptainHook\App\Config;
13
use CaptainHook\App\Git\DummyRepo;
14
use CaptainHook\App\Hook\Message\Action\Prepare;
15
use CaptainHook\App\Runner\BaseTestRunner;
16
use SebastianFeldmann\Git\Repository;
17
18
class PrepareCommitMsgTest extends BaseTestRunner
19
{
20
    /**
21
     * Tests PrepareCommitMsg::run
22
     */
23
    public function testRunHookEnabled(): void
24
    {
25
        if (\defined('PHP_WINDOWS_VERSION_MAJOR')) {
26
            $this->markTestSkipped('not tested on windows');
27
        }
28
29
        $io           = $this->getIOMock();
30
        $config       = $this->getConfigMock();
31
        $hookConfig   = $this->getHookConfigMock();
32
        $actionConfig = $this->getActionConfigMock();
33
        $actionConfig->method('getAction')->willReturn('\\' . Prepare::class);
34
        $actionConfig->method('getOptions')->willReturn(new Config\Options(['message' => 'Prepared commit msg']));
35
        $hookConfig->expects($this->once())->method('isEnabled')->willReturn(true);
36
        $hookConfig->expects($this->once())->method('getActions')->willReturn([$actionConfig]);
37
        $config->expects($this->once())->method('getHookConfig')->willReturn($hookConfig);
38
39
        $dummy = new DummyRepo();
40
        $dummy->setup();
41
        $repo = new Repository($dummy->getPath());
42
43
        $tmpDir = sys_get_temp_dir();
44
        $path   = tempnam($tmpDir, 'prepare-commit-msg');
45
        file_put_contents($path, 'Commit Message');
46
47
        $io->expects($this->exactly(2))->method('write');
48
        $io->expects($this->exactly(3))->method('getArgument')->willReturnOnConsecutiveCalls(
49
            $path,
50
            '',
51
            ''
52
        );
53
54
        try {
55
            $runner = new PrepareCommitMsg($io, $config, $repo);
56
            $runner->run();
57
            $this->assertEquals('Prepared commit msg', file_get_contents($path));
58
        } finally {
59
            unlink($path);
60
            $dummy->cleanup();
61
        }
62
    }
63
64
    /**
65
     * Tests PrepareCommitMsg::run
66
     */
67
    public function testRunHookNoMessageException(): void
68
    {
69
        $this->expectException(\Exception::class);
70
71
        $io           = $this->getIOMock();
72
        $config       = $this->getConfigMock();
73
        $repo         = $this->getRepositoryMock();
74
        $hookConfig   = $this->getHookConfigMock();
75
        $actionConfig = $this->getActionConfigMock();
76
        $actionConfig->method('getAction')->willReturn(Prepare::class);
77
        $actionConfig->method('getOptions')->willReturn(new Config\Options(['message' => 'Prepared commit msg']));
78
        $hookConfig->expects($this->once())->method('isEnabled')->willReturn(true);
79
        $hookConfig->expects($this->once())->method('getActions')->willReturn([$actionConfig]);
80
        $config->expects($this->once())->method('getHookConfig')->willReturn($hookConfig);
81
        $io->expects($this->exactly(3))->method('getArgument')->willReturn('');
82
83
        $runner = new PrepareCommitMsg($io, $config, $repo);
84
        $runner->run();
85
    }
86
}
87