|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Temporal\Tests\Acceptance\Harness\DataConverter\RawValue; |
| 6 | + |
| 7 | +use PHPUnit\Framework\Attributes\Test; |
| 8 | +use Temporal\Api\Common\V1\Payload; |
| 9 | +use Temporal\Api\Workflowservice\V1\StartWorkflowExecutionRequest; |
| 10 | +use Temporal\Client\GRPC\ContextInterface; |
| 11 | +use Temporal\Client\WorkflowStubInterface; |
| 12 | +use Temporal\DataConverter\EncodedValues; |
| 13 | +use Temporal\DataConverter\EncodingKeys; |
| 14 | +use Temporal\DataConverter\RawValue; |
| 15 | +use Temporal\Interceptor\GrpcClientInterceptor; |
| 16 | +use Temporal\Interceptor\PipelineProvider; |
| 17 | +use Temporal\Interceptor\SimplePipelineProvider; |
| 18 | +use Temporal\Interceptor\Trait\WorkflowClientCallsInterceptorTrait; |
| 19 | +use Temporal\Interceptor\WorkflowClient\GetResultInput; |
| 20 | +use Temporal\Interceptor\WorkflowClientCallsInterceptor; |
| 21 | +use Temporal\Tests\Acceptance\App\Attribute\Client; |
| 22 | +use Temporal\Tests\Acceptance\App\Attribute\Stub; |
| 23 | +use Temporal\Tests\Acceptance\App\TestCase; |
| 24 | +use Temporal\Workflow\WorkflowInterface; |
| 25 | +use Temporal\Workflow\WorkflowMethod; |
| 26 | + |
| 27 | +class RawValueTest extends TestCase |
| 28 | +{ |
| 29 | + private Interceptor $interceptor; |
| 30 | + |
| 31 | + public function pipelineProvider(): PipelineProvider |
| 32 | + { |
| 33 | + return new SimplePipelineProvider([$this->interceptor]); |
| 34 | + } |
| 35 | + |
| 36 | + #[Test] |
| 37 | + public function check( |
| 38 | + #[Stub('Harness_DataConverter_RawValue')] |
| 39 | + #[Client(pipelineProvider: [self::class, 'pipelineProvider'])] |
| 40 | + WorkflowStubInterface $stub, |
| 41 | + ): void { |
| 42 | + $result = $stub->getResult(RawValue::class); |
| 43 | + |
| 44 | + self::assertInstanceOf(RawValue::class, $result); |
| 45 | + self::assertInstanceOf(Payload::class, $result->getPayload()); |
| 46 | + self::assertSame('hello world', $result->getPayload()->getData()); |
| 47 | + |
| 48 | + # Check arguments |
| 49 | + self::assertNotNull($this->interceptor->startRequest); |
| 50 | + self::assertNotNull($this->interceptor->result); |
| 51 | + |
| 52 | + /** @var Payload $payload */ |
| 53 | + $payload = $this->interceptor->startRequest->getInput()?->getPayloads()[0] ?? null; |
| 54 | + self::assertNotNull($payload); |
| 55 | + |
| 56 | + self::assertSame(EncodingKeys::METADATA_ENCODING_RAW_VALUE, $payload->getMetadata()['encoding']); |
| 57 | + |
| 58 | + // Check result value from interceptor |
| 59 | + /** @var Payload $resultPayload */ |
| 60 | + $resultPayload = $this->interceptor->result->toPayloads()->getPayloads()[0]; |
| 61 | + self::assertSame(EncodingKeys::METADATA_ENCODING_RAW_VALUE, $resultPayload->getMetadata()['encoding']); |
| 62 | + } |
| 63 | + |
| 64 | + protected function setUp(): void |
| 65 | + { |
| 66 | + $this->interceptor = new Interceptor(); |
| 67 | + parent::setUp(); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +#[WorkflowInterface] |
| 72 | +class FeatureWorkflow |
| 73 | +{ |
| 74 | + #[WorkflowMethod('Harness_DataConverter_RawValue')] |
| 75 | + public function run() |
| 76 | + { |
| 77 | + return yield new RawValue(new Payload(['data' => 'hello world'])); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +class Interceptor implements GrpcClientInterceptor, WorkflowClientCallsInterceptor |
| 82 | +{ |
| 83 | + use WorkflowClientCallsInterceptorTrait; |
| 84 | + |
| 85 | + public ?StartWorkflowExecutionRequest $startRequest = null; |
| 86 | + public ?EncodedValues $result = null; |
| 87 | + |
| 88 | + public function interceptCall(string $method, object $arg, ContextInterface $ctx, callable $next): object |
| 89 | + { |
| 90 | + $arg instanceof StartWorkflowExecutionRequest and $this->startRequest = $arg; |
| 91 | + return $next($method, $arg, $ctx); |
| 92 | + } |
| 93 | + |
| 94 | + public function getResult(GetResultInput $input, callable $next): ?EncodedValues |
| 95 | + { |
| 96 | + return $this->result = $next($input); |
| 97 | + } |
| 98 | +} |
0 commit comments