|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ProtoneMedia\LaravelDuskFakes\Bus; |
| 4 | + |
| 5 | +use Illuminate\Bus\PendingBatch; |
| 6 | +use Illuminate\Contracts\Bus\QueueingDispatcher; |
| 7 | +use Illuminate\Filesystem\Filesystem; |
| 8 | +use Illuminate\Support\Arr; |
| 9 | +use Illuminate\Support\Testing\Fakes\BusFake; |
| 10 | +use Illuminate\Support\Testing\Fakes\PendingBatchFake; |
| 11 | + |
| 12 | +class PersistentBusFake extends BusFake |
| 13 | +{ |
| 14 | + private string $directory; |
| 15 | + |
| 16 | + private string $storage; |
| 17 | + |
| 18 | + public function __construct(QueueingDispatcher $dispatcher, $jobsToFake = []) |
| 19 | + { |
| 20 | + parent::__construct($dispatcher, $jobsToFake); |
| 21 | + |
| 22 | + $this->directory = rtrim(config('dusk-fakes.bus.storage_root'), '/'); |
| 23 | + |
| 24 | + $this->storage = $this->directory.'/serialized'; |
| 25 | + |
| 26 | + (new Filesystem)->ensureDirectoryExists($this->directory); |
| 27 | + |
| 28 | + $this->loadBus(); |
| 29 | + } |
| 30 | + |
| 31 | + public function jobsToFake($jobsToFake = []) |
| 32 | + { |
| 33 | + $this->jobsToFake = Arr::wrap($jobsToFake); |
| 34 | + |
| 35 | + $this->storeBus(); |
| 36 | + } |
| 37 | + |
| 38 | + public function cleanStorage() |
| 39 | + { |
| 40 | + (new Filesystem)->cleanDirectory($this->directory); |
| 41 | + } |
| 42 | + |
| 43 | + public function loadBus(): self |
| 44 | + { |
| 45 | + $unserialized = file_exists($this->storage) |
| 46 | + ? rescue(fn () => unserialize(file_get_contents($this->storage)), [], false) |
| 47 | + : []; |
| 48 | + |
| 49 | + $this->jobsToFake = $unserialized['jobsToFake'] ?? []; |
| 50 | + $this->commands = $unserialized['commands'] ?? []; |
| 51 | + $this->commandsSync = $unserialized['commandsSync'] ?? []; |
| 52 | + $this->commandsAfterResponse = $unserialized['commandsAfterResponse'] ?? []; |
| 53 | + $this->batches = $unserialized['batches'] ?? []; |
| 54 | + |
| 55 | + return $this; |
| 56 | + } |
| 57 | + |
| 58 | + public function dispatch($command) |
| 59 | + { |
| 60 | + return tap(parent::dispatch($command), fn () => $this->storeBus()); |
| 61 | + } |
| 62 | + |
| 63 | + public function dispatchSync($command, $handler = null) |
| 64 | + { |
| 65 | + return tap(parent::dispatchSync($command, $handler), fn () => $this->storeBus()); |
| 66 | + } |
| 67 | + |
| 68 | + public function dispatchNow($command, $handler = null) |
| 69 | + { |
| 70 | + return tap(parent::dispatchNow($command, $handler), fn () => $this->storeBus()); |
| 71 | + } |
| 72 | + |
| 73 | + public function dispatchToQueue($command) |
| 74 | + { |
| 75 | + return tap(parent::dispatchToQueue($command), fn () => $this->storeBus()); |
| 76 | + } |
| 77 | + |
| 78 | + public function dispatchAfterResponse($command) |
| 79 | + { |
| 80 | + return tap(parent::dispatchAfterResponse($command), fn () => $this->storeBus()); |
| 81 | + } |
| 82 | + |
| 83 | + public function recordPendingBatch(PendingBatch $pendingBatch) |
| 84 | + { |
| 85 | + return tap(parent::recordPendingBatch($pendingBatch), fn () => $this->storeBus()); |
| 86 | + } |
| 87 | + |
| 88 | + public function cleanupCommand(array $jobs): array |
| 89 | + { |
| 90 | + return collect($jobs)->map(function ($job) { |
| 91 | + tap(invade($job), function ($job) { |
| 92 | + if (! $job->job) { |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + $job = invade($job->job); |
| 97 | + $job->container = null; |
| 98 | + |
| 99 | + if (! $job->instance) { |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + invade($job->instance)->container = null; |
| 104 | + invade($job->instance)->dispatcher = null; |
| 105 | + }); |
| 106 | + |
| 107 | + return $job; |
| 108 | + })->all(); |
| 109 | + } |
| 110 | + |
| 111 | + private function storeBus() |
| 112 | + { |
| 113 | + (new Filesystem)->ensureDirectoryExists($this->directory); |
| 114 | + |
| 115 | + file_put_contents($this->storage, serialize([ |
| 116 | + 'jobsToFake' => $this->jobsToFake, |
| 117 | + 'commands' => collect($this->commands)->map([$this, 'cleanupCommand'])->all(), |
| 118 | + 'commandsSync' => collect($this->commandsSync)->map([$this, 'cleanupCommand'])->all(), |
| 119 | + 'commandsAfterResponse' => collect($this->commandsAfterResponse)->map([$this, 'cleanupCommand'])->all(), |
| 120 | + 'batches' => collect($this->batches)->each(function (PendingBatchFake $batch) { |
| 121 | + tap(invade($batch), function ($batch) { |
| 122 | + $batch->bus = null; |
| 123 | + }); |
| 124 | + |
| 125 | + return $batch; |
| 126 | + })->all(), |
| 127 | + ])); |
| 128 | + } |
| 129 | +} |
0 commit comments