Completed
Push — 1.0.x ( 9e1450...e6ac6e )
by Antonio
02:33
created

VisibilityContext::assertElementVisibility()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 10
loc 10
rs 9.4285
cc 3
eloc 6
nc 3
nop 2
1
<?php
2
3
namespace NuvoleWeb\Drupal\DrupalExtension\Context;
4
5
use Behat\Mink\Element\NodeElement;
6
use Behat\Mink\Exception\ExpectationException;
7
use Behat\Mink\Exception\UnsupportedDriverActionException;
8
9
/**
10
 * Class VisibilityContext.
11
 *
12
 * @package NuvoleWeb\Drupal\DrupalExtension\Context
13
 */
14
class VisibilityContext extends RawMinkContext {
15
16
  /**
17
   * Assert presence of given element on the page.
18
   *
19
   * @Then the element :tag with text :text should be visible
20
   */
21 View Code Duplication
  public function assertElementVisibility($tag, $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...
22
    $element = $this->getSession()->getPage();
23
    /** @var \Behat\Mink\Element\NodeElement[] $nodes */
24
    $nodes = $element->findAll('css', $tag);
25
    foreach ($nodes as $node) {
26
      if ($node->getText() === $text) {
27
        $this->assertElementVisible($text, $node);
28
      }
29
    }
30
  }
31
32
  /**
33
   * Assert absence of given element on the page.
34
   *
35
   * @Then the element :tag with text :text should not be visible
36
   */
37 View Code Duplication
  public function assertElementNonVisibility($tag, $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...
38
    $element = $this->getSession()->getPage();
39
    /** @var \Behat\Mink\Element\NodeElement[] $nodes */
40
    $nodes = $element->findAll('css', $tag);
41
    foreach ($nodes as $node) {
42
      if ($node->getText() === $text) {
43
        $this->assertElementNotVisible($text, $node);
44
      }
45
    }
46
  }
47
48
  /**
49
   * Assert presence of given field on the page.
50
   *
51
   * @Then I should see the field :field
52
   */
53
  public function iShouldSeeTheField($field) {
54
    $element = $this->getSession()->getPage();
55
    $result = $element->findField($field);
56
    $this->assertElementVisible($field, $result);
0 ignored issues
show
Bug introduced by
It seems like $result defined by $element->findField($field) on line 55 can be null; however, NuvoleWeb\Drupal\DrupalE...:assertElementVisible() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
57
  }
58
59
  /**
60
   * Assert absence of given field.
61
   *
62
   * @Then I should not see the field :field
63
   */
64
  public function iShouldNotSeeTheField($field) {
65
    $element = $this->getSession()->getPage();
66
    $this->assertElementNotVisible($field, $element->findField($field));
0 ignored issues
show
Bug introduced by
It seems like $element->findField($field) can be null; however, assertElementNotVisible() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
67
  }
68
69
  /**
70
   * Assert visibility of an element.
71
   *
72
   * @param string $element
73
   *    Element selector or a string that describes it.
74
   * @param \Behat\Mink\Element\NodeElement $node
75
   *    Node representing the element above, if any.
76
   *
77
   * @throws \Behat\Mink\Exception\ExpectationException
78
   *    Throws exception if element not found.
79
   */
80 View Code Duplication
  protected function assertElementVisible($element, NodeElement $node) {
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...
81
    try {
82
      if ($node && !$node->isVisible()) {
83
        throw new ExpectationException(sprintf("The element '%s' is not present on the page %s", $element, $this->getSession()->getCurrentUrl()), $this->getSession());
84
      }
85
    }
86
    catch (UnsupportedDriverActionException $e) {
87
      // We catch the UnsupportedDriverActionException exception in case
88
      // this step is not being performed by a driver that supports javascript.
89
      // All other exceptions are valid.
90
      if (empty($node)) {
91
        throw new ExpectationException(sprintf("The element '%s' is not present on the page %s", $element, $this->getSession()->getCurrentUrl()), $this->getSession());
92
      }
93
    }
94
  }
95
96
  /**
97
   * Assert non visibility of an element.
98
   *
99
   * @param string $element
100
   *    Element selector or a string that describes it.
101
   * @param \Behat\Mink\Element\NodeElement $node
102
   *    Node representing the element above, if any.
103
   *
104
   * @throws \Behat\Mink\Exception\ExpectationException
105
   *    Throws exception if element is found.
106
   */
107 View Code Duplication
  protected function assertElementNotVisible($element, NodeElement $node) {
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...
108
    try {
109
      if ($node && $node->isVisible()) {
110
        throw new ExpectationException(sprintf("The field '%s' was present on the page %s and was not supposed to be", $element, $this->getSession()->getCurrentUrl()), $this->getSession());
111
      }
112
    }
113
    catch (UnsupportedDriverActionException $e) {
114
      // We catch the UnsupportedDriverActionException exception in case
115
      // this step is not being performed by a driver that supports javascript.
116
      // All other exceptions are valid.
117
      if ($node) {
118
        throw new ExpectationException(sprintf("The field '%s' was present on the page %s and was not supposed to be", $element, $this->getSession()->getCurrentUrl()), $this->getSession());
119
      }
120
    }
121
  }
122
123
}
124