11
11
12
12
use function get_class ;
13
13
use function is_object ;
14
+ use PHPUnit \Framework \ActualValueIsNotAnObjectException ;
15
+ use PHPUnit \Framework \ComparisonMethodDoesNotAcceptParameterTypeException ;
16
+ use PHPUnit \Framework \ComparisonMethodDoesNotDeclareBoolReturnTypeException ;
17
+ use PHPUnit \Framework \ComparisonMethodDoesNotDeclareExactlyOneParameterException ;
18
+ use PHPUnit \Framework \ComparisonMethodDoesNotDeclareParameterTypeException ;
19
+ use PHPUnit \Framework \ComparisonMethodDoesNotExistException ;
14
20
use ReflectionNamedType ;
15
21
use ReflectionObject ;
16
22
19
25
*/
20
26
final class ObjectEquals extends Constraint
21
27
{
22
- private const ACTUAL_IS_NOT_AN_OBJECT = 1 ;
23
-
24
- private const ACTUAL_DOES_NOT_HAVE_METHOD = 2 ;
25
-
26
- private const METHOD_DOES_NOT_HAVE_BOOL_RETURN_TYPE = 3 ;
27
-
28
- private const METHOD_DOES_NOT_ACCEPT_EXACTLY_ONE_ARGUMENT = 4 ;
29
-
30
- private const PARAMETER_DOES_NOT_HAVE_DECLARED_TYPE = 5 ;
31
-
32
- private const EXPECTED_NOT_COMPATIBLE_WITH_PARAMETER_TYPE = 6 ;
33
-
34
- private const OBJECTS_ARE_NOT_EQUAL_ACCORDING_TO_METHOD = 7 ;
35
-
36
28
/**
37
29
* @var object
38
30
*/
@@ -43,11 +35,6 @@ final class ObjectEquals extends Constraint
43
35
*/
44
36
private $ method ;
45
37
46
- /**
47
- * @var int
48
- */
49
- private $ failureReason ;
50
-
51
38
public function __construct (object $ object , string $ method = 'equals ' )
52
39
{
53
40
$ this ->expected = $ object ;
@@ -59,71 +46,85 @@ public function toString(): string
59
46
return 'two objects are equal ' ;
60
47
}
61
48
49
+ /**
50
+ * @throws ActualValueIsNotAnObjectException
51
+ * @throws ComparisonMethodDoesNotExistException
52
+ * @throws ComparisonMethodDoesNotDeclareBoolReturnTypeException
53
+ * @throws ComparisonMethodDoesNotDeclareExactlyOneParameterException
54
+ * @throws ComparisonMethodDoesNotDeclareParameterTypeException
55
+ * @throws ComparisonMethodDoesNotAcceptParameterTypeException
56
+ */
62
57
protected function matches ($ other ): bool
63
58
{
64
59
if (!is_object ($ other )) {
65
- $ this ->failureReason = self ::ACTUAL_IS_NOT_AN_OBJECT ;
66
-
67
- return false ;
60
+ throw new ActualValueIsNotAnObjectException ;
68
61
}
69
62
70
63
$ object = new ReflectionObject ($ other );
71
64
72
65
if (!$ object ->hasMethod ($ this ->method )) {
73
- $ this ->failureReason = self ::ACTUAL_DOES_NOT_HAVE_METHOD ;
74
-
75
- return false ;
66
+ throw new ComparisonMethodDoesNotExistException (
67
+ get_class ($ other ),
68
+ $ this ->method
69
+ );
76
70
}
77
71
78
72
/** @noinspection PhpUnhandledExceptionInspection */
79
73
$ method = $ object ->getMethod ($ this ->method );
80
74
81
75
if (!$ method ->hasReturnType ()) {
82
- $ this ->failureReason = self ::METHOD_DOES_NOT_HAVE_BOOL_RETURN_TYPE ;
83
-
84
- return false ;
76
+ throw new ComparisonMethodDoesNotDeclareBoolReturnTypeException (
77
+ get_class ($ other ),
78
+ $ this ->method
79
+ );
85
80
}
86
81
87
82
$ returnType = $ method ->getReturnType ();
88
83
89
84
if (!$ returnType instanceof ReflectionNamedType) {
90
- $ this ->failureReason = self ::METHOD_DOES_NOT_HAVE_BOOL_RETURN_TYPE ;
91
-
92
- return false ;
85
+ throw new ComparisonMethodDoesNotDeclareBoolReturnTypeException (
86
+ get_class ($ other ),
87
+ $ this ->method
88
+ );
93
89
}
94
90
95
91
if ($ returnType ->allowsNull ()) {
96
- $ this ->failureReason = self ::METHOD_DOES_NOT_HAVE_BOOL_RETURN_TYPE ;
97
-
98
- return false ;
92
+ throw new ComparisonMethodDoesNotDeclareBoolReturnTypeException (
93
+ get_class ($ other ),
94
+ $ this ->method
95
+ );
99
96
}
100
97
101
98
if ($ returnType ->getName () !== 'bool ' ) {
102
- $ this ->failureReason = self ::METHOD_DOES_NOT_HAVE_BOOL_RETURN_TYPE ;
103
-
104
- return false ;
99
+ throw new ComparisonMethodDoesNotDeclareBoolReturnTypeException (
100
+ get_class ($ other ),
101
+ $ this ->method
102
+ );
105
103
}
106
104
107
105
if ($ method ->getNumberOfParameters () !== 1 || $ method ->getNumberOfRequiredParameters () !== 1 ) {
108
- $ this ->failureReason = self ::METHOD_DOES_NOT_ACCEPT_EXACTLY_ONE_ARGUMENT ;
109
-
110
- return false ;
106
+ throw new ComparisonMethodDoesNotDeclareExactlyOneParameterException (
107
+ get_class ($ other ),
108
+ $ this ->method
109
+ );
111
110
}
112
111
113
112
$ parameter = $ method ->getParameters ()[0 ];
114
113
115
114
if (!$ parameter ->hasType ()) {
116
- $ this ->failureReason = self ::PARAMETER_DOES_NOT_HAVE_DECLARED_TYPE ;
117
-
118
- return false ;
115
+ throw new ComparisonMethodDoesNotDeclareParameterTypeException (
116
+ get_class ($ other ),
117
+ $ this ->method
118
+ );
119
119
}
120
120
121
121
$ type = $ parameter ->getType ();
122
122
123
123
if (!$ type instanceof ReflectionNamedType) {
124
- $ this ->failureReason = self ::PARAMETER_DOES_NOT_HAVE_DECLARED_TYPE ;
125
-
126
- return false ;
124
+ throw new ComparisonMethodDoesNotDeclareParameterTypeException (
125
+ get_class ($ other ),
126
+ $ this ->method
127
+ );
127
128
}
128
129
129
130
$ typeName = $ type ->getName ();
@@ -133,73 +134,18 @@ protected function matches($other): bool
133
134
}
134
135
135
136
if (!$ this ->expected instanceof $ typeName ) {
136
- $ this ->failureReason = self ::EXPECTED_NOT_COMPATIBLE_WITH_PARAMETER_TYPE ;
137
-
138
- return false ;
137
+ throw new ComparisonMethodDoesNotAcceptParameterTypeException (
138
+ get_class ($ other ),
139
+ $ this ->method ,
140
+ get_class ($ this ->expected )
141
+ );
139
142
}
140
143
141
- if ($ other ->{$ this ->method }($ this ->expected )) {
142
- return true ;
143
- }
144
-
145
- $ this ->failureReason = self ::OBJECTS_ARE_NOT_EQUAL_ACCORDING_TO_METHOD ;
146
-
147
- return false ;
144
+ return $ other ->{$ this ->method }($ this ->expected );
148
145
}
149
146
150
147
protected function failureDescription ($ other ): string
151
148
{
152
149
return $ this ->toString ();
153
150
}
154
-
155
- protected function additionalFailureDescription ($ other ): string
156
- {
157
- switch ($ this ->failureReason ) {
158
- case self ::ACTUAL_IS_NOT_AN_OBJECT :
159
- return 'Actual value is not an object. ' ;
160
-
161
- case self ::ACTUAL_DOES_NOT_HAVE_METHOD :
162
- return sprintf (
163
- '%s::%s() does not exist. ' ,
164
- get_class ($ other ),
165
- $ this ->method
166
- );
167
-
168
- case self ::METHOD_DOES_NOT_HAVE_BOOL_RETURN_TYPE :
169
- return sprintf (
170
- '%s::%s() does not declare a bool return type. ' ,
171
- get_class ($ other ),
172
- $ this ->method
173
- );
174
-
175
- case self ::METHOD_DOES_NOT_ACCEPT_EXACTLY_ONE_ARGUMENT :
176
- return sprintf (
177
- '%s::%s() does not accept exactly one argument. ' ,
178
- get_class ($ other ),
179
- $ this ->method
180
- );
181
-
182
- case self ::PARAMETER_DOES_NOT_HAVE_DECLARED_TYPE :
183
- return sprintf (
184
- 'Parameter of %s::%s() does not have a declared type. ' ,
185
- get_class ($ other ),
186
- $ this ->method
187
- );
188
-
189
- case self ::EXPECTED_NOT_COMPATIBLE_WITH_PARAMETER_TYPE :
190
- return sprintf (
191
- '%s is not accepted an accepted argument type for %s::%s(). ' ,
192
- get_class ($ this ->expected ),
193
- get_class ($ other ),
194
- $ this ->method
195
- );
196
-
197
- default :
198
- return sprintf (
199
- 'The objects are not equal according to %s::%s(). ' ,
200
- get_class ($ other ),
201
- $ this ->method
202
- );
203
- }
204
- }
205
151
}
0 commit comments