replacementPossibilitiesWithGitMapping()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 13
rs 9.9
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;
13
14
use CaptainHook\App\Config\Run;
15
use PHPUnit\Framework\Attributes\DataProvider;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\Attributes\DataProvider was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use PHPUnit\Framework\TestCase;
17
use CaptainHook\App\Config\Mockery as ConfigMockery;
18
19
class DockerTest extends TestCase
20
{
21
    use ConfigMockery;
22
23
    public function testTemplateCaptainHookDevelopment(): void
24
    {
25
        $repo       = realpath(CH_PATH_FILES . '/template-ch');
26
        $config     = $repo . '/captainhook.json';
27
        $executable = $repo . '/does/not/matter';
28
        $pathInfo   = new PathInfo($repo, $config, $executable, false);
29
30
        $configMock = $this->createConfigMock(false, $repo . '/captainhook.json');
31
        $runConfig  = new Run(['mode' => 'docker', 'exec' => 'docker exec cap-container', 'path' => '']);
32
        $configMock->method('getBootstrap')->willReturn('vendor/autoload.php');
33
        $configMock->method('getRunConfig')->willReturn($runConfig);
34
35
        $template   = new Docker($pathInfo, $configMock);
36
        $code       = $template->getCode('commit-msg');
37
38
        $this->assertStringContainsString('#!/bin/sh', $code);
39
        $this->assertStringContainsString('docker exec -i cap-container', $code);
40
        $this->assertStringContainsString('./bin/captainhook', $code);
41
    }
42
43
    public function testTemplateCaptainHookAsLibrary(): void
44
    {
45
        $pathInfo = $this->createMock(PathInfo::class);
46
        $pathInfo->method('getExecutablePath')->willReturn('./vendor/bin/captainhook');
47
        $pathInfo->method('getConfigPath')->willReturn('captainhook.json');
48
49
        $configMock = $this->createConfigMock(false, 'captainhook.json');
50
        $runConfig  = new Run(['mode' => 'docker', 'exec' => 'docker exec cap-container', 'path' => '']);
51
        $configMock->method('getBootstrap')->willReturn('');
52
        $configMock->method('getRunConfig')->willReturn($runConfig);
53
54
        $template = new Docker($pathInfo, $configMock);
55
        $code     = $template->getCode('commit-msg');
56
57
        $this->assertStringContainsString('#!/bin/sh', $code);
58
        $this->assertStringContainsString('docker exec -i cap-container', $code);
59
        $this->assertStringContainsString('./vendor/bin/captainhook', $code);
60
    }
61
62
    public function testTemplateCustomPath(): void
63
    {
64
        $repo       = realpath(CH_PATH_FILES . '/template-ch');
65
        $executable = $repo . '/does/not/matter';
66
        $config     = $repo . '/captainhook.json';
67
        $pathInfo   = new PathInfo($repo, $config, $executable, false);
68
69
        $configMock = $this->createConfigMock(false, $repo . '/captainhook.json');
70
        $runConfig  = new Run(
71
            ['mode' => 'docker', 'exec' => 'docker exec cap-container', 'path' => './foo/captainhook']
72
        );
73
        $configMock->method('getBootstrap')->willReturn('vendor/autoload.php');
74
        $configMock->method('getRunConfig')->willReturn($runConfig);
75
76
        $template = new Docker($pathInfo, $configMock);
77
        $code     = $template->getCode('commit-msg');
78
79
        $this->assertStringContainsString('#!/bin/sh', $code);
80
        $this->assertStringContainsString('docker exec -i cap-container', $code);
81
        $this->assertStringContainsString('./foo/captainhook', $code);
82
        $this->assertStringContainsString('bootstrap=vendor/autoload.php', $code);
83
    }
84
85
    #[DataProvider('replacementPossibilitiesWithoutGitMapping')]
86
    public function testDockerCommandOptimizationWithoutGitMapping(string $exec, string $expected, string $msg): void
87
    {
88
        $pathInfo = $this->createMock(PathInfo::class);
89
        $pathInfo->method('getExecutablePath')->willReturn('./vendor/bin/captainhook');
90
        $pathInfo->method('getConfigPath')->willReturn('captainhook.json');
91
92
        $configMock = $this->createConfigMock(false, 'captainhook.json');
93
        $runConfig  = new Run([
94
            'mode' => 'docker',
95
            'exec' => 'docker exec ' . $exec,
96
            'path' => '/usr/local/bin/captainhook'
97
        ]);
98
        $configMock->method('getBootstrap')->willReturn('');
99
        $configMock->method('getRunConfig')->willReturn($runConfig);
100
101
        $template = new Docker($pathInfo, $configMock);
102
        $code     = $template->getCode('prepare-commit-msg');
103
104
        $this->assertStringContainsString('docker exec ' . $expected, $code, $msg);
105
        $this->assertStringContainsString('/usr/local/bin/captainhook', $code);
106
    }
107
108
    public static function replacementPossibilitiesWithoutGitMapping(): array
109
    {
110
        return [
111
            ['cap-container', '-i cap-container', 'none'],
112
            ['-it cap-container', '-it cap-container', '-it'],
113
            ['-ti cap-container', '-ti cap-container', '-ti'],
114
            ['--interactive --tty cap-container', '--interactive --tty cap-container', 'long it'],
115
            ['--tty --interactive cap-container', '--tty --interactive cap-container', 'long ti'],
116
            ['--tty cap-container', '-i --tty cap-container', 'no i'],
117
            ['-xit cap-container', '-xit cap-container', 'prefixed i'],
118
            ['-xit cap-container', '-xit cap-container', 'prefixed e'],
119
            ['cap-container', '-i cap-container', 'long e'],
120
        ];
121
    }
122
123
    #[DataProvider('replacementPossibilitiesWithGitMapping')]
124
    public function testDockerCommandOptimizationWithGitMapping(string $exec, string $expected, string $msg): void
125
    {
126
        $pathInfo = $this->createMock(PathInfo::class);
127
        $pathInfo->method('getExecutablePath')->willReturn('./vendor/bin/captainhook');
128
        $pathInfo->method('getConfigPath')->willReturn('captainhook.json');
129
130
        $configMock = $this->createConfigMock(false, 'captainhook.json');
131
        $runConfig  = new Run([
132
            'mode' => 'docker',
133
            'exec' => 'docker exec ' . $exec,
134
            'path' => '/usr/local/bin/captainhook',
135
            'git'  => '/docker/.git'
136
        ]);
137
        $configMock->method('getBootstrap')->willReturn('');
138
        $configMock->method('getRunConfig')->willReturn($runConfig);
139
140
        $template = new Docker($pathInfo, $configMock);
141
        $code     = $template->getCode('prepare-commit-msg');
142
        $codePush = $template->getCode('pre-push');
143
144
        $this->assertStringContainsString('docker exec' . $expected, $code, $msg);
145
        $this->assertStringContainsString('/usr/local/bin/captainhook', $code);
146
        $this->assertStringNotContainsString('docker exec' . $expected, $codePush, $msg);
147
    }
148
149
    public static function replacementPossibilitiesWithGitMapping(): array
150
    {
151
        $env = ' -e GIT_INDEX_FILE="/docker/.git/$(basename $GIT_INDEX_FILE)"';
152
        return [
153
            ['cap-container', ' -i' . $env . ' cap-container', 'none'],
154
            ['-it cap-container', $env . ' -it cap-container', '-it'],
155
            ['-ti cap-container', $env . ' -ti cap-container', '-ti'],
156
            ['--interactive --tty cap-container', $env . ' --interactive --tty cap-container', 'long it'],
157
            ['--tty --interactive cap-container', $env . ' --tty --interactive cap-container', 'long ti'],
158
            ['--tty cap-container', ' -i' . $env . ' --tty cap-container', 'no i'],
159
            ['-xit cap-container', $env . ' -xit cap-container', 'prefixed i'],
160
            ['-xit cap-container', $env . ' -xit cap-container', 'prefixed e'],
161
            ['cap-container', ' -i' . $env . ' cap-container', 'long e'],
162
        ];
163
    }
164
}
165