testExecuteSuccessWithFileExtension()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 17
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 26
rs 9.7
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\File\Action;
13
14
use CaptainHook\App\Config;
15
use CaptainHook\App\Console\IO\NullIO;
16
use CaptainHook\App\Mockery;
17
use Exception;
18
use PHPUnit\Framework\TestCase;
19
20
class DoesNotContainRegexTest extends TestCase
21
{
22
    use Mockery;
23
24
    public function testExecuteInvalidOption(): void
25
    {
26
        $this->expectException(Exception::class);
27
        $this->expectExceptionMessage('Missing option "regex" for DoesNotContainRegex action');
28
29
        $io     = new NullIO();
30
        $config = new Config(CH_PATH_FILES . '/captainhook.json');
31
        $repo    = $this->createRepositoryMock();
32
        $action = new Config\Action(DoesNotContainRegex::class);
33
34
        $standard = new DoesNotContainRegex();
35
        $standard->execute($config, $io, $repo, $action);
36
    }
37
38
    public function testExecuteSuccess(): void
39
    {
40
        $io     = new NullIO();
41
        $config = new Config(CH_PATH_FILES . '/captainhook.json');
42
        $action = new Config\Action(DoesNotContainRegex::class, [
43
            'regex' => '#some regex that does not match#'
44
        ]);
45
        $repo   = $this->createRepositoryMock();
46
        $repo->method('getIndexOperator')->willReturn(
47
            $this->createGitIndexOperator([
48
                CH_PATH_FILES . '/storage/regextest1.txt'
49
            ])
50
        );
51
52
        $standard = new DoesNotContainRegex();
53
        $standard->execute($config, $io, $repo, $action);
54
55
        $this->assertTrue(true);
56
    }
57
58
    public function testExecuteFailure(): void
59
    {
60
        $this->expectException(Exception::class);
61
62
        $io     = new NullIO();
63
        $config = new Config(CH_PATH_FILES . '/captainhook.json');
64
        $action = new Config\Action(DoesNotContainRegex::class, [
65
            'regex' => '#foo#'
66
        ]);
67
        $repo   = $this->createRepositoryMock();
68
        $repo->method('getIndexOperator')->willReturn(
69
            $this->createGitIndexOperator([
70
                CH_PATH_FILES . '/storage/regextest1.txt',
71
            ])
72
        );
73
74
        $standard = new DoesNotContainRegex();
75
        $standard->execute($config, $io, $repo, $action);
76
    }
77
78
    public function testExecuteMissConfiguration(): void
79
    {
80
        $this->expectException(Exception::class);
81
82
        $io     = new NullIO();
83
        $config = new Config(CH_PATH_FILES . '/captainhook.json');
84
        $action = new Config\Action(DoesNotContainRegex::class, [
85
            'regex'          => '#foo#',
86
            'fileExtensions' => ''
87
        ]);
88
        $repo   = $this->createRepositoryMock();
89
        $repo->method('getIndexOperator')->willReturn(
90
            $this->createGitIndexOperator([
91
                CH_PATH_FILES . '/storage/regextest1.txt',
92
            ])
93
        );
94
95
        $standard = new DoesNotContainRegex();
96
        $standard->execute($config, $io, $repo, $action);
97
    }
98
99
    public function testExecuteFailureWithCount(): void
100
    {
101
        $this->expectException(Exception::class);
102
103
        $io     = new NullIO();
104
        $config = new Config(CH_PATH_FILES . '/captainhook.json');
105
        $action = new Config\Action(DoesNotContainRegex::class, [
106
            'regex' => '#foo#'
107
        ]);
108
        $repo   = $this->createRepositoryMock();
109
        $repo->method('getIndexOperator')->willReturn(
110
            $this->createGitIndexOperator([
111
                CH_PATH_FILES . '/storage/regextest1.txt',
112
                CH_PATH_FILES . '/storage/regextest2.txt',
113
            ])
114
        );
115
116
        $standard = new DoesNotContainRegex();
117
        $standard->execute($config, $io, $repo, $action);
118
    }
119
120
    public function testExecuteSuccessWithFileExtension(): void
121
    {
122
        $io     = new NullIO();
123
        $config = new Config(CH_PATH_FILES . '/captainhook.json');
124
        $action = new Config\Action(DoesNotContainRegex::class, [
125
            'regex' => '#.#',
126
            'fileExtensions' => ['php']
127
        ]);
128
        $index  = $this->createGitIndexOperator([
129
            CH_PATH_FILES . '/storage/regextest1.txt'
130
        ]);
131
        $index->method('getStagedFilesOfType')->willReturnCallback(function ($ext) {
132
            if ($ext === 'txt') {
133
                return [
134
                    CH_PATH_FILES . '/storage/regextest1.txt'
135
                ];
136
            }
137
            return [];
138
        });
139
        $repo = $this->createRepositoryMock();
140
        $repo->method('getIndexOperator')->willReturn($index);
141
142
        $standard = new DoesNotContainRegex();
143
        $standard->execute($config, $io, $repo, $action);
144
145
        $this->assertTrue(true);
146
    }
147
148
    public function testExecuteFailureWithFileExtension(): void
149
    {
150
        $this->expectException(Exception::class);
151
152
        $io     = new NullIO();
153
        $config = new Config(CH_PATH_FILES . '/captainhook.json');
154
        $action = new Config\Action(DoesNotContainRegex::class, [
155
            'regex' => '#foo#',
156
            'fileExtensions' => ['txt']
157
        ]);
158
        $index  = $this->createGitIndexOperator([
159
            CH_PATH_FILES . '/storage/regextest1.txt'
160
        ]);
161
        $index->method('getStagedFilesOfType')->willReturnCallback(function ($ext) {
162
            if ($ext === 'txt') {
163
                return [
164
                    CH_PATH_FILES . '/storage/regextest1.txt'
165
                ];
166
            }
167
            return [];
168
        });
169
        $repo   = $this->createRepositoryMock();
170
        $repo->method('getIndexOperator')->willReturn($index);
171
172
        $standard = new DoesNotContainRegex();
173
        $standard->execute($config, $io, $repo, $action);
174
    }
175
}
176