Completed
Pull Request — master (#459)
by
unknown
06:20
created

TestModuleContext   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 54
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getTestModulesFromTags() 0 12 3
A installTestModules() 0 6 2
A uninstallTestModules() 0 6 2
1
<?php
2
3
namespace Drupal\DrupalExtension\Context;
4
5
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
6
use Behat\Behat\Hook\Scope\ScenarioScope;
7
8
/**
9
 * Provides the ability to automatically enable and disable test modules.
10
 */
11
class TestModuleContext extends RawDrupalContext
12
{
13
  /**
14
   * Gets all with-module tags from a scenario scope.
15
   *
16
   * @param \Behat\Behat\Hook\Scope\ScenarioScope $scope
17
   *   The scenario scope.
18
   *
19
   * @return string[]
20
   *   The machine names of the modules to enable during the scenario.
21
   */
22
  protected function getTestModulesFromTags(ScenarioScope $scope)
23
  {
24
    $modules = array();
25
    $tags = array_merge($scope->getFeature()->getTags(), $scope->getScenario()->getTags());
26
27
    foreach ($tags as $tag) {
28
      if (strpos($tag, 'with-module:') === 0) {
29
        array_push($modules, substr($tag, 12));
30
      }
31
    }
32
    return array_unique($modules);
33
  }
34
35
  /**
36
   * Installs test modules for this scenario.
37
   *
38
   * @param \Behat\Behat\Hook\Scope\BeforeScenarioScope $scope
39
   *   The scenario scope.
40
   *
41
   * @BeforeScenario
42
   */
43
  public function installTestModules(BeforeScenarioScope $scope)
44
  {
45
    foreach ($this->getTestModulesFromTags($scope) as $module) {
46
      $this->getDriver()->moduleInstall($module);
47
    }
48
  }
49
50
  /**
51
   * Unistalls test modules for this scenario.
52
   *
53
   * @param \Behat\Behat\Hook\Scope\BeforeScenarioScope $scope
54
   *   The scenario scope.
55
   *
56
   * @AfterScenario
57
   */
58
  public function uninstallTestModules(BeforeScenarioScope $scope)
59
  {
60
    foreach ($this->getTestModulesFromTags($scope) as $module) {
61
      $this->getDriver()->moduleUninstall($module);
62
    }
63
  }
64
}
65