-
Notifications
You must be signed in to change notification settings - Fork 11.8k
Expand file tree
/
Copy pathSeeInHtml.php
More file actions
138 lines (114 loc) · 2.94 KB
/
SeeInHtml.php
File metadata and controls
138 lines (114 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
namespace Illuminate\Testing\Constraints;
use PHPUnit\Framework\Constraint\Constraint;
use ReflectionClass;
class SeeInHtml extends Constraint
{
/**
* The string under validation.
*
* @var string
*/
protected $content;
/**
* The last value that failed to pass validation.
*
* @var string
*/
protected $failedValue;
/**
* Whether to negate the assertion.
*
* @var bool
*/
protected $negate;
/**
* The values must appear in order.
*
* @var bool
*/
protected $ordered;
/**
* Create a new constraint instance.
*
* @param string $content
*/
public function __construct($content, $ordered = false, $negate = false)
{
$this->content = $content;
$this->ordered = $ordered;
$this->negate = $negate;
}
/**
* Determine if the rule passes validation.
*
* @param array $values
* @return bool
*/
public function matches($values): bool
{
$normalizedContent = $this->normalize($this->content);
$position = 0;
foreach ($values as $value) {
if (empty($value)) {
continue;
}
$normalizedValue = $this->normalize($value);
$valuePosition = mb_strpos($normalizedContent, $normalizedValue, $position);
if ($this->negate) {
if ($valuePosition !== false) {
$this->failedValue = $value;
return false;
}
continue;
}
if ($valuePosition === false || $valuePosition < $position) {
$this->failedValue = $value;
return false;
}
if ($this->ordered) {
$position = $valuePosition + mb_strlen($normalizedValue);
}
}
return true;
}
/**
* Get the description of the failure.
*
* @param array $values
* @return string
*/
public function failureDescription($values): string
{
if ($this->negate) {
return sprintf(
'\'%s\' does not contain "%s".',
$this->content,
$this->failedValue
);
}
return sprintf(
'\'%s\' contains "%s"%s',
$this->content,
$this->failedValue,
$this->ordered ? ' in specified order.' : '.'
);
}
/**
* Get a string representation of the object.
*
* @return string
*/
public function toString(): string
{
return (new ReflectionClass($this))->name;
}
protected function normalize(string $value): ?string
{
$value = strip_tags($value);
$value = html_entity_decode($value, ENT_QUOTES, 'UTF-8');
$value = trim($value);
$value = preg_replace('/\s+/', ' ', $value);
return $value;
}
}