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 as GitMockery; |
17
|
|
|
use CaptainHook\App\Console\IO\Mockery as IOMockery; |
18
|
|
|
use PHPUnit\Framework\TestCase; |
19
|
|
|
|
20
|
|
|
class IsNotEmptyTest extends TestCase |
21
|
|
|
{ |
22
|
|
|
use GitMockery; |
23
|
|
|
use IOMockery; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Tests IsNotEmpty::getRestriction |
27
|
|
|
*/ |
28
|
|
|
public function testRestrictionValid(): void |
29
|
|
|
{ |
30
|
|
|
$restriction = IsNotEmpty::getRestriction(); |
31
|
|
|
$this->assertTrue($restriction->isApplicableFor('pre-commit')); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Tests IsNotEmpty::getRestriction |
36
|
|
|
*/ |
37
|
|
|
public function testRestrictionInvalid(): void |
38
|
|
|
{ |
39
|
|
|
$restriction = IsNotEmpty::getRestriction(); |
40
|
|
|
$this->assertFalse($restriction->isApplicableFor('pre-push')); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Tests IsNotEmpty::execute |
45
|
|
|
* |
46
|
|
|
* @throws \Exception |
47
|
|
|
*/ |
48
|
|
|
public function testCommitNotEmptyFile(): void |
49
|
|
|
{ |
50
|
|
|
|
51
|
|
|
$io = new NullIO(); |
52
|
|
|
$config = new Config(CH_PATH_FILES . '/captainhook.json'); |
53
|
|
|
$action = new Config\Action( |
54
|
|
|
IsNotEmpty::class, |
55
|
|
|
[ |
56
|
|
|
'files' => [ |
57
|
|
|
CH_PATH_FILES . '/storage/test.json' |
58
|
|
|
] |
59
|
|
|
] |
60
|
|
|
); |
61
|
|
|
|
62
|
|
|
$stagedFiles = [CH_PATH_FILES . '/storage/test.json']; |
63
|
|
|
$repo = $this->createRepositoryMock(); |
64
|
|
|
$repo->method('getIndexOperator')->willReturn($this->createGitIndexOperator($stagedFiles)); |
|
|
|
|
65
|
|
|
|
66
|
|
|
$isNotEmpty = new IsNotEmpty(); |
67
|
|
|
$isNotEmpty->execute($config, $io, $repo, $action); |
68
|
|
|
|
69
|
|
|
// no error should happen |
70
|
|
|
$this->assertTrue(true); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Tests IsNotEmpty::execute |
75
|
|
|
* |
76
|
|
|
* @throws \Exception |
77
|
|
|
*/ |
78
|
|
|
public function testConfigWithGlobs(): void |
79
|
|
|
{ |
80
|
|
|
$io = $this->createIOMock(); |
81
|
|
|
$config = new Config(CH_PATH_FILES . '/captainhook.json'); |
82
|
|
|
$action = new Config\Action( |
83
|
|
|
IsNotEmpty::class, |
84
|
|
|
[ |
85
|
|
|
'files' => [ |
86
|
|
|
CH_PATH_FILES . '/storage/*.txt', |
87
|
|
|
CH_PATH_FILES . '/storage/*.json', |
88
|
|
|
] |
89
|
|
|
] |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
// with this configuration the Captain should find 4 files for 2 patterns |
93
|
|
|
$io->expects($this->exactly(3))->method('write'); |
94
|
|
|
|
95
|
|
|
// two of those files should be in the commit |
96
|
|
|
$stagedFiles = [CH_PATH_FILES . '/storage/regextest1.txt', CH_PATH_FILES . '/storage/test.json']; |
97
|
|
|
$repo = $this->createRepositoryMock(); |
98
|
|
|
$repo->method('getIndexOperator')->willReturn($this->createGitIndexOperator($stagedFiles)); |
99
|
|
|
|
100
|
|
|
$isNotEmpty = new IsNotEmpty(); |
101
|
|
|
$isNotEmpty->execute($config, $io, $repo, $action); |
102
|
|
|
|
103
|
|
|
// no error should happen |
104
|
|
|
$this->assertTrue(true); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Tests IsNotEmpty::execute |
109
|
|
|
* |
110
|
|
|
* @throws \Exception |
111
|
|
|
*/ |
112
|
|
|
public function testFailCommitEmptyFile(): void |
113
|
|
|
{ |
114
|
|
|
$this->expectException(\Exception::class); |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
$io = new NullIO(); |
118
|
|
|
$config = new Config(CH_PATH_FILES . '/captainhook.json'); |
119
|
|
|
$action = new Config\Action( |
120
|
|
|
IsNotEmpty::class, |
121
|
|
|
[ |
122
|
|
|
'files' => [ |
123
|
|
|
CH_PATH_FILES . '/storage/empty.log', |
124
|
|
|
] |
125
|
|
|
] |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
$stagedFiles = [CH_PATH_FILES . '/storage/empty.log']; |
129
|
|
|
$repo = $this->createRepositoryMock(); |
130
|
|
|
$repo->method('getIndexOperator')->willReturn($this->createGitIndexOperator($stagedFiles)); |
131
|
|
|
|
132
|
|
|
$isNotEmpty = new IsNotEmpty(); |
133
|
|
|
$isNotEmpty->execute($config, $io, $repo, $action); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
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.