Skip to content

Better exceptions #222

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 18 commits into from
Mar 22, 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
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
run: vendor/bin/ecs check

- name: Run static analysis via PHPStan
run: vendor/bin/phpstan --xdebug analyse src tests
run: vendor/bin/phpstan analyse src tests

- name: Create databases
run: |
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ composer.lock
vendor
coverage
.env
.phpunit.cache
.phpunit.result.cache
.php_cs.cache
.php-cs-fixer.cache
3 changes: 1 addition & 2 deletions src/Traits/Timers.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Workflow\Traits;

use Carbon\CarbonInterval;
use Illuminate\Database\QueryException;
use React\Promise\Deferred;
use React\Promise\PromiseInterface;
use function React\Promise\resolve;
Expand Down Expand Up @@ -64,7 +63,7 @@ public static function timer($seconds): PromiseInterface
'class' => Signal::class,
'result' => Serializer::serialize(true),
]);
} catch (QueryException $exception) {
} catch (\Illuminate\Database\UniqueConstraintViolationException $exception) {
// already logged
}
}
Expand Down
17 changes: 6 additions & 11 deletions src/WorkflowStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Workflow;

use Illuminate\Database\QueryException;
use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
use Illuminate\Support\Traits\Macroable;
Expand Down Expand Up @@ -220,15 +219,11 @@ public function startAsChild(StoredWorkflow $parentWorkflow, int $index, $now, .

public function fail($exception): void
{
try {
$this->storedWorkflow->exceptions()
->create([
'class' => $this->storedWorkflow->class,
'exception' => Serializer::serialize($exception),
]);
} catch (QueryException) {
// already logged
}
$this->storedWorkflow->exceptions()
->create([
'class' => $this->storedWorkflow->class,
'exception' => Serializer::serialize($exception),
]);

$this->storedWorkflow->status->transitionTo(WorkflowFailedStatus::class);

Expand Down Expand Up @@ -267,7 +262,7 @@ public function next($index, $now, $class, $result): void
'class' => $class,
'result' => Serializer::serialize($result),
]);
} catch (QueryException) {
} catch (\Illuminate\Database\UniqueConstraintViolationException $exception) {
// already logged
}

Expand Down
6 changes: 1 addition & 5 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ public static function setUpBeforeClass(): void
}

for ($i = 0; $i < self::NUMBER_OF_WORKERS; $i++) {
self::$workers[$i] = new Process([
'php',
__DIR__ . '/../vendor/orchestra/testbench-core/laravel/artisan',
'queue:work',
]);
self::$workers[$i] = new Process(['php', __DIR__ . '/../vendor/bin/testbench', 'queue:work']);
self::$workers[$i]->start();
}
}
Expand Down
4 changes: 4 additions & 0 deletions tests/Unit/Listeners/MonitorActivityCompletedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ final class MonitorActivityCompletedTest extends TestCase
{
public function testHandle(): void
{
$this->app->make('cache')
->store()
->clear();

config([
'workflows.monitor_url' => 'http://test',
]);
Expand Down
4 changes: 4 additions & 0 deletions tests/Unit/Listeners/MonitorActivityFailedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ final class MonitorActivityFailedTest extends TestCase
{
public function testHandle(): void
{
$this->app->make('cache')
->store()
->clear();

config([
'workflows.monitor_url' => 'http://test',
]);
Expand Down
4 changes: 4 additions & 0 deletions tests/Unit/Listeners/MonitorActivityStartedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ final class MonitorActivityStartedTest extends TestCase
{
public function testHandle(): void
{
$this->app->make('cache')
->store()
->clear();

config([
'workflows.monitor_url' => 'http://test',
]);
Expand Down
4 changes: 4 additions & 0 deletions tests/Unit/Listeners/MonitorWorkflowCompletedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ final class MonitorWorkflowCompletedTest extends TestCase
{
public function testHandle(): void
{
$this->app->make('cache')
->store()
->clear();

config([
'workflows.monitor_url' => 'http://test',
]);
Expand Down
4 changes: 4 additions & 0 deletions tests/Unit/Listeners/MonitorWorkflowFailedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ final class MonitorWorkflowFailedTest extends TestCase
{
public function testHandle(): void
{
$this->app->make('cache')
->store()
->clear();

config([
'workflows.monitor_url' => 'http://test',
]);
Expand Down
4 changes: 4 additions & 0 deletions tests/Unit/Listeners/MonitorWorkflowStartedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ final class MonitorWorkflowStartedTest extends TestCase
{
public function testHandle(): void
{
$this->app->make('cache')
->store()
->clear();

config([
'workflows.monitor_url' => 'http://test',
]);
Expand Down
12 changes: 12 additions & 0 deletions tests/Unit/Middleware/WithoutOverlappingMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ final class WithoutOverlappingMiddlewareTest extends TestCase
{
public function testMiddleware(): void
{
$this->app->make('cache')
->store()
->clear();

$middleware = new WithoutOverlappingMiddleware(1, WithoutOverlappingMiddleware::WORKFLOW);
$this->assertSame($middleware->getLockKey(), 'laravel-workflow-overlap:1');
$this->assertSame($middleware->getWorkflowSemaphoreKey(), 'laravel-workflow-overlap:1:workflow');
Expand All @@ -28,6 +32,10 @@ public function testMiddleware(): void

public function testAllowsOnlyOneWorkflowInstance(): void
{
$this->app->make('cache')
->store()
->clear();

$workflow1 = $this->mock(TestWorkflow::class);
$middleware1 = new WithoutOverlappingMiddleware(1, WithoutOverlappingMiddleware::WORKFLOW);

Expand Down Expand Up @@ -65,6 +73,10 @@ public function testAllowsOnlyOneWorkflowInstance(): void

public function testAllowsMultipleActivityInstances(): void
{
$this->app->make('cache')
->store()
->clear();

$activity1 = $this->mock(TestActivity::class);
$middleware1 = new WithoutOverlappingMiddleware(1, WithoutOverlappingMiddleware::ACTIVITY);

Expand Down
56 changes: 56 additions & 0 deletions tests/Unit/Traits/TimersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

namespace Tests\Unit\Traits;

use Exception;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\UniqueConstraintViolationException;
use Mockery;
use Tests\Fixtures\TestWorkflow;
use Tests\TestCase;
use Workflow\Models\StoredWorkflow;
Expand Down Expand Up @@ -138,4 +142,56 @@ public function testLoadsStoredResult(): void
]);
$this->assertSame(true, Serializer::unserialize($workflow->logs()->firstWhere('index', 0)->result));
}

public function testHandlesDuplicateLogInsertionProperly(): void
{
$workflow = WorkflowStub::load(WorkflowStub::make(TestWorkflow::class)->id());
$storedWorkflow = StoredWorkflow::findOrFail($workflow->id());
$storedWorkflow->timers()
->create([
'index' => 0,
'stop_at' => now(),
]);
$storedWorkflow->logs()
->create([
'index' => 0,
'now' => now(),
'class' => Signal::class,
'result' => Serializer::serialize(true),
]);

$mockLogs = Mockery::mock(HasMany::class)
->shouldReceive('whereIndex')
->once()
->andReturnSelf()
->shouldReceive('first')
->once()
->andReturn(null)
->shouldReceive('create')
->andThrow(new UniqueConstraintViolationException('', '', [], new Exception()))
->getMock();

$mockStoredWorkflow = Mockery::spy($storedWorkflow);

$mockStoredWorkflow->shouldReceive('logs')
->andReturnUsing(static function () use ($mockLogs) {
return $mockLogs;
});

WorkflowStub::setContext([
'storedWorkflow' => $mockStoredWorkflow,
'index' => 0,
'now' => now(),
'replaying' => false,
]);

WorkflowStub::timer('1 minute')
->then(static function ($value) use (&$result) {
$result = $value;
});

Mockery::close();

$this->assertSame(true, $result);
}
}
36 changes: 36 additions & 0 deletions tests/Unit/WorkflowStubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Exception;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Queue;
use Tests\Fixtures\TestAwaitWorkflow;
use Tests\Fixtures\TestBadConnectionWorkflow;
use Tests\Fixtures\TestWorkflow;
Expand All @@ -14,6 +15,7 @@
use Workflow\Serializers\Serializer;
use Workflow\Signal;
use Workflow\States\WorkflowCompletedStatus;
use Workflow\States\WorkflowCreatedStatus;
use Workflow\States\WorkflowPendingStatus;
use Workflow\WorkflowStub;

Expand Down Expand Up @@ -207,4 +209,38 @@ public function testConnection(): void
$this->assertSame('redis', WorkflowStub::connection());
$this->assertSame('default', WorkflowStub::queue());
}

public function testHandlesDuplicateLogInsertionProperly(): void
{
Queue::fake();

$workflow = WorkflowStub::load(WorkflowStub::make(TestWorkflow::class)->id());
$storedWorkflow = StoredWorkflow::findOrFail($workflow->id());
$storedWorkflow->update([
'arguments' => Serializer::serialize([]),
'status' => WorkflowCreatedStatus::$name,
]);

$storedWorkflow->timers()
->create([
'index' => 0,
'stop_at' => now(),
]);
$storedWorkflow->logs()
->create([
'index' => 0,
'now' => now(),
'class' => Signal::class,
'result' => Serializer::serialize(true),
]);

$workflow = $storedWorkflow->toWorkflow();

$workflow->next(0, now(), Signal::class, true);

$this->assertSame(WorkflowPendingStatus::class, $workflow->status());
$this->assertSame(1, $workflow->logs()->count());

Queue::assertPushed(TestWorkflow::class, 1);
}
}
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