diff --git a/src/Middleware/ActivityMiddleware.php b/src/Middleware/ActivityMiddleware.php index f769722..3f6ea4f 100644 --- a/src/Middleware/ActivityMiddleware.php +++ b/src/Middleware/ActivityMiddleware.php @@ -41,6 +41,9 @@ public function handle($job, $next): void now() ->format('Y-m-d\TH:i:s.u\Z') ); + } catch (\Illuminate\Database\Eloquent\ModelNotFoundException $throwable) { + $job->storedWorkflow->toWorkflow() + ->fail($throwable); } catch (\Spatie\ModelStates\Exceptions\TransitionNotFound) { if ($job->storedWorkflow->toWorkflow()->running()) { $job->release(); diff --git a/tests/Fixtures/TestModelNotFoundWorkflow.php b/tests/Fixtures/TestModelNotFoundWorkflow.php new file mode 100644 index 0000000..94d5d48 --- /dev/null +++ b/tests/Fixtures/TestModelNotFoundWorkflow.php @@ -0,0 +1,19 @@ +start(); + + $storedWorkflow = StoredWorkflow::findOrFail($workflow->id()); + $storedWorkflow->update([ + 'status' => WorkflowWaitingStatus::class, + ]); + + $activity = $this->mock(TestActivity::class); + $activity->index = 0; + $activity->now = now() + ->toDateTimeString(); + $activity->storedWorkflow = $storedWorkflow; + + $middleware = new ActivityMiddleware(); + + try { + $middleware->handle($activity, static function ($job) { + throw new ModelNotFoundException('test'); + }); + } catch (Exception $exception) { + $this->assertSame('test', $exception->getMessage()); + } + + Event::assertDispatched(ActivityStarted::class); + Event::assertDispatched(ActivityFailed::class); + Queue::assertPushed(TestWorkflow::class, 1); + + $this->assertSame(WorkflowWaitingStatus::class, $workflow->status()); + } + + public function testModelNotFoundExceptionInNextMethod(): void + { + Event::fake(); + Queue::fake(); + + $deletedWorkflow = StoredWorkflow::create([ + 'class' => TestWorkflow::class, + ]); + + $workflow = WorkflowStub::make(TestModelNotFoundWorkflow::class); + $workflow->start($deletedWorkflow); + + $storedWorkflow = StoredWorkflow::findOrFail($workflow->id()); + $storedWorkflow->update([ + 'status' => WorkflowWaitingStatus::class, + ]); + + $deletedWorkflow->delete(); + + $activity = $this->mock(TestActivity::class); + $activity->index = 0; + $activity->now = now() + ->toDateTimeString(); + $activity->storedWorkflow = $storedWorkflow; + + $middleware = new ActivityMiddleware(); + + $middleware->handle($activity, static function ($job) { + return true; + }); + + $this->assertSame(WorkflowFailedStatus::class, $workflow->status()); + + Queue::assertPushed(TestWorkflow::class, 0); + } }
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: