Skip to content

Commit a8246cc

Browse files
ARDINStephanesardin
andauthored
Add access to JobExecution in CallbackWriter (#173)
Co-authored-by: sardin <[email protected]>
1 parent a7208ec commit a8246cc

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/batch/src/Job/Item/Writer/CallbackWriter.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,25 @@
55
namespace Yokai\Batch\Job\Item\Writer;
66

77
use Yokai\Batch\Job\Item\ItemWriterInterface;
8+
use Yokai\Batch\Job\JobExecutionAwareInterface;
9+
use Yokai\Batch\Job\JobExecutionAwareTrait;
810

911
/**
1012
* An {@see ItemWriterInterface} that write items with a {@see Closure} provided at construction.
1113
*
1214
* Provided {@see Closure} must accept items to write and must return nothing.
1315
*/
14-
final class CallbackWriter implements ItemWriterInterface
16+
final class CallbackWriter implements ItemWriterInterface, JobExecutionAwareInterface
1517
{
18+
use JobExecutionAwareTrait;
19+
1620
public function __construct(
1721
private \Closure $callback,
1822
) {
1923
}
2024

2125
public function write(iterable $items): void
2226
{
23-
($this->callback)($items);
27+
($this->callback)($items, $this->jobExecution);
2428
}
2529
}

src/batch/tests/Job/Item/Writer/CallbackWriterTest.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,27 @@
66

77
use PHPUnit\Framework\TestCase;
88
use Yokai\Batch\Job\Item\Writer\CallbackWriter;
9+
use Yokai\Batch\JobExecution;
910

1011
class CallbackWriterTest extends TestCase
1112
{
1213
public function testWrite(): void
1314
{
1415
$saveditems = [];
15-
$writer = new CallbackWriter(function (array $items) use (&$saveditems) {
16-
$saveditems = [...$saveditems, ...$items];
17-
});
16+
$writer = new CallbackWriter(
17+
function (array $items, JobExecution $jobExecution) use (&$saveditems, &$receivedJobExecution) {
18+
$saveditems = [...$saveditems, ...$items];
19+
$receivedJobExecution = $jobExecution;
20+
},
21+
);
22+
23+
$writer->setJobExecution(JobExecution::createRoot(id: '123', jobName: 'test'));
1824
$writer->write([1, 2, 3]);
1925
$writer->write([4, 5, 6]);
2026

2127
self::assertSame([1, 2, 3, 4, 5, 6], $saveditems);
28+
self::assertInstanceOf(JobExecution::class, $receivedJobExecution);
29+
self::assertSame('123', $receivedJobExecution->getId());
30+
self::assertSame('test', $receivedJobExecution->getJobName());
2231
}
2332
}

0 commit comments

Comments
 (0)