|
| 1 | +<?php |
| 2 | + |
| 3 | +use Kirschbaum\Loop\Facades\Loop as LoopFacade; |
| 4 | +use Kirschbaum\Loop\Loop; |
| 5 | +use Kirschbaum\Loop\Toolkits\LaravelModelToolkit; |
| 6 | +use Workbench\App\Models\User; |
| 7 | + |
| 8 | +beforeEach(function () { |
| 9 | + app()->forgetInstance(Loop::class); |
| 10 | + app()->forgetInstance('loop.registrations'); |
| 11 | +}); |
| 12 | + |
| 13 | +test('toolkits persist after instance recreation', function () { |
| 14 | + LoopFacade::toolkit(LaravelModelToolkit::make([User::class])); |
| 15 | + |
| 16 | + $firstInstance = app(Loop::class); |
| 17 | + $firstTools = $firstInstance->getPrismTools(); |
| 18 | + |
| 19 | + expect($firstTools->count()) |
| 20 | + ->toBeGreaterThan(0, 'First instance should have tools from registered toolkit'); |
| 21 | + |
| 22 | + app()->forgetInstance(Loop::class); // Simulates Octane instance recreation. |
| 23 | + |
| 24 | + $secondInstance = app(Loop::class); |
| 25 | + $secondTools = $secondInstance->getPrismTools(); |
| 26 | + |
| 27 | + expect($secondTools->count()) |
| 28 | + ->toBeGreaterThan(0, 'Second instance should have restored tools from registrations') |
| 29 | + ->and($secondTools->count()) |
| 30 | + ->toEqual($firstTools->count(), 'Both instances should have the same number of tools'); |
| 31 | +}); |
| 32 | + |
| 33 | +test('multiple toolkit registrations are preserved', function () { |
| 34 | + LoopFacade::toolkit(LaravelModelToolkit::make([User::class])); |
| 35 | + LoopFacade::toolkit(LaravelModelToolkit::make([User::class])); |
| 36 | + |
| 37 | + $firstInstance = app(Loop::class); |
| 38 | + $firstTools = $firstInstance->getPrismTools(); |
| 39 | + $firstCount = $firstTools->count(); |
| 40 | + |
| 41 | + expect($firstCount)->toBeGreaterThan(0, 'Should have tools from multiple registrations'); |
| 42 | + |
| 43 | + app()->forgetInstance(Loop::class); |
| 44 | + |
| 45 | + $secondInstance = app(Loop::class); |
| 46 | + $secondTools = $secondInstance->getPrismTools(); |
| 47 | + |
| 48 | + expect($secondTools->count())->toEqual($firstCount, 'All toolkit registrations should be preserved'); |
| 49 | +}); |
| 50 | + |
| 51 | +test('empty registrations handle gracefully', function () { |
| 52 | + $instance = app(Loop::class); |
| 53 | + $tools = $instance->getPrismTools(); |
| 54 | + |
| 55 | + expect($tools->count())->toEqual(0, 'Should have no tools when nothing is registered'); |
| 56 | +}); |
| 57 | + |
| 58 | +test('registrations are stored correctly', function () { |
| 59 | + LoopFacade::toolkit(LaravelModelToolkit::make([User::class])); |
| 60 | + |
| 61 | + expect(app()->bound('loop.registrations'))->toBeTrue('Registrations should be stored in container'); |
| 62 | + |
| 63 | + $registrations = app('loop.registrations'); |
| 64 | + expect($registrations) |
| 65 | + ->toBeArray('Registrations should be an array') |
| 66 | + ->and(count($registrations)) |
| 67 | + ->toBeGreaterThan(0, 'Should have at least one registration'); |
| 68 | + |
| 69 | + foreach ($registrations as $registration) { |
| 70 | + expect(is_callable($registration))->toBeTrue('Each registration should be callable'); |
| 71 | + } |
| 72 | +}); |
0 commit comments