|
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\Runner; |
|
13
|
|
|
|
|
14
|
|
|
use CaptainHook\App\Hook\Condition\FileChanged\Any; |
|
15
|
|
|
use Exception; |
|
16
|
|
|
use PHPUnit\Framework\TestCase; |
|
17
|
|
|
use CaptainHook\App\Config; |
|
18
|
|
|
use CaptainHook\App\Console\IO\Mockery as IOMockery; |
|
19
|
|
|
use CaptainHook\App\Mockery as CHMockery; |
|
20
|
|
|
|
|
21
|
|
|
class ConditionTest extends TestCase |
|
22
|
|
|
{ |
|
23
|
|
|
use IOMockery; |
|
24
|
|
|
use CHMockery; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Tests Condition::doesConditionApply |
|
28
|
|
|
*/ |
|
29
|
|
|
public function testPHPConditionApply(): void |
|
30
|
|
|
{ |
|
31
|
|
|
$io = $this->createIOMock(); |
|
32
|
|
|
$io->expects($this->exactly(2))->method('getArgument')->willReturn(''); |
|
33
|
|
|
|
|
34
|
|
|
$operator = $this->createGitDiffOperator(['fiz.php', 'baz.php', 'foo.php']); |
|
35
|
|
|
$repository = $this->createRepositoryMock(''); |
|
36
|
|
|
$repository->expects($this->once())->method('getDiffOperator')->willReturn($operator); |
|
37
|
|
|
|
|
38
|
|
|
$conditionConfig = new Config\Condition( |
|
39
|
|
|
'\\' . Any::class, |
|
40
|
|
|
[ |
|
41
|
|
|
['foo.php', 'bar.php'] |
|
42
|
|
|
] |
|
43
|
|
|
); |
|
44
|
|
|
|
|
45
|
|
|
$runner = new Condition($io, $repository, 'post-checkout'); |
|
46
|
|
|
$this->assertTrue($runner->doesConditionApply($conditionConfig)); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Tests Condition::doesConditionApply |
|
51
|
|
|
*/ |
|
52
|
|
|
public function testConditionNotExecutedDueToConstraint(): void |
|
53
|
|
|
{ |
|
54
|
|
|
$io = $this->createIOMock(); |
|
55
|
|
|
$repository = $this->createRepositoryMock(''); |
|
56
|
|
|
$repository->expects($this->never())->method('getDiffOperator'); |
|
57
|
|
|
|
|
58
|
|
|
$conditionConfig = new Config\Condition( |
|
59
|
|
|
'\\' . Any::class, |
|
60
|
|
|
[ |
|
61
|
|
|
['foo.php', 'bar.php'] |
|
62
|
|
|
] |
|
63
|
|
|
); |
|
64
|
|
|
|
|
65
|
|
|
$runner = new Condition($io, $repository, 'pre-commit'); |
|
66
|
|
|
$this->assertTrue($runner->doesConditionApply($conditionConfig)); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Test Condition::doesConditionApply |
|
71
|
|
|
*/ |
|
72
|
|
|
public function testClassNotFound(): void |
|
73
|
|
|
{ |
|
74
|
|
|
$this->expectException(Exception::class); |
|
75
|
|
|
|
|
76
|
|
|
$conditionConfig = new Config\Condition('\\NotFoundForSure', []); |
|
77
|
|
|
|
|
78
|
|
|
$runner = new Condition($this->createIOMock(), $this->createRepositoryMock(), 'pre-commit'); |
|
79
|
|
|
$runner->doesConditionApply($conditionConfig); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Test Condition::doesConditionApply |
|
84
|
|
|
*/ |
|
85
|
|
|
public function testDoesConditionApplyCli(): void |
|
86
|
|
|
{ |
|
87
|
|
|
if (\defined('PHP_WINDOWS_VERSION_MAJOR')) { |
|
88
|
|
|
$this->markTestSkipped('not tested on windows'); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$io = $this->createIOMock(); |
|
92
|
|
|
$repository = $this->createRepositoryMock(''); |
|
93
|
|
|
|
|
94
|
|
|
$conditionConfig = new Config\Condition(CH_PATH_FILES . '/bin/phpunit'); |
|
95
|
|
|
|
|
96
|
|
|
$runner = new Condition($io, $repository, 'pre-commit'); |
|
97
|
|
|
$this->assertTrue($runner->doesConditionApply($conditionConfig)); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|