Completed
Pull Request — 3.1 (#287)
by
unknown
12:38 queued 09:57
created

DrushContext::assertDrushOutput()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 3
Ratio 60 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 3
loc 5
rs 9.4285
c 0
b 0
f 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 View Code Duplication
    if (strpos((string) $this->readDrushOutput(), $this->fixStepArgument($output)) === FALSE) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 not contain :output
68
   */
69
  public function drushOutputShouldNotContain($output) {
70 View Code Duplication
    if (strpos((string) $this->readDrushOutput(), $this->fixStepArgument($output)) !== FALSE) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
        throw new \Exception(sprintf("The last drush command output did contain '%s' although it should not.\nOutput:\n\n%s'", $output, $this->drushOutput));
72
    }
73
  }
74
75
  /**
76
   * @Then print last drush output
77
   */
78
  public function printLastDrushOutput() {
79
    echo $this->readDrushOutput();
80
  }
81
82
  /**
83
   * Returns fixed step argument (with \\" replaced back to ").
84
   *
85
   * @param string $argument
86
   *
87
   * @return string
88
   */
89
  protected function fixStepArgument($argument) {
90
    return str_replace('\\"', '"', $argument);
91
  }
92
93
}
94