1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of CaptainHook. |
4
|
|
|
* |
5
|
|
|
* (c) Sebastian Feldmann <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace CaptainHook\App\Composer; |
11
|
|
|
|
12
|
|
|
use CaptainHook\App\CH; |
13
|
|
|
use CaptainHook\App\Git\DummyRepo; |
14
|
|
|
use Composer\Composer; |
15
|
|
|
use Composer\IO\NullIO; |
16
|
|
|
use Composer\Package\Package; |
17
|
|
|
use Composer\Script\Event; |
18
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
19
|
|
|
use PHPUnit\Framework\TestCase; |
20
|
|
|
|
21
|
|
|
class CmdTest extends TestCase |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Tests Cmd::setup |
25
|
|
|
*/ |
26
|
|
|
public function testSetupConfigExists(): void |
27
|
|
|
{ |
28
|
|
|
if (\defined('PHP_WINDOWS_VERSION_MAJOR')) { |
29
|
|
|
$this->markTestSkipped('not tested on windows'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$repo = new DummyRepo(); |
33
|
|
|
$repo->setup(); |
34
|
|
|
|
35
|
|
|
$config = $repo->getPath() . DIRECTORY_SEPARATOR . CH::CONFIG; |
36
|
|
|
$old = getcwd(); |
37
|
|
|
file_put_contents($config, '{}'); |
38
|
|
|
mkdir($repo->getPath() . DIRECTORY_SEPARATOR . 'vendor'); |
39
|
|
|
chdir($repo->getPath()); |
40
|
|
|
|
41
|
|
|
$event = $this->getEventMock([]); |
42
|
|
|
|
43
|
|
|
Cmd::setup($event); |
44
|
|
|
|
45
|
|
|
$this->assertFileExists($repo->getHookDir() . DIRECTORY_SEPARATOR . 'pre-commit', 'pre-commit'); |
46
|
|
|
$this->assertFileExists($repo->getHookDir() . DIRECTORY_SEPARATOR . 'pre-push', 'pre-push'); |
47
|
|
|
$this->assertFileExists($repo->getHookDir() . DIRECTORY_SEPARATOR . 'commit-msg', 'commit-msg'); |
48
|
|
|
|
49
|
|
|
$repo->cleanup(); |
50
|
|
|
chdir($old); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Tests Cmd::setup |
55
|
|
|
*/ |
56
|
|
|
public function testSetupNoConfig(): void |
57
|
|
|
{ |
58
|
|
|
if (\defined('PHP_WINDOWS_VERSION_MAJOR')) { |
59
|
|
|
$this->markTestSkipped('not tested on windows'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$repo = new DummyRepo(); |
63
|
|
|
$repo->setup(); |
64
|
|
|
|
65
|
|
|
mkdir($repo->getPath() . DIRECTORY_SEPARATOR . 'vendor'); |
66
|
|
|
|
67
|
|
|
$config = $repo->getPath() . DIRECTORY_SEPARATOR . CH::CONFIG; |
68
|
|
|
$old = getcwd(); |
69
|
|
|
chdir($repo->getPath()); |
70
|
|
|
|
71
|
|
|
$extra = ['captainhook-config' => $config]; |
72
|
|
|
$event = $this->getEventMock($extra); |
73
|
|
|
Cmd::setup($event); |
74
|
|
|
|
75
|
|
|
$this->assertFileExists($extra['captainhook-config']); |
76
|
|
|
|
77
|
|
|
$repo->cleanup(); |
78
|
|
|
chdir($old); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Create event mock to test composer scripts |
83
|
|
|
* |
84
|
|
|
* @param array $extra |
85
|
|
|
* @return \Composer\Script\Event&MockObject |
86
|
|
|
*/ |
87
|
|
|
private function getEventMock(array $extra = []) |
88
|
|
|
{ |
89
|
|
|
$composer = $this->getComposerMock($extra); |
90
|
|
|
$event = $this->getMockBuilder(Event::class) |
91
|
|
|
->disableOriginalConstructor() |
92
|
|
|
->getMock(); |
93
|
|
|
|
94
|
|
|
$event->expects($this->once())->method('getIO')->willReturn(new NullIO()); |
95
|
|
|
$event->expects($this->once())->method('getComposer')->willReturn($composer); |
96
|
|
|
|
97
|
|
|
return $event; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Create composer mock to return composer extra config |
102
|
|
|
* |
103
|
|
|
* @param array $extra |
104
|
|
|
* @return \Composer\Composer&MockObject |
105
|
|
|
*/ |
106
|
|
|
private function getComposerMock(array $extra = []) |
107
|
|
|
{ |
108
|
|
|
$package = $this->getMockBuilder(Package::class) |
109
|
|
|
->disableOriginalConstructor() |
110
|
|
|
->getMock(); |
111
|
|
|
$package->expects($this->atLeast(1))->method('getExtra')->willReturn($extra); |
112
|
|
|
|
113
|
|
|
$composer = $this->getMockBuilder(Composer::class) |
114
|
|
|
->disableOriginalConstructor() |
115
|
|
|
->getMock(); |
116
|
|
|
$composer->expects($this->atLeast(1))->method('getPackage')->willReturn($package); |
117
|
|
|
|
118
|
|
|
return $composer; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|