3 namespace Drupal\DrupalExtension\Context;
5 use Behat\Behat\Context\TranslatableContext;
8 * Provides step definitions for interacting directly with Drush commands.
10 class DrushContext extends RawDrupalContext implements TranslatableContext {
13 * Keep track of drush output.
17 protected $drushOutput;
22 public static function getTranslationResources() {
23 return glob(__DIR__ . '/../../../../i18n/*.xliff');
27 * Return the most recent drush command output.
31 public function readDrushOutput() {
32 if (!isset($this->drushOutput)) {
33 throw new \RuntimeException('No drush output was found.');
35 return $this->drushOutput;
39 * @Given I run drush :command
41 public function assertDrushCommand($command) {
42 if (!$this->drushOutput = $this->getDriver('drush')->$command()) {
43 $this->drushOutput = TRUE;
48 * @Given I run drush :command :arguments
50 public function assertDrushCommandWithArgument($command, $arguments) {
51 $this->drushOutput = $this->getDriver('drush')->$command($this->fixStepArgument($arguments));
52 if (!isset($this->drushOutput)) {
53 $this->drushOutput = TRUE;
58 * @Then drush output should contain :output
60 public function assertDrushOutput($output) {
61 if (strpos((string) $this->readDrushOutput(), $this->fixStepArgument($output)) === FALSE) {
62 throw new \Exception(sprintf("The last drush command output did not contain '%s'.\nInstead, it was:\n\n%s'", $output, $this->drushOutput));
67 * @Then drush output should match :regex
69 public function assertDrushOutputMatches($regex) {
70 if (!preg_match($regex, (string) $this->readDrushOutput())) {
71 throw new \Exception(sprintf("The pattern %s was not found anywhere in the drush output.\nOutput:\n\n%s", $regex, $this->drushOutput));
76 * @Then drush output should not contain :output
78 public function drushOutputShouldNotContain($output) {
79 if (strpos((string) $this->readDrushOutput(), $this->fixStepArgument($output)) !== FALSE) {
80 throw new \Exception(sprintf("The last drush command output did contain '%s' although it should not.\nOutput:\n\n%s'", $output, $this->drushOutput));
85 * @Then print last drush output
87 public function printLastDrushOutput() {
88 echo $this->readDrushOutput();
92 * Returns fixed step argument (with \\" replaced back to ").
94 * @param string $argument
98 protected function fixStepArgument($argument) {
99 return str_replace('\\"', '"', $argument);