Completed
Pull Request — master (#7)
by Haralan
07:47 queued 06:12
created

LocatorConstraint   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 59
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 59
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 12 2
B failureDescription() 0 22 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
	function __construct($type, $selector, array $filters = array())
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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
		{
24 6
			$other->find(array($this->_type, $this->_selector, $this->_filters));
25 6
			return TRUE;
26
		}
27 1
		catch (Exception_Notfound $excption)
28
		{
29 1
			return FALSE;
30
		}
31
	}
32
33 1
	public function failureDescription($other)
34
	{
35 1
		if ($other->is_root())
36
		{
37 1
			$node_string = 'HTML page';
38
		}
39
		else
40
		{
41 1
			$node_string = $other->tag_name();
42
43 1
			if ($id = $other->attribute('id'))
44
			{
45 1
				$node_string .= '#'.$id;
46
			}
47
48 1
			if ($class = $other->attribute('class'))
49
			{
50 1
				$node_string .= '.'.join('.', explode(' ', $class));
51
			}
52
		}
53 1
		return "$node_string ".$this->toString();
54
	}
55
56
	/**
57
	 * Returns a string representation of the constraint.
58
	 *
59
	 * @return string
60
	 */
61 1
	public function toString()
62
	{
63 1
		return "has '{$this->_type}' selector '{$this->_selector}', filter ".json_encode($this->_filters);
64
	}
65
}
66