Skip to content

Commit 570e189

Browse files
committed
minor tweaks
1 parent c9509ae commit 570e189

File tree

5 files changed

+35
-7
lines changed

5 files changed

+35
-7
lines changed

database/factories/WorkflowTransitionFactory.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ public function definition(): array
1616
{
1717
return [
1818
/** @phpstan-ignore-next-line */
19-
'workflow_id' => WorkflowFactory::new()->create()->id,
19+
'workflow_id' => fn() => WorkflowFactory::new()->create()->id,
2020
/** @phpstan-ignore-next-line */
21-
'from_id' => WorkflowStatusFactory::new()->create()->id,
21+
'from_id' => fn() => WorkflowStatusFactory::new()->create()->id,
2222
/** @phpstan-ignore-next-line */
23-
'to_id' => WorkflowStatusFactory::new()->create()->id,
23+
'to_id' => fn() => WorkflowStatusFactory::new()->create()->id,
2424
'order' => random_int(1, 999),
2525
];
2626
}

src/Contracts/Workflowable.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
interface Workflowable
88
{
9+
public function initWorkflow(int|Workflow $workflow): static;
10+
911
public function getDefaultWorkflowName(): ?string;
1012

1113
public function getDefaultWorkflow(): ?Workflow;

src/Models/WorkflowStatus.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ class WorkflowStatus extends Model
1818

1919
public static function findWithName(string $name): ?WorkflowStatus
2020
{
21-
return self::where('name', $name)->first();
21+
static $status = [];
22+
23+
return $status[$name] ?? ($status[$name] = self::where('name', $name)->first());
2224
}
2325

2426
public function __toString()

src/Traits/HasWorkflows.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ public function initWorkflow(int|Workflow $workflow): static
5757
{
5858
$this->with = array_unique(array_merge($this->with, ['modelStatus']));
5959

60-
$this->usingWorkflow($workflow);
60+
$this->usingWorkflow($workflow)
61+
->loadMissing('modelStatus');
62+
6163
if ($this->modelStatus === null) {
6264
$this->createModelStatus($workflow, TransitionService::getWorkflowStartStatus($workflow));
6365
}
@@ -67,12 +69,14 @@ public function initWorkflow(int|Workflow $workflow): static
6769

6870
public function getDefaultWorkflow(): ?Workflow
6971
{
72+
static $workflows = [];
73+
7074
$name = $this->getDefaultWorkflowName();
7175
if ($name === null) {
7276
return null;
7377
}
7478

75-
return Workflow::where('name', $name)->first();
79+
return $workflows[$name] ?? ($workflows[$name] = Workflow::where('name', $name)->first());
7680
}
7781

7882
/**
@@ -86,6 +90,7 @@ public function getDefaultWorkflowName(): ?string
8690
public function usingWorkflow(null|int|Workflow $workflow): static
8791
{
8892
$this->usingWorkflow = $workflow instanceof Workflow ? $workflow : Workflow::find($workflow);
93+
8994
$this->unsetRelation('modelStatus');
9095
$this->unsetRelation('modelStatuses');
9196
$this->unsetRelation('allModelStatus');

tests/Feature/WorkflowTest.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
// 2 exits
1818
WorkflowTransitionFactory::new()->workflow($workflow->id)->from($mid1->to_id)->exit()->create();
1919
WorkflowTransitionFactory::new()->workflow($workflow->id)->from($mid2->to_id)->exit()->create();
20+
21+
22+
$this->secondaryWorkflow = $workflow = WorkflowFactory::new()->create();
23+
WorkflowTransitionFactory::new()->workflow($this->secondaryWorkflow->id)->entry()->create();
2024
});
2125

2226
test('it has correct relationships', function () {
@@ -50,7 +54,7 @@
5054
$status = $modelB->getDefaultWorkflow()->entryTransitions->first()->toStatus;
5155

5256
expect($modelB->modelStatus->status)->toEqual($modelB->getStatus())->toEqual($status)
53-
->and($modelB->modelStatus->workflow)->toEqual($modelB->getDefaultWorkflow())
57+
->and($modelB->modelStatus->workflow->id)->toEqual($modelB->getDefaultWorkflow()?->id)
5458
->and($modelB->modelStatuses)->toHaveCount(1);
5559
});
5660

@@ -92,6 +96,21 @@
9296
expect($model->isAllowed($this->mid1))->toBeFalse();
9397
});
9498

99+
test('it deletes model statuses when model is deleted', function () {
100+
($model = new WorkflowableModel())->setDefaultWorkflowName($this->workflow->name)->save();
101+
expect(WorkflowModelStatus::count())->toBe(1);
102+
103+
$model->delete();
104+
expect(WorkflowModelStatus::count())->toBe(0);
105+
});
106+
107+
test('it queries by workflow', function () {
108+
($model = new WorkflowableModel())->setDefaultWorkflowName($this->workflow->name)->save();
109+
110+
expect(WorkflowableModel::query()->inWorkflow($this->workflow->name)->count())->toBe(1)
111+
->and(WorkflowableModel::query()->inWorkflow($this->secondaryWorkflow->name)->count())->toBe(0);
112+
});
113+
95114
test('it has toString', function () {
96115
($model = new WorkflowableModel())->setDefaultWorkflowName($this->workflow->name)->save();
97116

0 commit comments

Comments
 (0)