|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Temporal\Tests\Acceptance\Extra\Versioning\Deployment; |
| 6 | + |
| 7 | +use PHPUnit\Framework\Attributes\Test; |
| 8 | +use Temporal\Client\WorkflowClientInterface; |
| 9 | +use Temporal\Client\WorkflowOptions; |
| 10 | +use Temporal\Tests\Acceptance\App\Attribute\Worker; |
| 11 | +use Temporal\Tests\Acceptance\App\Runtime\Feature; |
| 12 | +use Temporal\Tests\Acceptance\App\Runtime\TemporalStarter; |
| 13 | +use Temporal\Tests\Acceptance\App\TestCase; |
| 14 | +use Temporal\Worker\Versioning\VersioningBehavior; |
| 15 | +use Temporal\Worker\Versioning\WorkerDeploymentOptions; |
| 16 | +use Temporal\Worker\Versioning\WorkerDeploymentVersion; |
| 17 | +use Temporal\Worker\WorkerOptions; |
| 18 | +use Temporal\Workflow\WorkflowInterface; |
| 19 | +use Temporal\Workflow\WorkflowMethod; |
| 20 | + |
| 21 | +#[Worker(options: [WorkerFactory::class, 'options'])] |
| 22 | +class DeploymentTest extends TestCase |
| 23 | +{ |
| 24 | + #[Test] |
| 25 | + public function sendEmpty( |
| 26 | + TemporalStarter $starter, |
| 27 | + WorkflowClientInterface $client, |
| 28 | + Feature $feature, |
| 29 | + ): void { |
| 30 | + $starter->executeTemporalCommand([ |
| 31 | + 'worker', |
| 32 | + 'deployment', |
| 33 | + 'set-current-version', |
| 34 | + '--deployment-name', WorkerFactory::DEPLOYMENT_NAME, |
| 35 | + '--build-id', WorkerFactory::BUILD_ID, |
| 36 | + '--yes', |
| 37 | + ], timeout: 5); |
| 38 | + |
| 39 | + try { |
| 40 | + # Create a Workflow stub with an execution timeout 12 seconds |
| 41 | + $stub = $client |
| 42 | + ->withTimeout(10) |
| 43 | + ->newUntypedWorkflowStub( |
| 44 | + 'Extra_Versioning_Deployment', |
| 45 | + WorkflowOptions::new() |
| 46 | + ->withTaskQueue($feature->taskQueue) |
| 47 | + ->withWorkflowExecutionTimeout(20), |
| 48 | + ); |
| 49 | + |
| 50 | + # Start the Workflow |
| 51 | + $client->start($stub); |
| 52 | + |
| 53 | + $result = $stub->getResult(timeout: 5); |
| 54 | + self::assertSame('default', $result); |
| 55 | + |
| 56 | + $version = null; |
| 57 | + $behavior = null; |
| 58 | + foreach ($client->getWorkflowHistory($stub->getExecution()) as $event) { |
| 59 | + if ($event->hasWorkflowTaskCompletedEventAttributes()) { |
| 60 | + $version = $event->getWorkflowTaskCompletedEventAttributes()?->getDeploymentVersion(); |
| 61 | + $behavior = $event->getWorkflowTaskCompletedEventAttributes()?->getVersioningBehavior(); |
| 62 | + break; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + self::assertNotNull($version); |
| 67 | + self::assertSame(WorkerFactory::DEPLOYMENT_NAME, $version->getDeploymentName()); |
| 68 | + self::assertSame(WorkerFactory::BUILD_ID, $version->getBuildId()); |
| 69 | + self::assertSame(VersioningBehavior::AutoUpgrade->value, $behavior); |
| 70 | + } finally { |
| 71 | + $starter->stop() and $starter->start(); |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +class WorkerFactory |
| 77 | +{ |
| 78 | + public const DEPLOYMENT_NAME = 'foo'; |
| 79 | + public const BUILD_ID = 'baz'; |
| 80 | + |
| 81 | + public static function options(): WorkerOptions |
| 82 | + { |
| 83 | + return WorkerOptions::new() |
| 84 | + ->withDeploymentOptions( |
| 85 | + WorkerDeploymentOptions::new() |
| 86 | + ->withUseVersioning(true) |
| 87 | + ->withVersion(WorkerDeploymentVersion::new(self::DEPLOYMENT_NAME, self::BUILD_ID)) |
| 88 | + ->withDefaultVersioningBehavior(VersioningBehavior::AutoUpgrade), |
| 89 | + ); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +#[WorkflowInterface] |
| 94 | +class TestWorkflow |
| 95 | +{ |
| 96 | + #[WorkflowMethod(name: "Extra_Versioning_Deployment")] |
| 97 | + public function handle() |
| 98 | + { |
| 99 | + return 'default'; |
| 100 | + } |
| 101 | +} |
0 commit comments