diff --git a/composer.json b/composer.json index d3425fb..1614389 100644 --- a/composer.json +++ b/composer.json @@ -70,7 +70,7 @@ "Tarfinlabs\\EventMachine\\MachineServiceProvider" ], "aliases": { - "MachineFacade": "Tarfinlabs\\EventMachine\\Facades\\MachineFacade" + "EventMachine": "Tarfinlabs\\EventMachine\\Facades\\EventMachine" } } }, diff --git a/src/EventMachine.php b/src/EventMachine.php new file mode 100644 index 0000000..71f54be --- /dev/null +++ b/src/EventMachine.php @@ -0,0 +1,18 @@ +mockery_getExpectations()) as $method) { + $mock->mockery_setExpectationsFor( + $method, + new Mockery\ExpectationDirector($method, $mock), + ); + } + + $mock->mockery_teardown(); + } + /** * Reset all fakes. */ public static function resetFakes(): void { - static::$fakes = []; - if (App::has(id: static::class)) { - App::forgetInstance(abstract: static::class); + if (isset(static::$fakes[static::class])) { + $mock = static::$fakes[static::class]; + + self::cleanupLaravelContainer(class: static::class); + self::cleanupMockeryExpectations($mock); + + unset(static::$fakes[static::class]); + } + } + + /** + * Reset all fakes in application container. + */ + public static function resetAllFakes(): void + { + foreach (static::$fakes as $class => $mock) { + self::cleanupLaravelContainer(class: $class); + self::cleanupMockeryExpectations($mock); } + + static::$fakes = []; } /** diff --git a/tests/InvokableBehaviorFakeTest.php b/tests/InvokableBehaviorFakeTest.php index 9b497b7..90563af 100644 --- a/tests/InvokableBehaviorFakeTest.php +++ b/tests/InvokableBehaviorFakeTest.php @@ -7,6 +7,7 @@ use RuntimeException; use Mockery\MockInterface; use Tarfinlabs\EventMachine\ContextManager; +use Tarfinlabs\EventMachine\Facades\EventMachine; use Tarfinlabs\EventMachine\Behavior\GuardBehavior; use Tarfinlabs\EventMachine\Behavior\ActionBehavior; @@ -273,3 +274,88 @@ public function __invoke(ContextManager $context): bool }); // endregion + +// region Reset All Fakes + +it('can reset all fakes at once', function (): void { + // 1. Arrange + TestIncrementAction::shouldRun()->once(); + TestCountGuard::shouldRun()->twice(); + + // 2. Act + EventMachine::resetAllFakes(); + + // 3. Assert + expect(TestIncrementAction::isFaked())->toBeFalse() + ->and(TestCountGuard::isFaked())->toBeFalse() + ->and(TestIncrementAction::getFake())->toBeNull() + ->and(TestCountGuard::getFake())->toBeNull(); +}); + +it('removes all fake instances from container when resetting', function (): void { + // 1. Arrange + TestIncrementAction::shouldRun()->once(); + TestCountGuard::shouldRun()->once(); + + // 2. Act + EventMachine::resetAllFakes(); + + // 3. Assert + expect(app()->bound(TestIncrementAction::class))->toBeFalse() + ->and(app()->bound(TestCountGuard::class))->toBeFalse(); +}); + +it('cleans mockery container when resetting fakes', function (): void { + // 1. Arrange + TestIncrementAction::shouldRun()->once(); + TestCountGuard::shouldRun()->twice(); + + // 2. Act + EventMachine::resetAllFakes(); + + // 3. Assert + TestIncrementAction::shouldRun()->never(); + TestIncrementAction::assertNotRan(); + + TestCountGuard::shouldRun()->never(); + TestCountGuard::assertNotRan(); +}); + +it('maintains behavior isolation after resetting all fakes', function (): void { + // 1. Arrange + TestIncrementAction::shouldRun()->once(); + TestCountGuard::shouldRun()->twice(); + EventMachine::resetAllFakes(); + + // 2. Act + TestIncrementAction::shouldRun()->once(); + TestIncrementAction::run(new ContextManager(['count' => 0])); + + // 3. Assert + TestIncrementAction::assertRan(); + expect(TestCountGuard::isFaked())->toBeFalse(); +}); + +it('can reset fakes with different trait instances consistently', function (): void { + // 1. Arrange + TestIncrementAction::shouldRun()->once(); + TestCountGuard::shouldRun()->twice(); + + // 2. Act + TestIncrementAction::resetAllFakes(); + + // Try to create new fakes + TestIncrementAction::shouldRun()->once(); + TestCountGuard::shouldRun()->once(); + + // Reset using another instance + TestCountGuard::resetAllFakes(); + + // 3. Assert + expect(TestIncrementAction::isFaked())->toBeFalse() + ->and(TestCountGuard::isFaked())->toBeFalse() + ->and(TestIncrementAction::getFake())->toBeNull() + ->and(TestCountGuard::getFake())->toBeNull(); +}); + +// endregion