Skip to content

Commit 3cf3f2e

Browse files
committed
Add the ability to create untyped activity stub
1 parent 216f325 commit 3cf3f2e

3 files changed

Lines changed: 21 additions & 10 deletions

File tree

src/Factory/ActivityStub.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Temporal\Support\Internal\Attribute\AttributeReader;
1515
use Temporal\Support\Internal\RetryOptions;
1616
use Temporal\Workflow;
17+
use Temporal\Workflow\ActivityStubInterface;
1718
use Throwable;
1819

1920
final class ActivityStub
@@ -23,7 +24,7 @@ final class ActivityStub
2324
*
2425
* @template T of object
2526
*
26-
* @param class-string<T> $class
27+
* @param class-string<T>|null $class Activity class name. When not set, an untyped activity stub is created.
2728
* @param non-empty-string|null $taskQueue
2829
* @param int<0, max>|null $retryAttempts Maximum number of attempts. When exceeded the retries stop even
2930
* if not expired yet. If not set or set to 0, it means unlimited, and rely on activity
@@ -57,10 +58,10 @@ final class ActivityStub
5758
* @param int $cancellationType Whether to wait for canceled activity to be completed (activity can be failed,
5859
* completed, cancel accepted). {@see \Temporal\Activity\ActivityCancellationType}
5960
*
60-
* @return T|ActivityProxy
61+
* @return ($class is class-string ? T|ActivityProxy : ActivityStubInterface)
6162
*/
6263
public static function activity(
63-
string $class,
64+
?string $class = null,
6465
?string $taskQueue = null,
6566
?int $retryAttempts = null,
6667
\DateInterval|string|int|null $retryInitInterval = null,
@@ -74,7 +75,7 @@ public static function activity(
7475
\Stringable|string|null $activityId = null,
7576
int $cancellationType = 0,
7677
): object {
77-
$attributes = self::readAttributes($class);
78+
$attributes = $class !== null ? self::readAttributes($class) : new AttributeCollection([]);
7879

7980
// Retry options
8081
$retryOptions = RetryOptions::create(
@@ -99,7 +100,9 @@ public static function activity(
99100
$activityId === null or $options = $options->withActivityId((string)$activityId);
100101
$cancellationType === null or $options = $options->withCancellationType($cancellationType);
101102

102-
return Workflow::newActivityStub($class, $options);
103+
return $class === null
104+
? Workflow::newUntypedActivityStub($options)
105+
: Workflow::newActivityStub($class, $options);
103106
}
104107

105108
/**

src/Internal/RetryOptions.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ public static function create(
4848
}
4949

5050
$retryOptions = \Temporal\Common\RetryOptions::new();
51-
$retryAttempts === 0 or $retryOptions = $retryOptions->withMaximumAttempts($retryAttempts);
52-
$retryInitInterval === 0 or $retryOptions = $retryOptions->withInitialInterval($retryInitInterval);
53-
$retryMaxInterval === 0 or $retryOptions = $retryOptions->withMaximumInterval($retryMaxInterval);
51+
$retryAttempts > 0 and $retryOptions = $retryOptions->withMaximumAttempts($retryAttempts);
52+
$retryInitInterval === null or $retryOptions = $retryOptions->withInitialInterval($retryInitInterval);
53+
$retryMaxInterval === null or $retryOptions = $retryOptions->withMaximumInterval($retryMaxInterval);
5454
$retryBackoff >= 1.0 and $retryOptions = $retryOptions->withBackoffCoefficient($retryBackoff);
5555
$nonRetryables === [] or $retryOptions = $retryOptions->withNonRetryableExceptions($nonRetryables);
5656
return $retryOptions;

tests/Unit/Factory/ActivityStubTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Temporal\Support\Factory\ActivityStub;
1111
use Temporal\Support\Tests\Stub\Activity\AttributedWithoutInterface;
1212
use Temporal\Workflow;
13+
use Temporal\Workflow\ActivityStubInterface;
1314

1415
final class ActivityStubTest extends TestCase
1516
{
@@ -27,7 +28,7 @@ protected function setUp(): void
2728
parent::setUp();
2829
}
2930

30-
public function testDefaultsFromAttributes()
31+
public function testDefaultsFromAttributes(): void
3132
{
3233
/** @var \Temporal\Activity\ActivityOptions $options */
3334
$options = ActivityStub::activity(
@@ -42,7 +43,7 @@ class: AttributedWithoutInterface::class,
4243
$this->assertSame('500.0', $options->retryOptions->maximumInterval->format('%s.%f'));
4344
}
4445

45-
public function testAttributeOverrides()
46+
public function testAttributeOverrides(): void
4647
{
4748
/** @var \Temporal\Activity\ActivityOptions $options */
4849
$options = ActivityStub::activity(
@@ -65,4 +66,11 @@ class: AttributedWithoutInterface::class,
6566
$this->assertSame('10.0', $options->retryOptions->initialInterval->format('%s.%f'));
6667
$this->assertSame('200.0', $options->retryOptions->maximumInterval->format('%s.%f'));
6768
}
69+
70+
public function testUntypedActivity(): void
71+
{
72+
$activity = ActivityStub::activity();
73+
74+
self::assertInstanceOf(ActivityStubInterface::class, $activity);
75+
}
6876
}

0 commit comments

Comments
 (0)