MinkContext::assertPageContainsText()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
dl 8
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
3
namespace Victoire\Tests\Features\Context;
4
5
use Behat\Mink\Element\Element;
6
use Behat\Mink\Exception\ExpectationException;
7
use Knp\FriendlyContexts\Context\MinkContext as KFMinkContext;
8
9
/**
10
 * Feature context.
11
 */
12
class MinkContext extends KFMinkContext
13
{
14
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$text" missing
Loading history...
introduced by
Doc comment for parameter "$timeout" missing
Loading history...
15
     * Checks, that page contains specified text
16
     * Example: Then I should see "Who is the Batman?"
17
     * Example: And I should see "Who is the Batman?".
18
     */
19 View Code Duplication
    public function assertPageContainsText($text, $timeout = 10000)
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...
20
    {
21
        $element = $this->findOrRetry($this->getSession()->getPage(), $text, $timeout);
22
        if (!$element) {
23
            $message = sprintf('The text "%s" was not found anywhere in the text of the current page.', $text);
24
            throw new \Behat\Mink\Exception\ResponseTextException($message, $this->getSession());
25
        }
26
    }
27
28
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$text" missing
Loading history...
introduced by
Doc comment for parameter "$timeout" missing
Loading history...
29
     * Checks, that page doesn't contain specified text
30
     * Example: Then I should not see "Batman is Bruce Wayne"
31
     * Example: And I should not see "Batman is Bruce Wayne".
32
     */
33 View Code Duplication
    public function assertPageNotContainsText($text, $timeout = 10000)
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...
34
    {
35
        $element = $this->findOrRetry($this->getSession()->getPage(), $text, $timeout);
36
        if ($element && $element->isVisible()) {
37
            $message = sprintf('The text "%s" was found in the text of the current page although it should not.', $text);
38
            throw new \Behat\Mink\Exception\ResponseTextException($message, $this->getSession());
39
        }
40
    }
41
42
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$element" missing
Loading history...
introduced by
Doc comment for parameter "$text" missing
Loading history...
introduced by
Doc comment for parameter "$timeout" missing
Loading history...
43
     * Checks, that element with specified CSS contains specified text
44
     * Example: Then I should see "Batman" in the "heroes_list" element
45
     * Example: And I should see "Batman" in the "heroes_list" element.
46
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
47
    public function assertElementContainsText($element, $text, $timeout = 10000)
48
    {
49
        if ($timeout <= 0) {
50
            $message = sprintf('The element "%s" was not found in the page.', $element);
51
            throw new \Behat\Mink\Exception\ResponseTextException($message, $this->getSession());
52
        }
53
        $selectorType = 'css';
54
55
        $node = $this->getSession()->getPage()->find($selectorType, $element);
56
57
        if (is_object($node)) {
58
            $item = $this->findOrRetry($node, $text);
59
            if (!$item) {
60
                $this->assertElementContainsText($element, $text, 0);
61
            }
62
        } else {
63
            $this->getSession()->wait(100);
64
65
            return $this->assertElementContainsText($element, $text, $timeout - 100);
66
        }
67
    }
68
69
    /**
70
     * @param Element   $page
71
     * @param float|int $timeout
72
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
73
    public function assertPageAddress($page, $timeout = 10000)
74
    {
75
        try {
76
            $this->assertSession()->addressEquals($this->locatePath($page));
0 ignored issues
show
Documentation introduced by
$page is of type object<Behat\Mink\Element\Element>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
77
        } catch (ExpectationException $e) {
78
            if ($timeout >= 0) {
79
                $this->getSession()->wait(100);
80
81
                return $this->assertPageAddress($page, $timeout - 100);
82
            }
83
        }
84
    }
85
86
    /**
87
     * Try to find value in element and retry for a given time.
88
     *
89
     * @param Element $element
90
     * @param string  $value
91
     * @param int     $timeout
92
     */
93
    protected function findOrRetry(Element $element, $value, $timeout = 10000)
94
    {
95
        if ($timeout <= 0) {
96
            return false;
97
        }
98
99
        // Sensitive case search
100
        /** @see http://stackoverflow.com/questions/3655549/xpath-containstext-some-string-doesnt-work-when-used-with-node-with-more/3655588#3655588 */
101
        $item = $element->find('xpath', '//*[text()[contains(., "'.$value.'")]]');
102
103 View Code Duplication
        if ($item) {
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...
104
            return $item;
105
        } else {
106
            $this->getSession()->wait(100);
107
108
            return $this->findOrRetry($element, $value, $timeout - 100);
109
        }
110
    }
111
}
112