Passed
Pull Request — master (#98)
by Sebastian
02:15
created

DoesNotContainRegexTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 166
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 81
c 1
b 0
f 0
dl 0
loc 166
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testExecuteFailureWithCount() 0 20 1
A testExecuteFailureWithFileExtension() 0 27 2
A testExecuteSuccess() 0 18 1
A testExecuteSuccessWithFileExtension() 0 26 2
A testExecuteFailure() 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(
1 ignored issue
show
Bug introduced by
The method method() does not exist on SebastianFeldmann\Git\Repository. ( Ignorable by Annotation )

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

56
        $repo->/** @scrutinizer ignore-call */ 
57
               method('getIndexOperator')->willReturn(

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...
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
        $this->expectExceptionMessage('<error>Regex \'#foo#\' failed:</error> 1 matches in 1 files');
77
78
        $io     = new NullIO();
79
        $config = new Config(CH_PATH_FILES . '/captainhook.json');
80
        $action = new Config\Action(DoesNotContainRegex::class, [
81
            'regex' => '#foo#'
82
        ]);
83
        $repo   = $this->createRepositoryMock();
84
        $repo->method('getIndexOperator')->willReturn(
85
            $this->createGitIndexOperator([
86
                CH_PATH_FILES . '/storage/regextest1.txt',
87
            ])
88
        );
89
90
        $standard = new DoesNotContainRegex();
91
        $standard->execute($config, $io, $repo, $action);
92
    }
93
94
    /**
95
     * Tests DoesNotContainRegex::execute
96
     *
97
     * @throws \Exception
98
     */
99
    public function testExecuteFailureWithCount(): void
100
    {
101
        $this->expectException(Exception::class);
102
        $this->expectExceptionMessage('<error>Regex \'#foo#\' failed:</error> 3 matches in 2 files');
103
104
        $io     = new NullIO();
105
        $config = new Config(CH_PATH_FILES . '/captainhook.json');
106
        $action = new Config\Action(DoesNotContainRegex::class, [
107
            'regex' => '#foo#'
108
        ]);
109
        $repo   = $this->createRepositoryMock();
110
        $repo->method('getIndexOperator')->willReturn(
111
            $this->createGitIndexOperator([
112
                CH_PATH_FILES . '/storage/regextest1.txt',
113
                CH_PATH_FILES . '/storage/regextest2.txt',
114
            ])
115
        );
116
117
        $standard = new DoesNotContainRegex();
118
        $standard->execute($config, $io, $repo, $action);
119
    }
120
121
    /**
122
     * Tests DoesNotContainRegex::execute
123
     *
124
     * @throws \Exception
125
     */
126
    public function testExecuteSuccessWithFileExtension(): void
127
    {
128
        $io     = new NullIO();
129
        $config = new Config(CH_PATH_FILES . '/captainhook.json');
130
        $action = new Config\Action(DoesNotContainRegex::class, [
131
            'regex' => '#.#',
132
            'fileExtensions' => ['php']
133
        ]);
134
        $index  = $this->createGitIndexOperator([
135
            CH_PATH_FILES . '/storage/regextest1.txt'
136
        ]);
137
        $index->method('getStagedFilesOfType')->willReturnCallback(function ($ext) {
1 ignored issue
show
Bug introduced by
The method method() does not exist on SebastianFeldmann\Git\Operator\Index. ( Ignorable by Annotation )

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

137
        $index->/** @scrutinizer ignore-call */ 
138
                method('getStagedFilesOfType')->willReturnCallback(function ($ext) {

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...
138
            if ($ext === 'txt') {
139
                return [
140
                    CH_PATH_FILES . '/storage/regextest1.txt'
141
                ];
142
            }
143
            return [];
144
        });
145
        $repo = $this->createRepositoryMock();
146
        $repo->method('getIndexOperator')->willReturn($index);
147
148
        $standard = new DoesNotContainRegex();
149
        $standard->execute($config, $io, $repo, $action);
150
151
        $this->assertTrue(true);
152
    }
153
154
    /**
155
     * Tests DoesNotContainRegex::execute
156
     *
157
     * @throws \Exception
158
     */
159
    public function testExecuteFailureWithFileExtension(): void
160
    {
161
        $this->expectException(Exception::class);
162
        $this->expectExceptionMessage('<error>Regex \'#foo#\' failed:</error> 1 matches in 1 files');
163
164
        $io     = new NullIO();
165
        $config = new Config(CH_PATH_FILES . '/captainhook.json');
166
        $action = new Config\Action(DoesNotContainRegex::class, [
167
            'regex' => '#foo#',
168
            'fileExtensions' => ['txt']
169
        ]);
170
        $index  = $this->createGitIndexOperator([
171
            CH_PATH_FILES . '/storage/regextest1.txt'
172
        ]);
173
        $index->method('getStagedFilesOfType')->willReturnCallback(function ($ext) {
174
            if ($ext === 'txt') {
175
                return [
176
                    CH_PATH_FILES . '/storage/regextest1.txt'
177
                ];
178
            }
179
            return [];
180
        });
181
        $repo   = $this->createRepositoryMock();
182
        $repo->method('getIndexOperator')->willReturn($index);
183
184
        $standard = new DoesNotContainRegex();
185
        $standard->execute($config, $io, $repo, $action);
186
    }
187
}
188