PHPTest::testSrcTemplateExtExecutable()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 16
rs 9.9
c 0
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\Hook\Template\PathInfo;
16
use PHPUnit\Framework\TestCase;
17
18
class PHPTest extends TestCase
19
{
20
    use ConfigMockery;
21
22
    public function testSrcTemplate(): void
23
    {
24
        $pathInfo = $this->createMock(PathInfo::class);
25
        $pathInfo->method('getExecutablePath')->willReturn('./vendor/bin/captainhook');
26
        $pathInfo->method('getConfigPath')->willReturn('captainhook.json');
27
28
        $config = $this->createConfigMock(false, 'captainhook.json');
29
        $config->method('getBootstrap')->willReturn('vendor/autoload.php');
30
31
        $template = new PHP($pathInfo, $config);
32
        $code     = $template->getCode('commit-msg');
33
34
        $this->assertStringContainsString('#!/usr/bin/env php', $code);
35
        $this->assertStringContainsString('commit-msg', $code);
36
        $this->assertStringContainsString('$captainHook->run(', $code);
37
    }
38
39
    public function testSrcStdInHook(): void
40
    {
41
        $pathInfo = $this->createMock(PathInfo::class);
42
        $pathInfo->method('getExecutablePath')->willReturn('./vendor/bin/captainhook');
43
        $pathInfo->method('getConfigPath')->willReturn('captainhook.json');
44
45
        $config = $this->createConfigMock(false, 'captainhook.json');
46
        $config->method('getBootstrap')->willReturn('vendor/autoload.php');
47
48
        $template = new PHP($pathInfo, $config);
49
        $code     = $template->getCode('pre-push');
50
51
        $this->assertStringContainsString('#!/usr/bin/env php', $code);
52
        $this->assertStringContainsString('STDIN', $code);
53
        $this->assertStringContainsString('$captainHook->run(', $code);
54
    }
55
56
    public function testSrcTemplateExtExecutable(): void
57
    {
58
        $pathInfo = $this->createMock(PathInfo::class);
59
        $pathInfo->method('getExecutablePath')->willReturn('./vendor/bin/captainhook');
60
        $pathInfo->method('getConfigPath')->willReturn('captainhook.json');
61
        $pathInfo->method('isPhar')->willReturn(false);
62
63
        $config = $this->createConfigMock(false, 'captainhook.json');
64
        $config->method('getBootstrap')->willReturn('vendor/autoload.php');
65
66
        $template   = new PHP($pathInfo, $config);
67
        $code       = $template->getCode('commit-msg');
68
69
        $this->assertStringContainsString('#!/usr/bin/env php', $code);
70
        $this->assertStringContainsString('commit-msg', $code);
71
        $this->assertStringContainsString('$captainHook->run(', $code);
72
    }
73
74
    public function testPharAbsoluteExecutablePath(): void
75
    {
76
        $pathInfo = $this->createMock(PathInfo::class);
77
        $pathInfo->method('getExecutablePath')->willReturn('/usr/local/bin/captainhook');
78
        $pathInfo->method('getConfigPath')->willReturn('captainhook.json');
79
        $pathInfo->method('isPhar')->willReturn(true);
80
81
        $config = $this->createConfigMock(false, 'captainhook.json');
82
        $config->method('getBootstrap')->willReturn('vendor/autoload.php');
83
84
        $template   = new PHP($pathInfo, $config);
85
        $code       = $template->getCode('commit-msg');
86
87
        $this->assertStringContainsString('#!/usr/bin/env php', $code);
88
        $this->assertStringContainsString('commit-msg', $code);
89
        $this->assertStringContainsString('/usr/local/bin/captainhook', $code);
90
    }
91
92
    public function testPharTemplate(): void
93
    {
94
        $pathInfo = $this->createMock(PathInfo::class);
95
        $pathInfo->method('getExecutablePath')->willReturn('tools/captainhook.phar');
96
        $pathInfo->method('getConfigPath')->willReturn('captainhook.json');
97
        $pathInfo->method('isPhar')->willReturn(true);
98
99
        $config = $this->createConfigMock(false, 'captainhook.json');
100
        $config->method('getBootstrap')->willReturn('vendor/autoload.php');
101
102
        $template   = new PHP($pathInfo, $config);
103
        $code       = $template->getCode('commit-msg');
104
105
        $this->assertStringContainsString('#!/usr/bin/env php', $code);
106
        $this->assertStringContainsString('hook:commit-msg', $code);
107
        $this->assertStringContainsString('tools/captainhook.phar', $code);
108
    }
109
110
    public function testPharStdIn(): void
111
    {
112
        $pathInfo = $this->createMock(PathInfo::class);
113
        $pathInfo->method('getExecutablePath')->willReturn('tools/captainhook.phar');
114
        $pathInfo->method('getConfigPath')->willReturn('captainhook.json');
115
        $pathInfo->method('isPhar')->willReturn(true);
116
117
        $config = $this->createConfigMock(false, 'captainhook.json');
118
        $config->method('getBootstrap')->willReturn('vendor/autoload.php');
119
120
        $template   = new PHP($pathInfo, $config);
121
        $code       = $template->getCode('post-rewrite');
122
123
        $this->assertStringContainsString('#!/usr/bin/env php', $code);
124
        $this->assertStringContainsString('hook:post-rewrite', $code);
125
        $this->assertStringContainsString('STDIN', $code);
126
    }
127
}
128