|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Temporal\Tests\Acceptance\Extra\Workflow\SideEffect; |
| 6 | + |
| 7 | +use PHPUnit\Framework\Attributes\Test; |
| 8 | +use Temporal\Api\Common\V1\Payload; |
| 9 | +use Temporal\Client\WorkflowClientInterface; |
| 10 | +use Temporal\Client\WorkflowStubInterface; |
| 11 | +use Temporal\Common\SideEffectOptions; |
| 12 | +use Temporal\DataConverter\DataConverterInterface; |
| 13 | +use Temporal\Tests\Acceptance\App\Attribute\Stub; |
| 14 | +use Temporal\Tests\Acceptance\App\TestCase; |
| 15 | +use Temporal\Workflow; |
| 16 | +use Temporal\Workflow\WorkflowInterface; |
| 17 | +use Temporal\Workflow\WorkflowMethod; |
| 18 | + |
| 19 | +class SideEffectTest extends TestCase |
| 20 | +{ |
| 21 | + #[Test] |
| 22 | + public static function currentTime( |
| 23 | + #[Stub('Extra_Workflow_SideEffect')] |
| 24 | + WorkflowStubInterface $stub, |
| 25 | + ): void { |
| 26 | + $result = $stub->getResult(type: 'array'); |
| 27 | + |
| 28 | + self::assertEquals($result['system'], $result['current']); |
| 29 | + } |
| 30 | + |
| 31 | + #[Test] |
| 32 | + public static function summaryRecordedOnMarker( |
| 33 | + #[Stub('Extra_Workflow_SideEffect')] |
| 34 | + WorkflowStubInterface $stub, |
| 35 | + WorkflowClientInterface $client, |
| 36 | + DataConverterInterface $dataConverter, |
| 37 | + ): void { |
| 38 | + $stub->getResult(); |
| 39 | + |
| 40 | + $summaries = self::collectSideEffectSummaries($client, $stub, $dataConverter); |
| 41 | + |
| 42 | + self::assertSame(['Side Effect Summary'], $summaries); |
| 43 | + } |
| 44 | + |
| 45 | + #[Test] |
| 46 | + public static function distinctSummariesPerSideEffect( |
| 47 | + #[Stub('Extra_Workflow_SideEffect_Multi')] |
| 48 | + WorkflowStubInterface $stub, |
| 49 | + WorkflowClientInterface $client, |
| 50 | + DataConverterInterface $dataConverter, |
| 51 | + ): void { |
| 52 | + $stub->getResult(); |
| 53 | + |
| 54 | + $summaries = self::collectSideEffectSummaries($client, $stub, $dataConverter); |
| 55 | + |
| 56 | + self::assertSame(['first summary', 'second summary'], $summaries); |
| 57 | + } |
| 58 | + |
| 59 | + #[Test] |
| 60 | + public static function noSummaryWhenOptionsOmitted( |
| 61 | + #[Stub('Extra_Workflow_SideEffect_NoOptions')] |
| 62 | + WorkflowStubInterface $stub, |
| 63 | + WorkflowClientInterface $client, |
| 64 | + ): void { |
| 65 | + $stub->getResult(); |
| 66 | + |
| 67 | + $markerCount = 0; |
| 68 | + foreach ($client->getWorkflowHistory($stub->getExecution()) as $event) { |
| 69 | + if (!$event->hasMarkerRecordedEventAttributes()) { |
| 70 | + continue; |
| 71 | + } |
| 72 | + if ($event->getMarkerRecordedEventAttributes()->getMarkerName() !== 'SideEffect') { |
| 73 | + continue; |
| 74 | + } |
| 75 | + |
| 76 | + ++$markerCount; |
| 77 | + self::assertNull($event->getUserMetadata()?->getSummary()); |
| 78 | + } |
| 79 | + |
| 80 | + self::assertSame(1, $markerCount, 'SideEffect marker must exist in the Workflow history'); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @return list<string> |
| 85 | + */ |
| 86 | + private static function collectSideEffectSummaries( |
| 87 | + WorkflowClientInterface $client, |
| 88 | + WorkflowStubInterface $stub, |
| 89 | + DataConverterInterface $dataConverter, |
| 90 | + ): array { |
| 91 | + $summaries = []; |
| 92 | + foreach ($client->getWorkflowHistory($stub->getExecution()) as $event) { |
| 93 | + if (!$event->hasMarkerRecordedEventAttributes()) { |
| 94 | + continue; |
| 95 | + } |
| 96 | + if ($event->getMarkerRecordedEventAttributes()->getMarkerName() !== 'SideEffect') { |
| 97 | + continue; |
| 98 | + } |
| 99 | + |
| 100 | + $payload = $event->getUserMetadata()?->getSummary(); |
| 101 | + self::assertInstanceOf(Payload::class, $payload); |
| 102 | + $summaries[] = $dataConverter->fromPayload($payload, 'string'); |
| 103 | + } |
| 104 | + |
| 105 | + return $summaries; |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +#[WorkflowInterface] |
| 110 | +class MainWorkflow |
| 111 | +{ |
| 112 | + #[WorkflowMethod('Extra_Workflow_SideEffect')] |
| 113 | + public function run() |
| 114 | + { |
| 115 | + yield Workflow::timer('1 seconds'); |
| 116 | + |
| 117 | + /** |
| 118 | + * @var \DateTimeImmutable $currentDate |
| 119 | + */ |
| 120 | + $currentDate = yield Workflow::sideEffect( |
| 121 | + static fn(): \DateTimeImmutable => new \DateTimeImmutable(), |
| 122 | + SideEffectOptions::new() |
| 123 | + ->withSummary('Side Effect Summary'), |
| 124 | + ); |
| 125 | + |
| 126 | + return yield [ |
| 127 | + 'current' => [ |
| 128 | + 'timestamp' => $currentDate->getTimestamp(), |
| 129 | + 'timezone.offset' => $currentDate->getTimeZone()->getOffset($currentDate), |
| 130 | + ], |
| 131 | + 'system' => [ |
| 132 | + 'timestamp' => Workflow::now()->getTimestamp(), |
| 133 | + 'timezone.offset' => Workflow::now()->getTimezone()->getOffset(Workflow::now()), |
| 134 | + ], |
| 135 | + ]; |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +#[WorkflowInterface] |
| 140 | +class MultiSummaryWorkflow |
| 141 | +{ |
| 142 | + #[WorkflowMethod('Extra_Workflow_SideEffect_Multi')] |
| 143 | + public function run() |
| 144 | + { |
| 145 | + yield Workflow::sideEffect( |
| 146 | + static fn(): int => 1, |
| 147 | + SideEffectOptions::new()->withSummary('first summary'), |
| 148 | + ); |
| 149 | + yield Workflow::sideEffect( |
| 150 | + static fn(): int => 2, |
| 151 | + SideEffectOptions::new()->withSummary('second summary'), |
| 152 | + ); |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +#[WorkflowInterface] |
| 157 | +class NoOptionsWorkflow |
| 158 | +{ |
| 159 | + #[WorkflowMethod('Extra_Workflow_SideEffect_NoOptions')] |
| 160 | + public function run() |
| 161 | + { |
| 162 | + yield Workflow::sideEffect(static fn(): int => 42); |
| 163 | + } |
| 164 | +} |
0 commit comments