Skip to content

Commit 569d322

Browse files
authored
Merge pull request #258 from laravel-workflow/serializes-models
Fixed support for SerializesModels
2 parents 92dbd6d + fc701f4 commit 569d322

6 files changed

Lines changed: 143 additions & 2 deletions

File tree

src/Activity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Illuminate\Contracts\Queue\ShouldQueue;
1212
use Illuminate\Foundation\Bus\Dispatchable;
1313
use Illuminate\Queue\InteractsWithQueue;
14-
use Illuminate\Queue\SerializesModels;
1514
use Illuminate\Routing\RouteDependencyResolverTrait;
1615
use Illuminate\Support\Facades\App;
1716
use Illuminate\Support\Facades\Cache;
@@ -24,6 +23,7 @@
2423
use Workflow\Middleware\WithoutOverlappingMiddleware;
2524
use Workflow\Models\StoredWorkflow;
2625
use Workflow\Serializers\Serializer;
26+
use Workflow\Traits\SerializesModels;
2727

2828
class Activity implements ShouldBeEncrypted, ShouldQueue
2929
{

src/Traits/SerializesModels.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Workflow\Traits;
6+
7+
use Illuminate\Queue\SerializesModels as LaravelSerializesModels;
8+
9+
trait SerializesModels
10+
{
11+
use LaravelSerializesModels {
12+
getSerializedPropertyValue as parentGetSerializedPropertyValue;
13+
getRestoredPropertyValue as parentGetRestoredPropertyValue;
14+
}
15+
16+
public function getSerializedPropertyValue($value)
17+
{
18+
if (is_array($value)) {
19+
$result = [];
20+
foreach ($value as $index => $item) {
21+
$result[$index] = $this->parentGetSerializedPropertyValue($item);
22+
}
23+
return $result;
24+
}
25+
26+
return $this->parentGetSerializedPropertyValue($value);
27+
}
28+
29+
public function getRestoredPropertyValue($value)
30+
{
31+
if (is_array($value)) {
32+
$result = [];
33+
foreach ($value as $index => $item) {
34+
$result[$index] = $this->parentGetRestoredPropertyValue($item);
35+
}
36+
return $result;
37+
}
38+
39+
return $this->parentGetRestoredPropertyValue($value);
40+
}
41+
}

src/Workflow.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use Illuminate\Contracts\Queue\ShouldQueue;
1313
use Illuminate\Foundation\Bus\Dispatchable;
1414
use Illuminate\Queue\InteractsWithQueue;
15-
use Illuminate\Queue\SerializesModels;
1615
use Illuminate\Routing\RouteDependencyResolverTrait;
1716
use Illuminate\Support\Carbon;
1817
use Illuminate\Support\Facades\App;
@@ -27,6 +26,7 @@
2726
use Workflow\States\WorkflowRunningStatus;
2827
use Workflow\States\WorkflowWaitingStatus;
2928
use Workflow\Traits\Sagas;
29+
use Workflow\Traits\SerializesModels;
3030

3131
class Workflow implements ShouldBeEncrypted, ShouldQueue
3232
{
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Fixtures;
6+
7+
use Workbench\App\Models\User;
8+
use Workflow\Activity;
9+
10+
class TestModelActivity extends Activity
11+
{
12+
public function execute(User $user)
13+
{
14+
return $user->id;
15+
}
16+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Fixtures;
6+
7+
use Workbench\App\Models\User;
8+
use Workflow\ActivityStub;
9+
use Workflow\Workflow;
10+
11+
class TestModelWorkflow extends Workflow
12+
{
13+
public function execute(User $user)
14+
{
15+
$result = yield ActivityStub::make(TestModelActivity::class, $user);
16+
17+
return $result;
18+
}
19+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Unit\Traits;
6+
7+
use Illuminate\Contracts\Database\ModelIdentifier;
8+
use Tests\Fixtures\TestModelActivity;
9+
use Tests\Fixtures\TestModelWorkflow;
10+
use Tests\TestCase;
11+
use Workbench\App\Models\User;
12+
use Workflow\Models\StoredWorkflow;
13+
14+
class SerializesModelsTest extends TestCase
15+
{
16+
public function testActivityJobSerializationWithModel(): void
17+
{
18+
$user = User::create([
19+
'name' => 'Test User',
20+
'email' => 'test@example.com',
21+
'password' => 'password',
22+
]);
23+
24+
$storedWorkflow = StoredWorkflow::create([
25+
'class' => TestModelWorkflow::class,
26+
]);
27+
$activity = new TestModelActivity(0, now()->toDateTimeString(), $storedWorkflow, $user);
28+
29+
$serialized = serialize($activity);
30+
31+
$unserialized = unserialize($serialized);
32+
33+
$userArgument = $unserialized->arguments[0];
34+
35+
$this->assertStringContainsString(ModelIdentifier::class, $serialized);
36+
$this->assertStringNotContainsString($user->email, $serialized);
37+
$this->assertInstanceOf(User::class, $userArgument);
38+
$this->assertEquals($user->id, $userArgument->id);
39+
}
40+
41+
public function testWorkflowJobSerializationWithModel(): void
42+
{
43+
$user = User::create([
44+
'name' => 'Test User',
45+
'email' => 'test@example.com',
46+
'password' => 'password',
47+
]);
48+
49+
$storedWorkflow = StoredWorkflow::create([
50+
'class' => TestModelWorkflow::class,
51+
]);
52+
$workflow = new TestModelWorkflow($storedWorkflow, $user);
53+
54+
$serialized = serialize($workflow);
55+
56+
$unserialized = unserialize($serialized);
57+
58+
$userArgument = $unserialized->arguments[0];
59+
60+
$this->assertStringContainsString(ModelIdentifier::class, $serialized);
61+
$this->assertStringNotContainsString($user->email, $serialized);
62+
$this->assertInstanceOf(User::class, $userArgument);
63+
$this->assertEquals($user->id, $userArgument->id);
64+
}
65+
}

0 commit comments

Comments
 (0)