ShellTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 102
dl 0
loc 149
rs 10
c 3
b 0
f 0
wmc 8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getTtyRedirectionLines() 0 3 1
A testTemplateWithDefinedPHP() 0 18 1
A testTemplateExtExecutableWithUserInput() 0 18 1
A testTemplateExtExecutableWithDefinedPHP() 0 19 1
A testTemplateWithoutBootstrap() 0 17 1
A testTemplateWithDefinedPHPAndRunPath() 0 20 1
A testTemplateExtExecutable() 0 18 1
A testTemplate() 0 18 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\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
    public function testTemplate(): void
24
    {
25
        $pathInfo = $this->createMock(PathInfo::class);
26
        $pathInfo->method('getExecutablePath')->willReturn('vendor/bin/captainhook');
27
        $pathInfo->method('getConfigPath')->willReturn('captainhook.json');
28
        $pathInfo->method('isPhar')->willReturn(false);
29
30
        $config = $this->createConfigMock(false, 'captainhook.json');
31
        $config->method('getBootstrap')->willReturn('vendor/autoload.php');
32
        $config->method('getPhpPath')->willReturn('');
33
34
        $template = new Shell($pathInfo, $config);
35
        $code     = $template->getCode('commit-msg');
36
37
        $this->assertStringContainsString('#!/bin/sh', $code);
38
        $this->assertStringContainsString('commit-msg', $code);
39
        $this->assertStringNotContainsString('php7.4', $code);
40
        $this->assertStringContainsString('vendor/bin/captainhook $INTERACTIVE', $code);
41
    }
42
43
    public function testTemplateWithoutBootstrap(): void
44
    {
45
        $pathInfo = $this->createMock(PathInfo::class);
46
        $pathInfo->method('getExecutablePath')->willReturn('vendor/bin/captainhook');
47
        $pathInfo->method('getConfigPath')->willReturn('captainhook.json');
48
        $pathInfo->method('isPhar')->willReturn(true);
49
50
        $config = $this->createConfigMock(false, 'captainhook.json');
51
        $config->method('getBootstrap')->willReturn('');
52
        $config->method('getPhpPath')->willReturn('');
53
54
        $template = new Shell($pathInfo, $config);
55
        $code     = $template->getCode('commit-msg');
56
57
        $this->assertStringContainsString('#!/bin/sh', $code);
58
        $this->assertStringNotContainsString('--bootstrap=', $code);
59
        $this->assertStringContainsString('vendor/bin/captainhook $INTERACTIVE', $code);
60
    }
61
62
    public function testTemplateWithDefinedPHP(): void
63
    {
64
        $pathInfo = $this->createMock(PathInfo::class);
65
        $pathInfo->method('getExecutablePath')->willReturn('vendor/bin/captainhook');
66
        $pathInfo->method('getConfigPath')->willReturn('captainhook.json');
67
        $pathInfo->method('isPhar')->willReturn(false);
68
69
        $config = $this->createConfigMock(false, 'captainhook.json');
70
        $config->method('getBootstrap')->willReturn('vendor/autoload.php');
71
        $config->method('getPhpPath')->willReturn('/usr/bin/php7.4');
72
73
        $template = new Shell($pathInfo, $config);
74
        $code     = $template->getCode('commit-msg');
75
76
        $this->assertStringContainsString('#!/bin/sh', $code);
77
        $this->assertStringContainsString('commit-msg', $code);
78
        $this->assertStringContainsString('/usr/bin/php7.4', $code);
79
        $this->assertStringContainsString('vendor/bin/captainhook $INTERACTIVE', $code);
80
    }
81
82
    public function testTemplateWithDefinedPHPAndRunPath(): void
83
    {
84
        $pathInfo = $this->createMock(PathInfo::class);
85
        $pathInfo->method('getExecutablePath')->willReturn('vendor/bin/captainhook');
86
        $pathInfo->method('getConfigPath')->willReturn('captainhook.json');
87
        $pathInfo->method('isPhar')->willReturn(false);
88
89
        $config    = $this->createConfigMock(false, 'captainhook.json');
90
        $runConfig = new Run(['path' => 'tools/captainhook.phar']);
91
        $config->method('getRunConfig')->willReturn($runConfig);
92
        $config->method('getBootstrap')->willReturn('vendor/autoload.php');
93
        $config->method('getPhpPath')->willReturn('/usr/bin/php7.4');
94
95
        $template = new Shell($pathInfo, $config);
96
        $code     = $template->getCode('commit-msg');
97
98
        $this->assertStringContainsString('#!/bin/sh', $code);
99
        $this->assertStringContainsString('commit-msg', $code);
100
        $this->assertStringContainsString('/usr/bin/php7.4', $code);
101
        $this->assertStringContainsString('tools/captainhook.phar $INTERACTIVE', $code);
102
    }
103
104
    public function testTemplateExtExecutable(): void
105
    {
106
        $pathInfo = $this->createMock(PathInfo::class);
107
        $pathInfo->method('getExecutablePath')->willReturn('/usr/local/bin/captainhook');
108
        $pathInfo->method('getConfigPath')->willReturn('captainhook.json');
109
        $pathInfo->method('isPhar')->willReturn(false);
110
111
        $config = $this->createConfigMock(false, 'captainhook.json');
112
        $config->method('getBootstrap')->willReturn('vendor/autoload.php');
113
        $config->method('getPhpPath')->willReturn('');
114
115
        $template = new Shell($pathInfo, $config);
116
        $code     = $template->getCode('commit-msg');
117
118
        $this->assertStringContainsString('#!/bin/sh', $code);
119
        $this->assertStringContainsString('commit-msg', $code);
120
        $this->assertStringNotContainsString('php7.4', $code);
121
        $this->assertStringContainsString('/usr/local/bin/captainhook $INTERACTIVE', $code);
122
    }
123
124
    public function testTemplateExtExecutableWithDefinedPHP(): void
125
    {
126
        $pathInfo = $this->createMock(PathInfo::class);
127
        $pathInfo->method('getExecutablePath')->willReturn('/usr/local/bin/captainhook');
128
        $pathInfo->method('getConfigPath')->willReturn('captainhook.json');
129
        $pathInfo->method('isPhar')->willReturn(false);
130
131
        $config = $this->createConfigMock(false, 'captainhook.json');
132
        $config->method('getBootstrap')->willReturn('vendor/autoload.php');
133
        $config->method('getPhpPath')->willReturn('/usr/bin/php7.4');
134
135
        $template = new Shell($pathInfo, $config);
136
        $code     = $template->getCode('commit-msg');
137
138
        $this->assertStringContainsString('#!/bin/sh', $code);
139
        $this->assertStringContainsString('commit-msg', $code);
140
141
        $this->assertStringContainsString('/usr/bin/php7.4', $code);
142
        $this->assertStringContainsString('/usr/local/bin/captainhook $INTERACTIVE', $code);
143
    }
144
145
    public function testTemplateExtExecutableWithUserInput(): void
146
    {
147
        $pathInfo = $this->createMock(PathInfo::class);
148
        $pathInfo->method('getExecutablePath')->willReturn('/usr/local/bin/captainhook');
149
        $pathInfo->method('getConfigPath')->willReturn('captainhook.json');
150
        $pathInfo->method('isPhar')->willReturn(false);
151
152
        $config = $this->createConfigMock(false, 'captainhook.json');
153
        $config->method('getBootstrap')->willReturn('vendor/autoload.php');
154
        $config->method('getPhpPath')->willReturn('');
155
156
        $template = new Shell($pathInfo, $config);
157
        $code     = $template->getCode('prepare-commit-msg');
158
159
        $this->assertStringContainsString('#!/bin/sh', $code);
160
        $this->assertStringContainsString('commit-msg', $code);
161
        $this->assertStringContainsString('/usr/local/bin/captainhook $INTERACTIVE', $code);
162
        $this->assertStringContainsString($this->getTtyRedirectionLines(), $code);
163
    }
164
165
    private function getTtyRedirectionLines(): string
166
    {
167
        return <<<'EOD'
168
if [ -t 1 ]; then
169
    # If we're in a terminal, redirect stdout and stderr to /dev/tty and
170
    # read stdin from /dev/tty. Allow interactive mode for CaptainHook.
171
    exec >/dev/tty 2>/dev/tty </dev/tty
172
    INTERACTIVE=""
173
fi
174
EOD;
175
    }
176
}
177