|
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\Condition\FileChanged; |
|
13
|
|
|
|
|
14
|
|
|
use CaptainHook\App\Console\IO\Mockery as IOMockery; |
|
15
|
|
|
use CaptainHook\App\Mockery as CHMockery; |
|
16
|
|
|
use PHPUnit\Framework\TestCase; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class OfTypeTest |
|
20
|
|
|
* |
|
21
|
|
|
* @package CaptainHook |
|
22
|
|
|
* @author Sebastian Feldmann <[email protected]> |
|
23
|
|
|
* @link https://github.com/captainhook-git/captainhook |
|
24
|
|
|
* @since Class available since Release 5.0.0 |
|
25
|
|
|
*/ |
|
26
|
|
|
class OfTypeTest extends TestCase |
|
27
|
|
|
{ |
|
28
|
|
|
use IOMockery; |
|
29
|
|
|
use CHMockery; |
|
30
|
|
|
|
|
31
|
|
|
public function testRestriction(): void |
|
32
|
|
|
{ |
|
33
|
|
|
$this->assertTrue(OfType::getRestriction()->isApplicableFor('pre-push')); |
|
34
|
|
|
$this->assertFalse(OfType::getRestriction()->isApplicableFor('pre-commit')); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function testIsTrue(): void |
|
38
|
|
|
{ |
|
39
|
|
|
$io = $this->createIOMock(); |
|
40
|
|
|
$io->method('getArgument')->willReturn('hook:pre-push'); |
|
41
|
|
|
$io->expects($this->atLeastOnce()) |
|
42
|
|
|
->method('getStandardInput') |
|
43
|
|
|
->willReturn( |
|
44
|
|
|
[ |
|
45
|
|
|
'refs/heads/main 9dfa0fa6221d75f48b2dfac359127324bedf8409' . |
|
46
|
|
|
' refs/heads/main 8309f6e16097754469c485e604900c573bf2c5d8' |
|
47
|
|
|
] |
|
48
|
|
|
); |
|
49
|
|
|
$operator = $this->createGitDiffOperator(['fiz.php', 'foo.txt']); |
|
50
|
|
|
$repository = $this->createRepositoryMock(''); |
|
51
|
|
|
$repository->expects($this->once())->method('getDiffOperator')->willReturn($operator); |
|
52
|
|
|
|
|
53
|
|
|
$fileChange = new OfType('php'); |
|
54
|
|
|
$this->assertTrue($fileChange->isTrue($io, $repository)); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function testChangedFileButNoneOfType(): void |
|
58
|
|
|
{ |
|
59
|
|
|
$io = $this->createIOMock(); |
|
60
|
|
|
$io->method('isVerbose')->willReturn(true); |
|
61
|
|
|
$io->method('getArgument')->willReturn('hook:pre-push'); |
|
62
|
|
|
$io->expects($this->atLeastOnce()) |
|
63
|
|
|
->method('getStandardInput') |
|
64
|
|
|
->willReturn( |
|
65
|
|
|
[ |
|
66
|
|
|
'refs/heads/main 9dfa0fa6221d75f48b2dfac359127324bedf8409' . |
|
67
|
|
|
' refs/heads/main 8309f6e16097754469c485e604900c573bf2c5d8' |
|
68
|
|
|
] |
|
69
|
|
|
); |
|
70
|
|
|
$operator = $this->createGitDiffOperator(['fiz.txt', 'foo.txt']); |
|
71
|
|
|
$repository = $this->createRepositoryMock(''); |
|
72
|
|
|
$repository->expects($this->once())->method('getDiffOperator')->willReturn($operator); |
|
73
|
|
|
|
|
74
|
|
|
$fileChange = new OfType('php'); |
|
75
|
|
|
$this->assertFalse($fileChange->isTrue($io, $repository)); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function testIsZeroHash(): void |
|
79
|
|
|
{ |
|
80
|
|
|
$io = $this->createIOMock(); |
|
81
|
|
|
$io->method('getArgument')->willReturn('hook:pre-push'); |
|
82
|
|
|
$io->expects($this->atLeastOnce()) |
|
83
|
|
|
->method('getStandardInput') |
|
84
|
|
|
->willReturn( |
|
85
|
|
|
[ |
|
86
|
|
|
'refs/heads/main 9dfa0fa6221d75f48b2dfac359127324bedf8409' . |
|
87
|
|
|
' refs/heads/main 0000000000000000000000000000000000000000' |
|
88
|
|
|
] |
|
89
|
|
|
); |
|
90
|
|
|
|
|
91
|
|
|
$repository = $this->createRepositoryMock(''); |
|
92
|
|
|
|
|
93
|
|
|
$fileChange = new OfType('php'); |
|
94
|
|
|
$this->assertFalse($fileChange->isTrue($io, $repository)); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function testIsFalse(): void |
|
98
|
|
|
{ |
|
99
|
|
|
$io = $this->createIOMock(); |
|
100
|
|
|
$io->method('getArgument')->willReturn('hook:pre-push'); |
|
101
|
|
|
$io->expects($this->atLeastOnce()) |
|
102
|
|
|
->method('getStandardInput') |
|
103
|
|
|
->willReturn( |
|
104
|
|
|
[ |
|
105
|
|
|
'refs/heads/main 9dfa0fa6221d75f48b2dfac359127324bedf8409' . |
|
106
|
|
|
' refs/heads/main 8309f6e16097754469c485e604900c573bf2c5d8' |
|
107
|
|
|
] |
|
108
|
|
|
); |
|
109
|
|
|
$operator = $this->createGitDiffOperator(); |
|
110
|
|
|
$repository = $this->createRepositoryMock(''); |
|
111
|
|
|
$operator->method('getChangedFilesOfType')->willReturn([]); |
|
112
|
|
|
$repository->expects($this->once())->method('getDiffOperator')->willReturn($operator); |
|
113
|
|
|
|
|
114
|
|
|
$fileChange = new OfType('php'); |
|
115
|
|
|
$this->assertFalse($fileChange->isTrue($io, $repository)); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|