Completed
Push — master ( 330635...475330 )
by Jonathan
11s
created

Drupal/DrupalExtension/Context/DrushContext.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
  /**
14
   * Keep track of drush output.
15
   *
16
   * @var string|boolean
17
   */
18
    protected $drushOutput;
19
20
  /**
21
   * {@inheritDoc}
22
   */
23
    public static function getTranslationResources()
24
    {
25
        return glob(__DIR__ . '/../../../../i18n/*.xliff');
26
    }
27
28
  /**
29
   * Return the most recent drush command output.
30
   *
31
   * @return string
32
   */
33
    public function readDrushOutput()
34
    {
35
        if (!isset($this->drushOutput)) {
36
            throw new \RuntimeException('No drush output was found.');
37
        }
38
        return $this->drushOutput;
39
    }
40
41
  /**
42
   * @Given I run drush :command
43
   */
44
    public function assertDrushCommand($command)
45
    {
46
        if (!$this->drushOutput = $this->getDriver('drush')->$command()) {
47
             $this->drushOutput = true;
48
        }
49
    }
50
51
  /**
52
   * @Given I run drush :command :arguments
53
   */
54
    public function assertDrushCommandWithArgument($command, $arguments)
55
    {
56
        $this->drushOutput = $this->getDriver('drush')->$command($this->fixStepArgument($arguments));
57
        if (!isset($this->drushOutput)) {
58
            $this->drushOutput = true;
59
        }
60
    }
61
62
  /**
63
   * @Then drush output should contain :output
64
   */
65
    public function assertDrushOutput($output)
66
    {
67 View Code Duplication
        if (strpos((string) $this->readDrushOutput(), $this->fixStepArgument($output)) === false) {
0 ignored issues
show
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...
68
            throw new \Exception(sprintf("The last drush command output did not contain '%s'.\nInstead, it was:\n\n%s'", $output, $this->drushOutput));
69
        }
70
    }
71
72
  /**
73
   * @Then drush output should match :regex
74
   */
75
    public function assertDrushOutputMatches($regex)
76
    {
77
        if (!preg_match($regex, (string) $this->readDrushOutput())) {
78
            throw new \Exception(sprintf("The pattern %s was not found anywhere in the drush output.\nOutput:\n\n%s", $regex, $this->drushOutput));
79
        }
80
    }
81
82
  /**
83
   * @Then drush output should not contain :output
84
   */
85
    public function drushOutputShouldNotContain($output)
86
    {
87 View Code Duplication
        if (strpos((string) $this->readDrushOutput(), $this->fixStepArgument($output)) !== false) {
0 ignored issues
show
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...
88
            throw new \Exception(sprintf("The last drush command output did contain '%s' although it should not.\nOutput:\n\n%s'", $output, $this->drushOutput));
89
        }
90
    }
91
92
  /**
93
   * @Then print last drush output
94
   */
95
    public function printLastDrushOutput()
96
    {
97
        echo $this->readDrushOutput();
98
    }
99
100
  /**
101
   * Returns fixed step argument (with \\" replaced back to ").
102
   *
103
   * @param string $argument
104
   *
105
   * @return string
106
   */
107
    protected function fixStepArgument($argument)
108
    {
109
        return str_replace('\\"', '"', $argument);
110
    }
111
}
112