Completed
Push — master ( 7243b0...3eb70f )
by Haralan
32:35 queued 30:47
created

LocatorConstraint::failureDescription()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 9
cts 9
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 5
nop 1
crap 4
1
<?php
2
3
namespace Openbuildings\PHPUnitSpiderling\Constraint;
4
5
use Openbuildings\Spiderling\Exception_Notfound;
6
7
class LocatorConstraint extends \PHPUnit\Framework\Constraint\Constraint
8
{
9
    protected $_type;
10
    protected $_selector;
11
    protected $_filters;
12
13 6
    public function __construct($type, $selector, array $filters = [])
14
    {
15 6
        $this->_type = $type;
16 6
        $this->_selector = $selector;
17 6
        $this->_filters = $filters;
18 6
    }
19
20 6
    protected function matches($other)
21
    {
22
        try {
23 6
            $other->find([$this->_type, $this->_selector, $this->_filters]);
24
25 6
            return true;
26 1
        } catch (Exception_Notfound $excption) {
27 1
            return false;
28
        }
29
    }
30
31 1
    public function failureDescription($other)
32
    {
33 1
        if ($other->is_root()) {
34 1
            $node_string = 'HTML page';
35
        } else {
36 1
            $node_string = $other->tag_name();
37
38 1
            if ($id = $other->attribute('id')) {
39 1
                $node_string .= '#'.$id;
40
            }
41
42 1
            if ($class = $other->attribute('class')) {
43 1
                $node_string .= '.'.implode('.', explode(' ', $class));
44
            }
45
        }
46
47 1
        return "$node_string ".$this->toString();
48
    }
49
50
    /**
51
     * Returns a string representation of the constraint.
52
     *
53
     * @return string
54
     */
55 1
    public function toString()
56
    {
57 1
        return "has '{$this->_type}' selector '{$this->_selector}', filter ".json_encode($this->_filters);
58
    }
59
}
60