Completed
Push — throw-helpful-exception-for-aj... ( 0f6ed6...dbb2a0 )
by Jonathan
03:37 queued 01:57
created

MarkupContext::assertNotRegionButton()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
dl 9
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 2
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
  /**
14
   * Return a region from the current page.
15
   *
16
   * @throws \Exception
17
   *   If region cannot be found.
18
   *
19
   * @param string $region
20
   *   The machine name of the region to return.
21
   *
22
   * @return \Behat\Mink\Element\NodeElement
23
   *
24
   * @todo this should be a trait when PHP 5.3 support is dropped.
25
   */
26
    public function getRegion($region)
27
    {
28
        $session = $this->getSession();
29
        $regionObj = $session->getPage()->find('region', $region);
30
        if (!$regionObj) {
31
            throw new \Exception(sprintf('No region "%s" found on the page %s.', $region, $session->getCurrentUrl()));
32
        }
33
34
        return $regionObj;
35
    }
36
37
  /**
38
   * Checks if a button with id|name|title|alt|value exists in a region
39
   *
40
   * @Then I should see the button :button in the :region( region)
41
   * @Then I should see the :button button in the :region( region)
42
   *
43
   * @param $button
44
   *   string The id|name|title|alt|value of the button
45
   * @param $region
46
   *   string The region in which the button should be found
47
   *
48
   * @throws \Exception
49
   *   If region or button within it cannot be found.
50
   */
51 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...
52
    {
53
        $regionObj = $this->getRegion($region);
54
55
        $buttonObj = $regionObj->findButton($button);
56
        if (empty($buttonObj)) {
57
            throw new \Exception(sprintf("The button '%s' was not found in the region '%s' on the page %s", $button, $region, $this->getSession()->getCurrentUrl()));
58
        }
59
    }
60
61
    /**
62
     * Asserts that a button does not exists in a region.
63
     *
64
     * @Then I should not see the button :button in the :region( region)
65
     * @Then I should not see the :button button in the :region( region)
66
     *
67
     * @param $button
68
     *   string The id|name|title|alt|value of the button
69
     * @param $region
70
     *   string The region in which the button should not be found
71
     *
72
     * @throws \Exception
73
     *   If region is not found or the button is found within the region.
74
     */
75 View Code Duplication
    public function assertNotRegionButton($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...
76
    {
77
        $regionObj = $this->getRegion($region);
78
79
        $buttonObj = $regionObj->findButton($button);
80
        if (!empty($buttonObj)) {
81
            throw new \Exception(sprintf("The button '%s' was found in the region '%s' on the page %s but should not", $button, $region, $this->getSession()->getCurrentUrl()));
82
        }
83
    }
84
85
  /**
86
   * @Then I( should) see the :tag element in the :region( region)
87
   */
88 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...
89
    {
90
        $regionObj = $this->getRegion($region);
91
        $elements = $regionObj->findAll('css', $tag);
92
        if (!empty($elements)) {
93
            return;
94
        }
95
        throw new \Exception(sprintf('The element "%s" was not found in the "%s" region on the page %s', $tag, $region, $this->getSession()->getCurrentUrl()));
96
    }
97
98
  /**
99
   * @Then I( should) not see the :tag element in the :region( region)
100
   */
101 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...
102
    {
103
        $regionObj = $this->getRegion($region);
104
        $result = $regionObj->findAll('css', $tag);
105
        if (!empty($result)) {
106
            throw new \Exception(sprintf('The element "%s" was found in the "%s" region on the page %s', $tag, $region, $this->getSession()->getCurrentUrl()));
107
        }
108
    }
109
110
  /**
111
   * @Then I( should) not see :text in the :tag element in the :region( region)
112
   */
113
    public function assertNotRegionElementText($text, $tag, $region)
114
    {
115
        $regionObj = $this->getRegion($region);
116
        $results = $regionObj->findAll('css', $tag);
117
        if (!empty($results)) {
118
            foreach ($results as $result) {
119
                if ($result->getText() == $text) {
120
                    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()));
121
                }
122
            }
123
        }
124
    }
125
126
  /**
127
   * @Then I( should) see the :tag element with the :attribute attribute set to :value in the :region( region)
128
   */
129
    public function assertRegionElementAttribute($tag, $attribute, $value, $region)
130
    {
131
        $regionObj = $this->getRegion($region);
132
        $elements = $regionObj->findAll('css', $tag);
133
        if (empty($elements)) {
134
            throw new \Exception(sprintf('The element "%s" was not found in the "%s" region on the page %s', $tag, $region, $this->getSession()->getCurrentUrl()));
135
        }
136
        if (!empty($attribute)) {
137
            $found = false;
138
            $attrfound = false;
139
            foreach ($elements as $element) {
140
                $attr = $element->getAttribute($attribute);
141
                if (!empty($attr)) {
142
                    $attrfound = true;
143
                    if (strpos($attr, "$value") !== false) {
144
                        $found = true;
145
                        break;
146
                    }
147
                }
148
            }
149
            if (!$found) {
150
                if (!$attrfound) {
151
                    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()));
152
                } else {
153
                    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()));
154
                }
155
            }
156
        }
157
    }
158
159
  /**
160
   * @Then I( should) see :text in the :tag element with the :attribute attribute set to :value in the :region( region)
161
   */
162
    public function assertRegionElementTextAttribute($text, $tag, $attribute, $value, $region)
163
    {
164
        $regionObj = $this->getRegion($region);
165
        $elements = $regionObj->findAll('css', $tag);
166
        if (empty($elements)) {
167
            throw new \Exception(sprintf('The element "%s" was not found in the "%s" region on the page %s', $tag, $region, $this->getSession()->getCurrentUrl()));
168
        }
169
170
        $found = false;
171
        foreach ($elements as $element) {
172
            if ($element->getText() == $text) {
173
                $found = true;
174
                break;
175
            }
176
        }
177
        if (!$found) {
178
            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()));
179
        }
180
181
        if (!empty($attribute)) {
182
            $attr = $element->getAttribute($attribute);
183
            if (empty($attr)) {
184
                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()));
185
            }
186 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...
187
                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()));
188
            }
189
        }
190
    }
191
192
  /**
193
   * @Then I( should) see :text in the :tag element with the :property CSS property set to :value in the :region( region)
194
   */
195
    public function assertRegionElementTextCss($text, $tag, $property, $value, $region)
196
    {
197
        $regionObj = $this->getRegion($region);
198
        $elements = $regionObj->findAll('css', $tag);
199
        if (empty($elements)) {
200
            throw new \Exception(sprintf('The element "%s" was not found in the "%s" region on the page %s', $tag, $region, $this->getSession()->getCurrentUrl()));
201
        }
202
203
        $found = false;
204
        foreach ($elements as $element) {
205
            if ($element->getText() == $text) {
206
                $found = true;
207
                break;
208
            }
209
        }
210
        if (!$found) {
211
            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()));
212
        }
213
214
        $found = false;
215
        if (!empty($property)) {
216
            $style = $element->getAttribute('style');
217
            $rules = explode(";", $style);
218
            foreach ($rules as $rule) {
219
                if (strpos($rule, $property) !== false) {
220 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...
221
                        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()));
222
                    }
223
                    $found = true;
224
                    break;
225
                }
226
            }
227
            if (!$found) {
228
                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()));
229
            }
230
        }
231
    }
232
}
233