-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathPatternsTest.php
More file actions
272 lines (261 loc) · 10.1 KB
/
PatternsTest.php
File metadata and controls
272 lines (261 loc) · 10.1 KB
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php
namespace Tests\Feature\Patterns;
use Exception;
use ProcessMaker\Nayra\Contracts\Bpmn\ActivityInterface;
use ProcessMaker\Nayra\Contracts\Bpmn\CallActivityInterface;
use ProcessMaker\Nayra\Contracts\Bpmn\ErrorInterface;
use ProcessMaker\Nayra\Contracts\Bpmn\IntermediateCatchEventInterface;
use ProcessMaker\Nayra\Contracts\Bpmn\ScriptTaskInterface;
use ProcessMaker\Nayra\Storage\BpmnDocument;
use Tests\Feature\Engine\EngineTestCase;
/**
* Tests for the ServiceTask element
*/
class PatternsTest extends EngineTestCase
{
private $basePath = __DIR__ . '/files/';
/**
* List the bpmn files
*
* @return array
*/
public function caseProvider()
{
$data = [];
foreach (glob($this->basePath . '*.bpmn') as $bpmnFile) {
$data[] = [basename($bpmnFile)];
}
return $data;
}
/**
* Tests the bpmn process completing all active tasks
*
* @param string $bpmnFile
*
* @dataProvider caseProvider
*/
public function testProcessPatterns($bpmnFile)
{
$file = $this->basePath . $bpmnFile;
$jsonFile = substr($file, 0, -4) . 'json';
if (file_exists($jsonFile)) {
$this->runProcessWithJson($jsonFile, $file);
} else {
$this->runProcessWithoutJson($file);
}
}
/**
* Run a process without json data
*
* @param string $bpmnFile
*
* @return void
*/
private function runProcessWithoutJson($bpmnFile)
{
$bpmnRepository = new BpmnDocument();
$bpmnRepository->setEngine($this->engine);
$bpmnRepository->setFactory($this->repository);
$bpmnRepository->load($bpmnFile);
$startEvents = $bpmnRepository->getElementsByTagNameNS(BpmnDocument::BPMN_MODEL, 'startEvent');
foreach ($startEvents as $startEvent) {
$data = [];
$result = [];
$this->runProcess($bpmnFile, $data, $startEvent->getAttribute('id'), $result, [], [], [], []);
}
}
/**
* Run a process with json data
*
* @param string $jsonFile
* @param string $bpmnFile
*
* @return void
*/
private function runProcessWithJson($jsonFile, $bpmnFile)
{
$tests = json_decode(file_get_contents($jsonFile), true);
foreach ($tests as $json) {
$events = isset($json['events']) ? $json['events'] : [];
$output = isset($json['output']) ? $json['output'] : [];
$errors = isset($json['errors']) ? $json['errors'] : [];
$this->runProcess($bpmnFile, $json['data'], $json['startEvent'], $json['result'], $events, $output, $errors, $json);
}
}
/**
* Run a process
*
* @param string $filename
* @param array $data
* @param string $startEvent
* @param array $result
* @param array $events
* @param mixed $output
* @param array $errors
* @param array $json
*
* @return void
*/
private function runProcess($filename, $data, $startEvent, $result, $events, $output, $errors, $json)
{
$bpmnRepository = new BpmnDocument();
$bpmnRepository->setEngine($this->engine);
$bpmnRepository->setFactory($this->repository);
$bpmnRepository->load($filename);
$start = $bpmnRepository->getStartEvent($startEvent);
$process = $start->getProcess();
$dataStore = $this->repository->createDataStore();
$dataStore->setData($data);
// set global data storage
$this->engine->setDataStore($dataStore);
$this->engine->loadBpmnDocument($bpmnRepository);
// create instance with initial data
if ($start->getEventDefinitions()->count() > 0) {
$start->execute($start->getEventDefinitions()->item(0));
$instance = $process->getInstances()->count() ? $process->getInstances()->item(0) : null;
} else {
$instance = $this->engine->createExecutionInstance($process, $dataStore);
$start->start($instance);
}
$this->engine->runToNextState();
$tasks = [];
if (!$instance) {
$this->assertEquals($result, $tasks);
if ($output) {
$this->assertEquals($output, $dataStore->getData());
}
return;
}
$tokens = $instance->getTokens();
$processes = $bpmnRepository->getElementsByTagNameNS(BpmnDocument::BPMN_MODEL, 'process');
$runtimeErrors = [];
$this->engine->getDispatcher()->listen('ActivityException', function ($payload) use (&$runtimeErrors) {
$error = $payload[1]->getProperty('error');
if ($error) {
$runtimeErrors[] = [
'element' => $payload[0]->getId(),
'error' => $error instanceof ErrorInterface ? $error->getId() : $error,
];
}
});
while ($tokens->count()) {
$submited = false;
foreach ($processes as $process) {
foreach ($process->getBpmnElementInstance()->getInstances() as $ins) {
foreach ($ins->getTokens() as $token) {
$element = $token->getOwnerElement();
$status = $token->getStatus();
if (
$element instanceof ActivityInterface && !($element instanceof CallActivityInterface)
&& $status === ActivityInterface::TOKEN_STATE_ACTIVE
) {
$tasks[] = $element->getId();
if ($element instanceof ScriptTaskInterface && $element->getScriptFormat() === 'application/x-betsy') {
$element->runScript($token);
} else {
$element->complete($token);
}
$this->engine->runToNextState();
$submited = true;
break;
}
if ($element instanceof IntermediateCatchEventInterface && $status === IntermediateCatchEventInterface::TOKEN_STATE_ACTIVE) {
if ($events && $element->getId() === $events[0]) {
$eventDefinition = $element->getEventDefinitions()->item(0);
$element->execute($eventDefinition, $token->getInstance());
$this->engine->runToNextState();
$submited = true;
array_shift($events);
}
}
}
}
}
$tokens = $instance->getTokens();
if (!$submited && $tokens->count()) {
$elements = '';
foreach ($processes as $process) {
foreach ($process->getBpmnElementInstance()->getInstances() as $ins) {
foreach ($ins->getTokens() as $token) {
$status = $token->getStatus();
$elements .= ' ' . $token->getOwnerElement()->getId() . ':' . $status;
if ($status == ActivityInterface::TOKEN_STATE_FAILING) {
$error = $token->getProperty('error');
$error = $error instanceof ErrorInterface ? $error->getId() : $error;
$runtimeErrors[] = [
'element' => $token->getOwnerElement()->getId(),
'error' => $error,
];
}
}
}
}
break;
//throw new Exception('The process got stuck in elements:' . $elements);
}
}
$testName = $json['comment'] ?? '';
$this->assertEquals($result, $tasks, $testName);
if ($output) {
$this->assertData($output, $dataStore->getData());
}
if ($errors) {
$this->assertData($errors, $runtimeErrors);
}
// Check data from all instances
$expectedDataByInstances = $json['expectedDataByInstances'] ?? false;
if ($expectedDataByInstances ?? false) {
$i = 0;
foreach ($processes as $process) {
foreach ($process->getBpmnElementInstance()->getInstances() as $ins) {
if (!isset($expectedDataByInstances[$i])) {
$this->fail('Data instance ' . $i . ' not found for test' . $testName);
}
$this->assertData($expectedDataByInstances[$i], $ins->getDataStore()->getData());
$i++;
}
}
}
}
/**
* Assert that $data contains the expected $subset
*
* @param mixed $subset
* @param mixed $data
* @param string $message
* @param bool $skip
*
* @return mixed
*/
private function assertData($subset, $data, $message = 'data', $skip = false)
{
if (!is_array($subset) || !is_array($data)) {
if ($skip) {
return $subset == $data;
} else {
return $this->assertEquals($subset, $data, "{$message} does not match " . \json_encode($subset));
}
}
foreach ($subset as $key => $value) {
if (substr($key, 0, 1) !== '*') {
$this->assertData($value, $data[$key], "{$message}.{$key}");
unset($subset[$key]);
unset($data[$key]);
}
}
foreach ($subset as $key => $value) {
foreach ($data as $key1 => $value1) {
if ($this->assertData($value, $value1, "{$message}.{$key}", true)) {
unset($subset[$key]);
unset($data[$key1]);
break;
}
}
}
if ($skip) {
return count($subset) === 0;
} else {
$this->assertCount(0, $subset, "{$message} does not match");
}
}
}