Completed
Push — master ( 4695cb...717a20 )
by Jonathan
03:10 queued 01:10
created

DrushContext::printLastDrushOutput()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Drupal\DrupalExtension\Context;
4
5
use Behat\Behat\Context\TranslatableContext;
6
7
/**
8
 * Provides step definitions for interacting directly with Drush commands.
9
 */
10
class DrushContext extends RawDrupalContext implements TranslatableContext {
11
12
  /**
13
   * Keep track of drush output.
14
   *
15
   * @var string|boolean
16
   */
17
  protected $drushOutput;
18
19
  /**
20
   * {@inheritDoc}
21
   */
22
  public static function getTranslationResources() {
23
    return glob(__DIR__ . '/../../../../i18n/*.xliff');
24
  }
25
26
  /**
27
   * Return the most recent drush command output.
28
   *
29
   * @return string
30
   */
31
  public function readDrushOutput() {
32
    if (!isset($this->drushOutput)) {
33
      throw new \RuntimeException('No drush output was found.');
34
    }
35
    return $this->drushOutput;
36
  }
37
38
  /**
39
   * @Given I run drush :command
40
   */
41
  public function assertDrushCommand($command) {
42
    if (!$this->drushOutput = $this->getDriver('drush')->$command()) {
43
       $this->drushOutput = TRUE;
44
    }
45
  }
46
47
  /**
48
   * @Given I run drush :command :arguments
49
   */
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;
54
    }
55
  }
56
57
  /**
58
   * @Then drush output should contain :output
59
   */
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));
63
    }
64
  }
65
66
  /**
67
   * @Then drush output should match :regex
68
   */
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));
72
    }
73
  }
74
75
  /**
76
   * @Then drush output should not contain :output
77
   */
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));
81
    }
82
  }
83
84
  /**
85
   * @Then print last drush output
86
   */
87
  public function printLastDrushOutput() {
88
    echo $this->readDrushOutput();
89
  }
90
91
  /**
92
   * Returns fixed step argument (with \\" replaced back to ").
93
   *
94
   * @param string $argument
95
   *
96
   * @return string
97
   */
98
  protected function fixStepArgument($argument) {
99
    return str_replace('\\"', '"', $argument);
100
  }
101
102
}
103