Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ Provides drush (`runDrush`) command.

### Configuration
- working_directory: Working directory where drush should be executed. Defaults to codeception root.
- timeout: Timeout in seconds for drush command. Set to 0 for no timeout. Default to 60 seconds.
- drush: Drush executable. Defaults to `drush`.
```
modules:
- DrupalDrush:
working_directory: './web'
timeout: 120
drush: './vendor/bin/drush'
options:
uri: http://mydomain.com
Expand All @@ -60,9 +62,17 @@ modules:

### Usage

Run drush config import and store output.
Run drush status and store output.

`$output = $i->runDrush('cim -y');`
`$output = $i->runDrush('status');`

Run drush config import and store output from STDERR.

`$output = $i->runDrush('cim -y', [], TRUE)->getErrorOutput();`

Run drush cache rebuild and store the exit code.

`$exit_code = $i->runDrush('cr', [], TRUE)->getExitCode();`

Get one-time login url.

Expand Down
12 changes: 8 additions & 4 deletions src/Codeception/Module/DrupalDrush.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* modules:
* - DrupalDrush:
* working_directory: './web'
* timeout: 120
* drush: './vendor/bin/drush'
* alias: '@mysite.com'
* options:
Expand Down Expand Up @@ -42,11 +43,14 @@ class DrupalDrush extends Module {
* e.g. "en devel -y".
* @param array $options
* Associative array of options.
* @param bool $return_process
* If TRUE, the Process object will be returned. If false, the output of
* Process::getOutput() will be returned. Defaults to FALSE.
*
* @return string
* The process output.
* @return string|\Symfony\Component\Process\Process
* The process output, or the process itself.
*/
public function runDrush($command, array $options = []) {
public function runDrush($command, array $options = [], $return_process = FALSE) {
if ($alias = $this->_getConfig('alias')) {
$command = $alias . ' ' . $command;
}
Expand All @@ -56,7 +60,7 @@ public function runDrush($command, array $options = []) {
elseif ($this->_getConfig('options')) {
$command = $this->normalizeOptions($this->_getConfig('options')) . $command;
}
return Drush::runDrush($command, $this->_getConfig('drush'), $this->_getConfig('working_directory'));
return Drush::runDrush($command, $this->_getConfig('drush'), $this->_getConfig('working_directory'), $this->_getConfig('timeout'), $return_process);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Codeception/Module/DrupalUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public function createUserWithRoles(array $roles = [], $password = FALSE) {
*/
public function logInAs($username) {
$alias = $this->_getConfig('alias') ? $this->_getConfig('alias') . ' ' : '';
$output = Drush::runDrush($alias. 'uli --name=' . $username, $this->_getConfig('drush'), $this->_getConfig('working_directory'));
$output = Drush::runDrush($alias. 'uli --name=' . $username, $this->_getConfig('drush'), $this->_getConfig('working_directory'), $this->_getConfig('timeout'));
$gen_url = str_replace(PHP_EOL, '', $output);
$url = substr($gen_url, strpos($gen_url, '/user/reset'));
$this->driver->amOnPage($url);
Expand Down
20 changes: 16 additions & 4 deletions src/Codeception/Util/Drush.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,16 @@ class Drush {
* The drush command to use.
* @param string $pwd
* Working directory.
* @param int|float $timeout.
* Drush execution timeout.
* @param bool $return_process
* If TRUE, the Process object will be returned. If false, the output of
* Process::getOutput() will be returned. Defaults to FALSE.
*
* @return string
* The process output.
* @return string|\Symfony\Component\Process\Process
* The process output, or the process object itself.
*/
public static function runDrush($command, $drush = 'drush', $pwd = NULL) {
public static function runDrush($command, $drush = 'drush', $pwd = NULL, $timeout = NULL, $return_process = FALSE) {
$command_args = array_merge([$drush], explode(' ', $command));
$process = new Process($command_args);

Expand All @@ -34,7 +39,14 @@ public static function runDrush($command, $drush = 'drush', $pwd = NULL) {
$process->setWorkingDirectory($pwd);
}

return $process->mustRun()->getOutput();
// Set timeout if configured.
if (isset($timeout)) {
$process->setTimeout($timeout);
}

$process->mustRun();

return $return_process ? $process : $process->getOutput();
}

}