Passed
Push — main ( dcadeb...04676c )
by Sebastian
14:28
created

PreCommitTest::testExecutePhar()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
c 0
b 0
f 0
dl 0
loc 20
rs 9.8666
cc 1
nc 1
nop 0
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\Console\Command\Hook;
13
14
use CaptainHook\App\Console\Runtime\Resolver;
15
use CaptainHook\App\Git\DummyRepo;
16
use Exception;
17
use Symfony\Component\Console\Formatter\OutputFormatterInterface;
18
use Symfony\Component\Console\Input\ArrayInput;
19
use Symfony\Component\Console\Output\ConsoleOutput;
20
use Symfony\Component\Console\Output\NullOutput;
21
use PHPUnit\Framework\TestCase;
22
use Symfony\Component\Console\Output\OutputInterface;
23
24
class PreCommitTest extends TestCase
25
{
26
    /**
27
     * Tests PreCommit::run
28
     *
29
     * @throws \Exception
30
     */
31
    public function testExecuteLib(): void
32
    {
33
        $repo   = new DummyRepo();
34
        $output = new NullOutput();
35
        $input  = new ArrayInput(
36
            [
37
                '--configuration' => CH_PATH_FILES . '/config/valid.json',
38
                '--git-directory' => $repo->getGitDir()
39
            ]
40
        );
41
42
        $cmd = new PreCommit(new Resolver());
43
        $cmd->run($input, $output);
44
45
        $this->assertTrue(true);
46
    }
47
48
    /**
49
     * Tests PreCommit::run
50
     *
51
     * @throws \Exception
52
     */
53
    public function testExecuteSkip(): void
54
    {
55
        $repo   = new DummyRepo();
56
        $output = new NullOutput();
57
        $input  = new ArrayInput(
58
            [
59
                '--configuration' => CH_PATH_FILES . '/config/valid.json',
60
                '--git-directory' => $repo->getGitDir()
61
            ]
62
        );
63
64
        $_SERVER['CAPTAINHOOK_SKIP_HOOKS'] = 1;
65
66
        $cmd = new PreCommit(new Resolver());
67
        $cmd->run($input, $output);
68
69
        $this->assertTrue(true);
70
71
        $_SERVER['CAPTAINHOOK_SKIP_HOOKS'] = 0;
72
    }
73
74
    /**
75
     * Tests PreCommit::run
76
     *
77
     * @throws \Exception
78
     */
79
    public function testExecutePhar(): void
80
    {
81
        $resolver = $this->createMock(Resolver::class);
82
        $resolver->expects($this->once())->method('isPharRelease')->willReturn(true);
83
84
        $repo     = new DummyRepo();
85
        $output   = new NullOutput();
86
        $input    = new ArrayInput(
87
            [
88
                '--configuration' => CH_PATH_FILES . '/config/empty.json',
89
                '--git-directory' => $repo->getGitDir(),
90
                '--bootstrap'     => '../bootstrap/constant.php'
91
            ]
92
        );
93
94
        $cmd = new PreCommit($resolver);
95
        $cmd->run($input, $output);
96
97
        $this->assertTrue(true);
98
        $this->assertTrue(defined('CH_BOOTSTRAP_WORKED'));
99
    }
100
101
    /**
102
     * Tests PreCommit::run
103
     *
104
     * @throws \Exception
105
     */
106
    public function testExecutePharBootstrapNotFound(): void
107
    {
108
        $resolver = $this->createMock(Resolver::class);
109
        $resolver->expects($this->once())->method('isPharRelease')->willReturn(true);
110
111
        $repo     = new DummyRepo();
112
        $output   = new NullOutput();
113
        $input    = new ArrayInput(
114
            [
115
                '--configuration' => CH_PATH_FILES . '/config/empty.json',
116
                '--git-directory' => $repo->getGitDir(),
117
                '--bootstrap'     => '../bootstrap/fail.php'
118
            ]
119
        );
120
121
        $cmd = new PreCommit($resolver);
122
        $this->assertEquals(1, $cmd->run($input, $output));
123
    }
124
125
    /**
126
     * Tests PreCommit::run
127
     */
128
    public function testExecutePharBootstrapNotSet(): void
129
    {
130
        $resolver = $this->createMock(Resolver::class);
131
        $resolver->expects($this->once())->method('isPharRelease')->willReturn(true);
132
133
        $repo     = new DummyRepo();
134
        $output   = new NullOutput();
135
        $input    = new ArrayInput(
136
            [
137
                '--configuration' => CH_PATH_FILES . '/config/empty.json',
138
                '--git-directory' => $repo->getGitDir()
139
            ]
140
        );
141
142
        $cmd = new PreCommit($resolver);
143
        $this->assertEquals(0, $cmd->run($input, $output));
144
    }
145
146
    /**
147
     * Tests PreCommit::run
148
     *
149
     * @throws \Exception
150
     */
151
    public function testExecuteFailingActionInDebugMode(): void
152
    {
153
        $this->expectException(Exception::class);
154
155
        $output = $this->createMock(NullOutput::class);
156
        $output->expects($this->once())->method('isDebug')->willReturn(true);
157
158
        $resolver = new Resolver();
159
        $repo     = new DummyRepo();
160
        $input    = new ArrayInput(
161
            [
162
                '--configuration' => CH_PATH_FILES . '/config/valid-but-failing.json',
163
                '--git-directory' => $repo->getGitDir(),
164
                '--bootstrap'     => '../bootstrap/empty.php'
165
            ]
166
        );
167
168
        $cmd = new PreCommit($resolver);
169
        $cmd->run($input, $output);
170
171
        $this->assertTrue(true);
172
    }
173
174
    /**
175
     * Tests PreCommit::run
176
     *
177
     * @throws \Exception
178
     */
179
    public function testExecuteFailingActionInVerboseMode(): void
180
    {
181
        $output = $this->createMock(NullOutput::class);
182
        $output->expects($this->once())->method('isDebug')->willReturn(false);
183
184
        $resolver = new Resolver();
185
        $repo     = new DummyRepo();
186
        $input    = new ArrayInput(
187
            [
188
                '--configuration' => CH_PATH_FILES . '/config/valid-but-failing.json',
189
                '--git-directory' => $repo->getGitDir(),
190
                '--bootstrap'     => '../bootstrap/empty.php'
191
            ]
192
        );
193
194
        $cmd = new PreCommit($resolver);
195
        $this->assertEquals(1, $cmd->run($input, $output));
196
    }
197
}
198