Completed
Push — eliza411-patch-1 ( a2be10 )
by
unknown
10:11
created

MarkupContext::getRegion()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 9
loc 9
rs 9.6666
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
namespace Drupal\DrupalExtension\Context;
4
5
use Behat\MinkExtension\Context\RawMinkContext;
6
7
/**
8
 * Extensions to the Mink Extension.
9
 */
10
class MarkupContext extends RawMinkContext {
11
12
  /**
13
   * Return a region from the current page.
14
   *
15
   * @throws \Exception
16
   *   If region cannot be found.
17
   *
18
   * @param string $region
19
   *   The machine name of the region to return.
20
   *
21
   * @return \Behat\Mink\Element\NodeElement
22
   *
23
   * @todo this should be a trait when PHP 5.3 support is dropped.
24
   */
25 View Code Duplication
  public function getRegion($region) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
26
    $session = $this->getSession();
27
    $regionObj = $session->getPage()->find('region', $region);
28
    if (!$regionObj) {
29
      throw new \Exception(sprintf('No region "%s" found on the page %s.', $region, $session->getCurrentUrl()));
30
    }
31
32
    return $regionObj;
33
  }
34
35
  /**
36
   * Checks if a button with id|name|title|alt|value exists in a region
37
   *
38
   * @Then I should see the button :button in the :region( region)
39
   * @Then I should see the :button button in the :region( region)
40
   *
41
   * @param $button
42
   *   string The id|name|title|alt|value of the button
43
   * @param $region
44
   *   string The region in which the button should be found
45
   *
46
   * @throws \Exception
47
   *   If region or button within it cannot be found.
48
   */
49 View Code Duplication
  public function assertRegionButton($button, $region) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
50
    $regionObj = $this->getRegion($region);
51
52
    $buttonObj = $regionObj->findButton($button);
53
    if (empty($buttonObj)) {
54
      throw new \Exception(sprintf("The button '%s' was not found in the region '%s' on the page %s", $button, $region, $this->getSession()->getCurrentUrl()));
55
    }
56
  }
57
58
  /**
59
   * @Then I( should) see the :tag element in the :region( region)
60
   */
61 View Code Duplication
  public function assertRegionElement($tag, $region) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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
    $regionObj = $this->getRegion($region);
63
    $elements = $regionObj->findAll('css', $tag);
64
    if (!empty($elements)) {
65
      return;
66
    }
67
    throw new \Exception(sprintf('The element "%s" was not found in the "%s" region on the page %s', $tag, $region, $this->getSession()->getCurrentUrl()));
68
  }
69
70
  /**
71
   * @Then I( should) not see the :tag element in the :region( region)
72
   */
73 View Code Duplication
  public function assertNotRegionElement($tag, $region) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
74
    $regionObj = $this->getRegion($region);
75
    $result = $regionObj->findAll('css', $tag);
76
    if (!empty($result)) {
77
      throw new \Exception(sprintf('The element "%s" was found in the "%s" region on the page %s', $tag, $region, $this->getSession()->getCurrentUrl()));
78
    }
79
  }
80
81
  /**
82
   * @Then I( should) not see :text in the :tag element in the :region( region)
83
   */
84
  public function assertNotRegionElementText($text, $tag, $region) {
85
    $regionObj = $this->getRegion($region);
86
    $results = $regionObj->findAll('css', $tag);
87
    if (!empty($results)) {
88
      foreach ($results as $result) {
89
        if ($result->getText() == $text) {
90
          throw new \Exception(sprintf('The text "%s" was found in the "%s" element in the "%s" region on the page %s', $text, $tag, $region, $this->getSession()->getCurrentUrl()));
91
        }
92
      }
93
    }
94
  }
95
96
  /**
97
   * @Then I( should) see the :tag element with the :attribute attribute set to :value in the :region( region)
98
   */
99
  public function assertRegionElementAttribute($tag, $attribute, $value, $region) {
100
    $regionObj = $this->getRegion($region);
101
    $elements = $regionObj->findAll('css', $tag);
102
    if (empty($elements)) {
103
      throw new \Exception(sprintf('The element "%s" was not found in the "%s" region on the page %s', $tag, $region, $this->getSession()->getCurrentUrl()));
104
    }
105
    if (!empty($attribute)) {
106
      $found = FALSE;
107
      foreach ($elements as $element) {
108
        $attr = $element->getAttribute($attribute);
109
        if (!empty($attr)) {
110
          $found = TRUE;
111 View Code Duplication
          if (strpos($attr, "$value") === 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...
112
            throw new \Exception(sprintf('The "%s" attribute does not equal "%s" on the element "%s" in the "%s" region on the page %s', $attribute, $value, $tag, $region, $this->getSession()->getCurrentUrl()));
113
          }
114
          break;
115
        }
116
      }
117
      if (!$found) {
118
        throw new \Exception(sprintf('The "%s" attribute is not present on the element "%s" in the "%s" region on the page %s', $attribute, $tag, $region, $this->getSession()->getCurrentUrl()));
119
      }
120
    }
121
  }
122
123
  /**
124
   * @Then I( should) see :text in the :tag element with the :attribute attribute set to :value in the :region( region)
125
   */
126
  public function assertRegionElementTextAttribute($text, $tag, $attribute, $value, $region) {
127
    $regionObj = $this->getRegion($region);
128
    $elements = $regionObj->findAll('css', $tag);
129
    if (empty($elements)) {
130
      throw new \Exception(sprintf('The element "%s" was not found in the "%s" region on the page %s', $tag, $region, $this->getSession()->getCurrentUrl()));
131
    }
132
133
    $found = FALSE;
134
    foreach ($elements as $element) {
135
      if ($element->getText() == $text) {
136
        $found = TRUE;
137
        break;
138
      }
139
    }
140
    if (!$found) {
141
      throw new \Exception(sprintf('The text "%s" was not found in the "%s" element in the "%s" region on the page %s', $text, $tag, $region, $this->getSession()->getCurrentUrl()));
142
    }
143
144
    if (!empty($attribute)) {
145
      $attr = $element->getAttribute($attribute);
0 ignored issues
show
Bug introduced by
The variable $element seems to be defined by a foreach iteration on line 134. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
146
      if (empty($attr)) {
147
        throw new \Exception(sprintf('The "%s" attribute is not present on the element "%s" in the "%s" region on the page %s', $attribute, $tag, $region, $this->getSession()->getCurrentUrl()));
148
      }
149 View Code Duplication
      if (strpos($attr, "$value") === 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...
150
        throw new \Exception(sprintf('The "%s" attribute does not equal "%s" on the element "%s" in the "%s" region on the page %s', $attribute, $value, $tag, $region, $this->getSession()->getCurrentUrl()));
151
      }
152
    }
153
  }
154
155
  /**
156
   * @Then I( should) see :text in the :tag element with the :property CSS property set to :value in the :region( region)
157
   */
158
  public function assertRegionElementTextCss($text, $tag, $property, $value, $region) {
159
    $regionObj = $this->getRegion($region);
160
    $elements = $regionObj->findAll('css', $tag);
161
    if (empty($elements)) {
162
      throw new \Exception(sprintf('The element "%s" was not found in the "%s" region on the page %s', $tag, $region, $this->getSession()->getCurrentUrl()));
163
    }
164
165
    $found = FALSE;
166
    foreach ($elements as $element) {
167
      if ($element->getText() == $text) {
168
        $found = TRUE;
169
        break;
170
      }
171
    }
172
    if (!$found) {
173
      throw new \Exception(sprintf('The text "%s" was not found in the "%s" element in the "%s" region on the page %s', $text, $tag, $region, $this->getSession()->getCurrentUrl()));
174
    }
175
176
    $found = FALSE;
177
    if (!empty($property)) {
178
      $style = $element->getAttribute('style');
0 ignored issues
show
Bug introduced by
The variable $element seems to be defined by a foreach iteration on line 166. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
179
      $rules = explode(";", $style);
180
      foreach ($rules as $rule) {
181
        if (strpos($rule, $property) !== FALSE) {
182 View Code Duplication
          if (strpos($rule, $value) === 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...
183
            throw new \Exception(sprintf('The "%s" style property does not equal "%s" on the element "%s" in the "%s" region on the page %s', $property, $value, $tag, $region, $this->getSession()->getCurrentUrl()));
184
          }
185
          $found = TRUE;
186
          break;
187
        }
188
      }
189
      if (!$found) {
190
        throw new \Exception(sprintf('The "%s" style property was not found in the "%s" element in the "%s" region on the page %s', $property, $tag, $region, $this->getSession()->getCurrentUrl()));
191
      }
192
    }
193
  }
194
}
195