Skip to content

Commit f4c4962

Browse files
authored
Fix prototype existence check in ArrayRepository (#631)
2 parents be8ccb4 + 874c5bd commit f4c4962

File tree

7 files changed

+25
-54
lines changed

7 files changed

+25
-54
lines changed

psalm-baseline.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,6 +1406,12 @@
14061406
</PossiblyNullArgument>
14071407
</file>
14081408
<file src="src/Worker/Transport/Codec/ProtoCodec.php">
1409+
<LessSpecificReturnStatement>
1410+
<code><![CDATA[$frame->serializeToString()]]></code>
1411+
</LessSpecificReturnStatement>
1412+
<MoreSpecificReturnType>
1413+
<code><![CDATA[string]]></code>
1414+
</MoreSpecificReturnType>
14091415
<PossiblyInvalidArgument>
14101416
<code><![CDATA[$e->getCode()]]></code>
14111417
<code><![CDATA[$e->getCode()]]></code>

src/Internal/Repository/ArrayRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function add(Identifiable $entry, bool $overwrite = false): void
5858
{
5959
$name = $entry->getID();
6060

61-
if ($overwrite === false && isset($this->prototypes[$name])) {
61+
if ($overwrite === false && isset($this->entries[$name])) {
6262
throw new \OutOfBoundsException(\sprintf(self::ERROR_ALREADY_EXISTS, $name));
6363
}
6464

src/Worker/Transport/Codec/CodecInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@ interface CodecInterface
2020
{
2121
/**
2222
* @param iterable<CommandInterface> $commands
23+
* @return non-empty-string
24+
*
2325
* @throws ProtocolException
2426
*/
2527
public function encode(iterable $commands): string;
2628

2729
/**
2830
* @return iterable<ServerRequestInterface|ServerResponseInterface>
31+
*
2932
* @throws ProtocolException
3033
*/
3134
public function decode(string $batch, array $headers = []): iterable;

src/Worker/Transport/HostConnectionInterface.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ interface HostConnectionInterface
2424
public function waitBatch(): ?CommandBatch;
2525

2626
/**
27+
* @param non-empty-string $frame
2728
* @throws TransportException
2829
*/
2930
public function send(string $frame): void;

src/WorkerFactory.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,9 @@ private function createCodec(): CodecInterface
284284
}
285285
}
286286

287+
/**
288+
* @return non-empty-string The encoded responses to be sent back to the parent process.
289+
*/
287290
private function dispatch(string $messages, array $headers): string
288291
{
289292
$commands = $this->codec->decode($messages, $headers);

tests/Fixtures/WorkerMock.php

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,14 @@ class WorkerMock implements HostConnectionInterface
2424
{
2525
private WorkerFactoryInterface $factory;
2626

27-
/** @var array */
2827
private array $in;
2928

30-
/** @var array */
3129
private array $out;
3230

33-
/** @var int */
3431
private int $indexIn;
3532

36-
/** @var int */
3733
private int $indexOut;
3834

39-
/** @var bool */
4035
private bool $debug;
4136

4237
private TestCase $testCase;
@@ -55,29 +50,27 @@ public static function createMock(): WorkerMock
5550
return $mock;
5651
}
5752

58-
public function registerWorkflowAndActivities()
53+
public function registerWorkflowAndActivities(): void
5954
{
6055
$taskQueue = $this->factory->newWorker('default');
6156

6257
foreach ($this->getClasses(__DIR__ . '/src/Workflow') as $name) {
63-
$taskQueue->registerWorkflowTypes('Temporal\\Tests\\Workflow\\' . $name);
58+
$class = 'Temporal\\Tests\\Workflow\\' . $name;
59+
if (\class_exists($class)) {
60+
$taskQueue->registerWorkflowTypes($class);
61+
}
6462
}
6563

6664
// register all activity
6765
foreach ($this->getClasses(__DIR__ . '/src/Activity') as $name) {
6866
$class = '\\Temporal\\Tests\\Activity\\' . $name;
6967
if (\class_exists($class)) {
70-
$taskQueue->registerActivityImplementations(new $class);
68+
$taskQueue->registerActivityImplementations(new $class());
7169
}
7270
}
7371
}
7472

75-
/**
76-
* @param TestCase $testCase
77-
* @param array $queue
78-
* @param bool $debug
79-
*/
80-
public function run(TestCase $testCase, array $queue, bool $debug = false)
73+
public function run(TestCase $testCase, array $queue, bool $debug = false): void
8174
{
8275
$this->debug = $debug;
8376

@@ -92,9 +85,6 @@ public function run(TestCase $testCase, array $queue, bool $debug = false)
9285
$this->factory->run($this);
9386
}
9487

95-
/**
96-
* @return CommandBatch|null
97-
*/
9888
public function waitBatch(): ?CommandBatch
9989
{
10090
if (!isset($this->out[$this->indexOut])) {
@@ -110,13 +100,10 @@ public function waitBatch(): ?CommandBatch
110100

111101
return new CommandBatch(
112102
$pair[0],
113-
json_decode($pair[1], true),
103+
\json_decode($pair[1], true),
114104
);
115105
}
116106

117-
/**
118-
* @param string $frame
119-
*/
120107
public function send(string $frame): void
121108
{
122109
$pair = $this->in[$this->indexIn];
@@ -149,7 +136,6 @@ public function send(string $frame): void
149136
}
150137

151138
/**
152-
* @param \Throwable $error
153139
* @throws \Throwable
154140
*/
155141
public function error(\Throwable $error): void

tests/Unit/Router/StartWorkflowTestCase.php

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use Temporal\DataConverter\EncodedValues;
1616
use Temporal\Exception\ExceptionInterceptorInterface;
1717
use Temporal\Interceptor\SimplePipelineProvider;
18-
use Temporal\Interceptor\WorkflowInboundCallsInterceptor;
1918
use Temporal\Internal\Declaration\Destroyable;
2019
use Temporal\Internal\Declaration\Prototype\WorkflowPrototype;
2120
use Temporal\Internal\Declaration\Reader\WorkflowReader;
@@ -76,42 +75,15 @@ public function testStartingAlreadyRunningWorkflow(): void
7675
$workflowInfo = new WorkflowInfo();
7776
$workflowInfo->type->name = 'DummyWorkflow';
7877
$workflowInfo->execution = new WorkflowExecution('123', (string) $request->getID());
78+
$this->services->running->add($this->createIdentifiable($request->getID()));
7979

8080
$this->marshaller->expects($this->once())
8181
->method('unmarshal')
8282
->willReturn(new Input($workflowInfo));
8383

84-
$identify = $this->createIdentifiable($request->getID());
84+
$this->expectException(\OutOfBoundsException::class);
8585

86-
$this->services->running->add($identify);
87-
88-
try {
89-
$this->router->handle($request, [], new Deferred());
90-
} catch (\LogicException $exception) {
91-
$this->fail($exception->getMessage());
92-
}
93-
}
94-
95-
public function testAlreadyRunningWorkflowIsReturned(): void
96-
{
97-
$request = new Request(Uuid::v4(), DummyWorkflow::class, EncodedValues::fromValues([]));
98-
99-
$workflowInfo = new WorkflowInfo();
100-
$workflowInfo->type->name = 'DummyWorkflow';
101-
$workflowInfo->execution = new WorkflowExecution('123', (string) $request->getID());
102-
103-
$this->marshaller->expects($this->once())
104-
->method('unmarshal')
105-
->willReturn(new Input($workflowInfo));
106-
107-
$identify = $this->createIdentifiable($request->getID());
108-
$this->services->running->add($identify);
109-
110-
try {
111-
$this->router->handle($request, [], new Deferred());
112-
} catch (\LogicException $exception) {
113-
$this->fail($exception->getMessage());
114-
}
86+
$this->router->handle($request, [], new Deferred());
11587
}
11688

11789
protected function setUp(): void

0 commit comments

Comments
 (0)