Completed
Push — master ( bac4fc...cf20ea )
by Haralan
13:32 queued 11:39
created

NegativeLocatorConstraint   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 77.27%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 55
ccs 17
cts 22
cp 0.7727
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 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_Found;
6
use PHPUnit\Framework\Constraint\Constraint;
7
8
class NegativeLocatorConstraint extends Constraint
9
{
10
    protected $_type;
11
    protected $_selector;
12
    protected $_filters;
13
14 2
    public function __construct($type, $selector, array $filters = [])
15
    {
16 2
        $this->_type = $type;
17 2
        $this->_selector = $selector;
18 2
        $this->_filters = $filters;
19
20 2
        parent::__construct();
21 2
    }
22
23 2
    protected function matches($other): bool
24
    {
25
        try {
26 2
            $other->not_present([$this->_type, $this->_selector, $this->_filters]);
27
28 2
            return true;
29 1
        } catch (Exception_Found $excption) {
30 1
            return false;
31
        }
32
    }
33
34 1
    protected function failureDescription($other): string
35
    {
36 1
        if ($other->is_root()) {
37 1
            $node_string = 'HTML page';
38
        } else {
39
            $node_string = $other->tag_name();
40
41
            if ($id = $other->attribute('id')) {
42
                $node_string .= '#'.$id;
43
            }
44
45
            if ($class = $other->attribute('class')) {
46
                $node_string .= '.'.implode('.', explode(' ', $class));
47
            }
48
        }
49
50 1
        return "$node_string ".$this->toString();
51
    }
52
53
    /**
54
     * Returns a string representation of the constraint.
55
     *
56
     * @return string
57
     */
58 1
    public function toString(): string
59
    {
60 1
        return "does not have '{$this->_type}' selector '{$this->_selector}', filter ".json_encode($this->_filters);
61
    }
62
}
63