1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WMDE\HamcrestHtml; |
4
|
|
|
|
5
|
|
|
use Hamcrest\Description; |
6
|
|
|
use Hamcrest\Matcher; |
7
|
|
|
|
8
|
|
|
class AttributeMatcher extends TagMatcher |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @var Matcher|string |
13
|
|
|
*/ |
14
|
|
|
private $attributeNameMatcher; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var Matcher|string|null |
18
|
|
|
*/ |
19
|
|
|
private $valueMatcher; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param Matcher|string $attributeName |
23
|
|
|
* |
24
|
|
|
* @return self |
25
|
|
|
*/ |
26
|
|
|
public static function withAttribute($attributeName) { |
27
|
|
|
return new static($attributeName); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* AttributeMatcher constructor. |
32
|
|
|
* @param \Hamcrest\Matcher|string $attributeNameMatcher |
33
|
|
|
*/ |
34
|
|
|
public function __construct($attributeNameMatcher) |
35
|
|
|
{ |
36
|
|
|
parent::__construct(); |
37
|
|
|
|
38
|
|
|
$this->attributeNameMatcher = $attributeNameMatcher; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param Matcher|mixed $value |
43
|
|
|
* @return AttributeMatcher |
44
|
|
|
*/ |
45
|
|
|
public function havingValue($value) |
46
|
|
|
{ |
47
|
|
|
//TODO: Throw exception if value is set |
48
|
|
|
$result = clone $this; |
49
|
|
|
$result->valueMatcher = $value; |
50
|
|
|
|
51
|
|
|
return $result; |
52
|
|
|
} |
53
|
|
|
|
54
|
2 |
|
public function describeTo(Description $description) |
55
|
|
|
{ |
56
|
2 |
|
$description->appendText('with attribute '); |
57
|
2 |
|
if ($this->attributeNameMatcher instanceof Matcher) { |
58
|
|
|
$description->appendDescriptionOf($this->attributeNameMatcher); |
59
|
|
|
} else { |
60
|
2 |
|
$description->appendValue($this->attributeNameMatcher); |
61
|
|
|
} |
62
|
2 |
|
if ($this->valueMatcher !== null) { |
63
|
1 |
|
$description->appendText(' having value '); |
64
|
1 |
|
if ($this->attributeNameMatcher instanceof Matcher) { |
65
|
|
|
$description->appendDescriptionOf($this->valueMatcher); |
|
|
|
|
66
|
|
|
} else { |
67
|
1 |
|
$description->appendValue($this->valueMatcher); |
68
|
|
|
} |
69
|
1 |
|
} |
70
|
2 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param \DOMElement $item |
74
|
|
|
* @param Description $mismatchDescription |
75
|
|
|
* |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
6 |
|
protected function matchesSafelyWithDiagnosticDescription($item, Description $mismatchDescription) |
79
|
|
|
{ |
80
|
6 |
|
if ($this->valueMatcher === null) { |
81
|
2 |
|
if ($this->attributeNameMatcher instanceof Matcher) { |
82
|
|
|
/** @var \DOMAttr $attribute */ |
83
|
|
|
foreach ($item->attributes as $attribute) { |
84
|
|
|
if ($this->attributeNameMatcher->matches($attribute->name)) { |
85
|
|
|
return true; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} else { |
89
|
2 |
|
return $item->hasAttribute($this->attributeNameMatcher); |
90
|
|
|
} |
91
|
|
|
} else { |
92
|
4 |
|
if ($this->attributeNameMatcher instanceof Matcher) { |
93
|
|
|
if ($this->valueMatcher instanceof Matcher) { |
94
|
|
|
/** @var \DOMAttr $attribute */ |
95
|
|
View Code Duplication |
foreach ($item->attributes as $attribute) { |
|
|
|
|
96
|
|
|
if ( $this->attributeNameMatcher->matches($attribute->name) |
97
|
|
|
&& $this->valueMatcher->matches($attribute->value) |
98
|
|
|
) { |
99
|
|
|
return true; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} else { |
103
|
|
|
/** @var \DOMAttr $attribute */ |
104
|
|
View Code Duplication |
foreach ($item->attributes as $attribute) { |
|
|
|
|
105
|
|
|
if ( $this->attributeNameMatcher->matches($attribute->name) |
106
|
|
|
&& $attribute->value === $this->valueMatcher |
107
|
|
|
) { |
108
|
|
|
return true; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} else { |
113
|
4 |
|
if ($this->valueMatcher instanceof Matcher) { |
114
|
|
|
return $item->hasAttribute($this->attributeNameMatcher) |
115
|
|
|
&& $this->valueMatcher->matches($item->getAttribute($this->attributeNameMatcher)); |
116
|
|
|
} else { |
117
|
4 |
|
return $item->getAttribute($this->attributeNameMatcher) === $this->valueMatcher; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return false; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.