/
AssertObjectEquals.php
234 lines (207 loc) · 6.92 KB
/
AssertObjectEquals.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
<?php
namespace Yoast\PHPUnitPolyfills\Polyfills;
use ReflectionClass;
use ReflectionException;
use ReflectionNamedType;
use ReflectionObject;
use ReflectionType;
use TypeError;
use Yoast\PHPUnitPolyfills\Exceptions\InvalidComparisonMethodException;
/**
* Polyfill the Assert::assertObjectEquals() methods.
*
* Introduced in PHPUnit 9.4.0.
*
* The polyfill implementation closely matches the PHPUnit native implementation with the exception
* of the return type check and the names of the thrown exceptions.
*
* @link https://github.com/sebastianbergmann/phpunit/issues/4467
* @link https://github.com/sebastianbergmann/phpunit/issues/4707
* @link https://github.com/sebastianbergmann/phpunit/commit/1dba8c3a4b2dd04a3ff1869f75daaeb6757a14ee
* @link https://github.com/sebastianbergmann/phpunit/commit/6099c5eefccfda860c889f575d58b5fe6cc10c83
*/
trait AssertObjectEquals {
/**
* Asserts that two objects are considered equal based on a custom object comparison
* using a comparator method in the target object.
*
* The custom comparator method is expected to have the following method
* signature: `equals(self $other): bool` (or similar with a different method name).
*
* Basically, the assertion checks the following:
* - A method with name $method must exist on the $actual object.
* - The method must accept exactly one argument and this argument must be required.
* - This parameter must have a classname-based declared type.
* - The $expected object must be compatible with this declared type.
* - The method must have a declared bool return type. (JRF: not verified in this implementation)
* - `$actual->$method($expected)` returns boolean true.
*
* @param object $expected Expected value.
* @param object $actual The value to test.
* @param string $method The name of the comparator method within the object.
* @param string $message Optional failure message to display.
*
* @return void
*
* @throws TypeError When any of the passed arguments do not meet the required type.
* @throws InvalidComparisonMethodException When the comparator method does not comply with the requirements.
*/
public static function assertObjectEquals( $expected, $actual, $method = 'equals', $message = '' ) {
/*
* Parameter input validation.
* In PHPUnit this is done via PHP native type declarations. Emulating this for the polyfill.
*/
if ( \is_object( $expected ) === false ) {
throw new TypeError(
\sprintf(
'Argument 1 passed to assertObjectEquals() must be an object, %s given',
\gettype( $expected )
);
);
}
if ( \is_object( $actual ) === false ) {
throw new TypeError(
\sprintf(
'Argument 2 passed to assertObjectEquals() must be an object, %s given',
\gettype( $actual )
);
);
}
if ( \is_scalar( $method ) === false ) {
throw new TypeError(
\sprintf(
'Argument 3 passed to assertObjectEquals() must be of the type string, %s given',
\gettype( $method )
);
);
}
else {
$method = (string) $method;
}
/*
* Comparator method validation.
*/
$reflObject = new ReflectionObject( $actual );
if ( $reflObject->hasMethod( $method ) === false ) {
throw new InvalidComparisonMethodException(
\sprintf(
'Comparison method %s::%s() does not exist.',
\get_class( $actual ),
$method
)
);
}
$reflMethod = $reflObject->getMethod( $method );
/*
* As the next step, PHPUnit natively would validate the return type,
* but as return type declarations is a PHP 7.0+ feature, the polyfill
* skips this check in favour of checking the type of the actual
* returned value.
*
* Also see the upstream discussion about this:
* {@link https://github.com/sebastianbergmann/phpunit/issues/4707}
*/
/*
* Comparator method parameter requirements validation.
*/
if ( $reflMethod->getNumberOfParameters() !== 1
|| $reflMethod->getNumberOfRequiredParameters() !== 1
) {
throw new InvalidComparisonMethodException(
\sprintf(
'Comparison method %s::%s() does not declare exactly one parameter.',
\get_class( $actual ),
$method
)
);
}
$noDeclaredTypeError = \sprintf(
'Parameter of comparison method %s::%s() does not have a declared type.',
\get_class( $actual ),
$method
);
$notAcceptableTypeError = \sprintf(
'%s is not an accepted argument type for comparison method %s::%s().',
\get_class( $expected ),
\get_class( $actual ),
$method
);
$reflParameter = $reflMethod->getParameters()[0];
if ( \method_exists( $reflParameter, 'hasType' ) ) {
// PHP >= 7.0.
$hasType = $reflParameter->hasType();
if ( $hasType === false ) {
throw new InvalidComparisonMethodException( $noDeclaredTypeError );
}
$type = $reflParameter->getType();
if ( \class_exists( 'ReflectionNamedType' ) ) {
// PHP >= 7.1.
if ( ( $type instanceof ReflectionNamedType ) === false ) {
throw new InvalidComparisonMethodException( $noDeclaredTypeError );
}
$typeName = $type->getName();
}
else {
/*
* PHP 7.0.
* Checking for `ReflectionType` will not throw an error on union types,
* but then again union types are not supported on PHP 7.0.
*/
if ( ( $type instanceof ReflectionType ) === false ) {
throw new InvalidComparisonMethodException( $noDeclaredTypeError );
}
$typeName = (string) $type;
}
}
else {
// PHP < 7.0.
try {
/*
* Using `ReflectionParameter::getClass()` will trigger an autoload of the class,
* but that's okay as for a valid class type that would be triggered on the
* function call to the $method (at the end of this assertion) anyway.
*/
$hasType = $reflParameter->getClass();
} catch ( ReflectionException $e ) {
// Class with a type declaration for a non-existent class.
throw new InvalidComparisonMethodException( $notAcceptableTypeError );
}
if ( ( $hasType instanceof ReflectionClass ) === false ) {
// Array or callable type.
throw new InvalidComparisonMethodException( $noDeclaredTypeError );
}
$typeName = $hasType->name;
}
/*
* Validate that the $expected object complies with the declared parameter type.
*/
if ( $typeName === 'self' ) {
$typeName = \get_class( $actual );
}
if ( ( $expected instanceof $typeName ) === false ) {
throw new InvalidComparisonMethodException( $notAcceptableTypeError );
}
/*
* Execute the comparator method.
*/
$result = $actual->{$method}( $expected );
if ( \is_bool( $result ) === false ) {
throw new InvalidComparisonMethodException(
\sprintf(
'%s::%s() does not return a boolean value.',
\get_class( $actual ),
$method
)
);
}
$msg = \sprintf(
'Failed asserting that two objects are equal. The objects are not equal according to %s::%s()',
\get_class( $actual ),
$method
);
if ( $message !== '' ) {
$msg = $message . \PHP_EOL . $msg;
}
static::assertTrue( $result, $msg );
}
}