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\Hook\Message\Action\Prepare; |
19
|
|
|
use CaptainHook\App\Mockery as CHMockery; |
20
|
|
|
use Exception; |
21
|
|
|
use PHPUnit\Framework\TestCase; |
22
|
|
|
use SebastianFeldmann\Git\Repository; |
23
|
|
|
|
24
|
|
|
class PrepareCommitMsgTest extends TestCase |
25
|
|
|
{ |
26
|
|
|
use ConfigMockery; |
27
|
|
|
use IOMockery; |
28
|
|
|
use CHMockery; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Tests PrepareCommitMsg::run |
32
|
|
|
* |
33
|
|
|
* @throws \Exception |
34
|
|
|
*/ |
35
|
|
|
public function testRunHookEnabled(): void |
36
|
|
|
{ |
37
|
|
|
$io = $this->createIOMock(); |
38
|
|
|
$config = $this->createConfigMock(); |
39
|
|
|
$hookConfig = $this->createHookConfigMock(); |
40
|
|
|
$actionConfig = $this->createActionConfigMock(); |
41
|
|
|
$actionConfig->method('getAction')->willReturn('\\' . Prepare::class); |
42
|
|
|
$actionConfig->method('getOptions')->willReturn(new Config\Options(['message' => 'Prepared commit msg'])); |
43
|
|
|
$hookConfig->method('isEnabled')->willReturn(true); |
44
|
|
|
$hookConfig->expects($this->once())->method('getActions')->willReturn([$actionConfig]); |
45
|
|
|
$config->expects($this->once())->method('getHookConfigToExecute')->willReturn($hookConfig); |
46
|
|
|
$config->expects($this->atLeastOnce())->method('isHookEnabled')->willReturn(true); |
47
|
|
|
|
48
|
|
|
$configOp = $this->createGitConfigOperator(); |
49
|
|
|
$configOp->method('getSettingSafely')->willReturn('#'); |
50
|
|
|
// setup fake vfs repos directory |
51
|
|
|
$dummy = new DummyRepo(['config' => '#config', 'hooks' => ['prepare-commit-msg' => '# hook script']]); |
52
|
|
|
$repo = new Repository($dummy->getRoot()); |
53
|
|
|
|
54
|
|
|
$commitMessageFile = $dummy->getGitDir() . '/prepare-commit-msg'; |
55
|
|
|
file_put_contents($commitMessageFile, 'Commit Message'); |
56
|
|
|
|
57
|
|
|
$io->expects($this->atLeast(1))->method('write'); |
58
|
|
|
$io->expects($this->exactly(3))->method('getArgument')->willReturnOnConsecutiveCalls( |
59
|
|
|
$commitMessageFile, |
60
|
|
|
'', |
61
|
|
|
'' |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
$runner = new PrepareCommitMsg($io, $config, $repo); |
65
|
|
|
$runner->run(); |
66
|
|
|
$this->assertEquals('Prepared commit msg', file_get_contents($commitMessageFile)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Tests PrepareCommitMsg::run |
71
|
|
|
* |
72
|
|
|
* @throws \Exception |
73
|
|
|
*/ |
74
|
|
|
public function testRunHookNoMessageException(): void |
75
|
|
|
{ |
76
|
|
|
$this->expectException(Exception::class); |
77
|
|
|
|
78
|
|
|
$dummy = new DummyRepo(['hooks' => ['prepare-commit-msg' => '# hook script']]); |
79
|
|
|
$repo = $this->createRepositoryMock($dummy->getRoot()); |
80
|
|
|
$repo->method('getHooksDir')->willReturn($dummy->getHookDir()); |
81
|
|
|
|
82
|
|
|
$configOp = $this->createGitConfigOperator(); |
83
|
|
|
$configOp->method('getSettingSafely')->willReturn('#'); |
84
|
|
|
$repo->method('getConfigOperator')->willReturn($configOp); |
85
|
|
|
|
86
|
|
|
$io = $this->createIOMock(); |
87
|
|
|
$config = $this->createConfigMock(); |
88
|
|
|
$actionConfig = $this->createActionConfigMock(); |
89
|
|
|
$actionConfig->method('getAction')->willReturn(Prepare::class); |
90
|
|
|
$actionConfig->method('getOptions')->willReturn(new Config\Options(['message' => 'Prepared commit msg'])); |
91
|
|
|
$config->expects($this->atLeastOnce())->method('isHookEnabled')->willReturn(true); |
92
|
|
|
$io->expects($this->exactly(3))->method('getArgument')->willReturn(''); |
93
|
|
|
|
94
|
|
|
$runner = new PrepareCommitMsg($io, $config, $repo); |
95
|
|
|
$runner->run(); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|