Skip to content

Fix deleted models #209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Middleware/ActivityMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
19 changes: 19 additions & 0 deletions tests/Fixtures/TestModelNotFoundWorkflow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures;

use Workflow\Models\StoredWorkflowLog;
use Workflow\Workflow;

class TestModelNotFoundWorkflow extends Workflow
{
public $connection = 'redis';

public $queue = 'default';

public function execute(StoredWorkflowLog $log)
{
}
}
75 changes: 75 additions & 0 deletions tests/Unit/Middleware/ActivityMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
namespace Tests\Unit\Middleware;

use Exception;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Queue;
use Mockery\MockInterface;
use Tests\Fixtures\TestActivity;
use Tests\Fixtures\TestModelNotFoundWorkflow;
use Tests\Fixtures\TestWorkflow;
use Tests\TestCase;
use Workflow\Events\ActivityCompleted;
Expand All @@ -17,6 +19,7 @@
use Workflow\Middleware\ActivityMiddleware;
use Workflow\Models\StoredWorkflow;
use Workflow\States\WorkflowCompletedStatus;
use Workflow\States\WorkflowFailedStatus;
use Workflow\States\WorkflowRunningStatus;
use Workflow\States\WorkflowWaitingStatus;
use Workflow\WorkflowStub;
Expand Down Expand Up @@ -151,4 +154,76 @@ public function testException(): void
Event::assertDispatched(ActivityFailed::class);
Queue::assertPushed(TestWorkflow::class, 1);
}

public function testModelNotFoundException(): void
{
Event::fake();
Queue::fake();

$workflow = WorkflowStub::make(TestWorkflow::class);
$workflow->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);
}
}
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

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:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy