Completed
Pull Request — master (#7)
by Haralan
09:53 queued 08:07
created

LocatorConstraint::matches()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 2
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