testRunHookNoMessageException()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 16
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 22
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\Hook\Message\Action\Prepare;
19
use CaptainHook\App\Mockery as CHMockery;
20
use Exception;
21
use PHPUnit\Framework\TestCase;
22
use SebastianFeldmann\Git\Repository;
23
24
class PrepareCommitMsgTest extends TestCase
25
{
26
    use ConfigMockery;
27
    use IOMockery;
28
    use CHMockery;
29
30
    public function testRunHookEnabled(): void
31
    {
32
        $io           = $this->createIOMock();
33
        $config       = $this->createConfigMock();
34
        $hookConfig   = $this->createHookConfigMock();
35
        $actionConfig = $this->createActionConfigMock();
36
        $actionConfig->method('getAction')->willReturn('\\' . Prepare::class);
37
        $actionConfig->method('getOptions')->willReturn(new Config\Options(['message' => 'Prepared commit msg']));
38
        $hookConfig->method('isEnabled')->willReturn(true);
39
        $hookConfig->expects($this->once())->method('getActions')->willReturn([$actionConfig]);
40
        $config->expects($this->once())->method('getHookConfigToExecute')->willReturn($hookConfig);
41
        $config->expects($this->atLeastOnce())->method('isHookEnabled')->willReturn(true);
42
43
        $configOp = $this->createGitConfigOperator();
44
        $configOp->method('getSettingSafely')->willReturn('#');
45
        // setup fake vfs repos directory
46
        $dummy = new DummyRepo(['config' => '#config', 'hooks' => ['prepare-commit-msg' => '# hook script']]);
47
        $repo  = new Repository($dummy->getRoot());
48
49
        $commitMessageFile = $dummy->getGitDir() . '/prepare-commit-msg';
50
        file_put_contents($commitMessageFile, 'Commit Message');
51
52
        $io->expects($this->atLeast(1))->method('write');
53
        $io->expects($this->exactly(3))->method('getArgument')->willReturn($commitMessageFile);
54
55
        $runner = new PrepareCommitMsg($io, $config, $repo);
56
        $runner->run();
57
        $this->assertEquals('Prepared commit msg', file_get_contents($commitMessageFile));
58
    }
59
60
    public function testRunHookNoMessageException(): void
61
    {
62
        $this->expectException(Exception::class);
63
64
        $dummy = new DummyRepo(['hooks' => ['prepare-commit-msg' => '# hook script']]);
65
        $repo  = $this->createRepositoryMock($dummy->getRoot());
66
        $repo->method('getHooksDir')->willReturn($dummy->getHookDir());
67
68
        $configOp = $this->createGitConfigOperator();
69
        $configOp->method('getSettingSafely')->willReturn('#');
70
        $repo->method('getConfigOperator')->willReturn($configOp);
71
72
        $io           = $this->createIOMock();
73
        $config       = $this->createConfigMock();
74
        $actionConfig = $this->createActionConfigMock();
75
        $actionConfig->method('getAction')->willReturn(Prepare::class);
76
        $actionConfig->method('getOptions')->willReturn(new Config\Options(['message' => 'Prepared commit msg']));
77
        $config->expects($this->atLeastOnce())->method('isHookEnabled')->willReturn(true);
78
        $io->expects($this->exactly(3))->method('getArgument')->willReturn('');
79
80
        $runner = new PrepareCommitMsg($io, $config, $repo);
81
        $runner->run();
82
    }
83
}
84