Passed
Push — main ( 388f81...59708d )
by Sebastian
03:37
created

ShellTest::testTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 13
nc 1
nop 0
dl 0
loc 18
rs 9.8333
c 1
b 0
f 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\Hook\Template\Local;
13
14
use CaptainHook\App\Config\Mockery as ConfigMockery;
15
use CaptainHook\App\Config\Run;
16
use CaptainHook\App\Hook\Template\PathInfo;
17
use PHPUnit\Framework\TestCase;
18
19
class ShellTest extends TestCase
20
{
21
    use ConfigMockery;
22
23
    /**
24
     * Tests Shell::getCode
25
     */
26
    public function testTemplate(): void
27
    {
28
        $pathInfo = $this->createMock(PathInfo::class);
29
        $pathInfo->method('getExecutablePath')->willReturn('vendor/bin/captainhook');
30
        $pathInfo->method('getConfigPath')->willReturn('captainhook.json');
31
        $pathInfo->method('isPhar')->willReturn(false);
32
33
        $config = $this->createConfigMock(false, 'captainhook.json');
34
        $config->method('getBootstrap')->willReturn('vendor/autoload.php');
35
        $config->method('getPhpPath')->willReturn('');
36
37
        $template = new Shell($pathInfo, $config);
38
        $code     = $template->getCode('commit-msg');
39
40
        $this->assertStringContainsString('#!/bin/sh', $code);
41
        $this->assertStringContainsString('commit-msg', $code);
42
        $this->assertStringNotContainsString('php7.4', $code);
43
        $this->assertStringContainsString('vendor/bin/captainhook $INTERACTIVE', $code);
44
    }
45
46
    /**
47
     * Tests Shell::getCode
48
     */
49
    public function testTemplateWithoutBootstrap(): void
50
    {
51
        $pathInfo = $this->createMock(PathInfo::class);
52
        $pathInfo->method('getExecutablePath')->willReturn('vendor/bin/captainhook');
53
        $pathInfo->method('getConfigPath')->willReturn('captainhook.json');
54
        $pathInfo->method('isPhar')->willReturn(true);
55
56
        $config = $this->createConfigMock(false, 'captainhook.json');
57
        $config->method('getBootstrap')->willReturn('');
58
        $config->method('getPhpPath')->willReturn('');
59
60
        $template = new Shell($pathInfo, $config);
61
        $code     = $template->getCode('commit-msg');
62
63
        $this->assertStringContainsString('#!/bin/sh', $code);
64
        $this->assertStringNotContainsString('--bootstrap=', $code);
65
        $this->assertStringContainsString('vendor/bin/captainhook $INTERACTIVE', $code);
66
    }
67
68
    /**
69
     * Tests Shell::getCode
70
     */
71
    public function testTemplateWithDefinedPHP(): void
72
    {
73
        $pathInfo = $this->createMock(PathInfo::class);
74
        $pathInfo->method('getExecutablePath')->willReturn('vendor/bin/captainhook');
75
        $pathInfo->method('getConfigPath')->willReturn('captainhook.json');
76
        $pathInfo->method('isPhar')->willReturn(false);
77
78
        $config = $this->createConfigMock(false, 'captainhook.json');
79
        $config->method('getBootstrap')->willReturn('vendor/autoload.php');
80
        $config->method('getPhpPath')->willReturn('/usr/bin/php7.4');
81
82
        $template = new Shell($pathInfo, $config);
83
        $code     = $template->getCode('commit-msg');
84
85
        $this->assertStringContainsString('#!/bin/sh', $code);
86
        $this->assertStringContainsString('commit-msg', $code);
87
        $this->assertStringContainsString('/usr/bin/php7.4', $code);
88
        $this->assertStringContainsString('vendor/bin/captainhook $INTERACTIVE', $code);
89
    }
90
91
    /**
92
     * Tests Shell::getCode
93
     */
94
    public function testTemplateWithDefinedPHPAndRunPath(): void
95
    {
96
        $pathInfo = $this->createMock(PathInfo::class);
97
        $pathInfo->method('getExecutablePath')->willReturn('vendor/bin/captainhook');
98
        $pathInfo->method('getConfigPath')->willReturn('captainhook.json');
99
        $pathInfo->method('isPhar')->willReturn(false);
100
101
        $config    = $this->createConfigMock(false, 'captainhook.json');
102
        $runConfig = new Run(['path' => 'tools/captainhook.phar']);
103
        $config->method('getRunConfig')->willReturn($runConfig);
104
        $config->method('getBootstrap')->willReturn('vendor/autoload.php');
105
        $config->method('getPhpPath')->willReturn('/usr/bin/php7.4');
106
107
        $template = new Shell($pathInfo, $config);
108
        $code     = $template->getCode('commit-msg');
109
110
        $this->assertStringContainsString('#!/bin/sh', $code);
111
        $this->assertStringContainsString('commit-msg', $code);
112
        $this->assertStringContainsString('/usr/bin/php7.4', $code);
113
        $this->assertStringContainsString('tools/captainhook.phar $INTERACTIVE', $code);
114
    }
115
116
    /**
117
     * Tests Shell::getCode
118
     */
119
    public function testTemplateExtExecutable(): void
120
    {
121
        $pathInfo = $this->createMock(PathInfo::class);
122
        $pathInfo->method('getExecutablePath')->willReturn('/usr/local/bin/captainhook');
123
        $pathInfo->method('getConfigPath')->willReturn('captainhook.json');
124
        $pathInfo->method('isPhar')->willReturn(false);
125
126
        $config = $this->createConfigMock(false, 'captainhook.json');
127
        $config->method('getBootstrap')->willReturn('vendor/autoload.php');
128
        $config->method('getPhpPath')->willReturn('');
129
130
        $template = new Shell($pathInfo, $config);
131
        $code     = $template->getCode('commit-msg');
132
133
        $this->assertStringContainsString('#!/bin/sh', $code);
134
        $this->assertStringContainsString('commit-msg', $code);
135
        $this->assertStringNotContainsString('php7.4', $code);
136
        $this->assertStringContainsString('/usr/local/bin/captainhook $INTERACTIVE', $code);
137
    }
138
139
    /**
140
     * Tests Shell::getCode
141
     */
142
    public function testTemplateExtExecutableWithDefinedPHP(): void
143
    {
144
        $pathInfo = $this->createMock(PathInfo::class);
145
        $pathInfo->method('getExecutablePath')->willReturn('/usr/local/bin/captainhook');
146
        $pathInfo->method('getConfigPath')->willReturn('captainhook.json');
147
        $pathInfo->method('isPhar')->willReturn(false);
148
149
        $config = $this->createConfigMock(false, 'captainhook.json');
150
        $config->method('getBootstrap')->willReturn('vendor/autoload.php');
151
        $config->method('getPhpPath')->willReturn('/usr/bin/php7.4');
152
153
        $template = new Shell($pathInfo, $config);
154
        $code     = $template->getCode('commit-msg');
155
156
        $this->assertStringContainsString('#!/bin/sh', $code);
157
        $this->assertStringContainsString('commit-msg', $code);
158
159
        $this->assertStringContainsString('/usr/bin/php7.4', $code);
160
        $this->assertStringContainsString('/usr/local/bin/captainhook $INTERACTIVE', $code);
161
    }
162
163
    /**
164
     * Tests Shell::getCode
165
     */
166
    public function testTemplateExtExecutableWithUserInput(): void
167
    {
168
        $pathInfo = $this->createMock(PathInfo::class);
169
        $pathInfo->method('getExecutablePath')->willReturn('/usr/local/bin/captainhook');
170
        $pathInfo->method('getConfigPath')->willReturn('captainhook.json');
171
        $pathInfo->method('isPhar')->willReturn(false);
172
173
        $config = $this->createConfigMock(false, 'captainhook.json');
174
        $config->method('getBootstrap')->willReturn('vendor/autoload.php');
175
        $config->method('getPhpPath')->willReturn('');
176
177
        $template = new Shell($pathInfo, $config);
178
        $code     = $template->getCode('prepare-commit-msg');
179
180
        $this->assertStringContainsString('#!/bin/sh', $code);
181
        $this->assertStringContainsString('commit-msg', $code);
182
        $this->assertStringContainsString('/usr/local/bin/captainhook $INTERACTIVE', $code);
183
        $this->assertStringContainsString($this->getTtyRedirectionLines(), $code);
184
    }
185
186
    /**
187
     * Returns the expected TTY redirection lines
188
     *
189
     * @return string
190
     */
191
    private function getTtyRedirectionLines(): string
192
    {
193
        return <<<'EOD'
194
if [ -t 1 ]; then
195
    # If we're in a terminal, redirect stdout and stderr to /dev/tty and
196
    # read stdin from /dev/tty. Allow interactive mode for CaptainHook.
197
    exec >/dev/tty 2>/dev/tty </dev/tty
198
    INTERACTIVE=""
199
fi
200
EOD;
201
    }
202
}
203