Skip to content

Commit afa9046

Browse files
committed
Add tests for ActivityStub Factory
1 parent 316a47e commit afa9046

11 files changed

Lines changed: 103 additions & 12 deletions

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"require": {
4040
"php": ">=8.1",
4141
"temporal/sdk": "^2.7",
42-
"buggregator/trap": "*"
42+
"buggregator/trap": "^1.3"
4343
},
4444
"require-dev": {
4545
"phpunit/phpunit": "^10.4",

src/Attribute/RetryPolicy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function __construct(
3434
public readonly \DateInterval|string|int|null $initInterval = null,
3535
public readonly \DateInterval|string|int|null $maxInterval = null,
3636
public readonly ?float $backoff = null,
37-
public readonly array $nonRetryables = []
37+
public readonly array $nonRetryables = [],
3838
) {
3939
}
4040
}
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\Activity;
6+
7+
use RuntimeException;
8+
use Temporal\Activity\ActivityInterface;
9+
use Temporal\Activity\ActivityMethod;
10+
use Temporal\Sugar\Attribute\RetryPolicy;
11+
use Temporal\Sugar\Attribute\TaskQueue;
12+
13+
#[TaskQueue(name: 'test-queue')]
14+
#[RetryPolicy(attempts: 3, initInterval: 5, maxInterval: 500, backoff: 10, nonRetryables: [RuntimeException::class])]
15+
#[ActivityInterface]
16+
final class AttributedWithoutInterface
17+
{
18+
#[ActivityMethod]
19+
public function handle()
20+
{
21+
}
22+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
}

tests/Unit/Internal/AttributeReaderTest.php renamed to tests/Unit/Internal/Attribute/AttributeReaderTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22

33
declare(strict_types=1);
44

5-
namespace Temporal\Sugar\Tests\Unit\Internal;
5+
namespace Temporal\Sugar\Tests\Unit\Internal\Attribute;
66

77
use PHPUnit\Framework\TestCase;
88
use Temporal\Sugar\Attribute\TaskQueue;
99
use Temporal\Sugar\Internal\Attribute\AttributeReader;
10+
use Temporal\Sugar\Tests\Unit\Internal\Attribute;
1011

1112
class AttributeReaderTest extends TestCase
1213
{
1314
public function testFromClass(): void
1415
{
1516
$result = AttributeReader::arrayFromClass(
16-
Stub\Attributed\SimpleClass::class,
17+
Attribute\Stub\Attributed\SimpleClass::class,
1718
[TaskQueue::class]
1819
);
1920

@@ -27,7 +28,7 @@ public function testFromClass(): void
2728
public function testFromExtendedClassWithInheritanceWithMerge(): void
2829
{
2930
$result = AttributeReader::arrayFromClass(
30-
Stub\Attributed\ExtendedAttributed::class,
31+
Attribute\Stub\Attributed\ExtendedAttributed::class,
3132
[TaskQueue::class],
3233
merge: true,
3334
);
@@ -48,7 +49,7 @@ public function testFromExtendedClassWithInheritanceWithMerge(): void
4849
public function testFromInterfaceWithInheritance(): void
4950
{
5051
$result = AttributeReader::arrayFromClass(
51-
Stub\Attributed\InterfaceAttributed::class,
52+
Attribute\Stub\Attributed\InterfaceAttributed::class,
5253
[TaskQueue::class],
5354
merge: true,
5455
);

tests/Unit/Internal/Stub/Attributed/AbstractAttributed.php renamed to tests/Unit/Internal/Attribute/Stub/Attributed/AbstractAttributed.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Temporal\Sugar\Tests\Unit\Internal\Stub\Attributed;
5+
namespace Temporal\Sugar\Tests\Unit\Internal\Attribute\Stub\Attributed;
66

77
use Temporal\Sugar\Attribute\TaskQueue;
88

tests/Unit/Internal/Stub/Attributed/ExtendedAttributed.php renamed to tests/Unit/Internal/Attribute/Stub/Attributed/ExtendedAttributed.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Temporal\Sugar\Tests\Unit\Internal\Stub\Attributed;
5+
namespace Temporal\Sugar\Tests\Unit\Internal\Attribute\Stub\Attributed;
66

77
use Temporal\Sugar\Attribute\TaskQueue;
88

tests/Unit/Internal/Stub/Attributed/InterfaceAttributed.php renamed to tests/Unit/Internal/Attribute/Stub/Attributed/InterfaceAttributed.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Temporal\Sugar\Tests\Unit\Internal\Stub\Attributed;
5+
namespace Temporal\Sugar\Tests\Unit\Internal\Attribute\Stub\Attributed;
66

77
use Temporal\Sugar\Attribute\TaskQueue;
88

tests/Unit/Internal/Stub/Attributed/ParentInterfaceAttributed.php renamed to tests/Unit/Internal/Attribute/Stub/Attributed/ParentInterfaceAttributed.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Temporal\Sugar\Tests\Unit\Internal\Stub\Attributed;
5+
namespace Temporal\Sugar\Tests\Unit\Internal\Attribute\Stub\Attributed;
66

77
use Temporal\Sugar\Attribute\TaskQueue;
88

tests/Unit/Internal/Stub/Attributed/ParentParentInterfaceAttributed.php renamed to tests/Unit/Internal/Attribute/Stub/Attributed/ParentParentInterfaceAttributed.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Temporal\Sugar\Tests\Unit\Internal\Stub\Attributed;
5+
namespace Temporal\Sugar\Tests\Unit\Internal\Attribute\Stub\Attributed;
66

77
use Temporal\Sugar\Attribute\TaskQueue;
88

0 commit comments

Comments
 (0)