Completed
Push — develop ( 723ec8...69e22a )
by Peter
08:18
created

WaitTrait   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 55
rs 10
wmc 8
lcom 1
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A sleep() 0 5 2
A waitFor() 0 11 3
A waitForAjax() 0 15 3
1
<?php
2
/**
3
 * Webino (http://webino.sk/)
4
 *
5
 * @link        https://github.com/webino/WebinoDev/ for the canonical source repository
6
 * @copyright   Copyright (c) 2018 Webino, s. r. o. (http://webino.sk/)
7
 * @license     BSD-3-Clause
8
 */
9
10
namespace WebinoDev\Test\Selenium;
11
12
use PHPWebDriver_WebDriverWait as Wait;
13
14
/**
15
 * Trait WaitTrait
16
 *
17
 * @property-read object $session
18
 */
19
trait WaitTrait
20
{
21
    /**
22
     * @param float $sec
23
     * @return $this
24
     */
25
    protected function sleep($sec = 2.0)
26
    {
27
        $sec && sleep($sec);
28
        return $this;
29
    }
30
31
    /**
32
     * Wait for something, then do something else
33
     *
34
     * @param callable|object $action
35
     * @param callable|object $callback
36
     * @return $this
37
     */
38
    protected function waitFor(callable $action, callable $callback = null)
39
    {
40
        try {
41
            $elm = (new Wait($this->session))->until($action);
42
            $callback and call_user_func($callback, $elm);
43
        } catch (\Throwable $exc) {
44
            $this->fail($exc->getMessage());
0 ignored issues
show
Bug introduced by
It seems like fail() 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...
45
        }
46
47
        return $this;
48
    }
49
50
    /**
51
     * Ajax wait
52
     *
53
     * Depends on jQuery.
54
     *
55
     * @param float $delay Seconds
56
     * @return $this
57
     */
58
    protected function waitForAjax($delay = .1)
59
    {
60
        try {
61
            // delay slightly more than required
62
            $delay and usleep($delay * 1100000);
63
64
            $this->waitFor(function () {
65
                return $this->session->execute(['script' => 'return !jQuery.active', 'args' => []]);
66
            });
67
        } catch (\Throwable $exc) {
68
            $this->fail($exc->getMessage());
0 ignored issues
show
Bug introduced by
It seems like fail() 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...
69
        }
70
71
        return $this;
72
    }
73
}
74