LocatorConstraint::matches()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

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