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

InputTrait   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 89
rs 10
wmc 11
lcom 0
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getSelectInput() 0 4 1
A getSelect() 0 4 1
B enterInput() 0 24 5
A submitInput() 0 8 1
A assertInput() 0 7 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_WebDriverElement as WebDriverElement;
13
14
/**
15
 * Trait InputTrait
16
 */
17
trait InputTrait
18
{
19
    /**
20
     * Return select input object
21
     *
22
     * @param string $name
23
     * @return WebDriver\Select
24
     */
25
    protected function getSelectInput($name)
26
    {
27
        return new WebDriver\Select($this->elementByName($name));
0 ignored issues
show
Bug introduced by
It seems like elementByName() 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...
28
    }
29
30
    /**
31
     * @param string $name
32
     * @return WebDriver\Select
33
     * @deprecated use getSelectInput() instead
34
     * @todo remove, deprecated
35
     */
36
    protected function getSelect($name)
37
    {
38
        return $this->getSelectInput($name);
39
    }
40
41
    /**
42
     * Enters the input value
43
     *
44
     * @param string|WebDriverElement $name
45
     * @param string $value
46
     * @param callable|false|null $callback
47
     * @return $this
48
     */
49
    protected function enterInput($name, $value, $callback = null)
50
    {
51
        $resolveElm = function () use ($name) {
52
            if ($name instanceof WebDriver\ElementInterface
53
                || $name instanceof WebDriverElement
0 ignored issues
show
Bug introduced by
The class PHPWebDriver_WebDriverElement does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
54
            ) {
55
                return $name;
56
            }
57
            return $this->elementByName($name);
0 ignored issues
show
Bug introduced by
It seems like elementByName() 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...
58
        };
59
60
        /** @var WebDriverElement|WebDriver\ElementInterface $elm */
61
        $elm = $resolveElm();
62
        if (null === $callback) {
63
            $this->sleep(1);
0 ignored issues
show
Bug introduced by
It seems like sleep() 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...
64
            $elm->clear();
65
            $elm->sendKeys('');
66
            $this->sleep(1);
0 ignored issues
show
Bug introduced by
It seems like sleep() 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...
67
        }
68
        $elm->clear();
69
        $elm->sendKeys($value);
70
        is_callable($callback) and call_user_func($callback, $elm);
71
        return $this;
72
    }
73
74
    /**
75
     * Submit the input value
76
     *
77
     * @param string|WebDriverElement $name
78
     * @param string $value
79
     * @return $this
80
     */
81
    protected function submitInput($name, $value)
82
    {
83
        $this->enterInput($name, $value, function ($elm) {
84
            /** @var $elm WebDriver\ElementInterface */
85
            $elm->submit();
86
        });
87
        return $this;
88
    }
89
90
    /**
91
     * Assert that input value is same than expected
92
     *
93
     * @param string|WebDriverElement|WebDriver\ElementInterface $name
94
     * @param string $expectedValue
95
     * @param callable $callback
96
     * @return $this
97
     */
98
    public function assertInput($name, $expectedValue, callable $callback = null)
99
    {
100
        $elm = ($name instanceof WebDriverElement) ? $name : $this->elementByName($name);
0 ignored issues
show
Bug introduced by
It seems like elementByName() 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...
Bug introduced by
The class PHPWebDriver_WebDriverElement does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
101
        $this->assertSame($expectedValue, $elm->attribute('value'));
0 ignored issues
show
Bug introduced by
It seems like assertSame() 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...
102
        $callback and call_user_func($callback, $elm);
103
        return $this;
104
    }
105
}
106