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\Message\Action; |
13
|
|
|
|
14
|
|
|
use CaptainHook\App\Config; |
15
|
|
|
use CaptainHook\App\Console\IO\NullIO; |
16
|
|
|
use CaptainHook\App\Mockery; |
17
|
|
|
use Exception; |
18
|
|
|
use SebastianFeldmann\Git\CommitMessage; |
19
|
|
|
use PHPUnit\Framework\TestCase; |
20
|
|
|
|
21
|
|
|
class RegexTest extends TestCase |
22
|
|
|
{ |
23
|
|
|
use Mockery; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Tests Regex::getRestriction |
27
|
|
|
*/ |
28
|
|
|
public function testConstraint(): void |
29
|
|
|
{ |
30
|
|
|
$this->assertTrue(Regex::getRestriction()->isApplicableFor('commit-msg')); |
31
|
|
|
$this->assertFalse(Regex::getRestriction()->isApplicableFor('pre-push')); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Tests RegexCheck::execute |
36
|
|
|
* |
37
|
|
|
* @throws \Exception |
38
|
|
|
*/ |
39
|
|
|
public function testExecuteDefaultSuccess(): void |
40
|
|
|
{ |
41
|
|
|
$io = $this->createPartialMock(NullIO::class, ['write']); |
42
|
|
|
$io->expects($this->once())->method('write')->with('Found matching pattern: bar'); |
43
|
|
|
/** @var NullIO $io */ |
44
|
|
|
|
45
|
|
|
$config = new Config(CH_PATH_FILES . '/captainhook.json'); |
46
|
|
|
$repo = $this->createRepositoryMock(); |
47
|
|
|
$action = new Config\Action(Regex::class, ['regex' => '#bar#']); |
48
|
|
|
$repo->expects($this->once())->method('getCommitMsg')->willReturn(new CommitMessage('Foo bar baz')); |
49
|
|
|
|
50
|
|
|
$standard = new Regex(); |
51
|
|
|
$standard->execute($config, $io, $repo, $action); |
52
|
|
|
|
53
|
|
|
$this->assertTrue(true); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Tests RegexCheck::execute |
58
|
|
|
* |
59
|
|
|
* @throws \Exception |
60
|
|
|
*/ |
61
|
|
|
public function testExecuteCustomSuccess(): void |
62
|
|
|
{ |
63
|
|
|
$successMessage = 'Regex matched'; |
64
|
|
|
$io = $this->createPartialMock(NullIO::class, ['write']); |
65
|
|
|
$io->expects($this->once())->method('write')->with($successMessage); |
66
|
|
|
/** @var NullIO $io */ |
67
|
|
|
|
68
|
|
|
$config = new Config(CH_PATH_FILES . '/captainhook.json'); |
69
|
|
|
$repo = $this->createRepositoryMock(); |
70
|
|
|
$action = new Config\Action( |
71
|
|
|
Regex::class, |
72
|
|
|
[ |
73
|
|
|
'regex' => '#.*#', |
74
|
|
|
'success' => $successMessage |
75
|
|
|
] |
76
|
|
|
); |
77
|
|
|
$repo->expects($this->once())->method('getCommitMsg')->willReturn(new CommitMessage('Foo bar baz')); |
78
|
|
|
|
79
|
|
|
$standard = new Regex(); |
80
|
|
|
$standard->execute($config, $io, $repo, $action); |
81
|
|
|
|
82
|
|
|
$this->assertTrue(true); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Tests RegexCheck::execute |
87
|
|
|
* |
88
|
|
|
* @throws \Exception |
89
|
|
|
*/ |
90
|
|
|
public function testExecuteInvalidOption(): void |
91
|
|
|
{ |
92
|
|
|
$this->expectException(Exception::class); |
93
|
|
|
|
94
|
|
|
$io = new NullIO(); |
95
|
|
|
$config = new Config(CH_PATH_FILES . '/captainhook.json'); |
96
|
|
|
$repo = $this->createRepositoryMock(); |
97
|
|
|
$action = new Config\Action(Regex::class); |
98
|
|
|
|
99
|
|
|
$standard = new Regex(); |
100
|
|
|
$standard->execute($config, $io, $repo, $action); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Tests RegexCheck::execute |
106
|
|
|
* |
107
|
|
|
* @throws \Exception |
108
|
|
|
*/ |
109
|
|
|
public function testMerging(): void |
110
|
|
|
{ |
111
|
|
|
$io = new NullIO(); |
112
|
|
|
$config = new Config(CH_PATH_FILES . '/captainhook.json'); |
113
|
|
|
$repo = $this->createRepositoryMock(); |
114
|
|
|
$repo->expects($this->once())->method('isMerging')->willReturn(true); |
115
|
|
|
$action = new Config\Action(Regex::class, ['regex' => '#.*#']); |
116
|
|
|
|
117
|
|
|
$standard = new Regex(); |
118
|
|
|
$standard->execute($config, $io, $repo, $action); |
119
|
|
|
|
120
|
|
|
$this->assertTrue(true, 'Since we are merging nothing should happen'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Tests RegexCheck::execute |
125
|
|
|
* |
126
|
|
|
* @throws \Exception |
127
|
|
|
*/ |
128
|
|
|
public function testExecuteNoMatchCustomErrorMessage(): void |
129
|
|
|
{ |
130
|
|
|
$this->expectException(Exception::class); |
131
|
|
|
$this->expectExceptionMessage('No match for #FooBarBaz#'); |
132
|
|
|
|
133
|
|
|
$io = new NullIO(); |
134
|
|
|
$config = new Config(CH_PATH_FILES . '/captainhook.json'); |
135
|
|
|
$action = new Config\Action( |
136
|
|
|
Regex::class, |
137
|
|
|
[ |
138
|
|
|
'regex' => '#FooBarBaz#', |
139
|
|
|
'error' => 'No match for %s' |
140
|
|
|
] |
141
|
|
|
); |
142
|
|
|
$repo = $this->createRepositoryMock(); |
143
|
|
|
$repo->expects($this->once())->method('getCommitMsg')->willReturn(new CommitMessage('Foo bar baz')); |
144
|
|
|
|
145
|
|
|
$standard = new Regex(); |
146
|
|
|
$standard->execute($config, $io, $repo, $action); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|