DoesNotContainRegexTest   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 188
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 90
c 1
b 0
f 0
dl 0
loc 188
rs 10
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testExecuteFailureWithCount() 0 19 1
A testExecuteFailureWithFileExtension() 0 26 2
A testExecuteSuccess() 0 18 1
A testExecuteSuccessWithFileExtension() 0 26 2
A testExecuteFailure() 0 18 1
A testExecuteMissConfiguration() 0 19 1
A testExecuteInvalidOption() 0 12 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\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
    /**
25
     * Tests DoesNotContainRegex::execute
26
     *
27
     * @throws \Exception
28
     */
29
    public function testExecuteInvalidOption(): void
30
    {
31
        $this->expectException(Exception::class);
32
        $this->expectExceptionMessage('Missing option "regex" for DoesNotContainRegex action');
33
34
        $io     = new NullIO();
35
        $config = new Config(CH_PATH_FILES . '/captainhook.json');
36
        $repo    = $this->createRepositoryMock();
37
        $action = new Config\Action(DoesNotContainRegex::class);
38
39
        $standard = new DoesNotContainRegex();
40
        $standard->execute($config, $io, $repo, $action);
41
    }
42
43
    /**
44
     * Tests DoesNotContainRegex::execute
45
     *
46
     * @throws \Exception
47
     */
48
    public function testExecuteSuccess(): void
49
    {
50
        $io     = new NullIO();
51
        $config = new Config(CH_PATH_FILES . '/captainhook.json');
52
        $action = new Config\Action(DoesNotContainRegex::class, [
53
            'regex' => '#some regex that does not match#'
54
        ]);
55
        $repo   = $this->createRepositoryMock();
56
        $repo->method('getIndexOperator')->willReturn(
57
            $this->createGitIndexOperator([
58
                CH_PATH_FILES . '/storage/regextest1.txt'
59
            ])
60
        );
61
62
        $standard = new DoesNotContainRegex();
63
        $standard->execute($config, $io, $repo, $action);
64
65
        $this->assertTrue(true);
66
    }
67
68
    /**
69
     * Tests DoesNotContainRegex::execute
70
     *
71
     * @throws \Exception
72
     */
73
    public function testExecuteFailure(): void
74
    {
75
        $this->expectException(Exception::class);
76
77
        $io     = new NullIO();
78
        $config = new Config(CH_PATH_FILES . '/captainhook.json');
79
        $action = new Config\Action(DoesNotContainRegex::class, [
80
            'regex' => '#foo#'
81
        ]);
82
        $repo   = $this->createRepositoryMock();
83
        $repo->method('getIndexOperator')->willReturn(
84
            $this->createGitIndexOperator([
85
                CH_PATH_FILES . '/storage/regextest1.txt',
86
            ])
87
        );
88
89
        $standard = new DoesNotContainRegex();
90
        $standard->execute($config, $io, $repo, $action);
91
    }
92
93
    /**
94
     * Tests DoesNotContainRegex::execute
95
     *
96
     * @throws \Exception
97
     */
98
    public function testExecuteMissConfiguration(): void
99
    {
100
        $this->expectException(Exception::class);
101
102
        $io     = new NullIO();
103
        $config = new Config(CH_PATH_FILES . '/captainhook.json');
104
        $action = new Config\Action(DoesNotContainRegex::class, [
105
            'regex'          => '#foo#',
106
            'fileExtensions' => ''
107
        ]);
108
        $repo   = $this->createRepositoryMock();
109
        $repo->method('getIndexOperator')->willReturn(
110
            $this->createGitIndexOperator([
111
                CH_PATH_FILES . '/storage/regextest1.txt',
112
            ])
113
        );
114
115
        $standard = new DoesNotContainRegex();
116
        $standard->execute($config, $io, $repo, $action);
117
    }
118
    /**
119
     * Tests DoesNotContainRegex::execute
120
     *
121
     * @throws \Exception
122
     */
123
    public function testExecuteFailureWithCount(): void
124
    {
125
        $this->expectException(Exception::class);
126
127
        $io     = new NullIO();
128
        $config = new Config(CH_PATH_FILES . '/captainhook.json');
129
        $action = new Config\Action(DoesNotContainRegex::class, [
130
            'regex' => '#foo#'
131
        ]);
132
        $repo   = $this->createRepositoryMock();
133
        $repo->method('getIndexOperator')->willReturn(
134
            $this->createGitIndexOperator([
135
                CH_PATH_FILES . '/storage/regextest1.txt',
136
                CH_PATH_FILES . '/storage/regextest2.txt',
137
            ])
138
        );
139
140
        $standard = new DoesNotContainRegex();
141
        $standard->execute($config, $io, $repo, $action);
142
    }
143
144
    /**
145
     * Tests DoesNotContainRegex::execute
146
     *
147
     * @throws \Exception
148
     */
149
    public function testExecuteSuccessWithFileExtension(): void
150
    {
151
        $io     = new NullIO();
152
        $config = new Config(CH_PATH_FILES . '/captainhook.json');
153
        $action = new Config\Action(DoesNotContainRegex::class, [
154
            'regex' => '#.#',
155
            'fileExtensions' => ['php']
156
        ]);
157
        $index  = $this->createGitIndexOperator([
158
            CH_PATH_FILES . '/storage/regextest1.txt'
159
        ]);
160
        $index->method('getStagedFilesOfType')->willReturnCallback(function ($ext) {
161
            if ($ext === 'txt') {
162
                return [
163
                    CH_PATH_FILES . '/storage/regextest1.txt'
164
                ];
165
            }
166
            return [];
167
        });
168
        $repo = $this->createRepositoryMock();
169
        $repo->method('getIndexOperator')->willReturn($index);
170
171
        $standard = new DoesNotContainRegex();
172
        $standard->execute($config, $io, $repo, $action);
173
174
        $this->assertTrue(true);
175
    }
176
177
    /**
178
     * Tests DoesNotContainRegex::execute
179
     *
180
     * @throws \Exception
181
     */
182
    public function testExecuteFailureWithFileExtension(): void
183
    {
184
        $this->expectException(Exception::class);
185
186
        $io     = new NullIO();
187
        $config = new Config(CH_PATH_FILES . '/captainhook.json');
188
        $action = new Config\Action(DoesNotContainRegex::class, [
189
            'regex' => '#foo#',
190
            'fileExtensions' => ['txt']
191
        ]);
192
        $index  = $this->createGitIndexOperator([
193
            CH_PATH_FILES . '/storage/regextest1.txt'
194
        ]);
195
        $index->method('getStagedFilesOfType')->willReturnCallback(function ($ext) {
196
            if ($ext === 'txt') {
197
                return [
198
                    CH_PATH_FILES . '/storage/regextest1.txt'
199
                ];
200
            }
201
            return [];
202
        });
203
        $repo   = $this->createRepositoryMock();
204
        $repo->method('getIndexOperator')->willReturn($index);
205
206
        $standard = new DoesNotContainRegex();
207
        $standard->execute($config, $io, $repo, $action);
208
    }
209
}
210