Completed
Push — master ( fe361a...edb2c6 )
by Toni
02:49
created

validateTextForSearchInSource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
/**
3
 * @author Toni Kolev, <[email protected]>
4
 */
5
namespace kolev\MultilingualExtension\Context;
6
use Behat\MinkExtension\Context\RawMinkContext;
7
8
/**
9
 * Class RawMultilingualContext.
10
 *
11
 * @package kolev\MultilingualExtension\Context
12
 */
13
class RawMultilingualContext extends RawMinkContext implements MultilingualContextInterface
14
{
15
16
    /**
17
     * Parameters of MultilingualExtension.
18
     *
19
     * @var array
20
     */
21
    public $multilingual_parameters = [];
22
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function setMultilingualParameters(array $parameters)
27
    {
28
        if (empty($this->multilingual_parameters)) {
29
            $this->multilingual_parameters = $parameters;
30
        }
31
    }
32
33
    /**
34
     * @param string $name
35
     *   The name of parameter from behat.yml.
36
     *
37
     * @return mixed
38
     */
39
    protected function getMultilingualParameter($name)
40
    {
41
        return isset($this->multilingual_parameters[$name]) ? $this->multilingual_parameters[$name] : false;
42
    }
43
44
    /**
45
     *  RAW definitions of simple functions to  be used in MultilingualContext
46
     */
47
48 View Code Duplication
    public function iClickOnTheText($text) {
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...
49
50
        $session = $this->getSession();
51
        $element = $session->getPage()->find(
52
            'xpath',
53
            $session->getSelectorsHandler()->selectorToXpath('xpath', '//*[contains(text(),"' . $text . '")]'));
54
55
        if (null === $element) {
56
            throw new \InvalidArgumentException(sprintf('Cannot find text: "%s"', $text));
57
        }
58
59
        $element->click();
60
    }
61
62
    public function assertValueInInput($value, $input) {
63
64
        if (substr($input,0,1) != "#") {
65
            $input = "#" . $input;
66
        }
67
        $session = $this->getSession();
68
        $element = $session->getPage()->find('css', $input);
69
        $text = "";
0 ignored issues
show
Unused Code introduced by
$text is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
70
71
        if(isset($element)) {
72
            $text = $element->getValue();
73
        }
74
        else {
75
            throw new \Exception(sprintf("Element is null"));
76
        }
77
78
        if($text === $value) {
79
            return true;
80
        }
81
        else {
82
            throw new \Exception(sprintf('Value of input : "%s" does not match the text "%s"', $text, $value));
83
        }
84
    }
85
86
    public function iWaitForTextToAppearWithMaxTime($text, $maxExecutionTime) {
87
88
        $isTextFound = false;
89
90
        for ($i = 0; $i < $maxExecutionTime; $i++) {
91
            try {
92
                $this->iShouldSeeInTheSourceOfThePage($text);
93
                $isTextFound = true;
94
                break;
95
            }
96
            catch (\Exception $e) {
97
                sleep(1);
98
            }
99
        }
100
101
        if (!$isTextFound) {
102
            throw new \Exception("'$text' didn't appear on the page for $maxExecutionTime seconds");
103
        }
104
    }
105
106
    public function iShouldSeeInTheSourceOfThePage($text) {
107
108
        $html = $this->getSession()->getDriver()->getContent();
109
        $text = $this->validateTextForSearchInSource($text);
110
        $regex = '/' . $text . '/';
111
112
        preg_match($regex, $html, $results);
113
114
        if ($results == null) {
115
            throw new \Exception('The searched text ' . $text . ' was not found in the source of the page.');
116
        }
117
118
        return true;
119
    }
120
121
    public function validateTextForSearchInSource($text) {
122
123
        $text = preg_replace("/(\\.)/", '\\\\.', $text);
124
        $text = preg_replace("/(\/)/", '\\/', $text);
125
        $text = preg_replace("/(\\?)/", '\\\\\?', $text);
126
127
        return $text;
128
    }
129
130 View Code Duplication
    public function iClickOnTheTextInRegion($text, $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...
131
132
        $session = $this->getSession();
133
        $element = $session->getPage()->find('region', $region)->find('xpath', $session->getSelectorsHandler()->selectorToXpath('xpath',
134
            '//*[contains(text(),"' . $text . '")]'));
135
136
        if (null === $element) {
137
            throw new \InvalidArgumentException(sprintf('Cannot find text: "%s"', $text));
138
        }
139
140
        $element->click();
141
    }
142
143 View Code Duplication
    public function selectOptionWithJavascript($text, $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...
144
145
        $session = $this->getSession();
146
        $element = $session->getPage()->find('region', $region)->find('xpath', $session->getSelectorsHandler()->selectorToXpath('xpath',
147
            '//*[contains(text(),"' . $text . '")]'));
148
149
        if (null === $element) {
150
            throw new \InvalidArgumentException(sprintf('Cannot find text: "%s"', $text));
151
        }
152
153
        $element->click();
154
    }
155
156
    public function assertSelectRadioById($label, $id = '') {
157
        $element = $this->getSession()->getPage();
158
        $radiobutton = $id ? $element->findById($id) : $element->find('named', array('radio', $this->getSession()->getSelectorsHandler()->xpathLiteral($label)));
0 ignored issues
show
Deprecated Code introduced by
The method Behat\Mink\Selector\Sele...Handler::xpathLiteral() has been deprecated with message: since Mink 1.7. Use \Behat\Mink\Selector\Xpath\Escaper::escapeLiteral when building Xpath or pass the unescaped value when using the named selector.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
159
        if ($radiobutton === NULL) {
160
            throw new \Exception(sprintf('The radio button with "%s" was not found on the page %s', $id ? $id : $label, $this->getSession()->getCurrentUrl()));
161
        }
162
        $value = $radiobutton->getAttribute('value');
163
        $labelonpage = $radiobutton->getParent()->getText();
164
        if ($label != $labelonpage) {
165
            throw new \Exception(sprintf("Button with id '%s' has label '%s' instead of '%s' on the page %s", $id, $labelonpage, $label, $this->getSession()->getCurrentUrl()));
166
        }
167
        $radiobutton->selectOption($value, FALSE);
168
    }
169
170
}
171