forked from drush-ops/drush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DrushTestTrait.php
117 lines (103 loc) · 3.7 KB
/
DrushTestTrait.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
<?php
namespace Drush\TestTraits;
/**
* DrushTestTrait provides a `drush()` method that may be
* used to write functional tests for Drush extensions.
*
* More information is available at https://github.com/drush-ops/drush/blob/10.x/docs/contribute/unish.md#drush-test-traits.
*/
trait DrushTestTrait
{
use CliTestTrait;
/**
* @return string
*/
public function getPathToDrush()
{
return dirname(dirname(__DIR__)) . '/drush';
}
/**
* Invoke drush in via execute().
*
* @param command
* A defined drush command such as 'cron', 'status' or any of the available ones such as 'drush pm'.
* @param args
* Command arguments.
* @param $options
* An associative array containing options.
* @param $site_specification
* A site alias or site specification. Include the '@' at start of a site alias.
* @param $cd
* A directory to change into before executing.
* @param $expected_return
* The expected exit code, e.g. 0 or 1 or some other expected value.
* @param $suffix
* Any code to append to the command. For example, redirection like 2>&1.
* @param array $env
* Environment variables to pass along to the subprocess.
*/
public function drush($command, array $args = [], array $options = [], $site_specification = null, $cd = null, $expected_return = 0, $suffix = null, array $env = [])
{
// Set UA for SimpleTests which typically extend BrowserTestCase (i.e. contrib modules).
if (isset($this->databasePrefix) && function_exists('drupal_generate_test_ua') && !isset($env['HTTP_USER_AGENT'])) {
$env['HTTP_USER_AGENT'] = drupal_generate_test_ua($this->databasePrefix);
}
$global_option_list = ['simulate', 'root', 'uri', 'include', 'config', 'alias-path', 'ssh-options'];
$cmd[] = self::getPathToDrush();
// Insert global options.
foreach ($options as $key => $value) {
if (in_array($key, $global_option_list)) {
unset($options[$key]);
$cmd[] = $this->convertKeyValueToFlag($key, $value);
}
}
$cmd[] = "--no-interaction";
// Insert site specification and drush command.
if (!empty($site_specification)) {
$cmd[] = self::escapeshellarg($site_specification);
}
$cmd[] = $command;
// Insert drush command arguments.
foreach ($args as $arg) {
$cmd[] = self::escapeshellarg($arg);
}
// insert drush command options
foreach ($options as $key => $value) {
$cmd[] = $this->convertKeyValueToFlag($key, $value);
}
$cmd[] = (string) $suffix;
$exec = array_filter($cmd, 'strlen'); // Removes empty strings.
$cmd = implode(' ', $exec);
$this->execute($cmd, $expected_return, $cd, $env);
}
/**
* Given an option key / value pair, convert to a
* "--key=value" string.
*
* @param string $key The option name
* @param string $value The option value (or empty)
* @return string
*/
protected function convertKeyValueToFlag(string $key, string $value)
{
if (!isset($value)) {
return "--$key";
}
return "--$key=" . self::escapeshellarg($value);
}
/**
* Return the major version of Drush
*
* @return string e.g. "8" or "9"
*/
public function drushMajorVersion()
{
static $major;
if (!isset($major)) {
$this->drush('version', [], ['field' => 'drush-version']);
$version = trim($this->getOutput());
list($major) = explode('.', $version);
}
return (int)$major;
}
}