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\Hook; |
13
|
|
|
|
14
|
|
|
use CaptainHook\App\Config; |
15
|
|
|
use CaptainHook\App\Config\Mockery as ConfigMockery; |
16
|
|
|
use CaptainHook\App\Console\IO\Mockery as IOMockery; |
17
|
|
|
use CaptainHook\App\Git\DummyRepo; |
18
|
|
|
use CaptainHook\App\Mockery as CHMockery; |
19
|
|
|
use PHPUnit\Framework\TestCase; |
20
|
|
|
|
21
|
|
|
class PostMergeTest extends TestCase |
22
|
|
|
{ |
23
|
|
|
use ConfigMockery; |
24
|
|
|
use IOMockery; |
25
|
|
|
use CHMockery; |
26
|
|
|
|
27
|
|
|
public function testRunHookEnabled(): void |
28
|
|
|
{ |
29
|
|
|
if (defined('PHP_WINDOWS_VERSION_MAJOR')) { |
30
|
|
|
$this->markTestSkipped('not tested on windows'); |
31
|
|
|
} |
32
|
|
|
$dummy = new DummyRepo(['hooks' => ['post-merge' => '# hook script']]); |
33
|
|
|
$repo = $this->createRepositoryMock($dummy->getRoot()); |
34
|
|
|
$repo->method('getHooksDir')->willReturn($dummy->getHookDir()); |
35
|
|
|
|
36
|
|
|
$io = $this->createIOMock(); |
37
|
|
|
$config = $this->createConfigMock(); |
38
|
|
|
$hookConfig = $this->createHookConfigMock(); |
39
|
|
|
$actionConfig = $this->createActionConfigMock(); |
40
|
|
|
$hookConfig->method('isEnabled')->willReturn(true); |
41
|
|
|
$hookConfig->expects($this->once())->method('getActions')->willReturn([$actionConfig]); |
42
|
|
|
$config->expects($this->once())->method('getHookConfigToExecute')->willReturn($hookConfig); |
43
|
|
|
$config->expects($this->atLeastOnce())->method('isHookEnabled')->willReturn(true); |
44
|
|
|
$io->expects($this->atLeast(1))->method('write'); |
45
|
|
|
|
46
|
|
|
$runner = new PostMerge($io, $config, $repo); |
47
|
|
|
$runner->run(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testRunHookWithConditionsApply(): void |
51
|
|
|
{ |
52
|
|
|
$dummy = new DummyRepo(['hooks' => ['post-merge' => '# hook script']]); |
53
|
|
|
$repo = $this->createRepositoryMock($dummy->getRoot()); |
54
|
|
|
$repo->method('getHooksDir')->willReturn($dummy->getHookDir()); |
55
|
|
|
|
56
|
|
|
$io = $this->createIOMock(); |
57
|
|
|
$config = $this->createConfigMock(); |
58
|
|
|
$hookConfig = $this->createHookConfigMock(); |
59
|
|
|
$actionConfig = $this->createActionConfigMock(); |
60
|
|
|
$conditionConfig = new Config\Condition(CH_PATH_FILES . '/bin/success'); |
61
|
|
|
$actionConfig->expects($this->once())->method('getConditions')->willReturn([$conditionConfig]); |
62
|
|
|
$hookConfig->method('isEnabled')->willReturn(true); |
63
|
|
|
$hookConfig->expects($this->once())->method('getActions')->willReturn([$actionConfig]); |
64
|
|
|
$config->expects($this->once())->method('getHookConfigToExecute')->willReturn($hookConfig); |
65
|
|
|
$config->expects($this->atLeastOnce())->method('isHookEnabled')->willReturn(true); |
66
|
|
|
$io->expects($this->atLeast(1))->method('write'); |
67
|
|
|
|
68
|
|
|
$runner = new PostMerge($io, $config, $repo); |
69
|
|
|
$runner->run(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testRunHookWithConditionsFail() |
73
|
|
|
{ |
74
|
|
|
$dummy = new DummyRepo(['hooks' => ['post-merge' => '# hook script']]); |
75
|
|
|
$repo = $this->createRepositoryMock($dummy->getRoot()); |
76
|
|
|
$repo->method('getHooksDir')->willReturn($dummy->getHookDir()); |
77
|
|
|
|
78
|
|
|
$io = $this->createIOMock(); |
79
|
|
|
$config = $this->createConfigMock(); |
80
|
|
|
$hookConfig = $this->createHookConfigMock(); |
81
|
|
|
$actionConfig = $this->createActionConfigMock(); |
82
|
|
|
$conditionConfig = new Config\Condition(CH_PATH_FILES . '/bin/failure'); |
83
|
|
|
$actionConfig->expects($this->once())->method('getConditions')->willReturn([$conditionConfig]); |
84
|
|
|
$hookConfig->method('isEnabled')->willReturn(true); |
85
|
|
|
$hookConfig->expects($this->once())->method('getActions')->willReturn([$actionConfig]); |
86
|
|
|
$config->expects($this->once())->method('getHookConfigToExecute')->willReturn($hookConfig); |
87
|
|
|
$config->expects($this->atLeastOnce())->method('isHookEnabled')->willReturn(true); |
88
|
|
|
$io->expects($this->atLeast(1))->method('write'); |
89
|
|
|
|
90
|
|
|
$runner = new PostMerge($io, $config, $repo); |
91
|
|
|
$runner->run(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testRunHookDisabled() |
95
|
|
|
{ |
96
|
|
|
$io = $this->createIOMock(); |
97
|
|
|
$config = $this->createConfigMock(); |
98
|
|
|
$repo = $this->createRepositoryMock(); |
99
|
|
|
$config->expects($this->atLeastOnce())->method('isHookEnabled')->willReturn(false); |
100
|
|
|
$io->expects($this->atLeast(1))->method('write'); |
101
|
|
|
|
102
|
|
|
$runner = new PostMerge($io, $config, $repo); |
103
|
|
|
$runner->run(); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|