Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function launch(string $name, array $configuration = []): JobExecution
// create and store execution before dispatching message
// guarantee job execution exists if message bus transport is asynchronous
$jobExecution = $this->jobExecutionFactory->create($name, $configuration);
$configuration['_id'] = $configuration['_id'] ?? $jobExecution->getId();
$jobExecution->setStatus(BatchStatus::PENDING);
$this->jobExecutionStorage->store($jobExecution);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;
use Yokai\Batch\BatchStatus;
Expand All @@ -17,6 +16,7 @@
use Yokai\Batch\Factory\UniqidJobExecutionIdGenerator;
use Yokai\Batch\JobExecution;
use Yokai\Batch\Storage\JobExecutionStorageInterface;
use Yokai\Batch\Test\Factory\SequenceJobExecutionIdGenerator;

final class DispatchMessageJobLauncherTest extends TestCase
{
Expand Down Expand Up @@ -63,4 +63,44 @@ static function ($message): bool {
$jobLauncher->launch('testing', ['_id' => '123456789', 'foo' => ['bar']])
);
}

public function testLaunchWithNoId(): void
{
$storage = $this->prophesize(JobExecutionStorageInterface::class);
$jobExecutionAssertions = Argument::that(
static function ($jobExecution): bool {
return $jobExecution instanceof JobExecution
&& $jobExecution->getJobName() === 'testing'
&& $jobExecution->getId() === '123456789';
}
);
$storage->store($jobExecutionAssertions)
->shouldBeCalled();
$storage->retrieve('testing', '123456789')
->shouldBeCalled()
->willReturn($jobExecution = JobExecution::createRoot('123456789-refreshed', 'testing'));

$messageBus = $this->prophesize(MessageBusInterface::class);
$messageAssertions = Argument::that(
static function ($message): bool {
return $message instanceof LaunchJobMessage
&& $message->getJobName() === 'testing'
&& $message->getConfiguration() === ['_id' => '123456789'];
}
);
$messageBus->dispatch($messageAssertions)
->shouldBeCalled()
->willReturn(new Envelope(new LaunchJobMessage('unused')));

$jobLauncher = new DispatchMessageJobLauncher(
new JobExecutionFactory(new SequenceJobExecutionIdGenerator(['123456789'])),
$storage->reveal(),
$messageBus->reveal()
);

self::assertSame(
$jobExecution,
$jobLauncher->launch('testing')
);
}
}
26 changes: 26 additions & 0 deletions src/batch/src/Test/Factory/SequenceJobExecutionIdGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Yokai\Batch\Test\Factory;

use Yokai\Batch\Factory\JobExecutionIdGeneratorInterface;

final class SequenceJobExecutionIdGenerator implements JobExecutionIdGeneratorInterface
{
private array $sequence;
private int $current = 0;

public function __construct(array $sequence)
{
$this->sequence = \array_values($sequence);
}

public function generate(): string
{
$current = $this->sequence[$this->current] ?? '';
$this->current++;

return (string)$current;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Yokai\Batch\Tests\Test\Factory;

use Yokai\Batch\Test\Factory\SequenceJobExecutionIdGenerator;
use PHPUnit\Framework\TestCase;

class SequenceJobExecutionIdGeneratorTest extends TestCase
{
public function test(): void
{
$generator = new SequenceJobExecutionIdGenerator(['123', '456', '789']);

self::assertSame('123', $generator->generate());
self::assertSame('456', $generator->generate());
self::assertSame('789', $generator->generate());

// at this point we are out of sequence bounds
// generator will keep returning empty string
self::assertSame('', $generator->generate());
self::assertSame('', $generator->generate());
}
}