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/captainhookphp/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
|
|
|
/** |
32
|
|
|
* Tests OfType::getRestriction |
33
|
|
|
*/ |
34
|
|
|
public function testRestriction(): void |
35
|
|
|
{ |
36
|
|
|
$this->assertTrue(OfType::getRestriction()->isApplicableFor('pre-push')); |
37
|
|
|
$this->assertFalse(OfType::getRestriction()->isApplicableFor('pre-commit')); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Tests OfType::isTrue |
42
|
|
|
*/ |
43
|
|
|
public function testIsTrue(): void |
44
|
|
|
{ |
45
|
|
|
$io = $this->createIOMock(); |
46
|
|
|
$io->method('getArgument')->willReturn('hook:pre-push'); |
47
|
|
|
$io->expects($this->atLeastOnce()) |
48
|
|
|
->method('getStandardInput') |
49
|
|
|
->willReturn( |
50
|
|
|
[ |
51
|
|
|
'refs/heads/main 9dfa0fa6221d75f48b2dfac359127324bedf8409' . |
52
|
|
|
' refs/heads/main 8309f6e16097754469c485e604900c573bf2c5d8' |
53
|
|
|
] |
54
|
|
|
); |
55
|
|
|
$operator = $this->createGitDiffOperator(['fiz.php', 'foo.txt']); |
56
|
|
|
$repository = $this->createRepositoryMock(''); |
57
|
|
|
$repository->expects($this->once())->method('getDiffOperator')->willReturn($operator); |
58
|
|
|
|
59
|
|
|
$fileChange = new OfType('php'); |
60
|
|
|
$this->assertTrue($fileChange->isTrue($io, $repository)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Tests OfType::isTrue |
65
|
|
|
*/ |
66
|
|
|
public function testChangedFileButNoneOfType(): void |
67
|
|
|
{ |
68
|
|
|
$io = $this->createIOMock(); |
69
|
|
|
$io->method('getArgument')->willReturn('hook:pre-push'); |
70
|
|
|
$io->expects($this->atLeastOnce()) |
71
|
|
|
->method('getStandardInput') |
72
|
|
|
->willReturn( |
73
|
|
|
[ |
74
|
|
|
'refs/heads/main 9dfa0fa6221d75f48b2dfac359127324bedf8409' . |
75
|
|
|
' refs/heads/main 8309f6e16097754469c485e604900c573bf2c5d8' |
76
|
|
|
] |
77
|
|
|
); |
78
|
|
|
$operator = $this->createGitDiffOperator(['fiz.txt', 'foo.txt']); |
79
|
|
|
$repository = $this->createRepositoryMock(''); |
80
|
|
|
$repository->expects($this->once())->method('getDiffOperator')->willReturn($operator); |
81
|
|
|
|
82
|
|
|
$fileChange = new OfType('php'); |
83
|
|
|
$this->assertFalse($fileChange->isTrue($io, $repository)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Tests OfType::isTrue |
89
|
|
|
*/ |
90
|
|
|
public function testIsZeroHash(): void |
91
|
|
|
{ |
92
|
|
|
$io = $this->createIOMock(); |
93
|
|
|
$io->method('getArgument')->willReturn('hook:pre-push'); |
94
|
|
|
$io->expects($this->atLeastOnce()) |
95
|
|
|
->method('getStandardInput') |
96
|
|
|
->willReturn( |
97
|
|
|
[ |
98
|
|
|
'refs/heads/main 9dfa0fa6221d75f48b2dfac359127324bedf8409' . |
99
|
|
|
' refs/heads/main 0000000000000000000000000000000000000000' |
100
|
|
|
] |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
$repository = $this->createRepositoryMock(''); |
104
|
|
|
|
105
|
|
|
$fileChange = new OfType('php'); |
106
|
|
|
$this->assertFalse($fileChange->isTrue($io, $repository)); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Tests OfType::isTrue |
111
|
|
|
*/ |
112
|
|
|
public function testIsFalse(): void |
113
|
|
|
{ |
114
|
|
|
$io = $this->createIOMock(); |
115
|
|
|
$io->method('getArgument')->willReturn('hook:pre-push'); |
116
|
|
|
$io->expects($this->atLeastOnce()) |
117
|
|
|
->method('getStandardInput') |
118
|
|
|
->willReturn( |
119
|
|
|
[ |
120
|
|
|
'refs/heads/main 9dfa0fa6221d75f48b2dfac359127324bedf8409' . |
121
|
|
|
' refs/heads/main 8309f6e16097754469c485e604900c573bf2c5d8' |
122
|
|
|
] |
123
|
|
|
); |
124
|
|
|
$operator = $this->createGitDiffOperator(); |
125
|
|
|
$repository = $this->createRepositoryMock(''); |
126
|
|
|
$operator->method('getChangedFilesOfType')->willReturn([]); |
127
|
|
|
$repository->expects($this->once())->method('getDiffOperator')->willReturn($operator); |
128
|
|
|
|
129
|
|
|
$fileChange = new OfType('php'); |
130
|
|
|
$this->assertFalse($fileChange->isTrue($io, $repository)); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|