Completed
Push — master ( 0d30a7...716818 )
by Sebastian
25s queued 11s
created

PreCommitTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 52
c 1
b 0
f 0
dl 0
loc 96
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testRunHookEnabled() 0 22 2
A testRunHookDontFailOnFirstError() 0 37 2
A testRunHookDisabled() 0 12 1
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\Mockery as ConfigMockery;
15
use CaptainHook\App\Console\IO\Mockery as IOMockery;
16
use CaptainHook\App\Exception\ActionFailed;
17
use CaptainHook\App\Mockery as CHMockery;
18
use PHPUnit\Framework\TestCase;
19
20
class PreCommitTest extends TestCase
21
{
22
    use ConfigMockery;
23
    use IOMockery;
24
    use CHMockery;
25
26
    /**
27
     * Tests PreCommit::run
28
     *
29
     * @throws \Exception
30
     */
31
    public function testRunHookEnabled(): void
32
    {
33
        if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
34
            $this->markTestSkipped('not tested on windows');
35
        }
36
37
        // fail on first error must be active
38
        $config = $this->createConfigMock();
39
        $config->method('failOnFirstError')->willReturn(true);
1 ignored issue
show
Bug introduced by
The method method() does not exist on CaptainHook\App\Config. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

39
        $config->/** @scrutinizer ignore-call */ 
40
                 method('failOnFirstError')->willReturn(true);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
40
41
        $io           = $this->createIOMock();
42
        $repo         = $this->createRepositoryMock();
43
        $hookConfig   = $this->createHookConfigMock();
44
        $actionConfig = $this->createActionConfigMock();
45
        $actionConfig->method('getAction')->willReturn(CH_PATH_FILES . '/bin/success');
1 ignored issue
show
Bug introduced by
The method method() does not exist on CaptainHook\App\Config\Action. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

45
        $actionConfig->/** @scrutinizer ignore-call */ 
46
                       method('getAction')->willReturn(CH_PATH_FILES . '/bin/success');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
46
        $hookConfig->expects($this->once())->method('isEnabled')->willReturn(true);
47
        $hookConfig->expects($this->once())->method('getActions')->willReturn([$actionConfig]);
48
        $config->expects($this->once())->method('getHookConfig')->willReturn($hookConfig);
49
        $io->expects($this->atLeast(1))->method('write');
50
51
        $runner = new PreCommit($io, $config, $repo);
52
        $runner->run();
53
    }
54
55
    /**
56
     * Tests PreCommit::run
57
     *
58
     * @throws \Exception
59
     */
60
    public function testRunHookDontFailOnFirstError(): void
61
    {
62
        $this->expectException(ActionFailed::class);
63
64
        if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
65
            $this->markTestSkipped('not tested on windows');
66
        }
67
        // we have to create a config that does not fail on first error
68
        $config              = $this->createConfigMock();
69
        $config->expects($this->once())->method('failOnFirstError')->willReturn(false);
70
71
        $io                  = $this->createIOMock();
72
        $repo                = $this->createRepositoryMock();
73
        $hookConfig          = $this->createHookConfigMock();
74
        $actionConfigFail    = $this->createActionConfigMock();
75
        $actionConfigSuccess = $this->createActionConfigMock();
76
77
        // every action has to get executed
78
        $actionConfigFail->expects($this->atLeastOnce())
79
                         ->method('getAction')
80
                         ->willReturn(CH_PATH_FILES . '/bin/failure');
81
82
        // so even if the first actions fails this action has to get executed
83
        $actionConfigSuccess->expects($this->atLeastOnce())
84
                            ->method('getAction')
85
                            ->willReturn(CH_PATH_FILES . '/bin/failure');
86
87
        $hookConfig->expects($this->once())->method('isEnabled')->willReturn(true);
88
        $hookConfig->expects($this->once())
89
                   ->method('getActions')
90
                   ->willReturn([$actionConfigFail, $actionConfigSuccess]);
91
92
        $config->expects($this->once())->method('getHookConfig')->willReturn($hookConfig);
93
        $io->expects($this->atLeast(1))->method('write');
94
95
        $runner = new PreCommit($io, $config, $repo);
96
        $runner->run();
97
    }
98
99
    /**
100
     * Tests PreCommit::run
101
     *
102
     * @throws \Exception
103
     */
104
    public function testRunHookDisabled(): void
105
    {
106
        $io           = $this->createIOMock();
107
        $config       = $this->createConfigMock();
108
        $hookConfig   = $this->createHookConfigMock();
109
        $repo         = $this->createRepositoryMock();
110
        $hookConfig->expects($this->once())->method('isEnabled')->willReturn(false);
111
        $config->expects($this->once())->method('getHookConfig')->willReturn($hookConfig);
112
        $io->expects($this->once())->method('write');
113
114
        $runner = new PreCommit($io, $config, $repo);
115
        $runner->run();
116
    }
117
}
118