-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBehatCommands.php
100 lines (87 loc) · 3.17 KB
/
BehatCommands.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
<?php
namespace Drush\Commands\drush_behat_params;
use Composer\Autoload\ClassLoader;
use Drupal\Core\Composer\Composer;
use Drush\Commands\DrushCommands;
use Consolidation\AnnotatedCommand\Events\CustomEventAwareInterface;
use Consolidation\AnnotatedCommand\Events\CustomEventAwareTrait;
use Drush\Drush;
use Symfony\Component\Yaml\Yaml;
/**
*
*/
class BehatCommands extends DrushCommands implements CustomEventAwareInterface
{
use CustomEventAwareTrait;
/**
* @description Run bin/behat with BEHAT_PARAMS set from the drush information.
*
* Additional arguments are passed to behat command line.
* To pass options to behat, add "--" and then the behat options.
*
* @command behat
* @param $arguments A list of arguments and options to pass to behat. When using options, put after "--" so the options are passed to behat and not drush.
* @usage drush behat
* Run all behat tests.
* @usage drush behat -- --format=progress
* Run all behat tests using "progress" format.
* @usage drush behat -- --help
* Run bin/behat --help
* @usage drush behat -- -dl
* Get a list of step definitions
* @usage drush behat -- --story-syntax
* Print out a sample test.
* @usage drush behat features/content
* Run bin/behat features/content
*/
public function behat(array $arguments, $options = [
'behat_command' => '',
])
{
global $_composer_bin_dir;
if (empty($this->input()->getOption('behat_command'))) {
$default_command = $_composer_bin_dir . '/behat';
$this->input()->setOption('behat_command', $default_command);
}
// Make sure Behat base_url has http in front. Drush works with or without it, but behat needs it to work.
$uri = $this->commandData->options()['uri'];
$uri = str_starts_with($uri, 'http')? $uri: 'http://' . $uri;
// The BEHAT_PARAMS environment variable.
// Options set in behat.yml will override these.
$behat_params = [
"extensions" => [
"Drupal\\MinkExtension" => [
"base_url" => $uri,
],
"Drupal\\DrupalExtension" => [
"drupal" => [
"drupal_root" => $this->commandData->options()['root']
],
"drush" => [
"alias" => Drush::aliasManager()->getSelf()->name(),
]
]
]
];
$env = [
"BEHAT_PARAMS" => json_encode($behat_params),
];
// Run in vendor/..
$reflection = new \ReflectionClass(ClassLoader::class);
$cwd = dirname(dirname(dirname($reflection->getFileName())));;
$command = $this->input()->getOption('behat_command');
$command .= ' ' . implode(' ', $arguments);
$this->io()->table(['Drush Alias', 'URL', 'Root'], [[
Drush::aliasManager()->getSelf()->name(),
$this->commandData->options()['uri'],
$this->commandData->options()['root'],
]]);
$this->io()->table([],[[Yaml::dump($behat_params, 10, 2)]]);
$this->io()->title("Running <comment>$command</comment> in <comment>$cwd</comment> with the above BEHAT_PARAMS environment variable...");
$exit = $this->processManager()->shell($command, $cwd, $env)->run(function ($type, $buffer) {
echo $buffer;
}
);
return $exit;
}
}