|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of CaptainHook. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Sebastian Feldmann <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace CaptainHook\App\Hook\Message\Action; |
|
11
|
|
|
|
|
12
|
|
|
use CaptainHook\App\Config; |
|
13
|
|
|
use CaptainHook\App\Console\IO\NullIO; |
|
14
|
|
|
use CaptainHook\App\Git\DummyRepo; |
|
15
|
|
|
use CaptainHook\App\Mockery; |
|
16
|
|
|
use SebastianFeldmann\Git\CommitMessage; |
|
17
|
|
|
use SebastianFeldmann\Git\Repository; |
|
18
|
|
|
use PHPUnit\Framework\TestCase; |
|
19
|
|
|
|
|
20
|
|
|
class RegexTest extends TestCase |
|
21
|
|
|
{ |
|
22
|
|
|
use Mockery; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Tests RegexCheck::execute |
|
26
|
|
|
*/ |
|
27
|
|
|
public function testExecuteDefaultSuccess(): void |
|
28
|
|
|
{ |
|
29
|
|
|
$io = $this->createPartialMock(NullIO::class, ['write']); |
|
30
|
|
|
$io->expects($this->once())->method('write')->with('Found matching pattern: bar'); |
|
31
|
|
|
/** @var NullIO $io */ |
|
32
|
|
|
|
|
33
|
|
|
$config = new Config(CH_PATH_FILES . '/captainhook.json'); |
|
34
|
|
|
$repo = $this->createRepositoryMock(); |
|
35
|
|
|
$action = new Config\Action(Regex::class, ['regex' => '#bar#']); |
|
36
|
|
|
$repo->expects($this->once())->method('getCommitMsg')->willReturn(new CommitMessage('Foo bar baz')); |
|
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
$standard = new Regex(); |
|
39
|
|
|
$standard->execute($config, $io, $repo, $action); |
|
40
|
|
|
|
|
41
|
|
|
$this->assertTrue(true); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Tests RegexCheck::execute |
|
46
|
|
|
*/ |
|
47
|
|
|
public function testExecuteCustomSuccess(): void |
|
48
|
|
|
{ |
|
49
|
|
|
$successMessage = 'Regex matched'; |
|
50
|
|
|
$io = $this->createPartialMock(NullIO::class, ['write']); |
|
51
|
|
|
$io->expects($this->once())->method('write')->with($successMessage); |
|
52
|
|
|
/** @var NullIO $io */ |
|
53
|
|
|
|
|
54
|
|
|
$config = new Config(CH_PATH_FILES . '/captainhook.json'); |
|
55
|
|
|
$repo = $this->createRepositoryMock(); |
|
56
|
|
|
$action = new Config\Action( |
|
57
|
|
|
Regex::class, |
|
58
|
|
|
[ |
|
59
|
|
|
'regex' => '#.*#', |
|
60
|
|
|
'success' => $successMessage |
|
61
|
|
|
] |
|
62
|
|
|
); |
|
63
|
|
|
$repo->expects($this->once())->method('getCommitMsg')->willReturn(new CommitMessage('Foo bar baz')); |
|
64
|
|
|
|
|
65
|
|
|
$standard = new Regex(); |
|
66
|
|
|
$standard->execute($config, $io, $repo, $action); |
|
67
|
|
|
|
|
68
|
|
|
$this->assertTrue(true); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Tests RegexCheck::execute |
|
73
|
|
|
*/ |
|
74
|
|
|
public function testExecuteInvalidOption(): void |
|
75
|
|
|
{ |
|
76
|
|
|
$this->expectException(\Exception::class); |
|
77
|
|
|
|
|
78
|
|
|
$io = new NullIO(); |
|
79
|
|
|
$config = new Config(CH_PATH_FILES . '/captainhook.json'); |
|
80
|
|
|
$repo = $this->createRepositoryMock(); |
|
81
|
|
|
$action = new Config\Action(Regex::class); |
|
82
|
|
|
|
|
83
|
|
|
$standard = new Regex(); |
|
84
|
|
|
$standard->execute($config, $io, $repo, $action); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Tests RegexCheck::execute |
|
90
|
|
|
*/ |
|
91
|
|
|
public function testMerging(): void |
|
92
|
|
|
{ |
|
93
|
|
|
$io = new NullIO(); |
|
94
|
|
|
$config = new Config(CH_PATH_FILES . '/captainhook.json'); |
|
95
|
|
|
$repo = $this->createRepositoryMock(); |
|
96
|
|
|
$repo->expects($this->once())->method('isMerging')->willReturn(true); |
|
97
|
|
|
$action = new Config\Action(Regex::class, ['regex' => '#.*#']); |
|
98
|
|
|
|
|
99
|
|
|
$standard = new Regex(); |
|
100
|
|
|
$standard->execute($config, $io, $repo, $action); |
|
101
|
|
|
|
|
102
|
|
|
$this->assertTrue(true, 'Since we are merging nothing should happen'); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Tests RegexCheck::execute |
|
107
|
|
|
*/ |
|
108
|
|
|
public function testExecuteNoMatchCustomErrorMessage(): void |
|
109
|
|
|
{ |
|
110
|
|
|
$this->expectException(\Exception::class); |
|
111
|
|
|
$this->expectExceptionMessage('No match for #FooBarBaz#'); |
|
112
|
|
|
|
|
113
|
|
|
$io = new NullIO(); |
|
114
|
|
|
$config = new Config(CH_PATH_FILES . '/captainhook.json'); |
|
115
|
|
|
$action = new Config\Action( |
|
116
|
|
|
Regex::class, |
|
117
|
|
|
[ |
|
118
|
|
|
'regex' => '#FooBarBaz#', |
|
119
|
|
|
'error' => 'No match for %s' |
|
120
|
|
|
] |
|
121
|
|
|
); |
|
122
|
|
|
$repo = $this->createRepositoryMock(); |
|
123
|
|
|
$repo->expects($this->once())->method('getCommitMsg')->willReturn(new CommitMessage('Foo bar baz')); |
|
124
|
|
|
|
|
125
|
|
|
$standard = new Regex(); |
|
126
|
|
|
$standard->execute($config, $io, $repo, $action); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
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.