Skip to content

Commit 741875e

Browse files
authored
Fixed missing job id propagration on messenger job launch (#17)
* Fixed missing job id propagration on messenger job launch * Exclude all src/Test directories from coverage * Do not exclude src/Test directories, add tests for these classes instead
1 parent 9aa2bab commit 741875e

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/DispatchMessageJobLauncher.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public function launch(string $name, array $configuration = []): JobExecution
4343
// create and store execution before dispatching message
4444
// guarantee job execution exists if message bus transport is asynchronous
4545
$jobExecution = $this->jobExecutionFactory->create($name, $configuration);
46+
$configuration['_id'] = $configuration['_id'] ?? $jobExecution->getId();
4647
$jobExecution->setStatus(BatchStatus::PENDING);
4748
$this->jobExecutionStorage->store($jobExecution);
4849

tests/DispatchMessageJobLauncherTest.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use PHPUnit\Framework\TestCase;
88
use Prophecy\Argument;
99
use Prophecy\PhpUnit\ProphecyTrait;
10-
use Prophecy\Prophecy\ObjectProphecy;
1110
use Symfony\Component\Messenger\Envelope;
1211
use Symfony\Component\Messenger\MessageBusInterface;
1312
use Yokai\Batch\BatchStatus;
@@ -17,6 +16,7 @@
1716
use Yokai\Batch\Factory\UniqidJobExecutionIdGenerator;
1817
use Yokai\Batch\JobExecution;
1918
use Yokai\Batch\Storage\JobExecutionStorageInterface;
19+
use Yokai\Batch\Test\Factory\SequenceJobExecutionIdGenerator;
2020

2121
final class DispatchMessageJobLauncherTest extends TestCase
2222
{
@@ -63,4 +63,44 @@ static function ($message): bool {
6363
$jobLauncher->launch('testing', ['_id' => '123456789', 'foo' => ['bar']])
6464
);
6565
}
66+
67+
public function testLaunchWithNoId(): void
68+
{
69+
$storage = $this->prophesize(JobExecutionStorageInterface::class);
70+
$jobExecutionAssertions = Argument::that(
71+
static function ($jobExecution): bool {
72+
return $jobExecution instanceof JobExecution
73+
&& $jobExecution->getJobName() === 'testing'
74+
&& $jobExecution->getId() === '123456789';
75+
}
76+
);
77+
$storage->store($jobExecutionAssertions)
78+
->shouldBeCalled();
79+
$storage->retrieve('testing', '123456789')
80+
->shouldBeCalled()
81+
->willReturn($jobExecution = JobExecution::createRoot('123456789-refreshed', 'testing'));
82+
83+
$messageBus = $this->prophesize(MessageBusInterface::class);
84+
$messageAssertions = Argument::that(
85+
static function ($message): bool {
86+
return $message instanceof LaunchJobMessage
87+
&& $message->getJobName() === 'testing'
88+
&& $message->getConfiguration() === ['_id' => '123456789'];
89+
}
90+
);
91+
$messageBus->dispatch($messageAssertions)
92+
->shouldBeCalled()
93+
->willReturn(new Envelope(new LaunchJobMessage('unused')));
94+
95+
$jobLauncher = new DispatchMessageJobLauncher(
96+
new JobExecutionFactory(new SequenceJobExecutionIdGenerator(['123456789'])),
97+
$storage->reveal(),
98+
$messageBus->reveal()
99+
);
100+
101+
self::assertSame(
102+
$jobExecution,
103+
$jobLauncher->launch('testing')
104+
);
105+
}
66106
}

0 commit comments

Comments
 (0)