This repository has been archived by the owner on Oct 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
/
RoboFile.php
304 lines (284 loc) · 9.32 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<?php
// @codingStandardsIgnoreStart
/**
* 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.
*
* @var string
*/
const DB_URL = 'mysql://[email protected]/drupal';
/**
* Command to run unit tests.
*
* @return \Robo\Result
* The result of the collection of tasks.
*/
public function jobRunUnitTests()
{
$collection = $this->collectionBuilder();
$collection->addTask($this->installDependencies());
$collection->addTask($this->waitForDatabase());
$collection->addTask($this->installDrupal());
$collection->addTaskList($this->runUnitTests());
return $collection->run();
}
/**
* Command to generate a coverage report.
*
* @return \Robo\Result
* The result of the collection of tasks.
*/
public function jobGenerateCoverageReport()
{
$collection = $this->collectionBuilder();
$collection->addTask($this->installDependencies());
$collection->addTask($this->waitForDatabase());
$collection->addTask($this->installDrupal());
$collection->addTaskList($this->runUnitTestsWithCoverage());
return $collection->run();
}
/**
* Command to check for Drupal's Coding Standards.
*
* @return \Robo\Result
* The result of the collection of tasks.
*/
public function jobCheckCodingStandards()
{
$collection = $this->collectionBuilder();
$collection->addTask($this->installDependencies());
$collection->addTaskList($this->runCodeSniffer());
return $collection->run();
}
/**
* Command to run behat tests.
*
* @return \Robo\Result
* The result tof the collection of tasks.
*/
public function jobRunBehatTests()
{
$collection = $this->collectionBuilder();
$collection->addTask($this->installDependencies());
$collection->addTask($this->waitForDatabase());
$collection->addTaskList($this->importDatabase());
$collection->addTaskList($this->runUpdatePath());
$collection->addTaskList($this->runBehatTests());
return $collection->run();
}
/**
* Command to run Cypress tests.
*
* @return \Robo\Result
* The result tof the collection of tasks.
*/
public function jobRunCypressTests()
{
$collection = $this->collectionBuilder();
$collection->addTask($this->installDependencies());
$collection->addTask($this->waitForDatabase());
$collection->addTaskList($this->importDatabase());
$collection->addTaskList($this->runUpdatePath());
$collection->addTaskList($this->runCypressTests());
return $collection->run();
}
/**
* Imports and updates the database.
*
* This task assumes that there is an environment variable $DB_DUMP_URL
* that contains a URL to 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/drupal9ci for further details.
*
* @return \Robo\Task\Base\Exec[]
* An array of tasks.
*/
protected function importDatabase()
{
$force = TRUE;
$tasks = [];
$tasks[] = $this->taskExec('mysql -u root -h 127.0.0.1 -e "create database drupal"');
$tasks[] = $this->taskFilesystemStack()
->copy('.circleci/config/settings.local.php', 'web/sites/default/settings.local.php', $force);
$tasks[] = $this->taskExec('wget -O dump.sql "' . getenv('DB_DUMP_URL') . '"');
$tasks[] = $this->drush()->rawArg('sql-cli < dump.sql');
return $tasks;
}
/**
* Updates the database.
*
*
* @return \Robo\Task\Base\Exec[]
* An array of tasks.
*/
protected function runUpdatePath()
{
$tasks = [];
$tasks[] = $this->drush()->args('updatedb')->option('yes')->option('verbose');
$tasks[] = $this->drush()->args('config-import')->option('yes')->option('verbose');
$tasks[] = $this->drush()->args('cr')->option('verbose');
return $tasks;
}
/**
* Runs Behat tests.
*
* @return \Robo\Task\Base\Exec[]
* An array of tasks.
*/
protected function runBehatTests()
{
$force = TRUE;
$tasks = [];
$tasks[] = $this->taskExec('chown -R www-data:www-data /opt/drupal');
$tasks[] = $this->taskExec('ln -sf /opt/drupal/web /var/www/html');
$tasks[] = $this->taskExec('echo "\nServerName localhost" >> /etc/apache2/apache2.conf');
$tasks[] = $this->taskExec('service apache2 start');
$tasks[] = $this->taskFilesystemStack()
->copy('.circleci/config/behat.yml', 'tests/behat.yml', $force);
$tasks[] = $this->taskExec('vendor/bin/behat --verbose -c tests/behat.yml');
return $tasks;
}
/**
* Runs Cypress tests.
*
* @return \Robo\Task\Base\Exec[]
* An array of tasks.
*/
protected function runCypressTests()
{
$force = TRUE;
$tasks = [];
$tasks[] = $this->taskExec('chown -R www-data:www-data /opt/drupal');
$tasks[] = $this->taskExec('ln -sf /opt/drupal/web /var/www/html');
$tasks[] = $this->taskExec('echo "\nServerName localhost" >> /etc/apache2/apache2.conf');
$tasks[] = $this->taskExec('service apache2 start');
$tasks[] = $this->taskFilesystemStack()
->copy('.cypress/cypress.json', 'cypress.json', $force)
->copy('.cypress/package.json', 'package.json', $force);
$tasks[] = $this->taskExec('sleep 30s');
$tasks[] = $this->taskExec('npm install cypress@9 --save-dev');
$tasks[] = $this->taskExec('$(npm bin)/cypress run');
return $tasks;
}
/**
* Installs composer dependencies.
*
* @return \Robo\Contract\TaskInterface
* A task instance.
*/
protected function installDependencies()
{
return $this->taskComposerInstall()
->optimizeAutoloader();
}
/**
* Waits for the database service to be ready.
*
* @return \Robo\Contract\TaskInterface
* A task instance.
*/
protected function waitForDatabase()
{
return $this->taskExec('dockerize -wait tcp://localhost:3306 -timeout 1m');
}
/**
* 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;
}
/**
* Run unit tests.
*
* @return \Robo\Task\Base\Exec[]
* An array of tasks.
*/
protected function runUnitTests()
{
$force = TRUE;
$tasks = [];
$tasks[] = $this->taskFilesystemStack()
->copy('.circleci/config/phpunit.xml', 'web/core/phpunit.xml', $force)
->mkdir('artifacts/phpunit', 777);
$tasks[] = $this->taskExecStack()
->dir('web')
->exec('../vendor/bin/phpunit -c core --debug --verbose --log-junit ../artifacts/phpunit/phpunit.xml modules/custom');
return $tasks;
}
/**
* Run unit tests and generates a code coverage report.
*
* @return \Robo\Task\Base\Exec[]
* An array of tasks.
*/
protected function runUnitTestsWithCoverage()
{
$force = TRUE;
$tasks = [];
$tasks[] = $this->taskFilesystemStack()
->copy('.circleci/config/phpunit.xml', 'web/core/phpunit.xml', $force)
->mkdir('artifacts/coverage-xml', 777)
->mkdir('artifacts/coverage-html', 777);
$tasks[] = $this->taskExecStack()
->dir('web')
->exec('../vendor/bin/phpunit -c core --debug --verbose --coverage-xml ../artifacts/coverage-xml --coverage-html ../artifacts/coverage-html modules/custom');
return $tasks;
}
/**
* Sets up and runs code sniffer.
*
* @return \Robo\Task\Base\Exec[]
* An array of tasks.
*/
protected function runCodeSniffer()
{
$tasks = [];
$tasks[] = $this->taskFilesystemStack()
->mkdir('artifacts/phpcs');
$tasks[] = $this->taskExecStack()
->exec('vendor/bin/phpcs --standard=Drupal --report=junit --report-junit=artifacts/phpcs/phpcs.xml web/modules/custom')
->exec('vendor/bin/phpcs --standard=DrupalPractice --report=junit --report-junit=artifacts/phpcs/phpcs.xml web/modules/custom');
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
*/
protected function getDocroot()
{
$docroot = (getcwd());
return $docroot;
}
}