Passed
Push — master ( b89ee9...82ea21 )
by Sebastian
03:18
created

testBuildDockerTemplateObservesConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 15
c 2
b 0
f 0
dl 0
loc 20
rs 9.7666
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
declare(strict_types=1);
13
14
namespace CaptainHook\App\Hook\Template;
15
16
use CaptainHook\App\Config\Mockery as ConfigMockery;
17
use CaptainHook\App\Git\DummyRepo;
18
use CaptainHook\App\Mockery as AppMockery;
19
use Exception;
20
use PHPUnit\Framework\TestCase;
1 ignored issue
show
Bug introduced by
The type PHPUnit\Framework\TestCase 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...
21
22
class BuilderTest extends TestCase
23
{
24
    use AppMockery;
25
    use ConfigMockery;
26
27
    /**
28
     * Tests Builder::build
29
     */
30
    public function testBuildDockerTemplate(): void
31
    {
32
        $repo = new DummyRepo(
33
            [],
34
            [
35
                'captainhook.json' => '{}',
36
                'vendor' => [
37
                    'autoload.php' => '',
38
                    'bin' => [
39
                        'captainhook' => ''
40
                    ]
41
                ]
42
            ]
43
        );
44
45
        $executable = $repo->getRoot() . '/vendor/bin/captainhook';
46
        $repository = $this->createRepositoryMock($repo->getRoot());
47
        $config     = $this->createConfigMock(true, $repo->getRoot() . '/captainhook.json');
48
        $config->method('getRunMode')->willReturn('docker');
1 ignored issue
show
Bug introduced by
The method method() does not exist on CaptainHook\App\Config. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
        $config->/** @scrutinizer ignore-call */ 
49
                 method('getRunMode')->willReturn('docker');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
49
        $config->method('getRunExec')->willReturn('docker exec captain-container');
50
        $config->method('getRunPath')->willReturn('');
51
        $config->method('getBootstrap')->willReturn('vendor/autoload.php');
52
53
        $template = Builder::build($config, $repository, $executable, false);
54
        $this->assertInstanceOf(Docker::class, $template);
55
56
        $code = $template->getCode('pre-commit');
57
        $this->assertStringContainsString('pre-commit', $code);
58
        $this->assertStringContainsString('docker exec captain-container', $code);
59
        $this->assertStringContainsString('vendor/bin/captainhook', $code);
60
    }
61
62
    /**
63
     * Tests Builder::build
64
     */
65
    public function testBuildDockerTemplateWithBinaryOutsideRepo(): void
66
    {
67
        $repo = new DummyRepo(
68
            [],
69
            [
70
                'captainhook.json' => '{}',
71
                'vendor' => [
72
                    'autoload.php' => '',
73
                ]
74
            ]
75
        );
76
77
        $executable = realpath(__DIR__ . '/../../../../bin/captainhook');
78
        $repository = $this->createRepositoryMock($repo->getRoot());
79
        $config     = $this->createConfigMock(true, $repo->getRoot() . '/captainhook.json');
80
        $config->method('getRunMode')->willReturn('docker');
81
        $config->method('getRunExec')->willReturn('docker exec captain-container');
82
        $config->method('getRunPath')->willReturn('');
83
        $config->method('getBootstrap')->willReturn('vendor/autoload.php');
84
85
        $template = Builder::build($config, $repository, $executable, false);
86
        $code     = $template->getCode('pre-commit');
87
88
        $this->assertInstanceOf(Docker::class, $template);
89
        $this->assertStringContainsString('pre-commit', $code);
90
        $this->assertStringContainsString('docker exec captain-container', $code);
91
        $this->assertStringContainsString($executable, $code);
92
    }
93
94
    /**
95
     * Tests Builder::build
96
     */
97
    public function testBuildLocalTemplate(): void
98
    {
99
        $repository = $this->createRepositoryMock(CH_PATH_FILES);
100
        $config     = $this->createConfigMock(true, CH_PATH_FILES . '/template/captainhook.json');
101
        $config->method('getRunMode')->willReturn('local');
102
        $config->method('getRunExec')->willReturn('');
103
        $config->method('getBootstrap')->willReturn('vendor/autoload.php');
104
105
        $template = Builder::build($config, $repository, CH_PATH_FILES . '/bin/captainhook', false);
106
        $this->assertInstanceOf(Local::class, $template);
107
108
        $code = $template->getCode('pre-commit');
109
        $this->assertStringContainsString('pre-commit', $code);
110
        $this->assertStringContainsString('$captainHook->run', $code);
111
    }
112
113
    /**
114
     * Tests Builder::build
115
     */
116
    public function testBuildInvalidVendor(): void
117
    {
118
        $this->expectException(Exception::class);
119
120
        $repository = $this->createRepositoryMock(CH_PATH_FILES . '/config');
121
        $config     = $this->createConfigMock(true, CH_PATH_FILES . '/config/valid.json');
122
        $config->method('getRunMode')->willReturn('local');
123
        $config->method('getRunExec')->willReturn('');
124
        $config->method('getBootstrap')->willReturn('file-not-there.php');
125
126
        Builder::build($config, $repository, './captainhook', false);
127
    }
128
}
129