-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Constraint.php
281 lines (250 loc) · 8.66 KB
/
Constraint.php
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Framework\Constraint;
use function assert;
use function gettype;
use function is_int;
use function is_object;
use function sprintf;
use function str_replace;
use function strpos;
use function strtolower;
use function substr;
use Countable;
use PHPUnit\Framework\ExpectationFailedException;
use PHPUnit\Framework\SelfDescribing;
use PHPUnit\Util\Exporter;
use ReflectionObject;
use SebastianBergmann\Comparator\ComparisonFailure;
/**
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
*/
abstract class Constraint implements Countable, SelfDescribing
{
/**
* Evaluates the constraint for parameter $other.
*
* If $returnResult is set to false (the default), an exception is thrown
* in case of a failure. null is returned otherwise.
*
* If $returnResult is true, the result of the evaluation is returned as
* a boolean value instead: true in case of success, false in case of a
* failure.
*
* @throws ExpectationFailedException
*/
public function evaluate(mixed $other, string $description = '', bool $returnResult = false): ?bool
{
$success = false;
if ($this->matches($other)) {
$success = true;
}
if ($returnResult) {
return $success;
}
if (!$success) {
$this->fail($other, $description);
}
return null;
}
/**
* Counts the number of constraint elements.
*/
public function count(): int
{
return 1;
}
/**
* Evaluates the constraint for parameter $other. Returns true if the
* constraint is met, false otherwise.
*
* This method can be overridden to implement the evaluation algorithm.
*/
protected function matches(mixed $other): bool
{
return false;
}
/**
* Throws an exception for the given compared value and test description.
*
* @throws ExpectationFailedException
*/
protected function fail(mixed $other, string $description, ?ComparisonFailure $comparisonFailure = null): never
{
$failureDescription = sprintf(
'Failed asserting that %s.',
$this->failureDescription($other),
);
$additionalFailureDescription = $this->additionalFailureDescription($other);
if ($additionalFailureDescription) {
$failureDescription .= "\n" . $additionalFailureDescription;
}
if (!empty($description)) {
$failureDescription = $description . "\n" . $failureDescription;
}
throw new ExpectationFailedException(
$failureDescription,
$comparisonFailure,
);
}
/**
* Return additional failure description where needed.
*
* The function can be overridden to provide additional failure
* information like a diff
*/
protected function additionalFailureDescription(mixed $other): string
{
return '';
}
/**
* Returns the description of the failure.
*
* The beginning of failure messages is "Failed asserting that" in most
* cases. This method should return the second part of that sentence.
*
* To provide additional failure information additionalFailureDescription
* can be used.
*/
protected function failureDescription(mixed $other): string
{
return Exporter::export($other) . ' ' . $this->toString();
}
/**
* Returns a custom string representation of the constraint object when it
* appears in context of an $operator expression.
*
* The purpose of this method is to provide meaningful descriptive string
* in context of operators such as LogicalNot. Native PHPUnit constraints
* are supported out of the box by LogicalNot, but externally developed
* ones had no way to provide correct strings in this context.
*
* The method shall return empty string, when it does not handle
* customization by itself.
*/
protected function toStringInContext(Operator $operator, mixed $role): string
{
return '';
}
/**
* Returns the description of the failure when this constraint appears in
* context of an $operator expression.
*
* The purpose of this method is to provide meaningful failure description
* in context of operators such as LogicalNot. Native PHPUnit constraints
* are supported out of the box by LogicalNot, but externally developed
* ones had no way to provide correct messages in this context.
*
* The method shall return empty string, when it does not handle
* customization by itself.
*/
protected function failureDescriptionInContext(Operator $operator, mixed $role, mixed $other): string
{
$string = $this->toStringInContext($operator, $role);
if ($string === '') {
return '';
}
return Exporter::export($other) . ' ' . $string;
}
/**
* Reduces the sub-expression starting at $this by skipping degenerate
* sub-expression and returns first descendant constraint that starts
* a non-reducible sub-expression.
*
* Returns $this for terminal constraints and for operators that start
* non-reducible sub-expression, or the nearest descendant of $this that
* starts a non-reducible sub-expression.
*
* A constraint expression may be modelled as a tree with non-terminal
* nodes (operators) and terminal nodes. For example:
*
* LogicalOr (operator, non-terminal)
* + LogicalAnd (operator, non-terminal)
* | + IsType('int') (terminal)
* | + GreaterThan(10) (terminal)
* + LogicalNot (operator, non-terminal)
* + IsType('array') (terminal)
*
* A degenerate sub-expression is a part of the tree, that effectively does
* not contribute to the evaluation of the expression it appears in. An example
* of degenerate sub-expression is a BinaryOperator constructed with single
* operand or nested BinaryOperators, each with single operand. An
* expression involving a degenerate sub-expression is equivalent to a
* reduced expression with the degenerate sub-expression removed, for example
*
* LogicalAnd (operator)
* + LogicalOr (degenerate operator)
* | + LogicalAnd (degenerate operator)
* | + IsType('int') (terminal)
* + GreaterThan(10) (terminal)
*
* is equivalent to
*
* LogicalAnd (operator)
* + IsType('int') (terminal)
* + GreaterThan(10) (terminal)
*
* because the subexpression
*
* + LogicalOr
* + LogicalAnd
* + -
*
* is degenerate. Calling reduce() on the LogicalOr object above, as well
* as on LogicalAnd, shall return the IsType('int') instance.
*
* Other specific reductions can be implemented, for example cascade of
* LogicalNot operators
*
* + LogicalNot
* + LogicalNot
* +LogicalNot
* + IsTrue
*
* can be reduced to
*
* LogicalNot
* + IsTrue
*/
protected function reduce(): self
{
return $this;
}
/**
* @return non-empty-string
*/
protected function valueToTypeStringFragment(mixed $value): string
{
if (is_object($value)) {
$reflector = new ReflectionObject($value);
if ($reflector->isAnonymous()) {
$name = str_replace('class@anonymous', '', $reflector->getName());
$length = strpos($name, '$');
assert(is_int($length));
$name = substr($name, 0, $length);
return 'an instance of anonymous class created at ' . $name . ' ';
}
return 'an instance of class ' . $reflector->getName() . ' ';
}
$type = strtolower(gettype($value));
if ($type === 'double') {
$type = 'float';
}
if ($type === 'resource (closed)') {
$type = 'closed resource';
}
return match ($type) {
'array', 'integer' => 'an ' . $type . ' ',
'boolean', 'closed resource', 'float', 'resource', 'string' => 'a ' . $type . ' ',
'null' => 'null ',
default => 'a value of ' . $type . ' ',
};
}
}