Skip to content

Commit 798148a

Browse files
committed
Add tests for Child Workflow stub factory
1 parent afa9046 commit 798148a

2 files changed

Lines changed: 91 additions & 0 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Temporal\Sugar\Tests\Stub\Workflow;
6+
7+
use RuntimeException;
8+
use Temporal\Sugar\Attribute\RetryPolicy;
9+
use Temporal\Sugar\Attribute\TaskQueue;
10+
use Temporal\Workflow\WorkflowInterface;
11+
use Temporal\Workflow\WorkflowMethod;
12+
13+
#[TaskQueue(name: 'test-queue')]
14+
#[RetryPolicy(attempts: 3, initInterval: 5, maxInterval: 500, backoff: 10, nonRetryables: [RuntimeException::class])]
15+
#[WorkflowInterface]
16+
final class AttributedWithoutInterface
17+
{
18+
#[WorkflowMethod]
19+
public function handle()
20+
{
21+
}
22+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Temporal\Sugar\Tests\Unit\Factory;
6+
7+
use LogicException;
8+
use PHPUnit\Framework\TestCase;
9+
use RuntimeException;
10+
use Temporal\Client\WorkflowOptions;
11+
use Temporal\Sugar\Factory\WorkflowStub;
12+
use Temporal\Sugar\Tests\Stub\Workflow\AttributedWithoutInterface;
13+
use Temporal\Workflow;
14+
15+
final class ChildWorkflowStubTest extends TestCase
16+
{
17+
protected function setUp(): void
18+
{
19+
$stub = $this->createStub(Workflow\WorkflowContextInterface::class);
20+
$stub->method('newChildWorkflowStub')
21+
->willReturnCallback(function ($class, $options) {
22+
return (object)[
23+
'class' => $class,
24+
'options' => $options,
25+
];
26+
});
27+
Workflow::setCurrentContext($stub);
28+
parent::setUp();
29+
}
30+
31+
public function testDefaultsFromAttributes()
32+
{
33+
/** @var WorkflowOptions $options */
34+
$options = WorkflowStub::childWorkflow(
35+
workflow: AttributedWithoutInterface::class,
36+
)->options;
37+
38+
$this->assertSame('test-queue', $options->taskQueue);
39+
$this->assertSame(3, $options->retryOptions->maximumAttempts);
40+
$this->assertSame(10.0, $options->retryOptions->backoffCoefficient);
41+
$this->assertSame([RuntimeException::class], $options->retryOptions->nonRetryableExceptions);
42+
$this->assertSame('5.0', $options->retryOptions->initialInterval->format('%s.%f'));
43+
$this->assertSame('500.0', $options->retryOptions->maximumInterval->format('%s.%f'));
44+
}
45+
46+
public function testAttributeOverrides()
47+
{
48+
/** @var WorkflowOptions $options */
49+
$options = WorkflowStub::childWorkflow(
50+
workflow: AttributedWithoutInterface::class,
51+
taskQueue: 'test-queue-override',
52+
retryAttempts: 0,
53+
retryInitInterval: 10,
54+
retryMaxInterval: 200,
55+
retryBackoff: 5.0,
56+
nonRetryables: [LogicException::class],
57+
)->options;
58+
59+
$this->assertSame('test-queue-override', $options->taskQueue);
60+
$this->assertSame(0, $options->retryOptions->maximumAttempts);
61+
$this->assertSame(5.0, $options->retryOptions->backoffCoefficient);
62+
$this->assertEquals(
63+
[LogicException::class, RuntimeException::class],
64+
$options->retryOptions->nonRetryableExceptions,
65+
);
66+
$this->assertSame('10.0', $options->retryOptions->initialInterval->format('%s.%f'));
67+
$this->assertSame('200.0', $options->retryOptions->maximumInterval->format('%s.%f'));
68+
}
69+
}

0 commit comments

Comments
 (0)