-
Notifications
You must be signed in to change notification settings - Fork 6
/
RoboFile.php
247 lines (227 loc) · 6.81 KB
/
RoboFile.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<?php
// @codingStandardsIgnoreStart
use Robo\Exception\TaskException;
/**
* Base tasks for setting up a module to test within a full Drupal environment.
*
* This file expects to be called from the root of a Drupal site.
*
* @class RoboFile
* @codeCoverageIgnore
*/
class RoboFile extends \Robo\Tasks
{
/**
* The database URL.
*/
const DB_URL = 'sqlite://tmp/site.sqlite';
/**
* The website's URL.
*/
const DRUPAL_URL = 'http://drupal.docker.localhost:8000';
/**
* RoboFile constructor.
*/
public function __construct()
{
// Treat this command like bash -e and exit as soon as there's a failure.
$this->stopOnFail();
}
/**
* Command to run unit tests.
*
* @return \Robo\Result
* The result of the collection of tasks.
*/
public function jobRunUnitTests()
{
$collection = $this->collectionBuilder();
$collection->addTask($this->installDrupal());
$collection->addTaskList($this->runUnitTests());
return $collection->run();
}
/**
* Command to check coding standards.
*
* @return null|\Robo\Result
* The result of the set of tasks.
*
* @throws \Robo\Exception\TaskException
*/
public function jobCheckCodingStandards()
{
return $this->taskExecStack()
->stopOnFail()
->exec('vendor/bin/phpcs --config-set installed_paths vendor/drupal/coder/coder_sniffer')
->exec('vendor/bin/phpcs --standard=Drupal web/modules/custom')
->exec('vendor/bin/phpcs --standard=DrupalPractice web/modules/custom')
->run();
}
/**
* Command to run behat tests.
*
* @return \Robo\Result
* The result tof the collection of tasks.
*/
public function jobRunBehatTests()
{
$collection = $this->collectionBuilder();
$collection->addTaskList($this->downloadDatabase());
$collection->addTaskList($this->buildEnvironment());
$collection->addTask($this->waitForDrupal());
$collection->addTaskList($this->runUpdatePath());
$collection->addTaskList($this->runBehatTests());
return $collection->run();
}
/**
* Download's database to use within a Docker environment.
*
* This task assumes that there is an environment variable that contains a URL
* that contains a database dump. Ideally, you should set up drush site
* aliases and then replace this task by a drush sql-sync one. See the
* README at lullabot/drupal8ci for further details.
*
* @return \Robo\Task\Base\Exec[]
* An array of tasks.
*/
protected function downloadDatabase()
{
$tasks = [];
$tasks[] = $this->taskFilesystemStack()
->mkdir('mariadb-init');
$tasks[] = $this->taskExec('wget ' . getenv('DB_DUMP_URL'))
->dir('mariadb-init');
return $tasks;
}
/**
* Builds the Docker environment.
*
* @return \Robo\Task\Base\Exec[]
* An array of tasks.
*/
protected function buildEnvironment()
{
$force = true;
$tasks = [];
$tasks[] = $this->taskFilesystemStack()
->copy('.travis/docker-compose.yml', 'docker-compose.yml', $force)
->copy('.travis/traefik.yml', 'traefik.yml', $force)
->copy('.travis/.env', '.env', $force)
->copy('.travis/config/settings.local.php',
'web/sites/default/settings.local.php', $force)
->copy('.travis/config/behat.yml', 'tests/behat.yml', $force);
$tasks[] = $this->taskExec('docker-compose pull --parallel');
$tasks[] = $this->taskExec('docker-compose up -d');
return $tasks;
}
/**
* Waits for Drupal to accept requests.
*
* @TODO Find an efficient way to wait for Drupal.
*
* @return \Robo\Task\Base\Exec
* A task to check that Drupal is ready.
*/
protected function waitForDrupal()
{
return $this->taskExec('sleep 30s');
}
/**
* Updates the database.
*
* We can't use the drush() method because this is running within a docker-compose
* environment.
*
* @return \Robo\Task\Base\Exec[]
* An array of tasks.
*/
protected function runUpdatePath()
{
$tasks = [];
$tasks[] = $this->taskExec('docker-compose exec -T php vendor/bin/drush --yes updatedb');
$tasks[] = $this->taskExec('docker-compose exec -T php vendor/bin/drush --yes config-import');
return $tasks;
}
/**
* Install Drupal.
*
* @return \Robo\Task\Base\Exec
* A task to install Drupal.
*/
protected function installDrupal()
{
$task = $this->drush()
->args('site-install')
->option('verbose')
->option('yes')
->option('db-url', static::DB_URL, '=');
return $task;
}
/**
* Starts the web server.
*
* @return \Robo\Task\Base\Exec[]
* An array of tasks.
*/
protected function startWebServer()
{
$tasks = [];
$tasks[] = $this->taskExec('vendor/bin/drush --root=' . $this->getDocroot() . '/web runserver ' . static::DRUPAL_URL . ' &')
->silent(true);
$tasks[] = $this->taskExec('until curl -s ' . static::DRUPAL_URL . '; do true; done > /dev/null');
return $tasks;
}
/**
* Run unit tests.
*
* @return \Robo\Task\Base\Exec[]
* An array of tasks.
*/
protected function runUnitTests()
{
$force = true;
$tasks = [];
$tasks[] = $this->taskFilesystemStack()
->copy('.travis/config/phpunit.xml', 'web/core/phpunit.xml', $force);
$tasks[] = $this->taskExecStack()
->dir('web')
->exec('../vendor/bin/phpunit -c core --debug --coverage-clover ../build/logs/clover.xml --verbose modules/custom');
return $tasks;
}
/**
* Runs Behat tests.
*
* @return \Robo\Task\Base\Exec[]
* An array of tasks.
*/
protected function runBehatTests()
{
$tasks = [];
$tasks[] = $this->taskExecStack()
->exec('docker-compose exec -T php vendor/bin/behat --verbose -c tests/behat.yml');
return $tasks;
}
/**
* Return drush with default arguments.
*
* @return \Robo\Task\Base\Exec
* A drush exec command.
*/
protected function drush()
{
// Drush needs an absolute path to the docroot.
$docroot = $this->getDocroot() . '/web';
return $this->taskExec('vendor/bin/drush')
->option('root', $docroot, '=');
}
/**
* Get the absolute path to the docroot.
*
* @return string
* The document root.
*/
protected function getDocroot()
{
return (getcwd());
}
}