|
| 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\Sugar\Factory\ActivityStub; |
| 11 | +use Temporal\Sugar\Tests\Stub\Activity\AttributedWithoutInterface; |
| 12 | +use Temporal\Workflow; |
| 13 | + |
| 14 | +final class ActivityStubTest extends TestCase |
| 15 | +{ |
| 16 | + protected function setUp(): void |
| 17 | + { |
| 18 | + $stub = $this->createStub(Workflow\WorkflowContextInterface::class); |
| 19 | + $stub->method('newActivityStub') |
| 20 | + ->willReturnCallback(function ($class, $options) { |
| 21 | + return (object)[ |
| 22 | + 'class' => $class, |
| 23 | + 'options' => $options, |
| 24 | + ]; |
| 25 | + }); |
| 26 | + Workflow::setCurrentContext($stub); |
| 27 | + parent::setUp(); |
| 28 | + } |
| 29 | + |
| 30 | + public function testDefaultsFromAttributes() |
| 31 | + { |
| 32 | + /** @var \Temporal\Activity\ActivityOptions $options */ |
| 33 | + $options = ActivityStub::activity( |
| 34 | + activity: AttributedWithoutInterface::class, |
| 35 | + )->options; |
| 36 | + |
| 37 | + $this->assertSame('test-queue', $options->taskQueue); |
| 38 | + $this->assertSame(3, $options->retryOptions->maximumAttempts); |
| 39 | + $this->assertSame(10.0, $options->retryOptions->backoffCoefficient); |
| 40 | + $this->assertSame([RuntimeException::class], $options->retryOptions->nonRetryableExceptions); |
| 41 | + $this->assertSame('5.0', $options->retryOptions->initialInterval->format('%s.%f')); |
| 42 | + $this->assertSame('500.0', $options->retryOptions->maximumInterval->format('%s.%f')); |
| 43 | + } |
| 44 | + |
| 45 | + public function testAttributeOverrides() |
| 46 | + { |
| 47 | + /** @var \Temporal\Activity\ActivityOptions $options */ |
| 48 | + $options = ActivityStub::activity( |
| 49 | + activity: AttributedWithoutInterface::class, |
| 50 | + taskQueue: 'test-queue-override', |
| 51 | + retryAttempts: 0, |
| 52 | + retryInitInterval: 10, |
| 53 | + retryMaxInterval: 200, |
| 54 | + retryBackoff: 5.0, |
| 55 | + nonRetryables: [LogicException::class], |
| 56 | + )->options; |
| 57 | + |
| 58 | + $this->assertSame('test-queue-override', $options->taskQueue); |
| 59 | + $this->assertSame(0, $options->retryOptions->maximumAttempts); |
| 60 | + $this->assertSame(5.0, $options->retryOptions->backoffCoefficient); |
| 61 | + $this->assertEquals( |
| 62 | + [LogicException::class, RuntimeException::class], |
| 63 | + $options->retryOptions->nonRetryableExceptions, |
| 64 | + ); |
| 65 | + $this->assertSame('10.0', $options->retryOptions->initialInterval->format('%s.%f')); |
| 66 | + $this->assertSame('200.0', $options->retryOptions->maximumInterval->format('%s.%f')); |
| 67 | + } |
| 68 | +} |
0 commit comments