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

LocatorConstraint   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 53
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A matches() 0 10 2
A failureDescription() 0 18 4
A toString() 0 4 1
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