|
1
|
|
|
<?php |
|
2
|
|
|
/* (c) Anton Medvedev <[email protected]> |
|
3
|
|
|
* |
|
4
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
5
|
|
|
* file that was distributed with this source code. |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Deployer; |
|
9
|
|
|
|
|
10
|
|
|
use Deployer\Configuration; |
|
11
|
|
|
use Deployer\Host\Host; |
|
12
|
|
|
use Deployer\Host\Localhost; |
|
13
|
|
|
use Deployer\Task\Context; |
|
14
|
|
|
use Deployer\Task\GroupTask; |
|
15
|
|
|
use Deployer\Task\Task; |
|
16
|
|
|
use PHPUnit\Framework\TestCase; |
|
17
|
|
|
use Symfony\Component\Console\Application; |
|
18
|
|
|
use Symfony\Component\Console\Input\Input; |
|
19
|
|
|
use Symfony\Component\Console\Output\Output; |
|
20
|
|
|
|
|
21
|
|
|
use function Deployer\localhost; |
|
22
|
|
|
|
|
23
|
|
|
class FunctionsTest extends TestCase |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var Deployer |
|
27
|
|
|
*/ |
|
28
|
|
|
private $deployer; |
|
29
|
|
|
|
|
30
|
|
|
protected function setUp(): void |
|
31
|
|
|
{ |
|
32
|
|
|
$console = new Application(); |
|
33
|
|
|
|
|
34
|
|
|
$input = $this->createMock(Input::class); |
|
35
|
|
|
$output = $this->createMock(Output::class); |
|
36
|
|
|
$host = new Localhost(); |
|
37
|
|
|
|
|
38
|
|
|
$this->deployer = new Deployer($console); |
|
39
|
|
|
$this->deployer['input'] = $input; |
|
40
|
|
|
$this->deployer['output'] = $output; |
|
41
|
|
|
Context::push(new Context($host)); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
protected function tearDown(): void |
|
45
|
|
|
{ |
|
46
|
|
|
Context::pop(); |
|
47
|
|
|
unset($this->deployer); |
|
48
|
|
|
$this->deployer = null; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function testHost() |
|
52
|
|
|
{ |
|
53
|
|
|
host('domain.com'); |
|
54
|
|
|
self::assertInstanceOf(Host::class, $this->deployer->hosts->get('domain.com')); |
|
55
|
|
|
|
|
56
|
|
|
host('a1.domain.com', 'a2.domain.com')->set('roles', 'app'); |
|
|
|
|
|
|
57
|
|
|
self::assertInstanceOf(Host::class, $this->deployer->hosts->get('a1.domain.com')); |
|
58
|
|
|
self::assertInstanceOf(Host::class, $this->deployer->hosts->get('a2.domain.com')); |
|
59
|
|
|
|
|
60
|
|
|
host('db[1:2].domain.com')->set('roles', 'db'); |
|
61
|
|
|
self::assertInstanceOf(Host::class, $this->deployer->hosts->get('db1.domain.com')); |
|
62
|
|
|
self::assertInstanceOf(Host::class, $this->deployer->hosts->get('db2.domain.com')); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function testLocalhost() |
|
66
|
|
|
{ |
|
67
|
|
|
localhost('domain.com'); |
|
68
|
|
|
self::assertInstanceOf(Localhost::class, $this->deployer->hosts->get('domain.com')); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function testTask() |
|
72
|
|
|
{ |
|
73
|
|
|
task('task', function () {}); |
|
74
|
|
|
|
|
75
|
|
|
$task = $this->deployer->tasks->get('task'); |
|
76
|
|
|
self::assertInstanceOf(Task::class, $task); |
|
77
|
|
|
|
|
78
|
|
|
$task = task('task'); |
|
79
|
|
|
self::assertInstanceOf(Task::class, $task); |
|
80
|
|
|
|
|
81
|
|
|
task('group', ['task']); |
|
82
|
|
|
$task = $this->deployer->tasks->get('group'); |
|
83
|
|
|
self::assertInstanceOf(GroupTask::class, $task); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function testBefore() |
|
87
|
|
|
{ |
|
88
|
|
|
task('main', function () {}); |
|
89
|
|
|
task('before', function () {}); |
|
90
|
|
|
before('main', 'before'); |
|
91
|
|
|
before('before', function () {}); |
|
92
|
|
|
|
|
93
|
|
|
$names = $this->taskToNames($this->deployer->scriptManager->getTasks('main')); |
|
94
|
|
|
self::assertEquals(['before:before', 'before', 'main'], $names); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function testAfter() |
|
98
|
|
|
{ |
|
99
|
|
|
task('main', function () {}); |
|
100
|
|
|
task('after', function () {}); |
|
101
|
|
|
after('main', 'after'); |
|
102
|
|
|
after('after', function () {}); |
|
103
|
|
|
|
|
104
|
|
|
$names = $this->taskToNames($this->deployer->scriptManager->getTasks('main')); |
|
105
|
|
|
self::assertEquals(['main', 'after', 'after:after'], $names); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function testRunLocally() |
|
109
|
|
|
{ |
|
110
|
|
|
$output = runLocally('echo "hello"'); |
|
111
|
|
|
self::assertEquals('hello', $output); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function testWithinSetsWorkingPaths() |
|
115
|
|
|
{ |
|
116
|
|
|
Context::get()->getConfig()->set('working_path', '/foo'); |
|
117
|
|
|
|
|
118
|
|
|
within('/bar', function () { |
|
119
|
|
|
$withinWorkingPath = Context::get()->getConfig()->get('working_path'); |
|
120
|
|
|
self::assertEquals('/bar', $withinWorkingPath); |
|
121
|
|
|
}); |
|
122
|
|
|
|
|
123
|
|
|
$originalWorkingPath = Context::get()->getConfig()->get('working_path'); |
|
124
|
|
|
self::assertEquals('/foo', $originalWorkingPath); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
public function testWithinRestoresWorkingPathInCaseOfException() |
|
128
|
|
|
{ |
|
129
|
|
|
Context::get()->getConfig()->set('working_path', '/foo'); |
|
130
|
|
|
|
|
131
|
|
|
try { |
|
132
|
|
|
within('/bar', function () { |
|
133
|
|
|
throw new \Exception('Dummy exception'); |
|
134
|
|
|
}); |
|
135
|
|
|
} catch (\Exception $exception) { |
|
136
|
|
|
// noop |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
$originalWorkingPath = Context::get()->getConfig()->get('working_path'); |
|
140
|
|
|
self::assertEquals('/foo', $originalWorkingPath); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
public function testWithinReturningValue() |
|
144
|
|
|
{ |
|
145
|
|
|
$output = within('/foo', function () { |
|
146
|
|
|
return 'bar'; |
|
147
|
|
|
}); |
|
148
|
|
|
|
|
149
|
|
|
self::assertEquals('bar', $output); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
public function testWithinWithVoidFunction() |
|
153
|
|
|
{ |
|
154
|
|
|
$output = within('/foo', function () { |
|
155
|
|
|
// noop |
|
156
|
|
|
}); |
|
157
|
|
|
|
|
158
|
|
|
self::assertNull($output); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
private function taskToNames($tasks) |
|
162
|
|
|
{ |
|
163
|
|
|
return array_map(function (Task $task) { |
|
164
|
|
|
return $task->getName(); |
|
165
|
|
|
}, $tasks); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|