FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
workflow-engine-core/tests/Unit/WorkflowEngineTest.php at main · solutionforest/workflow-engine-core · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
solutionforest
/
workflow-engine-core
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
3
Star
9
Code
Issues
0
Pull requests
2
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
workflow-engine-core
/
tests
/
Unit
/
WorkflowEngineTest.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
252 lines (214 loc) · 8.05 KB
Breadcrumbs
workflow-engine-core
/
tests
/
Unit
/
WorkflowEngineTest.php
Copy path
File metadata and controls
252 lines (214 loc) · 8.05 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
use
SolutionForest
\
WorkflowEngine
\
Core
\
DefinitionParser
;
use
SolutionForest
\
WorkflowEngine
\
Core
\
WorkflowEngine
;
use
SolutionForest
\
WorkflowEngine
\
Core
\
WorkflowInstance
;
use
SolutionForest
\
WorkflowEngine
\
Core
\
WorkflowState
;
use
SolutionForest
\
WorkflowEngine
\
Exceptions
\
InvalidWorkflowDefinitionException
;
use
SolutionForest
\
WorkflowEngine
\
Exceptions
\
WorkflowInstanceNotFoundException
;
use
SolutionForest
\
WorkflowEngine
\
Tests
\
Support
\
InMemoryStorage
;
beforeEach
(
function
() {
$
this
->
storage
=
new
InMemoryStorage
;
$
this
->
engine
=
new
WorkflowEngine
(
$
this
->
storage
);
});
test
(
'
it can start a workflow
'
,
function
() {
$
definition
= [
'
name
'
=>
'
Test Workflow
'
,
'
steps
'
=> [
[
'
id
'
=>
'
step1
'
,
'
name
'
=>
'
First Step
'
,
'
action
'
=>
'
log
'
,
'
parameters
'
=> [
'
message
'
=>
'
Hello World
'
],
],
],
];
$
workflowId
=
$
this
->
engine
->
start
(
'
test-workflow
'
,
$
definition
);
expect
(
$
workflowId
)->
not
->
toBeEmpty
();
// Verify the workflow instance was created
$
instance
=
$
this
->
engine
->
getInstance
(
$
workflowId
);
expect
(
$
instance
)->
toBeInstanceOf
(WorkflowInstance::class);
expect
(
$
instance
->
getState
())->
toBe
(WorkflowState::
COMPLETED
);
// Log action completes immediately
expect
(
$
instance
->
getName
())->
toBe
(
'
Test Workflow
'
);
});
test
(
'
it can start a workflow with context
'
,
function
() {
$
definition
= [
'
name
'
=>
'
Test Workflow
'
,
'
steps
'
=> [
[
'
id
'
=>
'
step1
'
,
'
name
'
=>
'
First Step
'
,
'
action
'
=>
'
log
'
,
'
parameters
'
=> [
'
message
'
=>
'
Hello {{name}}
'
],
],
],
];
$
context
= [
'
name
'
=>
'
John
'
];
$
workflowId
=
$
this
->
engine
->
start
(
'
test-workflow
'
,
$
definition
,
$
context
);
$
instance
=
$
this
->
engine
->
getInstance
(
$
workflowId
);
$
workflowData
=
$
instance
->
getContext
()->
getData
();
// Should contain original context plus any data added by actions
expect
(
$
workflowData
[
'
name
'
])->
toBe
(
'
John
'
);
expect
(
$
workflowData
)->
toHaveKey
(
'
logged_message
'
);
// Added by LogAction
expect
(
$
workflowData
)->
toHaveKey
(
'
logged_at
'
);
// Added by LogAction
});
test
(
'
it can resume a paused workflow
'
,
function
() {
// Create a workflow with multiple steps
$
definition
= [
'
name
'
=>
'
Test Workflow
'
,
'
steps
'
=> [
[
'
id
'
=>
'
step1
'
,
'
name
'
=>
'
First Step
'
,
'
action
'
=>
'
log
'
,
'
parameters
'
=> [
'
message
'
=>
'
Hello World
'
],
],
[
'
id
'
=>
'
step2
'
,
'
name
'
=>
'
Second Step
'
,
'
action
'
=>
'
log
'
,
'
parameters
'
=> [
'
message
'
=>
'
Second step
'
],
],
],
];
// Create a paused workflow manually (bypass auto-execution)
$
parser
=
new
DefinitionParser
;
$
workflowDef
=
$
parser
->
parse
(
$
definition
);
$
workflowId
=
'
test-workflow
'
;
$
instance
=
new
WorkflowInstance
(
id:
$
workflowId
,
definition:
$
workflowDef
,
state: WorkflowState::
PAUSED
,
);
$
this
->
storage
->
save
(
$
instance
);
// Verify workflow was created
expect
(
$
workflowId
)->
toBe
(
'
test-workflow
'
);
// Resume it
$
this
->
engine
->
resume
(
$
workflowId
);
$
instance
=
$
this
->
engine
->
getInstance
(
$
workflowId
);
// After resume, it should be completed since we have simple log actions
expect
(
$
instance
->
getState
())->
toBe
(WorkflowState::
COMPLETED
);
});
test
(
'
it can cancel a workflow
'
,
function
() {
$
definition
= [
'
name
'
=>
'
Test Workflow
'
,
'
steps
'
=> [
[
'
id
'
=>
'
step1
'
,
'
name
'
=>
'
First Step
'
,
'
action
'
=>
'
log
'
,
'
parameters
'
=> [
'
message
'
=>
'
Hello World
'
],
],
],
];
// Create a workflow in RUNNING state (so it can be cancelled)
$
parser
=
new
DefinitionParser
;
$
workflowDef
=
$
parser
->
parse
(
$
definition
);
$
workflowId
=
'
test-workflow
'
;
$
instance
=
new
WorkflowInstance
(
id:
$
workflowId
,
definition:
$
workflowDef
,
state: WorkflowState::
RUNNING
,
);
$
this
->
storage
->
save
(
$
instance
);
$
this
->
engine
->
cancel
(
$
workflowId
,
'
User cancelled
'
);
$
instance
=
$
this
->
engine
->
getInstance
(
$
workflowId
);
expect
(
$
instance
->
getState
())->
toBe
(WorkflowState::
CANCELLED
);
});
test
(
'
it can get workflow status
'
,
function
() {
$
definition
= [
'
name
'
=>
'
Test Workflow
'
,
'
steps
'
=> [
[
'
id
'
=>
'
step1
'
,
'
name
'
=>
'
First Step
'
,
'
action
'
=>
'
log
'
,
'
parameters
'
=> [
'
message
'
=>
'
Hello World
'
],
],
],
];
$
workflowId
=
$
this
->
engine
->
start
(
'
test-workflow
'
,
$
definition
);
$
status
=
$
this
->
engine
->
getStatus
(
$
workflowId
);
expect
(
$
status
)->
toBeArray
();
expect
(
$
status
[
'
state
'
])->
toBe
(WorkflowState::
COMPLETED
->
value
);
expect
(
$
status
[
'
name
'
])->
toBe
(
'
Test Workflow
'
);
expect
(
$
status
)->
toHaveKey
(
'
current_step
'
);
expect
(
$
status
)->
toHaveKey
(
'
progress
'
);
});
test
(
'
it throws exception for invalid workflow definition
'
,
function
() {
$
invalidDefinition
= [
'
steps
'
=> [],
];
$
this
->
engine
->
start
(
'
test-workflow
'
,
$
invalidDefinition
);
})->
throws
(InvalidWorkflowDefinitionException::class,
'
Required field
\'
name
\'
is missing from workflow definition
'
);
test
(
'
it throws exception for nonexistent workflow
'
,
function
() {
$
this
->
engine
->
getInstance
(
'
nonexistent
'
);
})->
throws
(WorkflowInstanceNotFoundException::class,
'
Workflow instance
\'
nonexistent
\'
was not found
'
);
test
(
'
it can list workflows
'
,
function
() {
$
definition
= [
'
name
'
=>
'
Test Workflow
'
,
'
steps
'
=> [
[
'
id
'
=>
'
step1
'
,
'
name
'
=>
'
First Step
'
,
'
action
'
=>
'
log
'
,
'
parameters
'
=> [
'
message
'
=>
'
Hello World
'
],
],
],
];
$
workflowId1
=
$
this
->
engine
->
start
(
'
test-workflow-1
'
,
$
definition
);
$
workflowId2
=
$
this
->
engine
->
start
(
'
test-workflow-2
'
,
$
definition
);
$
workflows
=
$
this
->
engine
->
getInstances
();
expect
(
$
workflows
)->
toHaveCount
(
2
);
expect
(
array_map
(
fn
(
$
w
) =>
$
w
->
getId
(),
$
workflows
))->
toContain
(
$
workflowId1
);
expect
(
array_map
(
fn
(
$
w
) =>
$
w
->
getId
(),
$
workflows
))->
toContain
(
$
workflowId2
);
});
test
(
'
it can filter workflows by state
'
,
function
() {
$
definition
= [
'
name
'
=>
'
Test Workflow
'
,
'
steps
'
=> [
[
'
id
'
=>
'
step1
'
,
'
name
'
=>
'
First Step
'
,
'
action
'
=>
'
log
'
,
'
parameters
'
=> [
'
message
'
=>
'
Hello World
'
],
],
],
];
// Create a workflow that completes
$
completedId
=
$
this
->
engine
->
start
(
'
completed-workflow
'
,
$
definition
);
// Create a workflow in RUNNING state, then cancel it
$
parser
=
new
DefinitionParser
;
$
workflowDef
=
$
parser
->
parse
(
$
definition
);
$
cancelledId
=
'
cancelled-workflow
'
;
$
instance
=
new
WorkflowInstance
(
id:
$
cancelledId
,
definition:
$
workflowDef
,
state: WorkflowState::
RUNNING
,
// Create in RUNNING state so we can cancel it
);
$
this
->
storage
->
save
(
$
instance
);
// Now cancel it
$
this
->
engine
->
cancel
(
$
cancelledId
);
$
completedWorkflows
=
$
this
->
engine
->
getInstances
([
'
state
'
=> WorkflowState::
COMPLETED
]);
$
cancelledWorkflows
=
$
this
->
engine
->
getInstances
([
'
state
'
=> WorkflowState::
CANCELLED
]);
// Debug: check if workflows exist
$
allWorkflows
=
$
this
->
engine
->
getInstances
();
expect
(
$
completedWorkflows
)->
toHaveCount
(
1
);
expect
(
$
cancelledWorkflows
)->
toHaveCount
(
1
);
// Find the workflows we created
$
found
=
false
;
foreach
(
$
completedWorkflows
as
$
workflow
) {
if
(
$
workflow
->
getId
() ===
'
completed-workflow
'
) {
$
found
=
true
;
break
;
}
}
expect
(
$
found
)->
toBeTrue
();
$
found
=
false
;
foreach
(
$
cancelledWorkflows
as
$
workflow
) {
if
(
$
workflow
->
getId
() ===
'
cancelled-workflow
'
) {
$
found
=
true
;
break
;
}
}
expect
(
$
found
)->
toBeTrue
();
});
Back
|
FazBrowse Home
|
New Git URL