-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathDynamicSignalWithPromisesTest.php
More file actions
73 lines (58 loc) · 2.1 KB
/
DynamicSignalWithPromisesTest.php
File metadata and controls
73 lines (58 loc) · 2.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
<?php
declare(strict_types=1);
namespace Temporal\Tests\Acceptance\Extra\Stability\DynamicSignalWithPromises;
use PHPUnit\Framework\Attributes\Test;
use React\Promise\Deferred;
use React\Promise\PromiseInterface;
use Temporal\Client\WorkflowStubInterface;
use Temporal\Tests\Acceptance\App\Attribute\Stub;
use Temporal\Tests\Acceptance\App\TestCase;
use Temporal\Workflow;
use Temporal\Workflow\WorkflowMethod;
use Temporal\Workflow\WorkflowInterface;
class DynamicSignalWithPromisesTest extends TestCase
{
#[Test]
public function steps(
#[Stub('Extra_Stability_DynamicSignalWithPromises')] WorkflowStubInterface $stub,
): void {
# Send signals to the workflow to trigger steps
$stub->signal('begin', 'foo');
$stub->signal('next1', 'bar');
# Assert that the workflow has processed the signals and updated the value
$this->assertSame(2, $stub->query('value')->getValue(0, 'int'));
# Send another signal to continue the workflow
$stub->signal('next2', 'baz');
# Assert that the workflow has processed the final signal and returned the expected value
$this->assertSame(3, $stub->query('value')->getValue(0, 'int'));
# Assert that the workflow has completed and returned the final result
$this->assertSame(3, $stub->getResult());
}
}
#[WorkflowInterface]
class TestWorkflow
{
#[WorkflowMethod(name: 'Extra_Stability_DynamicSignalWithPromises')]
public function handler()
{
$value = 0;
Workflow::registerQuery('value', static function () use (&$value) {
return $value;
});
yield $this->promiseSignal('begin');
$value++;
yield $this->promiseSignal('next1');
$value++;
yield $this->promiseSignal('next2');
$value++;
return $value;
}
private function promiseSignal(string $name): PromiseInterface
{
$signal = new Deferred();
Workflow::registerSignal($name, static function (mixed $value) use ($signal): void {
$signal->resolve($value);
});
return $signal->promise();
}
}