Skip to content

Commit b9b9d13

Browse files
authored
Fix WorkflowInit detection (#611)
* Fix WorkflowInit detection * Add tests
2 parents c8c16c5 + 3a31075 commit b9b9d13

File tree

4 files changed

+81
-2
lines changed

4 files changed

+81
-2
lines changed

src/Internal/Declaration/Reader/WorkflowReader.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,12 @@ private function withMethods(ClassNode $graph, WorkflowPrototype $prototype): Wo
128128

129129
// Check WorkflowInit method
130130
if ($method->isConstructor()) {
131-
$this->getAttributedMethod($graph, $method, WorkflowInit::class);
132-
$prototype->setHasInitializer(true);
131+
$attr = $this->getAttributedMethod($graph, $method, WorkflowInit::class);
132+
133+
if ($attr !== null) {
134+
$prototype->setHasInitializer(true);
135+
}
136+
133137
continue;
134138
}
135139

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/**
4+
* This file is part of Temporal package.
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace Temporal\Tests\Unit\Declaration\Fixture;
13+
14+
use Temporal\Workflow\WorkflowInterface;
15+
use Temporal\Workflow\WorkflowMethod;
16+
17+
/** @WorkflowInterface */
18+
#[WorkflowInterface]
19+
class WorkflowWithConstructor
20+
{
21+
public function __construct() {}
22+
23+
/**
24+
* @WorkflowMethod
25+
*/
26+
#[WorkflowMethod]
27+
public function handler(): void {}
28+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/**
4+
* This file is part of Temporal package.
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace Temporal\Tests\Unit\Declaration\Fixture;
13+
14+
use Temporal\Workflow\WorkflowInit;
15+
use Temporal\Workflow\WorkflowInterface;
16+
use Temporal\Workflow\WorkflowMethod;
17+
18+
/** @WorkflowInterface */
19+
#[WorkflowInterface]
20+
class WorkflowWithWorkflowInit
21+
{
22+
/**
23+
* @WorkflowInit
24+
*/
25+
#[WorkflowInit]
26+
public function __construct() {}
27+
28+
/**
29+
* @WorkflowMethod
30+
*/
31+
#[WorkflowMethod]
32+
public function handler(): void {}
33+
}

tests/Unit/Declaration/WorkflowDeclarationTestCase.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@
2121
use Temporal\Internal\Declaration\Reader\WorkflowReader;
2222
use Temporal\Tests\Unit\Declaration\Fixture\Inheritance\ExtendingWorkflow;
2323
use Temporal\Tests\Unit\Declaration\Fixture\SimpleWorkflow;
24+
use Temporal\Tests\Unit\Declaration\Fixture\WorkflowWithConstructor;
2425
use Temporal\Tests\Unit\Declaration\Fixture\WorkflowWithCron;
2526
use Temporal\Tests\Unit\Declaration\Fixture\WorkflowWithCronAndRetry;
2627
use Temporal\Tests\Unit\Declaration\Fixture\WorkflowWithCustomName;
2728
use Temporal\Tests\Unit\Declaration\Fixture\WorkflowWithoutHandler;
2829
use Temporal\Tests\Unit\Declaration\Fixture\WorkflowWithQueries;
2930
use Temporal\Tests\Unit\Declaration\Fixture\WorkflowWithRetry;
3031
use Temporal\Tests\Unit\Declaration\Fixture\WorkflowWithSignals;
32+
use Temporal\Tests\Unit\Declaration\Fixture\WorkflowWithWorkflowInit;
3133
use Temporal\Tests\Workflow\AggregatedWorkflowImpl;
3234

3335
/**
@@ -203,6 +205,18 @@ public function testWorkflowHandlerWithName(WorkflowReader $reader): void
203205
$this->assertSame('ExampleWorkflowName', $prototype->getID());
204206
}
205207

208+
/**
209+
* @param WorkflowReader $reader
210+
* @throws \ReflectionException
211+
*/
212+
#[TestDox("Reading workflow with WorkflowInit attribute")]
213+
#[DataProvider('workflowReaderDataProvider')]
214+
public function testWorkflowWithInitConstructor(WorkflowReader $reader): void
215+
{
216+
$this->assertTrue($reader->fromClass(WorkflowWithWorkflowInit::class)->hasInitializer());
217+
$this->assertFalse($reader->fromClass(WorkflowWithConstructor::class)->hasInitializer());
218+
}
219+
206220
public function testHierarchicalWorkflow(): void
207221
{
208222
$instantiator = new WorkflowInstantiator(new \Temporal\Interceptor\SimplePipelineProvider());

0 commit comments

Comments
 (0)