-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathStateMachineTest.php
339 lines (321 loc) · 18.2 KB
/
StateMachineTest.php
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
namespace eftec\tests;
use eftec\PdoOne;
use eftec\statemachineone\Flags;
use eftec\statemachineone\Job;
use eftec\statemachineone\StateMachineOne;
use Exception;
class StateMachineTest extends AbstractStateMachineOneTestCase
{
/**
* @throws Exception
*/
public function test1(): void
{
$this->statemachineone->setStates([1, 2, 3]);
$this->statemachineone->setDefaultInitState(1);
$this->statemachineone->fieldDefault = ['field1' => 1, 'field2' => 0, 'field3' => 0, 'field4' => 123, 'counter' => 0];
$this->statemachineone->addTransition(1, 2, 'when field1 = 1 set counter + 1 timeout 100 fulltimeout 200');
$this->statemachineone->addTransition(2, 2, 'when field2 = 0 set field3 = -1', 'stay');
$this->statemachineone->addTransition(2, 2, 'when field2 = 0 set field4 + field1-field3', 'stay'); // field4=field4(123)+field1(1)-field3(-1)
$this->statemachineone->addTransition(2, 3, 'when field2 = 0 set field1 = 2 , counter + 1', 'stop');
$this->statemachineone->createJob($this->statemachineone->fieldDefault);
$this->statemachineone->checkAllJobs();
$job = $this->statemachineone->getLastJob();
// let's check consistency
self::assertEquals(true, $this->statemachineone->checkConsistency(false), 'consistency must be true');
// testing if the timeouts are set
self::assertEquals('100', $this->statemachineone->getTransitions()[0]->getDuration($job), 'duration must be 100');
self::assertEquals('200', $this->statemachineone->getTransitions()[0]->getFullDuration($job), 'full duration must be 200');
// testing the result fields
self::assertEquals('2', $job->fields['field1'], 'field1 must be 2');
self::assertEquals('0', $job->fields['field2'], 'field0 must be 0');
self::assertEquals('-1', $job->fields['field3'], 'field3 must be -1');
self::assertEquals('125', $job->fields['field4'], 'field4 must be 125');
self::assertEquals('2', $job->fields['counter'], 'counter must be 2');
self::assertEquals('3', $job->state, 'state must be 3');
self::assertEquals('stop', $job->getActive(), 'active must be stop');
// this job will freeze for 100 seconds
$this->statemachineone->createJob(['field1' => 1, 'field2' => 2, 'counter' => 3]);
//$this->statemachineone->checkAllJobs();
$job = $this->statemachineone->getLastJob();
// testing event
self::assertEquals('1', $job->fields['field1'], 'field1 must be 1');
self::assertEquals('2', $job->fields['field2'], 'field2 must be 2');
self::assertEquals('3', $job->fields['counter'], 'counter must be 3');
self::assertEquals('1', $job->state, 'state must be 1');
self::assertEquals('active', $job->getActive(), 'active must be active');
// testing events..
$this->statemachineone->addEvent("testme", "set field1='hello' , field2='world'");
$this->statemachineone->callEvent("testme", $job);
self::assertEquals('hello', $job->fields['field1'], 'field1 must be "hello"');
self::assertEquals('world', $job->fields['field2'], 'field2 must be "world"');
}
public function testAditionals(): void
{
// $this->statemachineone->setDebug(true);
$this->statemachineone->setStates([1, 2, 3]);
$this->statemachineone->resetTransition();
$this->statemachineone->addTransition(1, 2, 'where 1=1 set v1=1');
$this->statemachineone->addTransition(2, 3, 'where 1=1 set v1=2');
$this->statemachineone->setDefaultInitState(1);
$this->statemachineone->fieldDefault = ['v1' => 0];
$this->statemachineone->createJob();
$this->statemachineone->checkAllJobs();
$job = $this->statemachineone->getLastJob();
self::assertEquals(2, $job->fields['v1']);
$this->statemachineone->removeTransition(0);
$this->statemachineone->removeTransitions(0, 1);
$this->statemachineone->setDefaultInitState(1);
$this->statemachineone->fieldDefault = ['v1' => 0];
$this->statemachineone->createJob();
$this->statemachineone->checkAllJobs();
$job = $this->statemachineone->getLastJob();
self::assertEquals(0, $job->fields['v1']);
}
public function notestPdo(): void
{
$smo = new StateMachineOne(null);
$pdo = new PdoOne('test', '', '', '');
$pdo->open();
$pdo->logLevel = 3;
$smo->setPdoOne($pdo);
//$smo->loadDBAllJob();
}
/**
* @throws Exception
*/
public function test2(): void
{
$this->statemachineone->setStates([10 => "STATE1", 20 => "STATE2", 30 => "STATE3"]);
$this->statemachineone->setDefaultInitState(10);
$this->statemachineone->fieldDefault = ['field1' => 1, 'field2' => 0, 'counter' => 0];
$this->statemachineone->addTransition(10, 20, 'when field1 = 1 set field2=200', 'stay');
$this->statemachineone->createJob();
$this->statemachineone->checkAllJobs();
$job = $this->statemachineone->getLastJob();
// let's check consistency
self::assertEquals(true, $this->statemachineone->checkConsistency(false), 'consistency must be true');
self::assertEquals('200', $job->fields['field2'], 'field2 must be 200');
self::assertEquals('STATE1', $this->statemachineone->getJobStateName($job), 'current state must be STATE1');
self::assertEquals('active', $job->getActive(), 'active must be stop');
}
public function testVariableTransition(): void
{
$this->statemachineone->setStates([10 => "STATE1", 20 => "STATE2", 30 => "STATE3"]);
$this->statemachineone->setDefaultInitState(10);
$this->statemachineone->fieldDefault = ['field1' => 1, 'field2' => 0, 'counter' => 0, 'field3' => 0, 'field4' => 0, 'field5' => 0];
$this->statemachineone->addTransition(10, 20
, 'when field1 = 1 set field2=200 and field3=_state1 and field4=_idjob and field5=_time');
$this->statemachineone->createJob();
$this->statemachineone->checkAllJobs();
$job = $this->statemachineone->getLastJob();
// let's check consistency
self::assertEquals(true, $this->statemachineone->checkConsistency(false), 'consistency must be true');
self::assertEquals('200', $job->fields['field2'], 'field2 must be 200');
self::assertEquals('20', $job->fields['field3'], 'field3 must be 20'); // the current status
self::assertEquals(1, $job->fields['field4'], 'field4 must be 1'); // the number of job
self::assertGreaterThan(100, $job->fields['field5'], 'field5 great than 100'); // the current timestamp.
self::assertEquals('STATE2', $this->statemachineone->getJobStateName($job), 'current state must be STATE1');
self::assertEquals('active', $job->getActive(), 'active must be stop');
}
public function test4Duration(): void
{
$this->statemachineone->resetTransition();
$this->statemachineone->setStates([10 => "STATE1", 20 => "STATE2", 30 => "STATE3"]);
$this->statemachineone->setDefaultInitState(10);
$this->statemachineone->fieldDefault = ['field1' => 1, 'delta' => 0];
$this->statemachineone->addTransition(10, 20, 'when field1 = 1 set field2=200');
$this->statemachineone->addTransition(20, 30, 'when field1 = 1 set field2=200');
$this->statemachineone->duringState(30, 'set delta=timestate()');
// jobs
$this->statemachineone->createJob($this->statemachineone->fieldDefault);
$this->statemachineone->checkAllJobs();
sleep(1);
$this->statemachineone->checkAllJobs();
$job = $this->statemachineone->getLastJob();
self::assertGreaterThanOrEqual(1, $job->fields['delta']);
sleep(1);
$this->statemachineone->checkAllJobs();
$job = $this->statemachineone->getLastJob();
self::assertGreaterThanOrEqual(2, $job->fields['delta']);
}
public function test3duringState(): void
{
// definitions
$this->statemachineone->resetTransition();
$this->statemachineone->setStates([10 => "STATE1", 20 => "STATE2", 30 => "STATE3"]);
$this->statemachineone->setDefaultInitState(10);
$this->statemachineone->fieldDefault = ['field1' => 1, 'field2' => 0, 'field3' => 0];
$this->statemachineone->addTransition(10, 20, 'when field1 = 1 set field2=200');
$this->statemachineone->duringState(20, 'set field2=300');
$this->statemachineone->duringState(20, 'when true() set field3=300');
// jobs
$this->statemachineone->createJob($this->statemachineone->fieldDefault);
$this->statemachineone->checkAllJobs();
$job = $this->statemachineone->getLastJob();
self::assertEquals('300', $job->fields['field2'], 'field2 must be 300');
self::assertEquals('300', $job->fields['field3'], 'field3 must be 300');
}
public function test2new(): void
{
$this->statemachineone->setStates([10 => "STATE1", 20 => "STATE2", 30 => "STATE3"]);
$this->statemachineone->setDefaultInitState(10);
$this->statemachineone->fieldDefault = ['field1' => 1, 'field2' => 0, 'counter' => 0];
$this->statemachineone->addTransition([10, 20], 30, 'when field1 = 1 set field2=200', 'stay');
$this->statemachineone->createJob($this->statemachineone->fieldDefault);
$this->statemachineone->checkAllJobs();
$job = $this->statemachineone->getLastJob();
// let's check consistency
self::assertEquals(true, $this->statemachineone->checkConsistency(false), 'consistency must be true');
self::assertEquals('200', $job->fields['field2'], 'field2 must be 200');
self::assertEquals(10, $this->statemachineone->getJobState($job));
self::assertEquals('STATE1', $this->statemachineone->getJobStateName($job), 'current state must be STATE1');
self::assertEquals('active', $job->getActive(), 'active must be stop');
}
/**
* @throws Exception
*/
public function test2doc(): void
{
$tmpstate = new StateMachineOne(null);
$tmpstate->setDocDB(__DIR__ . '/tmpdoc');
$tmpstate->getDocOne()->delete('job1');
$tmpstate->getDocOne()->delete('job2');
$tmpstate->getDocOne()->delete('job3');
$tmpstate->getDocOne()->delete('job4');
$tmpstate->getDocOne()->delete('job4');
//die(1);
//$tmpstate->getDocOne()->collection(dirname(__FILE__). '/tmpdoc',true);
$tmpstate->setStates([10 => "STATE1", 20 => "STATE2", 30 => "STATE3"]);
$tmpstate->setDefaultInitState(10);
$tmpstate->fieldDefault = ['field1' => 1, 'field2' => 0, 'counter' => 0];
$tmpstate->addTransition(10, 20, 'when field1 = 1 set field2=200', 'stay');
$tmpstate->createJob($tmpstate->fieldDefault);
$tmpstate->checkAllJobs();
$tmpstate->saveDBAllJob();
$tmpstate->setJobQueue([]); // we deleted all the jobs
$tmpstate->loadDBAllJob();
$job = $tmpstate->getLastJob();
// let's check consistency
self::assertEquals(true, $tmpstate->checkConsistency(false), 'consistency must be true');
self::assertEquals('200', $job->fields['field2'], 'field2 must be 200');
self::assertEquals('STATE1', $tmpstate->getJobStateName($job), 'current state must be STATE1');
self::assertEquals('active', $job->getActive(), 'active must be stop');
}
/**
* @throws Exception
*/
public function test3(): void
{
$this->statemachineone->setStates([10 => "STATE1", 20 => "STATE2", 30 => "STATE3"]);
$this->statemachineone->setDefaultInitState(10);
$this->statemachineone->fieldDefault = ['field1' => 1, 'field2' => new Flags(), 'counter' => 0];
$this->statemachineone->addTransition(10, 20, 'when field1 = 1 set field2.push("msg",2)', 'stay');
$this->statemachineone->createJob($this->statemachineone->fieldDefault);
$this->statemachineone->checkAllJobs();
$job = $this->statemachineone->getLastJob();
// let's check consistency
self::assertEquals(true, $this->statemachineone->checkConsistency(false), 'consistency must be true');
/** @see \eftec\statemachineone\Flags::toString */
self::assertEquals('a:5:{s:5:"stack";a:1:{s:3:"msg";s:1:"2";}s:7:"stackId";' .
'a:1:{s:3:"msg";i:0;}s:10:"timeExpire";a:1:{s:3:"msg";i:-1;}s:5:' .
'"level";a:1:{s:3:"msg";i:0;}s:7:"changed";i:1;}'
, $job->fields['field2']->toString(), 'field2 must be a flag');
/** @see \eftec\statemachineone\Flags::getFlag */
self::assertEquals(['flag' => '2', 'id' => 0, 'level' => 0, 'time' => -1], $job->fields['field2']->getFlag('msg'), 'field2.msg must returns a flag');
self::assertEquals('STATE1', $this->statemachineone->getJobStateName($job), 'current state must be STATE1');
self::assertEquals('active', $job->getActive(), 'active must be stop');
}
public function testFlagSimple(): void
{
$f = new Flags('flag_array', true, $this->statemachineone);
$job = $this->statemachineone->createJob();
$f->setParent($job);
$f->push('flag1', 'value');
$this->assertEquals([
'flag' => 'value',
'id' => 0,
'level' => 0,
'time' => -1
], $f->getFlag('flag1'));
}
public $conteo = 0;
/** @noinspection ForgottenDebugOutputInspection */
public function testFlagMessage(): void
{
$this->conteo = 0;
$this->statemachineone->customSaveDBJobLog = function($arg = null, $arg2 = null) {
// here we save in the log
$this->conteo++;
var_dump('save in log ');
var_dump($arg2);
//var_dump($arg);
};
$this->statemachineone->setStates([10 => "STATE1", 20 => "STATE2", 30 => "STATE3"]);
$this->statemachineone->setDefaultInitState(10);
$this->statemachineone->fieldDefault = ['field1' => 1, 'field2' => new Flags('myflag', true, $this->statemachineone), 'counter' => 0];
$this->statemachineone->addTransition(10, 20, 'when field1 = 1 set field2.message("hello world2")');
$this->statemachineone->addTransition(20, 30, 'when field1 = 1 set field2.message("hello world")', 'stay');
$this->statemachineone->addTransition(20, 30, 'when field1 = 1 set field2.message("hello world")', 'stay');
$this->statemachineone->createJob($this->statemachineone->fieldDefault);
$this->statemachineone->checkAllJobs();
$job = $this->statemachineone->getLastJob();
// let's check consistency
//$a1=new Flags();
//$a1->getStack()
self::assertEquals(3, $this->conteo, 'it must save the log twice');
self::assertEquals(true, $this->statemachineone->checkConsistency(false), 'consistency must be true');
/** @see \eftec\statemachineone\Flags::toString */
self::assertEquals('a:5:{s:5:"stack";a:1:{s:4:"_msg";s:11:"hello world";}s:7:"stackId";a:1:{s:4:"_msg";i:1;}s:10:"timeExpire";a:1:{s:4:"_msg";i:-1;}s:5:"level";a:1:{s:4:"_msg";i:0;}s:7:"changed";i:1;}'
, $job->fields['field2']->toString(), 'field2 must be a correct flag');
/** @see \eftec\statemachineone\Flags::getFlag */
self::assertEquals(['flag' => 'hello world', 'id' => 1, 'level' => 0, 'time' => -1], $job->fields['field2']->getFlag('_msg'), 'field2.msg has some wrong values');
self::assertEquals('STATE2', $this->statemachineone->getJobStateName($job), 'current state must be STATE2');
self::assertEquals('active', $job->getActive(), 'it must be active');
$this->statemachineone->customSaveDBJobLog = null;
}
/**
* @throws Exception
*/
public function testSC(): void
{
$this->statemachineone->setStates([10 => "STATE1"]);
$this->statemachineone->setDefaultInitState(10);
$this->statemachineone->fieldDefault = ['field1' => 1, 'field2' => new Flags(), 'counter' => 0];
/** @see \eftec\tests\ServiceClass::ping we are calling this method */
$this->statemachineone->addTransition(10, 10, 'when field1 = 1 set field2=ping("hello")', 'stop');
$this->statemachineone->createJob($this->statemachineone->fieldDefault);
$this->statemachineone->checkAllJobs();
$job = $this->statemachineone->getLastJob();
// let's check consistency
self::assertEquals(true, $this->statemachineone->checkConsistency(false), 'consistency must be true');
self::assertEquals('pong hello', $job->fields['field2'], 'field2 must returns pong hello');
}
/**
* @throws Exception
*/
public function testMultipleJobs(): void
{
$this->statemachineone->setStates([10 => "STATE_START", 20 => 'STATE_END']);
$this->statemachineone->setDefaultInitState(10);
$this->statemachineone->fieldDefault = ['field1' => 123, 'field2' => -1, 'counter' => 0];
/** @see \eftec\tests\ServiceClass::ping we are calling this method */
$this->statemachineone->addTransition(10, 20, 'when true() set field2=field1');
$this->statemachineone->addTransition(20, 20, 'when true()', 'stop');
$this->statemachineone->createJob($this->statemachineone->fieldDefault);
$state2 = $this->statemachineone->fieldDefault;
$state2['field1'] = 456;
$this->statemachineone->createJob($state2);
$this->statemachineone->checkAllJobs();
//var_dump($this->statemachineone->getJobQueue());
$job = $this->statemachineone->getJob(1);
// let's check consistency
self::assertEquals(123, $job->fields['field2'], 'field2 must returns 123');
self::assertEquals([[10, 20], [20, 20]], $job->stateFlow, 'stateFlow must returns the flow');
$job = $this->statemachineone->getJob(2);
// let's check consistency
self::assertEquals(456, $job->fields['field2'], 'field2 must returns 456 for the second job');
self::assertEquals([[10, 20], [20, 20]], $job->stateFlow, 'stateFlow must returns the flow');
}
}