48
48
* * 'value_callback': callable || method name (in the TestCase class)
49
49
* The return value of that callback is taken as value for the test.
50
50
*
51
+ * * 'expect': If the setter modifies the value, you can specify the expected value
52
+ * for the getter here.
53
+ * * 'expect_object': see 'value_object'
54
+ * * 'expect_callback': see 'value_callback'
55
+ *
51
56
* * 'getter': - Name of getter method (a '*' gets replaced by the property name)
52
57
* - [GetterName, [arg, arg, ...]]
53
58
* Use this format to provide arguments to the getter method.
@@ -146,17 +151,22 @@ public function testSetterAndGetter($name, $spec = null): void
146
151
147
152
// Test setter
148
153
if (false !== $ spec ['setter ' ][0 ]) {
149
- $ setter = str_replace ( ' * ' , $ name , $ spec ['setter ' ][ 0 ]) ;
150
- $ setterValue = $ target ->$ setter ($ value , ...$ spec [ ' setter ' ][ 1 ] );
154
+ [ $ setter , $ setterArgs ] = $ spec ['setter ' ];
155
+ $ setterValue = $ target ->$ setter ($ value , ...$ setterArgs );
151
156
152
157
if ('__SETTER_AND_GETTER__ ' != $ spec ['setter_value ' ]) {
153
158
$ spec ['setter_assert ' ]($ spec ['setter_value ' ], $ setterValue );
154
159
}
155
160
}
156
161
157
162
if (false !== $ spec ['getter ' ][0 ]) {
158
- $ getter = str_replace ('* ' , $ name , $ spec ['getter ' ][0 ]);
159
- $ getterValue = $ target ->$ getter (...$ spec ['getter ' ][1 ]);
163
+ [$ getter , $ getterArgs ] = $ spec ['getter ' ];
164
+ $ getterValue = $ target ->$ getter (...$ getterArgs );
165
+
166
+ if ($ spec ['expect ' ] != '__SETTER_AND_GETTER__ ' ) {
167
+ $ value = $ spec ['expect ' ];
168
+ }
169
+
160
170
$ spec ['assert ' ]($ value , $ getterValue );
161
171
}
162
172
}
@@ -173,12 +183,13 @@ public function testSetterAndGetter($name, $spec = null): void
173
183
private function setterAndGetterNormalizeSpec ($ spec , string $ name , object $ target ): array
174
184
{
175
185
$ normalized = [
176
- 'getter ' => ["get* " , []],
186
+ 'getter ' => ["get $ name " , []],
177
187
'assert ' => [static ::class, 'assertEquals ' ],
178
- 'setter ' => ["set* " , []],
188
+ 'setter ' => ["set $ name " , []],
179
189
'setter_assert ' => [static ::class, 'assertEquals ' ],
180
190
'setter_value ' => '__SETTER_AND_GETTER__ ' ,
181
191
'value ' => null ,
192
+ 'expect ' => '__SETTER_AND_GETTER__ ' ,
182
193
'exception ' => null ,
183
194
];
184
195
@@ -213,10 +224,15 @@ private function setterAndGetterNormalizeSpec($spec, string $name, object $targe
213
224
$ value = [$ value , []];
214
225
}
215
226
227
+ if (is_string ($ value [0 ])) {
228
+ $ value [0 ] = str_replace ('* ' , $ name , $ value [0 ]);
229
+ }
230
+
216
231
break ;
217
232
218
233
case 'value_object ' :
219
234
case 'setter_value_object ' :
235
+ case 'expect_object ' :
220
236
if (!is_array ($ value )) {
221
237
$ value = [$ value ];
222
238
}
@@ -228,23 +244,21 @@ private function setterAndGetterNormalizeSpec($spec, string $name, object $targe
228
244
229
245
$ assertKey = str_replace ('value ' , 'assert ' , $ key );
230
246
231
- if (!isset ($ spec [$ assertKey ])) {
247
+ if (!isset ($ spec [$ assertKey ]) && array_key_exists ( $ assertKey , $ normalized ) ) {
232
248
$ normalized [$ assertKey ] = [static ::class, 'assertSame ' ];
233
249
}
234
- if ('value ' == $ key && !isset ($ spec ['property_assert ' ])) {
235
- $ normalized ['property_assert ' ] = [static ::class, 'assertAttributeSame ' ];
236
- }
237
250
238
251
break ;
239
252
240
253
case 'value_callback ' :
241
254
case 'setter_value_callback ' :
255
+ case 'expect_callback ' :
242
256
if (is_string ($ value )) {
243
257
$ value = [$ this , $ value ];
244
258
}
245
259
246
260
if (!is_callable ($ value )) {
247
- throw new \PHPUnit \Framework \Exception ($ err . 'Invalid value callback. ' );
261
+ throw new \PHPUnit \Framework \Exception ($ err . 'Invalid callback for " ' . $ key . ' " . ' );
248
262
}
249
263
250
264
$ key = substr ($ key , 0 , -9 );
@@ -263,7 +277,7 @@ private function setterAndGetterNormalizeSpec($spec, string $name, object $targe
263
277
}
264
278
265
279
if (!is_callable ($ value )) {
266
- throw new \PHPUnit \Framework \Exception ($ err . 'Invalid assert callback. ' );
280
+ throw new \PHPUnit \Framework \Exception ($ err . 'Invalid callback for " ' . $ key . ' " . ' );
267
281
}
268
282
269
283
break ;
0 commit comments