@@ -91,34 +91,27 @@ public function __invoke(ContextManager $context): bool
91
91
92
92
// region Action Behaviors
93
93
94
- it ('can set run expectations ' , function (): void {
94
+ it ('can set run expectations with various configurations ' , function (): void {
95
95
// 1. Arrange
96
96
$ context = new ContextManager (['count ' => 0 ]);
97
97
98
+ // Test basic expectation
98
99
TestIncrementAction::fake ();
99
100
TestIncrementAction::shouldRun ();
100
-
101
- // 2. Act
102
101
TestIncrementAction::run ($ context );
103
-
104
- // 3. Assert
105
102
TestIncrementAction::assertRan ();
106
- });
107
103
108
- it ('works with multiple calls ' , function (): void {
109
- // 1. Arrange
110
- $ context = new ContextManager (['count ' => 0 ]);
104
+ // Reset for next test
105
+ TestIncrementAction::resetFakes ();
111
106
107
+ // Test with multiple calls and specific configuration
112
108
TestIncrementAction::fake ();
113
109
TestIncrementAction::shouldRun ()
114
110
->twice ()
115
111
->withAnyArgs ();
116
112
117
- // 2. Act
118
113
TestIncrementAction::run ($ context );
119
114
TestIncrementAction::run ($ context );
120
-
121
- // 3. Assert
122
115
TestIncrementAction::assertRan ();
123
116
});
124
117
@@ -197,44 +190,135 @@ public function __invoke(ContextManager $context): bool
197
190
198
191
// endregion
199
192
193
+ // region Edge Cases for shouldRun and shouldReturn
194
+
195
+ it ('handles calling shouldReturn without explicit fake call ' , function (): void {
196
+ $ context = new ContextManager (['value ' => 5 ]);
197
+
198
+ // shouldReturn implicitly calls fake()
199
+ TestCountGuard::shouldReturn (true );
200
+ expect (TestCountGuard::isFaked ())->toBeTrue ();
201
+ expect (TestCountGuard::run ($ context ))->toBeTrue ();
202
+ });
203
+
204
+ it ('handles calling shouldRun without explicit fake call ' , function (): void {
205
+ $ context = new ContextManager (['count ' => 0 ]);
206
+
207
+ // shouldRun implicitly calls fake()
208
+ TestIncrementAction::shouldRun ()->once ();
209
+ expect (TestIncrementAction::isFaked ())->toBeTrue ();
210
+
211
+ TestIncrementAction::run ($ context );
212
+ TestIncrementAction::assertRan ();
213
+ });
214
+
215
+ it ('verifies expectation counts accurately ' , function (): void {
216
+ TestIncrementAction::shouldRun ()->times (3 );
217
+
218
+ $ context = new ContextManager ();
219
+ TestIncrementAction::run ($ context );
220
+ TestIncrementAction::run ($ context );
221
+
222
+ // Should fail if we don't call it a third time
223
+ expect (fn () => TestIncrementAction::getFake ()->mockery_verify ())
224
+ ->toThrow (\Mockery \Exception \InvalidCountException::class);
225
+ });
226
+
227
+ // endregion
228
+
200
229
// region Guard Behavior Tests
201
230
202
- it ('can fake guard behavior with different return values ' , function (): void {
231
+ it ('can fake guard behavior with various return value patterns ' , function (): void {
203
232
// 1. Arrange
204
233
$ context = new ContextManager (['count ' => 0 ]);
205
234
235
+ // Test ordered expectations with different return values
206
236
TestCountGuard::fake ();
207
237
TestCountGuard::shouldRun ()
208
238
->once ()
209
239
->andReturn (true )
210
240
->ordered ();
211
241
242
+ expect (TestCountGuard::run ($ context ))->toBeTrue ();
243
+
212
244
TestCountGuard::shouldRun ()
213
245
->once ()
214
246
->andReturn (false )
215
247
->ordered ();
216
248
217
- // 2. Act & 3. Assert
218
- expect (TestCountGuard::run ($ context ))->toBeTrue ();
219
249
expect (TestCountGuard::run ($ context ))->toBeFalse ();
220
250
TestCountGuard::assertRan ();
221
- });
222
-
223
- it ('can handle consecutive different return values ' , function (): void {
224
- // 1. Arrange
225
- $ context = new ContextManager (['count ' => 0 ]);
226
251
252
+ // Reset and test consecutive return values
253
+ TestCountGuard::resetFakes ();
227
254
TestCountGuard::fake ();
228
255
TestCountGuard::shouldRun ()
229
256
->times (3 )
230
257
->andReturn (true , false , true );
231
258
232
- // 2. Act & 3. Assert
233
259
expect (TestCountGuard::run ($ context ))->toBeTrue ();
234
260
expect (TestCountGuard::run ($ context ))->toBeFalse ();
235
261
expect (TestCountGuard::run ($ context ))->toBeTrue ();
236
262
});
237
263
264
+ it ('can handle multiple shouldReturn calls in the same test ' , function (): void {
265
+ // 1. Arrange
266
+ $ context = new ContextManager (['value ' => 10 ]);
267
+
268
+ // 2. Act & 3. Assert - First call
269
+ TestCountGuard::shouldReturn (true );
270
+ expect (TestCountGuard::run ($ context ))->toBeTrue ();
271
+
272
+ TestCountGuard::shouldReturn (false );
273
+ expect (TestCountGuard::run ($ context ))->toBeFalse ();
274
+
275
+ TestCountGuard::shouldReturn (true );
276
+ expect (TestCountGuard::run ($ context ))->toBeTrue ();
277
+ });
278
+
279
+ it ('can mix shouldRun and shouldReturn calls in the same test ' , function (): void {
280
+ // 1. Arrange
281
+ $ context = new ContextManager (['value ' => 5 ]);
282
+
283
+ TestCountGuard::shouldRun ()
284
+ ->once ()
285
+ ->andReturn (true );
286
+ expect (TestCountGuard::run ($ context ))->toBeTrue ();
287
+
288
+ TestCountGuard::shouldReturn (false );
289
+ expect (TestCountGuard::run ($ context ))->toBeFalse ();
290
+
291
+ TestCountGuard::shouldRun ()
292
+ ->once ()
293
+ ->andReturn (true );
294
+ expect (TestCountGuard::run ($ context ))->toBeTrue ();
295
+ });
296
+
297
+ // endregion
298
+
299
+ // region Complex Return Types and Edge Cases
300
+
301
+ it ('handles multiple consecutive calls with never() expectation ' , function (): void {
302
+ TestIncrementAction::shouldRun ()->never ();
303
+
304
+ // Should not throw since we're not calling it
305
+ TestIncrementAction::assertNotRan ();
306
+ });
307
+
308
+ it ('can chain multiple mock configurations ' , function (): void {
309
+ $ context = new ContextManager (['count ' => 5 ]);
310
+
311
+ TestIncrementAction::shouldRun ()
312
+ ->once ()
313
+ ->with (\Mockery::type (ContextManager::class))
314
+ ->andReturnUsing (function (ContextManager $ ctx ): void {
315
+ $ ctx ->set ('count ' , $ ctx ->get ('count ' ) * 2 );
316
+ });
317
+
318
+ TestIncrementAction::run ($ context );
319
+ expect ($ context ->get ('count ' ))->toBe (10 );
320
+ });
321
+
238
322
// endregion
239
323
240
324
// region Behavior Isolation Tests
@@ -255,6 +339,39 @@ public function __invoke(ContextManager $context): bool
255
339
TestIncrementAction::assertNotRan ();
256
340
});
257
341
342
+ it ('handles multiple shouldRun calls across different behaviors ' , function (): void {
343
+ // 1. Arrange
344
+ $ context = new ContextManager (['count ' => 0 ]);
345
+
346
+ TestIncrementAction::shouldRun ()
347
+ ->once ()
348
+ ->withAnyArgs ();
349
+
350
+ TestCountGuard::shouldReturn (false );
351
+
352
+ // 2. Act
353
+ TestIncrementAction::run ($ context );
354
+ expect (TestCountGuard::run ($ context ))->toBeFalse ();
355
+
356
+ // 3. Assert first round
357
+ TestIncrementAction::assertRan ();
358
+
359
+ // First behavior - second expectation
360
+ TestIncrementAction::shouldRun ()
361
+ ->once ()
362
+ ->andReturnUsing (function (ContextManager $ ctx ): void {
363
+ $ ctx ->set ('count ' , 10 );
364
+ });
365
+
366
+ // Second behavior - second expectation
367
+ TestCountGuard::shouldReturn (true );
368
+
369
+ // 4. Act again
370
+ TestIncrementAction::run ($ context );
371
+ expect (TestCountGuard::run ($ context ))->toBeTrue ();
372
+ expect ($ context ->get ('count ' ))->toBe (10 );
373
+ });
374
+
258
375
// endregion
259
376
260
377
// region Exception Handling Tests
@@ -277,81 +394,55 @@ public function __invoke(ContextManager $context): bool
277
394
278
395
// region Reset All Fakes
279
396
280
- it ('can reset all fakes at once ' , function (): void {
397
+ it ('completely resets all fakes and cleans up resources ' , function (): void {
281
398
// 1. Arrange
282
399
TestIncrementAction::shouldRun ()->once ();
283
400
TestCountGuard::shouldRun ()->twice ();
284
401
285
402
// 2. Act
286
403
EventMachine::resetAllFakes ();
287
404
288
- // 3. Assert
405
+ // 3. Assert - Verify fakes are reset
289
406
expect (TestIncrementAction::isFaked ())->toBeFalse ()
290
407
->and (TestCountGuard::isFaked ())->toBeFalse ()
291
408
->and (TestIncrementAction::getFake ())->toBeNull ()
292
409
->and (TestCountGuard::getFake ())->toBeNull ();
293
- });
294
-
295
- it ('removes all fake instances from container when resetting ' , function (): void {
296
- // 1. Arrange
297
- TestIncrementAction::shouldRun ()->once ();
298
- TestCountGuard::shouldRun ()->once ();
299
-
300
- // 2. Act
301
- EventMachine::resetAllFakes ();
302
410
303
- // 3. Assert
411
+ // Verify container cleanup
304
412
expect (app ()->bound (TestIncrementAction::class))->toBeFalse ()
305
413
->and (app ()->bound (TestCountGuard::class))->toBeFalse ();
306
- });
307
414
308
- it ('cleans mockery container when resetting fakes ' , function (): void {
309
- // 1. Arrange
310
- TestIncrementAction::shouldRun ()->once ();
311
- TestCountGuard::shouldRun ()->twice ();
312
-
313
- // 2. Act
314
- EventMachine::resetAllFakes ();
315
-
316
- // 3. Assert
415
+ // Verify mockery cleanup by setting new expectations
317
416
TestIncrementAction::shouldRun ()->never ();
318
- TestIncrementAction::assertNotRan ();
319
-
320
417
TestCountGuard::shouldRun ()->never ();
418
+
419
+ TestIncrementAction::assertNotRan ();
321
420
TestCountGuard::assertNotRan ();
322
421
});
323
422
324
- it ('maintains behavior isolation after resetting all fakes ' , function (): void {
325
- // 1. Arrange
423
+ it ('maintains behavior isolation and consistency after resetting fakes ' , function (): void {
424
+ // Test isolation after reset
326
425
TestIncrementAction::shouldRun ()->once ();
327
426
TestCountGuard::shouldRun ()->twice ();
328
427
EventMachine::resetAllFakes ();
329
428
330
- // 2. Act
331
429
TestIncrementAction::shouldRun ()->once ();
332
430
TestIncrementAction::run (new ContextManager (['count ' => 0 ]));
333
431
334
- // 3. Assert
335
432
TestIncrementAction::assertRan ();
336
433
expect (TestCountGuard::isFaked ())->toBeFalse ();
337
- });
338
434
339
- it ('can reset fakes with different trait instances consistently ' , function (): void {
340
- // 1. Arrange
435
+ // Test consistent reset with different trait instances
341
436
TestIncrementAction::shouldRun ()->once ();
342
437
TestCountGuard::shouldRun ()->twice ();
343
438
344
- // 2. Act
345
439
TestIncrementAction::resetAllFakes ();
346
440
347
- // Try to create new fakes
348
441
TestIncrementAction::shouldRun ()->once ();
349
442
TestCountGuard::shouldRun ()->once ();
350
443
351
- // Reset using another instance
352
444
TestCountGuard::resetAllFakes ();
353
445
354
- // 3. Assert
355
446
expect (TestIncrementAction::isFaked ())->toBeFalse ()
356
447
->and (TestCountGuard::isFaked ())->toBeFalse ()
357
448
->and (TestIncrementAction::getFake ())->toBeNull ()
0 commit comments