FormManipulationTrait::selectOption()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 3
nop 3
1
<?php
2
3
namespace Zumba\Mink\Driver;
4
5
use Behat\Mink\Exception\DriverException;
6
7
/**
8
 * Trait FormManipulationTrait
9
 * @package Zumba\Mink\Driver
10
 */
11
trait FormManipulationTrait {
12
13
14
  /**
15
   * Returns the value of a given xpath element
16
   * @param string $xpath
17
   * @return string
18
   * @throws DriverException
19
   */
20
  public function getValue($xpath) {
21
    $this->findElement($xpath, 1);
0 ignored issues
show
Bug introduced by
It seems like findElement() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
22
    $javascript = $this->javascriptTemplateRender("get_value.js.twig", array("xpath" => $xpath));
0 ignored issues
show
Bug introduced by
It seems like javascriptTemplateRender() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
23
    return $this->browser->evaluate($javascript);
0 ignored issues
show
Bug introduced by
The property browser does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
24
  }
25
26
  /**
27
   * @param string $xpath
28
   * @param string $value
29
   * @throws DriverException
30
   */
31
  public function setValue($xpath, $value) {
32
    $this->findElement($xpath, 1);
0 ignored issues
show
Bug introduced by
It seems like findElement() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
33
    //This stuff is BECAUSE the way the driver works for setting values when being checkboxes, radios, etc.
34
    if (is_bool($value)) {
35
      $value = $this->boolToString($value);
36
    } elseif (is_numeric($value)) {
37
      $value = (string) $value;
38
    }
39
40
    $javascript = $this->javascriptTemplateRender("set_value.js.twig", array("xpath" => $xpath, "value" => json_encode($value)));
0 ignored issues
show
Bug introduced by
It seems like javascriptTemplateRender() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
41
    $this->browser->evaluate($javascript);
42
  }
43
44
45
  /**
46
   * Submits a form given an xpath selector
47
   * @param string $xpath
48
   * @throws DriverException
49
   */
50
  public function submitForm($xpath) {
51
    $element = $this->findElement($xpath, 1);
0 ignored issues
show
Bug introduced by
It seems like findElement() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
52
    $tagName = $this->browser->tagName($element["page_id"], $element["ids"][0]);
53
    if (strcmp(strtolower($tagName), "form") !== 0) {
54
      throw new DriverException("Can not submit something that is not a form");
55
    }
56
    $this->browser->trigger($element["page_id"], $element["ids"][0], "submit");
57
  }
58
59
  /**
60
   * Helper method needed for twig and javascript stuff
61
   * @param $boolValue
62
   * @return string
63
   */
64
  protected function boolToString($boolValue) {
65
    if ($boolValue === true) {
66
      return "1";
67
    }
68
    return "0";
69
  }
70
71
  /**
72
   * Selects an option
73
   * @param string $xpath
74
   * @param string $value
75
   * @param bool   $multiple
76
   * @return bool
77
   * @throws DriverException
78
   */
79
  public function selectOption($xpath, $value, $multiple = false) {
80
    $element = $this->findElement($xpath, 1);
0 ignored issues
show
Bug introduced by
It seems like findElement() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
81
    $tagName = strtolower($this->browser->tagName($element["page_id"], $element["ids"][0]));
82
    $attributes = $this->browser->attributes($element["page_id"], $element["ids"][0]);
83
84
    if (!in_array($tagName, array("input", "select"))) {
85
      throw new DriverException(sprintf('Impossible to select an option on the element with XPath "%s" as it is not a select or radio input', $xpath));
86
    }
87
88
    if ($tagName === "input" && $attributes["type"] != "radio") {
89
      throw new DriverException(sprintf('Impossible to select an option on the element with XPath "%s" as it is not a select or radio input', $xpath));
90
    }
91
92
    return $this->browser->selectOption($element["page_id"], $element["ids"][0], $value, $multiple);
93
  }
94
95
  /**
96
   * Check control over an input element of radio or checkbox type
97
   * @param $xpath
98
   * @return bool
99
   * @throws DriverException
100
   */
101
  protected function inputCheckableControl($xpath) {
102
    $element = $this->findElement($xpath, 1);
0 ignored issues
show
Bug introduced by
It seems like findElement() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
103
    $tagName = strtolower($this->browser->tagName($element["page_id"], $element["ids"][0]));
104
    $attributes = $this->browser->attributes($element["page_id"], $element["ids"][0]);
105
    if ($tagName != "input") {
106
      throw new DriverException("Can not check when the element is not of the input type");
107
    }
108
    if (!in_array($attributes["type"], array("checkbox", "radio"))) {
109
      throw new DriverException("Can not check when the element is not checkbox or radio");
110
    }
111
    return true;
112
  }
113
114
  /**
115
   * We click on the checkbox or radio when possible and needed
116
   * @param string $xpath
117
   * @throws DriverException
118
   */
119
  public function check($xpath) {
120
    $this->inputCheckableControl($xpath);
121
    $javascript = $this->javascriptTemplateRender("check_element.js.twig", array("xpath" => $xpath, "check" => "true"));
0 ignored issues
show
Bug introduced by
It seems like javascriptTemplateRender() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
122
    $this->browser->evaluate($javascript);
123
  }
124
125
  /**
126
   * We click on the checkbox or radio when possible and needed
127
   * @param string $xpath
128
   * @throws DriverException
129
   */
130
  public function uncheck($xpath) {
131
    $this->inputCheckableControl($xpath);
132
    $javascript = $this->javascriptTemplateRender("check_element.js.twig", array("xpath" => $xpath, "check" => "false"));
0 ignored issues
show
Bug introduced by
It seems like javascriptTemplateRender() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
133
    $this->browser->evaluate($javascript);
134
  }
135
136
  /**
137
   * Checks if the radio or checkbox is checked
138
   * @param string $xpath
139
   * @return bool
140
   * @throws DriverException
141
   */
142
  public function isChecked($xpath) {
143
    $this->findElement($xpath, 1);
0 ignored issues
show
Bug introduced by
It seems like findElement() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
144
    $javascript = $this->javascriptTemplateRender("is_checked.js.twig", array("xpath" => $xpath));
0 ignored issues
show
Bug introduced by
It seems like javascriptTemplateRender() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
145
    $checked = $this->browser->evaluate($javascript);
146
147
    if ($checked === null) {
148
      throw new DriverException("Can not check when the element is not checkbox or radio");
149
    }
150
151
    return $checked;
152
  }
153
154
  /**
155
   * Checks if the option is selected or not
156
   * @param string $xpath
157
   * @return bool
158
   * @throws DriverException
159
   */
160
  public function isSelected($xpath) {
161
    $elements = $this->findElement($xpath, 1);
0 ignored issues
show
Bug introduced by
It seems like findElement() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
162
    $javascript = $this->javascriptTemplateRender("is_selected.js.twig", array("xpath" => $xpath));
0 ignored issues
show
Bug introduced by
It seems like javascriptTemplateRender() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
163
    $tagName = $this->browser->tagName($elements["page_id"], $elements["ids"][0]);
164
    if (strcmp(strtolower($tagName), "option") !== 0) {
165
      throw new DriverException("Can not assert on element that is not an option");
166
    }
167
168
    return $this->browser->evaluate($javascript);
169
  }
170
}
171